summaryrefslogtreecommitdiff
path: root/src/test/strings.zig
blob: 629bc463afe096e96282dd0019a9a98efbd7bb70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const std = @import("std");

const testing = std.testing;

pub const value = @import("../zisp/value.zig");

test "istr" {
    const istr = value.istr;
    const fx = value.fixnum;

    const s1 = "foo bar baz";
    const v1 = istr.intern(s1, false);
    const v1_len: usize = @intCast(fx.unpack(istr.len(v1)));
    try testing.expectEqualStrings(s1, istr.getHeader(v1).bytes());
    try testing.expectEqual(s1.len, v1_len);

    const file = try std.fs.cwd().openFile("src/test/data/string.txt", .{});
    defer file.close();
    var s2_buf: [4096]u8 = undefined;
    const s2_len = try file.readAll(&s2_buf);
    var s2: []u8 = s2_buf[0..s2_len];
    const v2 = istr.intern(s2, false);
    const v2_len: usize = @intCast(fx.unpack(istr.len(v2)));
    var s2_orig_buf: [4096]u8 = undefined;
    @memcpy(&s2_orig_buf, &s2_buf);
    const s2_orig = s2_orig_buf[0..s2_len];
    s2[0] = s2[0] +% 1;
    try testing.expectEqualStrings(s2_orig, istr.getHeader(v2).bytes());
    try testing.expectEqual(s2_len, v2_len);
}