aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/byteswap.zig
blob: 4affc99b5943d858fbdeaee7395bf2b4207ce1d9 (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
52
53
54
55
56
57
58
59
60
61
62
const std = @import("std");
const expect = std.testing.expect;

test "@byteSwap integers" {
    const ByteSwapIntTest = struct {
        fn run() !void {
            try t(u0, 0, 0);
            try t(u8, 0x12, 0x12);
            try t(u16, 0x1234, 0x3412);
            try t(u24, 0x123456, 0x563412);
            try t(u32, 0x12345678, 0x78563412);
            try t(u40, 0x123456789a, 0x9a78563412);
            try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
            try t(u56, 0x123456789abcde, 0xdebc9a78563412);
            try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
            try t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);

            try t(u0, @as(u0, 0), 0);
            try t(i8, @as(i8, -50), -50);
            try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));
            try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));
            try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
            try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
            try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));
            try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));
            try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
            try t(
                i128,
                @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)),
                @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)),
            );
        }
        fn t(comptime I: type, input: I, expected_output: I) !void {
            try std.testing.expectEqual(expected_output, @byteSwap(I, input));
        }
    };
    comptime try ByteSwapIntTest.run();
    try ByteSwapIntTest.run();
}

test "@byteSwap vectors" {
    const ByteSwapVectorTest = struct {
        fn run() !void {
            try t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
            try t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
            try t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
        }

        fn t(
            comptime I: type,
            comptime n: comptime_int,
            input: std.meta.Vector(n, I),
            expected_vector: std.meta.Vector(n, I),
        ) !void {
            const actual_output: [n]I = @byteSwap(I, input);
            const expected_output: [n]I = expected_vector;
            try std.testing.expectEqual(expected_output, actual_output);
        }
    };
    comptime try ByteSwapVectorTest.run();
    try ByteSwapVectorTest.run();
}