authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-10 13:58:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-10 13:58:04-07:00
log4a3bce4b638cf94482afe010af9bae722249f548
treee7c278c03d811df926cf715f90ecdbd1a1cb8c8a
parentb117b5907c50f495c53770bfc351e0431e6474b3

move some tests into self hosted


2 files changed, 194 insertions(+), 222 deletions(-)

test/run_tests.cpp-222
...@@ -252,82 +252,6 @@ pub const b_text = a_text;...@@ -252,82 +252,6 @@ pub const b_text = a_text;
252 )SOURCE");252 )SOURCE");
253 }253 }
254254
255 add_simple_case("params", R"SOURCE(
256const io = @import("std").io;
257
258fn add(a: i32, b: i32) -> i32 {
259 a + b
260}
261
262pub fn main(args: [][]u8) -> %void {
263 if (add(22, 11) == 33) {
264 %%io.stdout.printf("pass\n");
265 }
266}
267 )SOURCE", "pass\n");
268
269 add_simple_case("void parameters", R"SOURCE(
270const io = @import("std").io;
271
272pub fn main(args: [][]u8) -> %void {
273 void_fun(1, void{}, 2);
274}
275
276fn void_fun(a : i32, b : void, c : i32) {
277 const v = b;
278 const vv : void = if (a == 1) {v} else {};
279 if (a + c == 3) { %%io.stdout.printf("OK\n"); }
280 return vv;
281}
282 )SOURCE", "OK\n");
283
284 add_simple_case("mutable local variables", R"SOURCE(
285const io = @import("std").io;
286
287pub fn main(args: [][]u8) -> %void {
288 var zero : i32 = 0;
289 if (zero == 0) { %%io.stdout.printf("zero\n"); }
290
291 var i = i32(0);
292 while (i != 3) {
293 %%io.stdout.printf("loop\n");
294 i += 1;
295 }
296}
297 )SOURCE", "zero\nloop\nloop\nloop\n");
298
299 add_simple_case("arrays", R"SOURCE(
300const io = @import("std").io;
301
302pub fn main(args: [][]u8) -> %void {
303 var array : [5]i32 = undefined;
304
305 var i : i32 = 0;
306 while (i < 5) {
307 array[i] = i + 1;
308 i = array[i];
309 }
310
311 i = 0;
312 var accumulator = i32(0);
313 while (i < 5) {
314 accumulator += array[i];
315
316 i += 1;
317 }
318
319 if (accumulator == 15) {
320 %%io.stdout.printf("OK\n");
321 }
322
323 if (get_array_len(array) != 5) {
324 %%io.stdout.printf("BAD\n");
325 }
326}
327fn get_array_len(a: []i32) -> isize {
328 a.len
329}
330 )SOURCE", "OK\n");
331255
332256
333 add_simple_case("hello world without libc", R"SOURCE(257 add_simple_case("hello world without libc", R"SOURCE(
...@@ -339,49 +263,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -339,49 +263,6 @@ pub fn main(args: [][]u8) -> %void {
339 )SOURCE", "Hello, world!\n");263 )SOURCE", "Hello, world!\n");
340264
341265
342 add_simple_case("short circuit", R"SOURCE(
343const io = @import("std").io;
344
345pub fn main(args: [][]u8) -> %void {
346 if (true || { %%io.stdout.printf("BAD 1\n"); false }) {
347 %%io.stdout.printf("OK 1\n");
348 }
349 if (false || { %%io.stdout.printf("OK 2\n"); false }) {
350 %%io.stdout.printf("BAD 2\n");
351 }
352
353 if (true && { %%io.stdout.printf("OK 3\n"); false }) {
354 %%io.stdout.printf("BAD 3\n");
355 }
356 if (false && { %%io.stdout.printf("BAD 4\n"); false }) {
357 } else {
358 %%io.stdout.printf("OK 4\n");
359 }
360}
361 )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n");
362
363 add_simple_case("modify operators", R"SOURCE(
364const io = @import("std").io;
365
366pub fn main(args: [][]u8) -> %void {
367 var i : i32 = 0;
368 i += 5; if (i != 5) { %%io.stdout.printf("BAD +=\n"); }
369 i -= 2; if (i != 3) { %%io.stdout.printf("BAD -=\n"); }
370 i *= 20; if (i != 60) { %%io.stdout.printf("BAD *=\n"); }
371 i /= 3; if (i != 20) { %%io.stdout.printf("BAD /=\n"); }
372 i %= 11; if (i != 9) { %%io.stdout.printf("BAD %=\n"); }
373 i <<= 1; if (i != 18) { %%io.stdout.printf("BAD <<=\n"); }
374 i >>= 2; if (i != 4) { %%io.stdout.printf("BAD >>=\n"); }
375 i = 6;
376 i &= 5; if (i != 4) { %%io.stdout.printf("BAD &=\n"); }
377 i ^= 6; if (i != 2) { %%io.stdout.printf("BAD ^=\n"); }
378 i = 6;
379 i |= 3; if (i != 7) { %%io.stdout.printf("BAD |=\n"); }
380
381 %%io.stdout.printf("OK\n");
382}
383 )SOURCE", "OK\n");
384
385 add_simple_case_libc("number literals", R"SOURCE(266 add_simple_case_libc("number literals", R"SOURCE(
386const c = @c_import(@c_include("stdio.h"));267const c = @c_import(@c_include("stdio.h"));
387268
...@@ -508,110 +389,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {...@@ -508,110 +389,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
5080o10700.00010e0: 0x1.1c0001p+123890o10700.00010e0: 0x1.1c0001p+12
509)OUTPUT");390)OUTPUT");
510391
511 add_simple_case("structs", R"SOURCE(
512const io = @import("std").io;
513
514pub fn main(args: [][]u8) -> %void {
515 var foo : Foo = undefined;
516 @memset(&foo, 0, @sizeof(Foo));
517 foo.a += 1;
518 foo.b = foo.a == 1;
519 test_foo(foo);
520 test_mutation(&foo);
521 if (foo.c != 100) {
522 %%io.stdout.printf("BAD\n");
523 }
524 test_point_to_self();
525 test_byval_assign();
526 test_initializer();
527 %%io.stdout.printf("OK\n");
528}
529struct Foo {
530 a : i32,
531 b : bool,
532 c : f32,
533}
534fn test_foo(foo : Foo) {
535 if (!foo.b) {
536 %%io.stdout.printf("BAD\n");
537 }
538}
539fn test_mutation(foo : &Foo) {
540 foo.c = 100;
541}
542struct Node {
543 val: Val,
544 next: &Node,
545}
546
547struct Val {
548 x: i32,
549}
550fn test_point_to_self() {
551 var root : Node = undefined;
552 root.val.x = 1;
553
554 var node : Node = undefined;
555 node.next = &root;
556 node.val.x = 2;
557
558 root.next = &node;
559
560 if (node.next.next.next.val.x != 1) {
561 %%io.stdout.printf("BAD\n");
562 }
563}
564fn test_byval_assign() {
565 var foo1 : Foo = undefined;
566 var foo2 : Foo = undefined;
567392
568 foo1.a = 1234;
569
570 if (foo2.a != 0) { %%io.stdout.printf("BAD\n"); }
571
572 foo2 = foo1;
573
574 if (foo2.a != 1234) { %%io.stdout.printf("BAD - byval assignment failed\n"); }
575}
576fn test_initializer() {
577 const val = Val { .x = 42 };
578 if (val.x != 42) { %%io.stdout.printf("BAD\n"); }
579}
580 )SOURCE", "OK\n");
581
582 add_simple_case("global variables", R"SOURCE(
583const io = @import("std").io;
584
585const g1 : i32 = 1233 + 1;
586var g2 : i32 = 0;
587
588pub fn main(args: [][]u8) -> %void {
589 if (g2 != 0) { %%io.stdout.printf("BAD\n"); }
590 g2 = g1;
591 if (g2 != 1234) { %%io.stdout.printf("BAD\n"); }
592 %%io.stdout.printf("OK\n");
593}
594 )SOURCE", "OK\n");
595
596 add_simple_case("while loop", R"SOURCE(
597const io = @import("std").io;
598pub fn main(args: [][]u8) -> %void {
599 var i : i32 = 0;
600 while (i < 4) {
601 %%io.stdout.printf("loop\n");
602 i += 1;
603 }
604 g();
605}
606fn g() -> i32 {
607 return f();
608}
609fn f() -> i32 {
610 while (true) {
611 return 0;
612 }
613}
614 )SOURCE", "loop\nloop\nloop\nloop\n");
615393
616 add_simple_case("continue and break", R"SOURCE(394 add_simple_case("continue and break", R"SOURCE(
617const io = @import("std").io;395const io = @import("std").io;
test/self_hosted.zig+194
...@@ -44,6 +44,15 @@ fn first_eql_third(a: i32, b: i32, c: i32) {...@@ -44,6 +44,15 @@ fn first_eql_third(a: i32, b: i32, c: i32) {
44}44}
4545
4646
47#attribute("test")
48fn params() {
49 assert(test_params_add(22, 11) == 33);
50}
51fn test_params_add(a: i32, b: i32) -> i32 {
52 a + b
53}
54
55
47#attribute("test")56#attribute("test")
48fn local_variables() {57fn local_variables() {
49 test_loc_vars(2);58 test_loc_vars(2);
...@@ -59,6 +68,99 @@ fn bool_literals() {...@@ -59,6 +68,99 @@ fn bool_literals() {
59 assert(!false);68 assert(!false);
60}69}
6170
71#attribute("test")
72fn void_parameters() {
73 void_fun(1, void{}, 2, {});
74}
75fn void_fun(a : i32, b : void, c : i32, d : void) {
76 const v = b;
77 const vv : void = if (a == 1) {v} else {};
78 assert(a + c == 3);
79 return vv;
80}
81
82#attribute("test")
83fn mutable_local_variables() {
84 var zero : i32 = 0;
85 assert(zero == 0);
86
87 var i = i32(0);
88 while (i != 3) {
89 i += 1;
90 }
91 assert(i == 3);
92}
93
94#attribute("test")
95fn arrays() {
96 var array : [5]i32 = undefined;
97
98 var i : i32 = 0;
99 while (i < 5) {
100 array[i] = i + 1;
101 i = array[i];
102 }
103
104 i = 0;
105 var accumulator = i32(0);
106 while (i < 5) {
107 accumulator += array[i];
108
109 i += 1;
110 }
111
112 assert(accumulator == 15);
113 assert(get_array_len(array) == 5);
114}
115fn get_array_len(a: []i32) -> isize {
116 a.len
117}
118
119#attribute("test")
120fn short_circuit() {
121 var hit_1 = false;
122 var hit_2 = false;
123 var hit_3 = false;
124 var hit_4 = false;
125
126 if (true || { assert(false); false }) {
127 hit_1 = true;
128 }
129 if (false || { hit_2 = true; false }) {
130 assert(false);
131 }
132
133 if (true && { hit_3 = true; false }) {
134 %%io.stdout.printf("BAD 3\n");
135 }
136 if (false && { assert(false); false }) {
137 assert(false);
138 } else {
139 hit_4 = true;
140 }
141 assert(hit_1);
142 assert(hit_2);
143 assert(hit_3);
144 assert(hit_4);
145}
146
147#attribute("test")
148fn modify_operators() {
149 var i : i32 = 0;
150 i += 5; assert(i == 5);
151 i -= 2; assert(i == 3);
152 i *= 20; assert(i == 60);
153 i /= 3; assert(i == 20);
154 i %= 11; assert(i == 9);
155 i <<= 1; assert(i == 18);
156 i >>= 2; assert(i == 4);
157 i = 6;
158 i &= 5; assert(i == 4);
159 i ^= 6; assert(i == 2);
160 i = 6;
161 i |= 3; assert(i == 7);
162}
163
62164
63#attribute("test")165#attribute("test")
64fn separate_block_scopes() {166fn separate_block_scopes() {
...@@ -92,6 +194,98 @@ struct VoidStructFieldsFoo {...@@ -92,6 +194,98 @@ struct VoidStructFieldsFoo {
92}194}
93195
94196
197
198#attribute("test")
199pub fn structs() {
200 var foo : StructFoo = undefined;
201 @memset(&foo, 0, @sizeof(StructFoo));
202 foo.a += 1;
203 foo.b = foo.a == 1;
204 test_foo(foo);
205 test_mutation(&foo);
206 assert(foo.c == 100);
207}
208struct StructFoo {
209 a : i32,
210 b : bool,
211 c : f32,
212}
213fn test_foo(foo : StructFoo) {
214 assert(foo.b);
215}
216fn test_mutation(foo : &StructFoo) {
217 foo.c = 100;
218}
219struct Node {
220 val: Val,
221 next: &Node,
222}
223
224struct Val {
225 x: i32,
226}
227
228#attribute("test")
229fn struct_point_to_self() {
230 var root : Node = undefined;
231 root.val.x = 1;
232
233 var node : Node = undefined;
234 node.next = &root;
235 node.val.x = 2;
236
237 root.next = &node;
238
239 assert(node.next.next.next.val.x == 1);
240}
241
242#attribute("test")
243fn struct_byval_assign() {
244 var foo1 : StructFoo = undefined;
245 var foo2 : StructFoo = undefined;
246
247 foo1.a = 1234;
248 foo2.a = 0;
249 assert(foo2.a == 0);
250 foo2 = foo1;
251 assert(foo2.a == 1234);
252}
253
254fn struct_initializer() {
255 const val = Val { .x = 42 };
256 assert(val.x == 42);
257}
258
259
260const g1 : i32 = 1233 + 1;
261var g2 : i32 = 0;
262
263#attribute("test")
264fn global_variables() {
265 assert(g2 == 0);
266 g2 = g1;
267 assert(g2 == 1234);
268}
269
270
271#attribute("test")
272fn while_loop() {
273 var i : i32 = 0;
274 while (i < 4) {
275 i += 1;
276 }
277 assert(i == 4);
278 assert(while_loop_1() == 1);
279}
280fn while_loop_1() -> i32 {
281 return while_loop_2();
282}
283fn while_loop_2() -> i32 {
284 while (true) {
285 return 1;
286 }
287}
288
95#attribute("test")289#attribute("test")
96fn void_arrays() {290fn void_arrays() {
97 var array: [4]void = undefined;291 var array: [4]void = undefined;