move some tests into self hosted

This commit is contained in:
Andrew Kelley 2016-04-10 13:58:04 -07:00
parent b117b5907c
commit 4a3bce4b63
2 changed files with 194 additions and 222 deletions

View File

@ -252,82 +252,6 @@ pub const b_text = a_text;
)SOURCE");
}
add_simple_case("params", R"SOURCE(
const io = @import("std").io;
fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn main(args: [][]u8) -> %void {
if (add(22, 11) == 33) {
%%io.stdout.printf("pass\n");
}
}
)SOURCE", "pass\n");
add_simple_case("void parameters", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
void_fun(1, void{}, 2);
}
fn void_fun(a : i32, b : void, c : i32) {
const v = b;
const vv : void = if (a == 1) {v} else {};
if (a + c == 3) { %%io.stdout.printf("OK\n"); }
return vv;
}
)SOURCE", "OK\n");
add_simple_case("mutable local variables", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
var zero : i32 = 0;
if (zero == 0) { %%io.stdout.printf("zero\n"); }
var i = i32(0);
while (i != 3) {
%%io.stdout.printf("loop\n");
i += 1;
}
}
)SOURCE", "zero\nloop\nloop\nloop\n");
add_simple_case("arrays", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
var array : [5]i32 = undefined;
var i : i32 = 0;
while (i < 5) {
array[i] = i + 1;
i = array[i];
}
i = 0;
var accumulator = i32(0);
while (i < 5) {
accumulator += array[i];
i += 1;
}
if (accumulator == 15) {
%%io.stdout.printf("OK\n");
}
if (get_array_len(array) != 5) {
%%io.stdout.printf("BAD\n");
}
}
fn get_array_len(a: []i32) -> isize {
a.len
}
)SOURCE", "OK\n");
add_simple_case("hello world without libc", R"SOURCE(
@ -339,49 +263,6 @@ pub fn main(args: [][]u8) -> %void {
)SOURCE", "Hello, world!\n");
add_simple_case("short circuit", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
if (true || { %%io.stdout.printf("BAD 1\n"); false }) {
%%io.stdout.printf("OK 1\n");
}
if (false || { %%io.stdout.printf("OK 2\n"); false }) {
%%io.stdout.printf("BAD 2\n");
}
if (true && { %%io.stdout.printf("OK 3\n"); false }) {
%%io.stdout.printf("BAD 3\n");
}
if (false && { %%io.stdout.printf("BAD 4\n"); false }) {
} else {
%%io.stdout.printf("OK 4\n");
}
}
)SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n");
add_simple_case("modify operators", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
var i : i32 = 0;
i += 5; if (i != 5) { %%io.stdout.printf("BAD +=\n"); }
i -= 2; if (i != 3) { %%io.stdout.printf("BAD -=\n"); }
i *= 20; if (i != 60) { %%io.stdout.printf("BAD *=\n"); }
i /= 3; if (i != 20) { %%io.stdout.printf("BAD /=\n"); }
i %= 11; if (i != 9) { %%io.stdout.printf("BAD %=\n"); }
i <<= 1; if (i != 18) { %%io.stdout.printf("BAD <<=\n"); }
i >>= 2; if (i != 4) { %%io.stdout.printf("BAD >>=\n"); }
i = 6;
i &= 5; if (i != 4) { %%io.stdout.printf("BAD &=\n"); }
i ^= 6; if (i != 2) { %%io.stdout.printf("BAD ^=\n"); }
i = 6;
i |= 3; if (i != 7) { %%io.stdout.printf("BAD |=\n"); }
%%io.stdout.printf("OK\n");
}
)SOURCE", "OK\n");
add_simple_case_libc("number literals", R"SOURCE(
const c = @c_import(@c_include("stdio.h"));
@ -508,110 +389,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
0o10700.00010e0: 0x1.1c0001p+12
)OUTPUT");
add_simple_case("structs", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
var foo : Foo = undefined;
@memset(&foo, 0, @sizeof(Foo));
foo.a += 1;
foo.b = foo.a == 1;
test_foo(foo);
test_mutation(&foo);
if (foo.c != 100) {
%%io.stdout.printf("BAD\n");
}
test_point_to_self();
test_byval_assign();
test_initializer();
%%io.stdout.printf("OK\n");
}
struct Foo {
a : i32,
b : bool,
c : f32,
}
fn test_foo(foo : Foo) {
if (!foo.b) {
%%io.stdout.printf("BAD\n");
}
}
fn test_mutation(foo : &Foo) {
foo.c = 100;
}
struct Node {
val: Val,
next: &Node,
}
struct Val {
x: i32,
}
fn test_point_to_self() {
var root : Node = undefined;
root.val.x = 1;
var node : Node = undefined;
node.next = &root;
node.val.x = 2;
root.next = &node;
if (node.next.next.next.val.x != 1) {
%%io.stdout.printf("BAD\n");
}
}
fn test_byval_assign() {
var foo1 : Foo = undefined;
var foo2 : Foo = undefined;
foo1.a = 1234;
if (foo2.a != 0) { %%io.stdout.printf("BAD\n"); }
foo2 = foo1;
if (foo2.a != 1234) { %%io.stdout.printf("BAD - byval assignment failed\n"); }
}
fn test_initializer() {
const val = Val { .x = 42 };
if (val.x != 42) { %%io.stdout.printf("BAD\n"); }
}
)SOURCE", "OK\n");
add_simple_case("global variables", R"SOURCE(
const io = @import("std").io;
const g1 : i32 = 1233 + 1;
var g2 : i32 = 0;
pub fn main(args: [][]u8) -> %void {
if (g2 != 0) { %%io.stdout.printf("BAD\n"); }
g2 = g1;
if (g2 != 1234) { %%io.stdout.printf("BAD\n"); }
%%io.stdout.printf("OK\n");
}
)SOURCE", "OK\n");
add_simple_case("while loop", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
var i : i32 = 0;
while (i < 4) {
%%io.stdout.printf("loop\n");
i += 1;
}
g();
}
fn g() -> i32 {
return f();
}
fn f() -> i32 {
while (true) {
return 0;
}
}
)SOURCE", "loop\nloop\nloop\nloop\n");
add_simple_case("continue and break", R"SOURCE(
const io = @import("std").io;

View File

@ -44,6 +44,15 @@ fn first_eql_third(a: i32, b: i32, c: i32) {
}
#attribute("test")
fn params() {
assert(test_params_add(22, 11) == 33);
}
fn test_params_add(a: i32, b: i32) -> i32 {
a + b
}
#attribute("test")
fn local_variables() {
test_loc_vars(2);
@ -59,6 +68,99 @@ fn bool_literals() {
assert(!false);
}
#attribute("test")
fn void_parameters() {
void_fun(1, void{}, 2, {});
}
fn void_fun(a : i32, b : void, c : i32, d : void) {
const v = b;
const vv : void = if (a == 1) {v} else {};
assert(a + c == 3);
return vv;
}
#attribute("test")
fn mutable_local_variables() {
var zero : i32 = 0;
assert(zero == 0);
var i = i32(0);
while (i != 3) {
i += 1;
}
assert(i == 3);
}
#attribute("test")
fn arrays() {
var array : [5]i32 = undefined;
var i : i32 = 0;
while (i < 5) {
array[i] = i + 1;
i = array[i];
}
i = 0;
var accumulator = i32(0);
while (i < 5) {
accumulator += array[i];
i += 1;
}
assert(accumulator == 15);
assert(get_array_len(array) == 5);
}
fn get_array_len(a: []i32) -> isize {
a.len
}
#attribute("test")
fn short_circuit() {
var hit_1 = false;
var hit_2 = false;
var hit_3 = false;
var hit_4 = false;
if (true || { assert(false); false }) {
hit_1 = true;
}
if (false || { hit_2 = true; false }) {
assert(false);
}
if (true && { hit_3 = true; false }) {
%%io.stdout.printf("BAD 3\n");
}
if (false && { assert(false); false }) {
assert(false);
} else {
hit_4 = true;
}
assert(hit_1);
assert(hit_2);
assert(hit_3);
assert(hit_4);
}
#attribute("test")
fn modify_operators() {
var i : i32 = 0;
i += 5; assert(i == 5);
i -= 2; assert(i == 3);
i *= 20; assert(i == 60);
i /= 3; assert(i == 20);
i %= 11; assert(i == 9);
i <<= 1; assert(i == 18);
i >>= 2; assert(i == 4);
i = 6;
i &= 5; assert(i == 4);
i ^= 6; assert(i == 2);
i = 6;
i |= 3; assert(i == 7);
}
#attribute("test")
fn separate_block_scopes() {
@ -92,6 +194,98 @@ struct VoidStructFieldsFoo {
}
#attribute("test")
pub fn structs() {
var foo : StructFoo = undefined;
@memset(&foo, 0, @sizeof(StructFoo));
foo.a += 1;
foo.b = foo.a == 1;
test_foo(foo);
test_mutation(&foo);
assert(foo.c == 100);
}
struct StructFoo {
a : i32,
b : bool,
c : f32,
}
fn test_foo(foo : StructFoo) {
assert(foo.b);
}
fn test_mutation(foo : &StructFoo) {
foo.c = 100;
}
struct Node {
val: Val,
next: &Node,
}
struct Val {
x: i32,
}
#attribute("test")
fn struct_point_to_self() {
var root : Node = undefined;
root.val.x = 1;
var node : Node = undefined;
node.next = &root;
node.val.x = 2;
root.next = &node;
assert(node.next.next.next.val.x == 1);
}
#attribute("test")
fn struct_byval_assign() {
var foo1 : StructFoo = undefined;
var foo2 : StructFoo = undefined;
foo1.a = 1234;
foo2.a = 0;
assert(foo2.a == 0);
foo2 = foo1;
assert(foo2.a == 1234);
}
fn struct_initializer() {
const val = Val { .x = 42 };
assert(val.x == 42);
}
const g1 : i32 = 1233 + 1;
var g2 : i32 = 0;
#attribute("test")
fn global_variables() {
assert(g2 == 0);
g2 = g1;
assert(g2 == 1234);
}
#attribute("test")
fn while_loop() {
var i : i32 = 0;
while (i < 4) {
i += 1;
}
assert(i == 4);
assert(while_loop_1() == 1);
}
fn while_loop_1() -> i32 {
return while_loop_2();
}
fn while_loop_2() -> i32 {
while (true) {
return 1;
}
}
#attribute("test")
fn void_arrays() {
var array: [4]void = undefined;