Added items
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
pub module attribute_test;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Debuggable {
|
||||
value i32,
|
||||
}
|
||||
|
||||
pub fn void test_derive_attribute() {
|
||||
let d = Debuggable { value: 42 };
|
||||
assert_eq!(d.value, 42);
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn void test_allow_attribute() {
|
||||
let x = 42;
|
||||
let y = "unused";
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_derive_attribute();
|
||||
test_allow_attribute();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
pub module class_test;
|
||||
|
||||
pub class Animal<T> {
|
||||
pub name String;
|
||||
pub val T;
|
||||
|
||||
constructor() {
|
||||
self.name = "Rex".to_string();
|
||||
}
|
||||
|
||||
pub fn void speak(*self) {
|
||||
println!("Unknown");
|
||||
}
|
||||
|
||||
pub fn void test_speak(*self) {
|
||||
self.speak();
|
||||
}
|
||||
|
||||
pub fn void static_method() {}
|
||||
}
|
||||
|
||||
pub class Mammal<T> : Animal<T> {
|
||||
constructor() {
|
||||
super = Super::new();
|
||||
}
|
||||
|
||||
pub fn void args_test(*self, x i32, y *str) {}
|
||||
|
||||
pub fn *'a str returning<'a>(*self) {
|
||||
"Mammal"
|
||||
}
|
||||
}
|
||||
|
||||
pub class Dog : Mammal<i32> {
|
||||
constructor() {
|
||||
super = Super::new();
|
||||
}
|
||||
|
||||
pub override(Animal<i32>) fn void speak(*self) {
|
||||
println!("Woof!");
|
||||
}
|
||||
|
||||
pub override fn *str returning(*self) {
|
||||
"Dog"
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
let dawg = Dog::new();
|
||||
|
||||
dawg.args_test(10, "hello");
|
||||
|
||||
dawg.test_speak();
|
||||
|
||||
println!("{}", dawg.returning());
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
pub module enum_test;
|
||||
|
||||
enum OptionInt {
|
||||
None,
|
||||
Some(i32),
|
||||
}
|
||||
|
||||
pub fn void test_enum_named() {
|
||||
let x = OptionInt::None;
|
||||
match x {
|
||||
OptionInt::None => { assert_eq!(1, 1); }
|
||||
OptionInt::Some(_) => { assert_eq!(0, 1); }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_enum_tuple() {
|
||||
let x = OptionInt::Some(42);
|
||||
match x {
|
||||
OptionInt::None => { assert_eq!(0, 1); }
|
||||
OptionInt::Some(v) => { assert_eq!(v, 42); }
|
||||
}
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Circle { radius i32 },
|
||||
Rect { w i32, h i32 },
|
||||
}
|
||||
|
||||
pub fn void test_enum_struct() {
|
||||
let c = Shape::Circle { radius: 5 };
|
||||
match c {
|
||||
Shape::Circle { radius } => { assert_eq!(radius, 5); }
|
||||
Shape::Rect { .. } => { assert_eq!(0, 1); }
|
||||
}
|
||||
|
||||
let r = Shape::Rect { w: 3, h: 4 };
|
||||
match r {
|
||||
Shape::Circle { .. } => { assert_eq!(0, 1); }
|
||||
Shape::Rect { w, h } => { assert_eq!(w * h, 12); }
|
||||
}
|
||||
}
|
||||
|
||||
enum Color {
|
||||
Red,
|
||||
Green,
|
||||
Blue,
|
||||
}
|
||||
|
||||
pub fn void test_enum_eq() {
|
||||
let c = Color::Red;
|
||||
match c {
|
||||
Color::Red => { assert_eq!(1, 1); }
|
||||
_ => { assert_eq!(0, 1); }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_enum_named();
|
||||
test_enum_tuple();
|
||||
test_enum_struct();
|
||||
test_enum_eq();
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
pub module function_test;
|
||||
|
||||
fn void greet() {
|
||||
let mut x = 1;
|
||||
x++;
|
||||
}
|
||||
|
||||
pub fn void test_basic_function() {
|
||||
greet();
|
||||
}
|
||||
|
||||
fn i32 add(a i32, b i32) {
|
||||
a + b
|
||||
}
|
||||
|
||||
pub fn void test_fn_params() {
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_return() {
|
||||
42
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_early_return() {
|
||||
return 100;
|
||||
0
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_mut_param(mut x i32) {
|
||||
x = x + 10;
|
||||
x
|
||||
}
|
||||
|
||||
pub fn void test_method_call() {
|
||||
let s = "hello";
|
||||
assert_eq!(s.len(), 5);
|
||||
assert_eq!(s.to_uppercase(), "HELLO");
|
||||
}
|
||||
|
||||
fn i32 triple(x i32) {
|
||||
add(x, add(x, x))
|
||||
}
|
||||
|
||||
pub fn void test_nested_fn_calls() {
|
||||
assert_eq!(triple(5), 15);
|
||||
}
|
||||
|
||||
fn i32 factorial(n i32) {
|
||||
if n <= 1 { 1 }
|
||||
else { n * factorial(n - 1) }
|
||||
}
|
||||
|
||||
pub fn void test_recursive_fn() {
|
||||
assert_eq!(factorial(5), 120);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_basic_function();
|
||||
test_fn_params();
|
||||
assert_eq!(test_fn_return(), 42);
|
||||
assert_eq!(test_fn_early_return(), 100);
|
||||
assert_eq!(test_fn_mut_param(5), 15);
|
||||
test_method_call();
|
||||
test_nested_fn_calls();
|
||||
test_recursive_fn();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
pub module impl_test;
|
||||
|
||||
struct Vector2 {
|
||||
x i32,
|
||||
y i32,
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn i32 magnitude(*self) {
|
||||
self.x * self.x + self.y * self.y
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_inherent_impl() {
|
||||
let v = Vector2 { x: 3, y: 4 };
|
||||
assert_eq!(v.magnitude(), 25);
|
||||
}
|
||||
|
||||
trait Display {
|
||||
fn String display(*self);
|
||||
}
|
||||
|
||||
impl Display for Vector2 {
|
||||
fn String display(*self) {
|
||||
format!("({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_trait_impl_block() {
|
||||
let v = Vector2 { x: 1, y: 2 };
|
||||
let s = v.display();
|
||||
assert_eq!(s.len() > 0, true);
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn Vector2 add(*self, other Vector2) {
|
||||
Vector2 {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_multiple_impls() {
|
||||
let a = Vector2 { x: 1, y: 2 };
|
||||
let b = Vector2 { x: 3, y: 4 };
|
||||
let c = a.add(b);
|
||||
assert_eq!(c.x, 4);
|
||||
assert_eq!(c.y, 6);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_inherent_impl();
|
||||
test_trait_impl_block();
|
||||
test_multiple_impls();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
pub module struct_test;
|
||||
|
||||
struct Point {
|
||||
x i32,
|
||||
y i32,
|
||||
}
|
||||
|
||||
pub fn void test_struct_create_access() {
|
||||
let p = Point { x: 10, y: 20 };
|
||||
assert_eq!(p.x, 10);
|
||||
assert_eq!(p.y, 20);
|
||||
}
|
||||
|
||||
pub fn void test_struct_mut_field() {
|
||||
let mut p = Point { x: 1, y: 2 };
|
||||
p.x = 100;
|
||||
assert_eq!(p.x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_struct_destructure() {
|
||||
let p = Point { x: 3, y: 4 };
|
||||
let Point { x, y } = p;
|
||||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 4);
|
||||
}
|
||||
|
||||
struct Line {
|
||||
start Point,
|
||||
end Point,
|
||||
}
|
||||
|
||||
pub fn void test_struct_nested() {
|
||||
let l = Line {
|
||||
start: Point { x: 0, y: 0 },
|
||||
end: Point { x: 5, y: 10 },
|
||||
};
|
||||
assert_eq!(l.start.x, 0);
|
||||
assert_eq!(l.end.y, 10);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_struct_create_access();
|
||||
test_struct_mut_field();
|
||||
test_struct_destructure();
|
||||
test_struct_nested();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
pub module trait_test;
|
||||
|
||||
trait Speak {
|
||||
fn String speak(*self);
|
||||
}
|
||||
|
||||
trait Greet : Speak {
|
||||
fn String greet(*self);
|
||||
}
|
||||
|
||||
struct Dog {
|
||||
name String,
|
||||
}
|
||||
|
||||
impl Speak for Dog {
|
||||
fn String speak(*self) {
|
||||
"woof".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Greet for Dog {
|
||||
fn String greet(*self) {
|
||||
"hello".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_trait_impl() {
|
||||
let d = Dog { name: "Rex".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
}
|
||||
|
||||
pub fn void test_trait_with_requirement() {
|
||||
let d = Dog { name: "Buddy".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
assert_eq!(d.greet(), "hello");
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_trait_impl();
|
||||
test_trait_with_requirement();
|
||||
}
|
||||
Reference in New Issue
Block a user