summaryrefslogtreecommitdiff
path: root/src/test/strings.zig
blob: 3a0585e4e2443b1e86d10a47debee1a752894bbd (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
31
32
33
34
35
36
37
38
39
const std = @import("std");

const testing = std.testing;

const gstIo = std.Io.Threaded.global_single_threaded.io();

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

const istr = value.istr;
const fx = value.fixnum;

test "istr" {
    const s1 = "foo bar baz";
    const v1 = istr.intern(s1);
    const v1_len: usize = @intCast(fx.unpack(istr.len(v1)));

    try testing.expectEqualStrings(s1, istr.assert(v1).bytes());
    try testing.expectEqual(s1.len, v1_len);

    const path = "src/test/data/string.txt";
    var file = try std.Io.Dir.cwd().openFile(gstIo, path, .{});
    defer file.close(gstIo);

    var s2_buf: [4096]u8 = undefined;
    const s2_len = try file.readStreaming(gstIo, &.{&s2_buf});
    var s2: []u8 = s2_buf[0..s2_len];

    const v2 = istr.intern(s2);
    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.assert(v2).bytes());
    try testing.expectEqual(s2_len, v2_len);
}