aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/basic.zig
diff options
context:
space:
mode:
authorPavel Verigo <paul.verigo@gmail.com>2024-07-18 17:18:17 +0200
committerPavel Verigo <paul.verigo@gmail.com>2024-07-18 17:18:17 +0200
commitd1bd9518f97abc9ab80795962b8e0dfd8e4d8768 (patch)
tree4e41594948a124da7f8e41602807f9ee3fefdcf7 /test/behavior/basic.zig
parenta57479afc2cea1b7c2c6802b7e8a1a7db973a3a3 (diff)
downloadzig-d1bd9518f97abc9ab80795962b8e0dfd8e4d8768.tar.gz
zig-d1bd9518f97abc9ab80795962b8e0dfd8e4d8768.zip
stage2-wasm: fix big int comparison
Unexpected to be found only now
Diffstat (limited to 'test/behavior/basic.zig')
-rw-r--r--test/behavior/basic.zig113
1 files changed, 69 insertions, 44 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index fb61247b11..05d6549683 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -1134,55 +1134,80 @@ test "pointer to struct literal with runtime field is constant" {
try expect(@typeInfo(@TypeOf(ptr)).Pointer.is_const);
}
-test "integer compare" {
+fn testSignedCmp(comptime T: type) !void {
+ var z: T = 0;
+ var p: T = 123;
+ var n: T = -123;
+ var min: T = std.math.minInt(T);
+ var max: T = std.math.maxInt(T);
+ var half_min: T = std.math.minInt(T) / 2;
+ var half_max: T = std.math.minInt(T) / 2;
+ _ = .{ &z, &p, &n, &min, &max, &half_min, &half_max };
+ try expect(z == z and z != p and z != n);
+ try expect(p == p and p != n and n == n);
+ try expect(z > n and z < p and z >= n and z <= p);
+ try expect(!(z < n or z > p or z <= n or z >= p or z > z or z < z));
+ try expect(p > n and n < p and p >= n and n <= p and p >= p and p <= p and n >= n and n <= n);
+ try expect(!(p < n or n > p or p <= n or n >= p or p > p or p < p or n > n or n < n));
+ try expect(z == 0 and z != 123 and z != -123 and 0 == z and 0 != p and 0 != n);
+ try expect(z > -123 and p > -123 and !(n > 123));
+ try expect(z < 123 and !(p < 123) and n < 123);
+ try expect(-123 <= z and -123 <= p and -123 <= n);
+ try expect(123 >= z and 123 >= p and 123 >= n);
+ try expect(!(0 != z or 123 != p or -123 != n));
+ try expect(!(z > 0 or -123 > p or 123 < n));
+
+ try expect(min <= max and z <= max and p <= max and n <= max and half_max <= max and half_min <= max);
+ try expect(min <= max and min <= z and min <= p and min <= n and min <= half_min and min <= half_max);
+}
+
+fn testUnsignedCmp(comptime T: type) !void {
+ var z: T = 0;
+ var p: T = 123;
+ var max: T = std.math.maxInt(T);
+ var half_max: T = std.math.minInt(T) / 2;
+ _ = .{ &z, &p, &max, &half_max };
+ try expect(z == z and z != p);
+ try expect(p == p);
+ try expect(z < p and z <= p);
+ try expect(!(z > p or z >= p or z > z or z < z));
+ try expect(p >= p and p <= p);
+ try expect(!(p > p or p < p));
+ try expect(z == 0 and z != 123 and z != -123 and 0 == z and 0 != p);
+ try expect(z > -123 and p > -123);
+ try expect(z < 123 and !(p < 123));
+ try expect(-123 <= z and -123 <= p);
+ try expect(123 >= z and 123 >= p);
+ try expect(!(0 != z or 123 != p));
+ try expect(!(z > 0 or -123 > p));
+
+ try expect(z <= max and p <= max and half_max <= max);
+ try expect(half_max != max);
+}
+
+test "integer compare <= 64 bits" {
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
- const S = struct {
- fn doTheTestSigned(comptime T: type) !void {
- var z: T = 0;
- var p: T = 123;
- var n: T = -123;
- _ = .{ &z, &p, &n };
- try expect(z == z and z != p and z != n);
- try expect(p == p and p != n and n == n);
- try expect(z > n and z < p and z >= n and z <= p);
- try expect(!(z < n or z > p or z <= n or z >= p or z > z or z < z));
- try expect(p > n and n < p and p >= n and n <= p and p >= p and p <= p and n >= n and n <= n);
- try expect(!(p < n or n > p or p <= n or n >= p or p > p or p < p or n > n or n < n));
- try expect(z == 0 and z != 123 and z != -123 and 0 == z and 0 != p and 0 != n);
- try expect(z > -123 and p > -123 and !(n > 123));
- try expect(z < 123 and !(p < 123) and n < 123);
- try expect(-123 <= z and -123 <= p and -123 <= n);
- try expect(123 >= z and 123 >= p and 123 >= n);
- try expect(!(0 != z or 123 != p or -123 != n));
- try expect(!(z > 0 or -123 > p or 123 < n));
- }
- fn doTheTestUnsigned(comptime T: type) !void {
- var z: T = 0;
- var p: T = 123;
- _ = .{ &z, &p };
- try expect(z == z and z != p);
- try expect(p == p);
- try expect(z < p and z <= p);
- try expect(!(z > p or z >= p or z > z or z < z));
- try expect(p >= p and p <= p);
- try expect(!(p > p or p < p));
- try expect(z == 0 and z != 123 and z != -123 and 0 == z and 0 != p);
- try expect(z > -123 and p > -123);
- try expect(z < 123 and !(p < 123));
- try expect(-123 <= z and -123 <= p);
- try expect(123 >= z and 123 >= p);
- try expect(!(0 != z or 123 != p));
- try expect(!(z > 0 or -123 > p));
- }
- };
inline for (.{ u8, u16, u32, u64, usize, u10, u20, u30, u60 }) |T| {
- try S.doTheTestUnsigned(T);
- try comptime S.doTheTestUnsigned(T);
+ try testUnsignedCmp(T);
+ try comptime testUnsignedCmp(T);
}
inline for (.{ i8, i16, i32, i64, isize, i10, i20, i30, i60 }) |T| {
- try S.doTheTestSigned(T);
- try comptime S.doTheTestSigned(T);
+ try testSignedCmp(T);
+ try comptime testSignedCmp(T);
+ }
+}
+
+test "integer compare <= 128 bits" {
+ if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+
+ inline for (.{ u65, u96, u127, u128 }) |T| {
+ try testUnsignedCmp(T);
+ try comptime testUnsignedCmp(T);
+ }
+ inline for (.{ i65, i96, i127, i128 }) |T| {
+ try testSignedCmp(T);
+ try comptime testSignedCmp(T);
}
}