authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-14 10:52:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-14 11:15:02-07:00
log7f56e4ac16a7480036b8a2557f8176aae0d0d2b7
treedc23463af3eea8918135cd3acba4f6e63c7d3d38
parent579856e502eab87ea8a73a76dc63a2108e5a8cc8

move more tests to self-hosted land


3 files changed, 181 insertions(+), 294 deletions(-)

src/codegen.cpp+1
...@@ -1228,6 +1228,7 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {...@@ -1228,6 +1228,7 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
1228 } else {1228 } else {
1229 err_val = expr_val;1229 err_val = expr_val;
1230 }1230 }
1231 add_debug_source_node(g, node);
1231 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);1232 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
1232 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");1233 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
1233 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapErrError");1234 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapErrError");
test/run_tests.cpp-294
...@@ -390,168 +390,6 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {...@@ -390,168 +390,6 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
3900o10700.00010e0: 0x1.1c0001p+123900o10700.00010e0: 0x1.1c0001p+12
391)OUTPUT");391)OUTPUT");
392392
393
394
395 add_simple_case("continue and break", R"SOURCE(
396const io = @import("std").io;
397pub fn main(args: [][]u8) -> %void {
398 var i : i32 = 0;
399 while (true) {
400 %%io.stdout.printf("loop\n");
401 i += 1;
402 if (i < 4) {
403 continue;
404 }
405 break;
406 }
407}
408 )SOURCE", "loop\nloop\nloop\nloop\n");
409
410 add_simple_case("@sizeof() and @typeof()", R"SOURCE(
411const io = @import("std").io;
412const x: u16 = 13;
413const z: @typeof(x) = 19;
414pub fn main(args: [][]u8) -> %void {
415 const y: @typeof(x) = 120;
416 %%io.stdout.print_u64(@sizeof(@typeof(y)));
417 %%io.stdout.printf("\n");
418}
419 )SOURCE", "2\n");
420
421 add_simple_case("pointer dereferencing", R"SOURCE(
422const io = @import("std").io;
423
424pub fn main(args: [][]u8) -> %void {
425 var x = i32(3);
426 const y = &x;
427
428 *y += 1;
429
430 if (x != 4) {
431 %%io.stdout.printf("BAD\n");
432 }
433 if (*y != 4) {
434 %%io.stdout.printf("BAD\n");
435 }
436 %%io.stdout.printf("OK\n");
437}
438 )SOURCE", "OK\n");
439
440 add_simple_case("constant expressions", R"SOURCE(
441const io = @import("std").io;
442
443const ARRAY_SIZE : i8 = 20;
444
445pub fn main(args: [][]u8) -> %void {
446 var array : [ARRAY_SIZE]u8 = undefined;
447 %%io.stdout.print_u64(@sizeof(@typeof(array)));
448 %%io.stdout.printf("\n");
449}
450 )SOURCE", "20\n");
451
452 add_simple_case("@min_value() and @max_value()", R"SOURCE(
453const io = @import("std").io;
454pub fn main(args: [][]u8) -> %void {
455 %%io.stdout.printf("max u8: ");
456 %%io.stdout.print_u64(@max_value(u8));
457 %%io.stdout.printf("\n");
458
459 %%io.stdout.printf("max u16: ");
460 %%io.stdout.print_u64(@max_value(u16));
461 %%io.stdout.printf("\n");
462
463 %%io.stdout.printf("max u32: ");
464 %%io.stdout.print_u64(@max_value(u32));
465 %%io.stdout.printf("\n");
466
467 %%io.stdout.printf("max u64: ");
468 %%io.stdout.print_u64(@max_value(u64));
469 %%io.stdout.printf("\n");
470
471 %%io.stdout.printf("max i8: ");
472 %%io.stdout.print_i64(@max_value(i8));
473 %%io.stdout.printf("\n");
474
475 %%io.stdout.printf("max i16: ");
476 %%io.stdout.print_i64(@max_value(i16));
477 %%io.stdout.printf("\n");
478
479 %%io.stdout.printf("max i32: ");
480 %%io.stdout.print_i64(@max_value(i32));
481 %%io.stdout.printf("\n");
482
483 %%io.stdout.printf("max i64: ");
484 %%io.stdout.print_i64(@max_value(i64));
485 %%io.stdout.printf("\n");
486
487 %%io.stdout.printf("min u8: ");
488 %%io.stdout.print_u64(@min_value(u8));
489 %%io.stdout.printf("\n");
490
491 %%io.stdout.printf("min u16: ");
492 %%io.stdout.print_u64(@min_value(u16));
493 %%io.stdout.printf("\n");
494
495 %%io.stdout.printf("min u32: ");
496 %%io.stdout.print_u64(@min_value(u32));
497 %%io.stdout.printf("\n");
498
499 %%io.stdout.printf("min u64: ");
500 %%io.stdout.print_u64(@min_value(u64));
501 %%io.stdout.printf("\n");
502
503 %%io.stdout.printf("min i8: ");
504 %%io.stdout.print_i64(@min_value(i8));
505 %%io.stdout.printf("\n");
506
507 %%io.stdout.printf("min i16: ");
508 %%io.stdout.print_i64(@min_value(i16));
509 %%io.stdout.printf("\n");
510
511 %%io.stdout.printf("min i32: ");
512 %%io.stdout.print_i64(@min_value(i32));
513 %%io.stdout.printf("\n");
514
515 %%io.stdout.printf("min i64: ");
516 %%io.stdout.print_i64(@min_value(i64));
517 %%io.stdout.printf("\n");
518}
519 )SOURCE",
520 "max u8: 255\n"
521 "max u16: 65535\n"
522 "max u32: 4294967295\n"
523 "max u64: 18446744073709551615\n"
524 "max i8: 127\n"
525 "max i16: 32767\n"
526 "max i32: 2147483647\n"
527 "max i64: 9223372036854775807\n"
528 "min u8: 0\n"
529 "min u16: 0\n"
530 "min u32: 0\n"
531 "min u64: 0\n"
532 "min i8: -128\n"
533 "min i16: -32768\n"
534 "min i32: -2147483648\n"
535 "min i64: -9223372036854775808\n");
536
537
538 add_simple_case("overflow intrinsics", R"SOURCE(
539const io = @import("std").io;
540pub fn main(args: [][]u8) -> %void {
541 var result: u8 = undefined;
542 if (!@add_with_overflow(u8, 250, 100, &result)) {
543 %%io.stdout.printf("BAD\n");
544 }
545 if (@add_with_overflow(u8, 100, 150, &result)) {
546 %%io.stdout.printf("BAD\n");
547 }
548 if (result != 250) {
549 %%io.stdout.printf("BAD\n");
550 }
551 %%io.stdout.printf("OK\n");
552}
553 )SOURCE", "OK\n");
554
555 add_simple_case("order-independent declarations", R"SOURCE(393 add_simple_case("order-independent declarations", R"SOURCE(
556const io = @import("std").io;394const io = @import("std").io;
557const z = io.stdin_fileno;395const z = io.stdin_fileno;
...@@ -567,18 +405,6 @@ fn print_ok(val: @typeof(x)) -> @typeof(foo) {...@@ -567,18 +405,6 @@ fn print_ok(val: @typeof(x)) -> @typeof(foo) {
567const foo : i32 = 0;405const foo : i32 = 0;
568 )SOURCE", "OK\n");406 )SOURCE", "OK\n");
569407
570 add_simple_case("nested arrays", R"SOURCE(
571const io = @import("std").io;
572
573pub fn main(args: [][]u8) -> %void {
574 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
575 for (array_of_strings) |str| {
576 %%io.stdout.printf(str);
577 %%io.stdout.printf("\n");
578 }
579}
580 )SOURCE", "hello\nthis\nis\nmy\nthing\n");
581
582 add_simple_case("for loops", R"SOURCE(408 add_simple_case("for loops", R"SOURCE(
583const io = @import("std").io;409const io = @import("std").io;
584410
...@@ -604,126 +430,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -604,126 +430,6 @@ pub fn main(args: [][]u8) -> %void {
604}430}
605 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");431 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");
606432
607 add_simple_case("function pointers", R"SOURCE(
608const io = @import("std").io;
609
610pub fn main(args: [][]u8) -> %void {
611 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
612 for (fns) |f| {
613 %%io.stdout.print_u64(f());
614 %%io.stdout.printf("\n");
615 }
616}
617
618fn fn1() -> u32 {5}
619fn fn2() -> u32 {6}
620fn fn3() -> u32 {7}
621fn fn4() -> u32 {8}
622 )SOURCE", "5\n6\n7\n8\n");
623
624 add_simple_case("statically initialized struct", R"SOURCE(
625const io = @import("std").io;
626struct Foo {
627 x: i32,
628 y: bool,
629}
630var foo = Foo { .x = 13, .y = true, };
631pub fn main(args: [][]u8) -> %void {
632 foo.x += 1;
633 if (foo.x != 14) {
634 %%io.stdout.printf("BAD\n");
635 }
636
637 %%io.stdout.printf("OK\n");
638}
639 )SOURCE", "OK\n");
640
641 add_simple_case("statically initialized array literal", R"SOURCE(
642const io = @import("std").io;
643const x = []u8{1,2,3,4};
644pub fn main(args: [][]u8) -> %void {
645 const y : [4]u8 = x;
646 if (y[3] != 4) {
647 %%io.stdout.printf("BAD\n");
648 }
649
650 %%io.stdout.printf("OK\n");
651}
652 )SOURCE", "OK\n");
653
654 add_simple_case("return with implicit cast from while loop", R"SOURCE(
655const io = @import("std").io;
656pub fn main(args: [][]u8) -> %void {
657 while (true) {
658 %%io.stdout.printf("OK\n");
659 return;
660 }
661}
662 )SOURCE", "OK\n");
663
664 add_simple_case("return struct byval from function", R"SOURCE(
665const io = @import("std").io;
666struct Foo {
667 x: i32,
668 y: i32,
669}
670fn make_foo(x: i32, y: i32) -> Foo {
671 Foo {
672 .x = x,
673 .y = y,
674 }
675}
676pub fn main(args: [][]u8) -> %void {
677 const foo = make_foo(1234, 5678);
678 if (foo.y != 5678) {
679 %%io.stdout.printf("BAD\n");
680 }
681 %%io.stdout.printf("OK\n");
682}
683 )SOURCE", "OK\n");
684
685 add_simple_case("string concatenation", R"SOURCE(
686const io = @import("std").io;
687pub fn main(args: [][]u8) -> %void {
688 %%io.stdout.printf("OK" ++ " IT " ++ "WORKED\n");
689}
690 )SOURCE", "OK IT WORKED\n");
691
692 add_simple_case("constant struct with negation", R"SOURCE(
693const io = @import("std").io;
694struct Vertex {
695 x: f32,
696 y: f32,
697 r: f32,
698 g: f32,
699 b: f32,
700}
701const vertices = []Vertex {
702 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
703 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
704 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
705};
706pub fn main(args: [][]u8) -> %void {
707 if (vertices[0].x != -0.6) {
708 %%io.stdout.printf("BAD\n");
709 }
710 %%io.stdout.printf("OK\n");
711}
712 )SOURCE", "OK\n");
713
714 add_simple_case("int to ptr cast", R"SOURCE(
715const io = @import("std").io;
716pub fn main(args: [][]u8) -> %void {
717 const x = isize(13);
718 const y = (&u8)(x);
719 const z = usize(y);
720 if (z != 13) {
721 %%io.stdout.printf("BAD\n");
722 }
723 %%io.stdout.printf("OK\n");
724}
725 )SOURCE", "OK\n");
726
727 add_simple_case("pointer to void return type", R"SOURCE(433 add_simple_case("pointer to void return type", R"SOURCE(
728const io = @import("std").io;434const io = @import("std").io;
729const x = void{};435const x = void{};
test/self_hosted.zig+180
...@@ -986,3 +986,183 @@ fn get_byte(ptr: ?&u8) -> u8 {*??ptr}...@@ -986,3 +986,183 @@ fn get_byte(ptr: ?&u8) -> u8 {*??ptr}
986fn get_first_byte(T: type)(mem: []T) -> u8 {986fn get_first_byte(T: type)(mem: []T) -> u8 {
987 get_byte((&u8)(&mem[0]))987 get_byte((&u8)(&mem[0]))
988}988}
989
990#attribute("test")
991fn continue_and_break() {
992 run_continue_and_break_test();
993 assert(continue_and_break_counter == 8);
994}
995var continue_and_break_counter: i32 = 0;
996fn run_continue_and_break_test() {
997 var i : i32 = 0;
998 while (true) {
999 continue_and_break_counter += 2;
1000 i += 1;
1001 if (i < 4) {
1002 continue;
1003 }
1004 break;
1005 }
1006 assert(i == 4);
1007}
1008
1009#attribute("test")
1010fn sizeof_and_typeof() {
1011 const y: @typeof(sizeof_and_typeof_x) = 120;
1012 assert(@sizeof(@typeof(y)) == 2);
1013}
1014const sizeof_and_typeof_x: u16 = 13;
1015const sizeof_and_typeof_z: @typeof(sizeof_and_typeof_x) = 19;
1016
1017
1018#attribute("test")
1019fn pointer_dereferencing() {
1020 var x = i32(3);
1021 const y = &x;
1022
1023 *y += 1;
1024
1025 assert(x == 4);
1026 assert(*y == 4);
1027}
1028
1029#attribute("test")
1030fn constant_expressions() {
1031 var array : [ARRAY_SIZE]u8 = undefined;
1032 assert(@sizeof(@typeof(array)) == 20);
1033}
1034const ARRAY_SIZE : i8 = 20;
1035
1036
1037#attribute("test")
1038fn min_value_and_max_value() {
1039 assert(@max_value(u8) == 255);
1040 assert(@max_value(u16) == 65535);
1041 assert(@max_value(u32) == 4294967295);
1042 assert(@max_value(u64) == 18446744073709551615);
1043
1044 assert(@max_value(i8) == 127);
1045 assert(@max_value(i16) == 32767);
1046 assert(@max_value(i32) == 2147483647);
1047 assert(@max_value(i64) == 9223372036854775807);
1048
1049 assert(@min_value(u8) == 0);
1050 assert(@min_value(u16) == 0);
1051 assert(@min_value(u32) == 0);
1052 assert(@min_value(u64) == 0);
1053
1054 assert(@min_value(i8) == -128);
1055 assert(@min_value(i16) == -32768);
1056 assert(@min_value(i32) == -2147483648);
1057 assert(@min_value(i64) == -9223372036854775808);
1058}
1059
1060#attribute("test")
1061fn overflow_intrinsics() {
1062 var result: u8 = undefined;
1063 assert(@add_with_overflow(u8, 250, 100, &result));
1064 assert(!@add_with_overflow(u8, 100, 150, &result));
1065 assert(result == 250);
1066}
1067
1068
1069#attribute("test")
1070fn nested_arrays() {
1071 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1072 for (array_of_strings) |str, i| {
1073 if (i == 0) assert(str_eql(str, "hello"));
1074 if (i == 1) assert(str_eql(str, "this"));
1075 if (i == 2) assert(str_eql(str, "is"));
1076 if (i == 3) assert(str_eql(str, "my"));
1077 if (i == 4) assert(str_eql(str, "thing"));
1078 }
1079}
1080
1081#attribute("test")
1082fn int_to_ptr_cast() {
1083 const x = isize(13);
1084 const y = (&u8)(x);
1085 const z = usize(y);
1086 assert(z == 13);
1087}
1088
1089#attribute("test")
1090fn string_concatenation() {
1091 assert(str_eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
1092}
1093
1094#attribute("test")
1095fn constant_struct_with_negation() {
1096 assert(vertices[0].x == -0.6);
1097}
1098struct Vertex {
1099 x: f32,
1100 y: f32,
1101 r: f32,
1102 g: f32,
1103 b: f32,
1104}
1105const vertices = []Vertex {
1106 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
1107 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
1108 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
1109};
1110
1111
1112#attribute("test")
1113fn return_with_implicit_cast_from_while_loop() {
1114 %%return_with_implicit_cast_from_while_loop_test();
1115}
1116fn return_with_implicit_cast_from_while_loop_test() -> %void {
1117 while (true) {
1118 return;
1119 }
1120}
1121
1122#attribute("test")
1123fn return_struct_byval_from_function() {
1124 const bar = make_bar(1234, 5678);
1125 assert(bar.y == 5678);
1126}
1127struct Bar {
1128 x: i32,
1129 y: i32,
1130}
1131fn make_bar(x: i32, y: i32) -> Bar {
1132 Bar {
1133 .x = x,
1134 .y = y,
1135 }
1136}
1137
1138#attribute("test")
1139fn function_pointers() {
1140 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
1141 for (fns) |f, i| {
1142 assert(f() == u32(i) + 5);
1143 }
1144}
1145fn fn1() -> u32 {5}
1146fn fn2() -> u32 {6}
1147fn fn3() -> u32 {7}
1148fn fn4() -> u32 {8}
1149
1150
1151
1152#attribute("test")
1153fn statically_initalized_struct() {
1154 st_init_str_foo.x += 1;
1155 assert(st_init_str_foo.x == 14);
1156}
1157struct StInitStrFoo {
1158 x: i32,
1159 y: bool,
1160}
1161var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
1162
1163#attribute("test")
1164fn statically_initialized_array_literal() {
1165 const y : [4]u8 = st_init_arr_lit_x;
1166 assert(y[3] == 4);
1167}
1168const st_init_arr_lit_x = []u8{1,2,3,4};