Skip to content

Commit

Permalink
feat: add more testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
caelansar committed Dec 16, 2023
1 parent f1d0310 commit d85ce46
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/eval/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ impl Display for Object {
}
}

#[test]
fn cstring_should_work() {
let s1 = CString("aa".into());
let s2 = CString("bb".into());

assert_eq!(CString("aabb".into()), &s1 + &s2);
assert_eq!(CString("aabb".into()), s1 + s2);
}

#[test]
fn object_display_should_work() {
assert_eq!("123", Object::Int(123).to_string());
Expand Down
89 changes: 85 additions & 4 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<'a> VM<'a> {
let frame = self.current_frame_mut();
let idx = frame.bp + pos;
if idx >= self.stack.len() {
self.stack.insert(idx, val)
panic!("stack overflow")
} else {
self.stack[idx] = val
}
Expand Down Expand Up @@ -406,8 +406,8 @@ impl<'a> VM<'a> {
.expect("func not found");
match func.borrow() {
object::Object::Closure(closure) => {
assert!(
closure.func.num_params == num_args,
assert_eq!(
closure.func.num_params, num_args,
"wrong number of argument"
);
let frame = frame::Frame::new(closure.clone(), self.sp - num_args);
Expand All @@ -419,7 +419,10 @@ impl<'a> VM<'a> {
self.sp = self.sp - num_args - 1;
self.push(Rc::new(builtin.call(args.to_vec())));
}
_ => panic!("{} not a function", func),
_ => self.push(Rc::new(object::Object::Error(format!(
"{} not a function",
func
)))),
}
}
code::Op::GetBuiltin => {
Expand Down Expand Up @@ -567,8 +570,13 @@ mod test {
("true", Some(object::Object::Bool(true))),
("false", Some(object::Object::Bool(false))),
("1>2", Some(object::Object::Bool(false))),
("1!=2", Some(object::Object::Bool(true))),
("1!=1", Some(object::Object::Bool(false))),
("1.0>2.0", Some(object::Object::Bool(false))),
("2>=2", Some(object::Object::Bool(true))),
("2.0>=2.0", Some(object::Object::Bool(true))),
("1<2", Some(object::Object::Bool(true))),
("1.0<2.0", Some(object::Object::Bool(true))),
("2<=2", Some(object::Object::Bool(true))),
("1==2", Some(object::Object::Bool(false))),
("1==1", Some(object::Object::Bool(true))),
Expand All @@ -591,6 +599,72 @@ mod test {
("10 & 2", Some(object::Object::Int(2))),
("10 << 2", Some(object::Object::Int(40))),
("10 >> 2", Some(object::Object::Int(2))),
(
"1 && 1",
Some(object::Object::Error(
"&& not supported between '1' and '1'".into(),
)),
),
(
"1 || 1",
Some(object::Object::Error(
"|| not supported between '1' and '1'".into(),
)),
),
(
"1 < 2.1",
Some(object::Object::Error(
"> not supported between '2.1' and '1'".into(),
)),
),
(
"1 <= 2.1",
Some(object::Object::Error(
">= not supported between '2.1' and '1'".into(),
)),
),
(
"1 > 2.1",
Some(object::Object::Error(
"> not supported between '1' and '2.1'".into(),
)),
),
(
"1 >= 2.1",
Some(object::Object::Error(
">= not supported between '1' and '2.1'".into(),
)),
),
(
"1 | 2.1",
Some(object::Object::Error(
"| not supported between '1' and '2.1'".into(),
)),
),
(
"1 ^ 2.1",
Some(object::Object::Error(
"^ not supported between '1' and '2.1'".into(),
)),
),
(
"1 & 2.1",
Some(object::Object::Error(
"& not supported between '1' and '2.1'".into(),
)),
),
(
"1 << 2.1",
Some(object::Object::Error(
"<< not supported between '1' and '2.1'".into(),
)),
),
(
"1 >> 2.1",
Some(object::Object::Error(
">> not supported between '1' and '2.1'".into(),
)),
),
];

tests.into_iter().for_each(|test| run(test.0, test.1))
Expand Down Expand Up @@ -712,6 +786,13 @@ mod test {
"let r = fn() {1}; let r1 = fn() {r}; r1()()",
Some(object::Object::Int(1)),
),
(
r#"
let no_function = "aa";
no_function()
"#,
Some(object::Object::Error("aa not a function".into())),
),
(
r#"
let f = fn() {
Expand Down

0 comments on commit d85ce46

Please sign in to comment.