authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 00:55:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 00:55:21-05:00
log5fc95c2a538b10ad346273112b1d4ed5371f8135
treef0700978c5f7f51e37b45b74c9d79e66d3afcb1c
parent46033a2128b09ed15f38f6ed2602bf80989a770f

IR: port some tests


7 files changed, 123 insertions(+), 116 deletions(-)

src/ir.cpp+1-1
......@@ -5462,7 +5462,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
54625462 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
54635463 if (!val)
54645464 return ira->codegen->builtin_types.entry_invalid;
5465 bool ptr_is_const = true;
5465 bool ptr_is_const = false;
54665466 return ir_analyze_const_ptr(ira, source_instruction, val, value->type_entry,
54675467 false, ConstPtrSpecialNone, ptr_is_const);
54685468 }
test/cases3/array.zig created+31
......@@ -0,0 +1,31 @@
1fn arrays() {
2 @setFnTest(this);
3
4 var array : [5]u32 = undefined;
5
6 var i : u32 = 0;
7 while (i < 5) {
8 array[i] = i + 1;
9 i = array[i];
10 }
11
12 i = 0;
13 var accumulator = u32(0);
14 while (i < 5) {
15 accumulator += array[i];
16
17 i += 1;
18 }
19
20 assert(accumulator == 15);
21 assert(getArrayLen(array) == 5);
22}
23fn getArrayLen(a: []u32) -> usize {
24 a.len
25}
26
27// TODO const assert = @import("std").debug.assert;
28fn assert(ok: bool) {
29 if (!ok)
30 @unreachable();
31}
test/cases3/fn.zig+30
......@@ -32,6 +32,36 @@ fn voidFun(a: i32, b: void, c: i32, d: void) {
3232}
3333
3434
35fn mutableLocalVariables() {
36 @setFnTest(this);
37
38 var zero : i32 = 0;
39 assert(zero == 0);
40
41 var i = i32(0);
42 while (i != 3) {
43 i += 1;
44 }
45 assert(i == 3);
46}
47
48fn separateBlockScopes() {
49 @setFnTest(this);
50
51 {
52 const no_conflict : i32 = 5;
53 assert(no_conflict == 5);
54 }
55
56 const c = {
57 const no_conflict = i32(10);
58 no_conflict
59 };
60 assert(c == 10);
61}
62
63
64
3565// TODO const assert = @import("std").debug.assert;
3666fn assert(ok: bool) {
3767 if (!ok)
test/cases3/math.zig+19
......@@ -50,6 +50,25 @@ fn countTrailingZeroes() {
5050 assert(@ctz(u8(0b00000000)) == 8);
5151}
5252
53fn modifyOperators() {
54 @setFnTest(this);
55
56 var i : i32 = 0;
57 i += 5; assert(i == 5);
58 i -= 2; assert(i == 3);
59 i *= 20; assert(i == 60);
60 i /= 3; assert(i == 20);
61 i %= 11; assert(i == 9);
62 i <<= 1; assert(i == 18);
63 i >>= 2; assert(i == 4);
64 i = 6;
65 i &= 5; assert(i == 4);
66 i ^= 6; assert(i == 2);
67 i = 6;
68 i |= 3; assert(i == 7);
69}
70
71
5372// TODO const assert = @import("std").debug.assert;
5473fn assert(ok: bool) {
5574 if (!ok)
test/cases3/struct.zig+41
......@@ -21,6 +21,47 @@ fn invokeStaticMethodInGlobalScope() {
2121 assert(should_be_11 == 11);
2222}
2323
24fn voidStructFields() {
25 @setFnTest(this);
26
27 const foo = VoidStructFieldsFoo {
28 .a = void{},
29 .b = 1,
30 .c = void{},
31 };
32 assert(foo.b == 1);
33 assert(@sizeOf(VoidStructFieldsFoo) == 4);
34}
35const VoidStructFieldsFoo = struct {
36 a : void,
37 b : i32,
38 c : void,
39};
40
41
42pub fn structs() {
43 @setFnTest(this);
44
45 var foo: StructFoo = undefined;
46 @memset((&u8)(&foo), 0, @sizeOf(StructFoo));
47 foo.a += 1;
48 foo.b = foo.a == 1;
49 testFoo(foo);
50 testMutation(&foo);
51 assert(foo.c == 100);
52}
53const StructFoo = struct {
54 a : i32,
55 b : bool,
56 c : f32,
57};
58fn testFoo(foo : StructFoo) {
59 assert(foo.b);
60}
61fn testMutation(foo : &StructFoo) {
62 foo.c = 100;
63}
64
2465
2566
2667// TODO const assert = @import("std").debug.assert;
test/self_hosted.zig-115
......@@ -17,121 +17,6 @@ const test_this = @import("cases/this.zig");
1717
1818
1919
20fn mutableLocalVariables() {
21 @setFnTest(this, true);
22
23 var zero : i32 = 0;
24 assert(zero == 0);
25
26 var i = i32(0);
27 while (i != 3) {
28 i += 1;
29 }
30 assert(i == 3);
31}
32
33fn arrays() {
34 @setFnTest(this, true);
35
36 var array : [5]u32 = undefined;
37
38 var i : u32 = 0;
39 while (i < 5) {
40 array[i] = i + 1;
41 i = array[i];
42 }
43
44 i = 0;
45 var accumulator = u32(0);
46 while (i < 5) {
47 accumulator += array[i];
48
49 i += 1;
50 }
51
52 assert(accumulator == 15);
53 assert(getArrayLen(array) == 5);
54}
55fn getArrayLen(a: []u32) -> usize {
56 a.len
57}
58
59fn modifyOperators() {
60 @setFnTest(this, true);
61
62 var i : i32 = 0;
63 i += 5; assert(i == 5);
64 i -= 2; assert(i == 3);
65 i *= 20; assert(i == 60);
66 i /= 3; assert(i == 20);
67 i %= 11; assert(i == 9);
68 i <<= 1; assert(i == 18);
69 i >>= 2; assert(i == 4);
70 i = 6;
71 i &= 5; assert(i == 4);
72 i ^= 6; assert(i == 2);
73 i = 6;
74 i |= 3; assert(i == 7);
75}
76
77
78fn separateBlockScopes() {
79 @setFnTest(this, true);
80
81 {
82 const no_conflict : i32 = 5;
83 assert(no_conflict == 5);
84 }
85
86 const c = {
87 const no_conflict = i32(10);
88 no_conflict
89 };
90 assert(c == 10);
91}
92
93
94fn voidStructFields() {
95 @setFnTest(this, true);
96
97 const foo = VoidStructFieldsFoo {
98 .a = void{},
99 .b = 1,
100 .c = void{},
101 };
102 assert(foo.b == 1);
103 assert(@sizeOf(VoidStructFieldsFoo) == 4);
104}
105struct VoidStructFieldsFoo {
106 a : void,
107 b : i32,
108 c : void,
109}
110
111
112
113pub fn structs() {
114 @setFnTest(this, true);
115
116 var foo : StructFoo = undefined;
117 @memset(&foo, 0, @sizeOf(StructFoo));
118 foo.a += 1;
119 foo.b = foo.a == 1;
120 testFoo(foo);
121 testMutation(&foo);
122 assert(foo.c == 100);
123}
124struct StructFoo {
125 a : i32,
126 b : bool,
127 c : f32,
128}
129fn testFoo(foo : StructFoo) {
130 assert(foo.b);
131}
132fn testMutation(foo : &StructFoo) {
133 foo.c = 100;
134}
13520struct Node {
13621 val: Val,
13722 next: &Node,
test/self_hosted3.zig+1
......@@ -1,4 +1,5 @@
11// TODO '_' identifier for unused variable bindings
2const test_array = @import("cases3/array.zig");
23const test_atomics = @import("cases3/atomics.zig");
34const test_defer = @import("cases3/defer.zig");
45const test_enum = @import("cases3/enum.zig");