Add test code
This commit is contained in:
parent
3d17813eb4
commit
b013ba0e55
43
test/arith.w12
Normal file
43
test/arith.w12
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
fn factorial(n: int) -> int {
|
||||||
|
var result: int;
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
result = 1;
|
||||||
|
} else {
|
||||||
|
result = n * factorial(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_positive(n: int) -> bool {
|
||||||
|
return n > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> int {
|
||||||
|
var a: int = 5;
|
||||||
|
printf("a = %d\n", a);
|
||||||
|
|
||||||
|
var b: int = 3;
|
||||||
|
printf("b = %d\n", b);
|
||||||
|
|
||||||
|
var c: int = (a + b) * 2 - 1;
|
||||||
|
c += factorial(5);
|
||||||
|
|
||||||
|
printf("c = %d\n", c);
|
||||||
|
|
||||||
|
while c > 0 {
|
||||||
|
printf("c = %d\n", c);
|
||||||
|
|
||||||
|
if c > 50 {
|
||||||
|
printf("c is greater than 50\n");
|
||||||
|
} else {
|
||||||
|
printf("c is less than or equal to 50\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
c -= 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("End result: %d\n", c);
|
||||||
|
return 0;
|
||||||
|
}
|
1
test/bad.w12
Normal file
1
test/bad.w12
Normal file
@ -0,0 +1 @@
|
|||||||
|
function test() {}
|
21
test/basic-structs.w12
Normal file
21
test/basic-structs.w12
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
struct Test {
|
||||||
|
a: int,
|
||||||
|
b: char,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> int {
|
||||||
|
var t: Test;
|
||||||
|
t.a = 5;
|
||||||
|
t.b = 'a';
|
||||||
|
|
||||||
|
printf("t.a = %d\n", t.a);
|
||||||
|
printf("t.b = %c\n", t.b);
|
||||||
|
|
||||||
|
t.a += 3;
|
||||||
|
t.b = 'b';
|
||||||
|
|
||||||
|
printf("t.a = %d\n", t.a);
|
||||||
|
printf("t.b = %c\n", t.b);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
0
test/empty.w12
Normal file
0
test/empty.w12
Normal file
38
test/fib.w12
Normal file
38
test/fib.w12
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
fn rec_fib(n: int) -> int {
|
||||||
|
if n <= 1 {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rec_fib(n - 1) + rec_fib(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loop_fib(n: int) -> int {
|
||||||
|
var a: int = 0;
|
||||||
|
var b: int = 1;
|
||||||
|
var i: int = 0;
|
||||||
|
|
||||||
|
while i < n {
|
||||||
|
var c: int = a + b;
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> int {
|
||||||
|
var n: int = 20;
|
||||||
|
var rec_result: int = rec_fib(n);
|
||||||
|
var loop_result: int = loop_fib(n);
|
||||||
|
printf("Fibonacci of %d via recursion is %d\n", n, rec_result);
|
||||||
|
printf("Fibonacci of %d via loop is %d\n", n, loop_result);
|
||||||
|
|
||||||
|
if rec_result == loop_result {
|
||||||
|
printf("Results match\n");
|
||||||
|
} else {
|
||||||
|
printf("Results do not match\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
32
test/functions.w12
Normal file
32
test/functions.w12
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
fn mult3(a: int, b: int, c: int) -> int {
|
||||||
|
return a * b * c;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loop_test() -> int {
|
||||||
|
var i: int = 0;
|
||||||
|
var result: int = 0;
|
||||||
|
|
||||||
|
while i < 10 {
|
||||||
|
printf("i = %d\n", i);
|
||||||
|
result += i;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> int {
|
||||||
|
var a: int = 5;
|
||||||
|
var b: int = 3;
|
||||||
|
var c: int = 2;
|
||||||
|
|
||||||
|
var result: int = mult3(a, b, c);
|
||||||
|
printf("Result: %d\n", result);
|
||||||
|
|
||||||
|
result = loop_test();
|
||||||
|
printf("Result: %d\n", result);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user