aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2016-05-06 15:02:02 -0700
committerAndrew Kelley <superjoe30@gmail.com>2016-05-06 15:02:02 -0700
commit0c96920172dee530f445e4ef304954d0fe233bfa (patch)
treeb781ecb743002eae997c00faca03f69a02b90195 /test
parentb8d17b11a7eba696200ab9b5819121f48ad123d1 (diff)
downloadzig-0c96920172dee530f445e4ef304954d0fe233bfa.tar.gz
zig-0c96920172dee530f445e4ef304954d0fe233bfa.zip
add tests for integer wrapping
See #46
Diffstat (limited to 'test')
-rw-r--r--test/self_hosted.zig66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/self_hosted.zig b/test/self_hosted.zig
index 23c0097bc2..f7a19a2d3d 100644
--- a/test/self_hosted.zig
+++ b/test/self_hosted.zig
@@ -1456,3 +1456,69 @@ fn fence() {
@fence(AtomicOrder.SeqCst);
x = 5678;
}
+
+#attribute("test")
+fn unsigned_wrapping() {
+ var x_u32: u32w = @max_value(u32);
+ x_u32 += 1;
+ assert(x_u32 == 0);
+ x_u32 -= 1;
+ assert(x_u32 == @max_value(u32));
+ test_unsigned_wrapping_noeval(@max_value(u32));
+}
+#static_eval_enable(false)
+fn test_unsigned_wrapping_noeval(x: u32w) {
+ var x_u32 = x;
+ x_u32 += 1;
+ assert(x_u32 == 0);
+ x_u32 -= 1;
+ assert(x_u32 == @max_value(u32));
+}
+
+#attribute("test")
+fn signed_wrapping() {
+ var x_i32: i32w = @max_value(i32);
+ x_i32 += 1;
+ assert(x_i32 == @min_value(i32));
+ x_i32 -= 1;
+ assert(x_i32 == @max_value(i32));
+ test_signed_wrapping_noeval(@max_value(i32));
+}
+#static_eval_enable(false)
+fn test_signed_wrapping_noeval(x: i32w) {
+ var x_i32 = x;
+ x_i32 += 1;
+ assert(x_i32 == @min_value(i32));
+ x_i32 -= 1;
+ assert(x_i32 == @max_value(i32));
+}
+
+#attribute("test")
+fn negation_wrapping() {
+ var x_i16 = @min_value(i16w);
+ assert(x_i16 == -32768);
+ x_i16 = -x_i16;
+ assert(x_i16 == -32768);
+ test_negation_wrapping_noeval(@min_value(i16));
+}
+#static_eval_enable(false)
+fn test_negation_wrapping_noeval(x: i16w) {
+ var x_i16 = x;
+ assert(x_i16 == -32768);
+ x_i16 = -x_i16;
+ assert(x_i16 == -32768);
+}
+
+#attribute("test")
+fn shl_wrapping() {
+ var x_u16 = @max_value(u16w);
+ x_u16 <<= 1;
+ assert(x_u16 == 65534);
+ test_shl_wrapping_noeval(@max_value(u16));
+}
+#static_eval_enable(false)
+fn test_shl_wrapping_noeval(x: u16w) {
+ var x_u16 = x;
+ x_u16 <<= 1;
+ assert(x_u16 == 65534);
+}