Updated common tests
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
pub module decl_test;
|
||||
|
||||
pub fn void test_let_basic() {
|
||||
let x = 42;
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void test_let_typed() {
|
||||
let x i32 = 42;
|
||||
assert_eq!(x, 42);
|
||||
|
||||
let s *str = "hello";
|
||||
assert_eq!(s, "hello");
|
||||
|
||||
let b bool = true;
|
||||
assert_eq!(b, true);
|
||||
|
||||
let f f64 = 3.14;
|
||||
assert_eq!(f, 3.14);
|
||||
}
|
||||
|
||||
pub fn void test_let_mut() {
|
||||
let mut _x = 42;
|
||||
_x = 100;
|
||||
assert_eq!(_x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_let_tuple_destructure() {
|
||||
let (a, b) = (10, "hello");
|
||||
assert_eq!(a, 10);
|
||||
assert_eq!(b, "hello");
|
||||
}
|
||||
|
||||
pub fn void test_let_nested_tuple() {
|
||||
let (a, (b, c)) = (1, (2, 3));
|
||||
assert_eq!(a, 1);
|
||||
assert_eq!(b, 2);
|
||||
assert_eq!(c, 3);
|
||||
}
|
||||
|
||||
pub fn void test_let_pattern_irrefutable() {
|
||||
let x = 5;
|
||||
let (y, z) = (1, 2);
|
||||
assert_eq!(x, 5);
|
||||
assert_eq!(y, 1);
|
||||
assert_eq!(z, 2);
|
||||
}
|
||||
|
||||
pub fn void test_multiple_decls() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
assert_eq!(a + b + c, 6);
|
||||
}
|
||||
|
||||
pub fn void test_let_with_type_and_init() {
|
||||
let x f64 = 10.0;
|
||||
assert_eq!(x, 10.0);
|
||||
}
|
||||
|
||||
pub fn void test_string_literal() {
|
||||
let s = "hello, world!";
|
||||
assert_eq!(s.len(), 13);
|
||||
}
|
||||
|
||||
pub fn void test_let_bool() {
|
||||
let t = true;
|
||||
let f = false;
|
||||
assert_eq!(t, !f);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_let_basic();
|
||||
test_let_typed();
|
||||
test_let_mut();
|
||||
test_let_tuple_destructure();
|
||||
test_let_nested_tuple();
|
||||
test_let_pattern_irrefutable();
|
||||
test_multiple_decls();
|
||||
test_let_with_type_and_init();
|
||||
test_string_literal();
|
||||
test_let_bool();
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
pub module expr_test;
|
||||
|
||||
pub fn void test_literals() {
|
||||
assert_eq!(42, 42);
|
||||
assert_eq!(3.14, 3.14);
|
||||
assert_eq!(true, true);
|
||||
assert_eq!(false, false);
|
||||
assert_eq!("hello", "hello");
|
||||
}
|
||||
|
||||
pub fn void test_tuples() {
|
||||
let t = (1, true, "three");
|
||||
assert_eq!(t.0, 1);
|
||||
assert_eq!(t.1, true);
|
||||
assert_eq!(t.2, "three");
|
||||
}
|
||||
|
||||
pub fn void test_arrays() {
|
||||
let arr = [1, 2, 3];
|
||||
assert_eq!(arr[0], 1);
|
||||
assert_eq!(arr[1], 2);
|
||||
assert_eq!(arr[2], 3);
|
||||
}
|
||||
|
||||
pub fn void test_array_repeat() {
|
||||
let arr = [0; 5];
|
||||
assert_eq!(arr[0], 0);
|
||||
assert_eq!(arr[4], 0);
|
||||
assert_eq!(arr.len(), 5);
|
||||
}
|
||||
|
||||
pub fn void test_binary_arith() {
|
||||
assert_eq!(10 + 5, 15);
|
||||
assert_eq!(10 - 5, 5);
|
||||
assert_eq!(10 * 5, 50);
|
||||
assert_eq!(10 / 5, 2);
|
||||
assert_eq!(10 % 3, 1);
|
||||
}
|
||||
|
||||
pub fn void test_binary_cmp() {
|
||||
assert_eq!(5 < 10, true);
|
||||
assert_eq!(10 <= 10, true);
|
||||
assert_eq!(10 > 5, true);
|
||||
assert_eq!(10 >= 10, true);
|
||||
assert_eq!(5 == 5, true);
|
||||
assert_eq!(5 != 10, true);
|
||||
}
|
||||
|
||||
pub fn void test_binary_logic() {
|
||||
assert_eq!(true && true, true);
|
||||
assert_eq!(true && false, false);
|
||||
assert_eq!(true || false, true);
|
||||
assert_eq!(false || false, false);
|
||||
}
|
||||
|
||||
pub fn void test_binary_bit() {
|
||||
assert_eq!(5 & 3, 1);
|
||||
assert_eq!(5 | 3, 7);
|
||||
assert_eq!(5 ^ 3, 6);
|
||||
assert_eq!(1 << 2, 4);
|
||||
assert_eq!(4 >> 1, 2);
|
||||
}
|
||||
|
||||
pub fn void test_prefix() {
|
||||
let x = 42;
|
||||
assert_eq!(-x, -42);
|
||||
assert_eq!(!true, false);
|
||||
assert_eq!(!false, true);
|
||||
}
|
||||
|
||||
pub fn void test_ref_deref() {
|
||||
let x = 42;
|
||||
let r = &x;
|
||||
assert_eq!(*r, 42);
|
||||
}
|
||||
|
||||
pub fn void test_postfix_inc_dec() {
|
||||
let mut x = 5;
|
||||
x++;
|
||||
assert_eq!(x, 6);
|
||||
x--;
|
||||
assert_eq!(x, 5);
|
||||
}
|
||||
|
||||
pub fn void test_as_cast() {
|
||||
let x = 42;
|
||||
assert_eq!(x as f64, 42.0);
|
||||
}
|
||||
|
||||
pub fn void test_closure() {
|
||||
let add = fn(a, b) -> a + b;
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
pub fn void test_block_expr() {
|
||||
let x = {
|
||||
let y = 10;
|
||||
y + 5
|
||||
};
|
||||
assert_eq!(x, 15);
|
||||
}
|
||||
|
||||
pub fn void test_if_expr() {
|
||||
let x = if true { 1 } else { 2 };
|
||||
assert_eq!(x, 1);
|
||||
|
||||
let y = if false { 1 } else { 2 };
|
||||
assert_eq!(y, 2);
|
||||
}
|
||||
|
||||
pub fn void test_range_expr() {
|
||||
let r = 0..5;
|
||||
let mut count = 0;
|
||||
for i in r {
|
||||
count++;
|
||||
}
|
||||
assert_eq!(count, 5);
|
||||
}
|
||||
|
||||
pub fn void test_binary_assign() {
|
||||
let mut x = 10;
|
||||
x += 5;
|
||||
assert_eq!(x, 15);
|
||||
x -= 3;
|
||||
assert_eq!(x, 12);
|
||||
x *= 2;
|
||||
assert_eq!(x, 24);
|
||||
x /= 4;
|
||||
assert_eq!(x, 6);
|
||||
x %= 4;
|
||||
assert_eq!(x, 2);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_literals();
|
||||
test_tuples();
|
||||
test_arrays();
|
||||
test_array_repeat();
|
||||
test_binary_arith();
|
||||
test_binary_cmp();
|
||||
test_binary_logic();
|
||||
test_binary_bit();
|
||||
test_prefix();
|
||||
test_ref_deref();
|
||||
test_postfix_inc_dec();
|
||||
test_as_cast();
|
||||
test_closure();
|
||||
test_block_expr();
|
||||
test_if_expr();
|
||||
test_range_expr();
|
||||
test_binary_assign();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub module common;
|
||||
|
||||
pub fn void test_package() {}
|
||||
@@ -0,0 +1,167 @@
|
||||
pub module statement_test;
|
||||
|
||||
pub fn void test_var_decl() {
|
||||
let x = 42;
|
||||
assert_eq!(x, 42);
|
||||
|
||||
let y i32 = 10;
|
||||
assert_eq!(y, 10);
|
||||
|
||||
let name *str = "mist";
|
||||
assert_eq!(name, "mist");
|
||||
}
|
||||
|
||||
pub fn void test_var_decl_with_init() {
|
||||
let mut x = 0;
|
||||
x = 5;
|
||||
assert_eq!(x, 5);
|
||||
}
|
||||
|
||||
pub fn void test_if_statement() {
|
||||
let mut x = 0;
|
||||
|
||||
if true {
|
||||
x = 1;
|
||||
}
|
||||
assert_eq!(x, 1);
|
||||
|
||||
if false {
|
||||
x = 2;
|
||||
} else {
|
||||
x = 3;
|
||||
}
|
||||
assert_eq!(x, 3);
|
||||
}
|
||||
|
||||
pub fn void test_if_else_if() {
|
||||
let mut x = 0;
|
||||
|
||||
if x == 1 {
|
||||
x = 10;
|
||||
} else if x == 0 {
|
||||
x = 20;
|
||||
} else {
|
||||
x = 30;
|
||||
}
|
||||
assert_eq!(x, 20);
|
||||
}
|
||||
|
||||
pub fn void test_while_loop() {
|
||||
let mut count = 0;
|
||||
|
||||
while count < 5 {
|
||||
count++;
|
||||
}
|
||||
assert_eq!(count, 5);
|
||||
}
|
||||
|
||||
pub fn void test_for_loop() {
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 0..4 {
|
||||
sum += i;
|
||||
}
|
||||
assert_eq!(sum, 6);
|
||||
}
|
||||
|
||||
pub fn void test_loop_stmt() {
|
||||
let mut count = 0;
|
||||
|
||||
loop {
|
||||
count++;
|
||||
if count >= 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_eq!(count, 3);
|
||||
}
|
||||
|
||||
pub fn void test_break_continue() {
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 0..10 {
|
||||
if i % 2 == 0 {
|
||||
continue;
|
||||
}
|
||||
if i >= 7 {
|
||||
break;
|
||||
}
|
||||
sum += i;
|
||||
}
|
||||
assert_eq!(sum, 1 + 3 + 5);
|
||||
}
|
||||
|
||||
pub fn i32 test_return() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
pub fn void test_block_scope() {
|
||||
let x = {
|
||||
let inner = 10;
|
||||
inner + 5
|
||||
};
|
||||
assert_eq!(x, 15);
|
||||
}
|
||||
|
||||
pub fn void test_match_literal() {
|
||||
let x = 2;
|
||||
let result;
|
||||
|
||||
match x {
|
||||
1 => { result = 10; }
|
||||
2 => { result = 20; }
|
||||
3 => { result = 30; }
|
||||
_ => panic!();
|
||||
}
|
||||
assert_eq!(result, 20);
|
||||
}
|
||||
|
||||
pub fn void test_match_or_pattern() {
|
||||
let x = 1;
|
||||
let result i32;
|
||||
|
||||
match x {
|
||||
1 | 2 => { result = 100; }
|
||||
3 => { result = 200; }
|
||||
_ => panic!();
|
||||
}
|
||||
assert_eq!(result, 100);
|
||||
}
|
||||
|
||||
pub fn void test_match_default() {
|
||||
let x = 99;
|
||||
let result;
|
||||
|
||||
match x {
|
||||
1 => { result = 10; }
|
||||
y => { result = y; }
|
||||
}
|
||||
assert_eq!(result, 99);
|
||||
}
|
||||
|
||||
pub fn void test_for_with_pattern_destructure() {
|
||||
let pairs = [(1, "a"), (2, "b")];
|
||||
let mut keys = 0;
|
||||
|
||||
for (k, _) in pairs {
|
||||
keys += k;
|
||||
}
|
||||
assert_eq!(keys, 3);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_var_decl();
|
||||
test_var_decl_with_init();
|
||||
test_if_statement();
|
||||
test_if_else_if();
|
||||
test_while_loop();
|
||||
test_for_loop();
|
||||
test_loop_stmt();
|
||||
test_break_continue();
|
||||
assert_eq!(test_return(), 42);
|
||||
test_block_scope();
|
||||
test_match_literal();
|
||||
test_match_or_pattern();
|
||||
test_match_default();
|
||||
test_for_with_pattern_destructure();
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
pub module types_test;
|
||||
|
||||
pub fn void test_integer_type() {
|
||||
let x i32 = 42;
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void test_float_type() {
|
||||
let x f64 = 3.14;
|
||||
assert_eq!(x, 3.14);
|
||||
}
|
||||
|
||||
pub fn void test_bool_type() {
|
||||
let x bool = true;
|
||||
assert_eq!(x, true);
|
||||
|
||||
let y bool = false;
|
||||
assert_eq!(y, false);
|
||||
}
|
||||
|
||||
pub fn void test_string_type() {
|
||||
let s *str = "hello";
|
||||
assert_eq!(s, "hello");
|
||||
}
|
||||
|
||||
pub fn void test_tuple_type() {
|
||||
let t (i32, *str) = (42, "answer");
|
||||
assert_eq!(t.0, 42);
|
||||
assert_eq!(t.1, "answer");
|
||||
}
|
||||
|
||||
pub fn void test_ref_type() {
|
||||
let x i32 = 42;
|
||||
let r *i32 = &x;
|
||||
assert_eq!(*r, 42);
|
||||
}
|
||||
|
||||
pub fn void test_mut_ref_type() {
|
||||
let mut x = 42;
|
||||
let r *mut i32 = &mut x;
|
||||
*r = 100;
|
||||
assert_eq!(x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_void_type() {}
|
||||
|
||||
fn i32 add(a i32, b i32) {
|
||||
a + b
|
||||
}
|
||||
|
||||
pub fn void test_fn_return_type() {
|
||||
assert_eq!(add(3, 4), 7);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_integer_type();
|
||||
test_float_type();
|
||||
test_bool_type();
|
||||
test_string_type();
|
||||
test_tuple_type();
|
||||
test_ref_type();
|
||||
test_mut_ref_type();
|
||||
test_void_type();
|
||||
test_fn_return_type();
|
||||
}
|
||||
Reference in New Issue
Block a user