summaryrefslogtreecommitdiff
path: root/src/libzisp.zig
blob: 542b84cb8d685fc00ff0eabe415b2fe2f898d5e4 (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
40
41
42
43
44
45
46
47
48
49
50
51
//! By convention, root.zig is the root source file when making a library. If
//! you are making an executable, the convention is to delete this file and
//! start with main.zig instead.
const std = @import("std");
const builtin = @import("builtin");
const testing = std.testing;

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

test "double" {
    const d1: f64 = 0.123456789;
    const d2: f64 = -0.987654321;
    const v1 = value.double.pack(d1);
    const v2 = value.double.pack(d2);
    const v3 = value.double.add(v1, v2);
    const result = value.double.unpack(v3);

    try std.testing.expect(value.double.check(v1));
    try std.testing.expect(value.double.check(v2));
    try std.testing.expect(value.double.check(v3));
    try std.testing.expect(result == d1 + d2);
}

test "fixnum" {
    const int1: i64 = 123456789;
    const int2: i64 = -987654321;
    const v1 = value.fixnum.pack(int1);
    const v2 = value.fixnum.pack(int2);
    const v3 = value.fixnum.add(v1, v2);
    const result = value.fixnum.unpack(v3);

    try std.testing.expect(value.fixnum.check(v1));
    try std.testing.expect(value.fixnum.check(v2));
    try std.testing.expect(value.fixnum.check(v3));
    try std.testing.expect(result == int1 + int2);
}

test "ptr" {
    const ptr1 = value.ptr.pack(@ptrFromInt(256), value.ptr.Tag.string);
    try std.testing.expect(value.ptr.check(ptr1));
    try std.testing.expect(value.ptr.checkZisp(ptr1));
    try std.testing.expect(value.ptr.checkNormal(ptr1));

    const ptr2 = value.ptr.makeWeak(ptr1);
    try std.testing.expect(value.ptr.check(ptr2));
    try std.testing.expect(value.ptr.checkZisp(ptr2));
    try std.testing.expect(value.ptr.checkWeak(ptr2));

    // Make sure ptr1 wasn't modified
    try std.testing.expect(value.ptr.checkNormal(ptr1));
}