From c2ca7fcc7a9358f6bbc3f59197d4dd0203d9e7df Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 3 Dec 2024 17:20:29 -0500 Subject: [PATCH] Modify and add logic tests --- dust-lang/tests/logic_and.rs | 137 ++++++++++++++++++++++++++++++ dust-lang/tests/logic_booleans.rs | 69 --------------- dust-lang/tests/logic_or.rs | 35 ++++++++ 3 files changed, 172 insertions(+), 69 deletions(-) create mode 100644 dust-lang/tests/logic_and.rs delete mode 100644 dust-lang/tests/logic_booleans.rs create mode 100644 dust-lang/tests/logic_or.rs diff --git a/dust-lang/tests/logic_and.rs b/dust-lang/tests/logic_and.rs new file mode 100644 index 0000000..7c56b07 --- /dev/null +++ b/dust-lang/tests/logic_and.rs @@ -0,0 +1,137 @@ +use dust_lang::*; + +#[test] +fn true_and_true() { + let source = "true && true"; + + assert_eq!( + compile(source), + Ok(Chunk::with_data( + None, + FunctionType { + type_parameters: None, + value_parameters: None, + return_type: Box::new(Type::Boolean), + }, + vec![ + ( + Instruction::load_boolean(Destination::Register(0), true, false), + Span(0, 4) + ), + (Instruction::test(Argument::Register(0), true), Span(5, 7)), + (Instruction::jump(1, true), Span(5, 7)), + ( + Instruction::load_boolean(Destination::Register(1), true, false), + Span(8, 12) + ), + (Instruction::r#return(true), Span(12, 12)), + ], + vec![], + vec![] + )) + ); + + assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true)))); +} + +#[test] +fn false_and_false() { + let source = "false && false"; + + assert_eq!( + compile(source), + Ok(Chunk::with_data( + None, + FunctionType { + type_parameters: None, + value_parameters: None, + return_type: Box::new(Type::Boolean), + }, + vec![ + ( + Instruction::load_boolean(Destination::Register(0), false, false), + Span(0, 5) + ), + (Instruction::test(Argument::Register(0), true), Span(6, 8)), + (Instruction::jump(1, true), Span(6, 8)), + ( + Instruction::load_boolean(Destination::Register(1), false, false), + Span(9, 14) + ), + (Instruction::r#return(true), Span(14, 14)), + ], + vec![], + vec![] + )) + ); + + assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); +} + +#[test] +fn false_and_true() { + let source = "false && true"; + + assert_eq!( + compile(source), + Ok(Chunk::with_data( + None, + FunctionType { + type_parameters: None, + value_parameters: None, + return_type: Box::new(Type::Boolean), + }, + vec![ + ( + Instruction::load_boolean(Destination::Register(0), false, false), + Span(0, 5) + ), + (Instruction::test(Argument::Register(0), true), Span(6, 8)), + (Instruction::jump(1, true), Span(6, 8)), + ( + Instruction::load_boolean(Destination::Register(1), true, false), + Span(9, 13) + ), + (Instruction::r#return(true), Span(13, 13)), + ], + vec![], + vec![] + )) + ); + + assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); +} + +#[test] +fn true_and_false() { + let source = "true && false"; + + assert_eq!( + compile(source), + Ok(Chunk::with_data( + None, + FunctionType { + type_parameters: None, + value_parameters: None, + return_type: Box::new(Type::Boolean), + }, + vec![ + ( + Instruction::load_boolean(Destination::Register(0), true, false), + Span(0, 4) + ), + (Instruction::test(Argument::Register(0), true), Span(5, 7)), + (Instruction::jump(1, true), Span(5, 7)), + ( + Instruction::load_boolean(Destination::Register(1), false, false), + Span(8, 13) + ), + (Instruction::r#return(true), Span(13, 13)), + ], + vec![], + vec![] + )) + ); + + assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); +} diff --git a/dust-lang/tests/logic_booleans.rs b/dust-lang/tests/logic_booleans.rs deleted file mode 100644 index ca5b635..0000000 --- a/dust-lang/tests/logic_booleans.rs +++ /dev/null @@ -1,69 +0,0 @@ -use dust_lang::*; - -#[test] -fn and() { - let source = "true && false"; - - assert_eq!( - compile(source), - Ok(Chunk::with_data( - None, - FunctionType { - type_parameters: None, - value_parameters: None, - return_type: Box::new(Type::Boolean), - }, - vec![ - ( - Instruction::load_boolean(Destination::Register(0), true, false), - Span(0, 4) - ), - (Instruction::test(Argument::Register(0), true), Span(5, 7)), - (Instruction::jump(1, true), Span(5, 7)), - ( - Instruction::load_boolean(Destination::Register(1), false, false), - Span(8, 13) - ), - (Instruction::r#return(true), Span(13, 13)), - ], - vec![], - vec![] - )) - ); - - assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); -} - -#[test] -fn or() { - let source = "true || false"; - - assert_eq!( - compile(source), - Ok(Chunk::with_data( - None, - FunctionType { - type_parameters: None, - value_parameters: None, - return_type: Box::new(Type::Boolean), - }, - vec![ - ( - Instruction::load_boolean(Destination::Register(0), true, false), - Span(0, 4) - ), - (Instruction::test(Argument::Register(0), false), Span(5, 7)), - (Instruction::jump(1, true), Span(5, 7)), - ( - Instruction::load_boolean(Destination::Register(1), false, false), - Span(8, 13) - ), - (Instruction::r#return(true), Span(13, 13)), - ], - vec![], - vec![] - )) - ); - - assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true)))); -} diff --git a/dust-lang/tests/logic_or.rs b/dust-lang/tests/logic_or.rs new file mode 100644 index 0000000..11f2bc2 --- /dev/null +++ b/dust-lang/tests/logic_or.rs @@ -0,0 +1,35 @@ +use dust_lang::*; + +#[test] +fn true_or_false() { + let source = "true || false"; + + assert_eq!( + compile(source), + Ok(Chunk::with_data( + None, + FunctionType { + type_parameters: None, + value_parameters: None, + return_type: Box::new(Type::Boolean), + }, + vec![ + ( + Instruction::load_boolean(Destination::Register(0), true, false), + Span(0, 4) + ), + (Instruction::test(Argument::Register(0), false), Span(5, 7)), + (Instruction::jump(1, true), Span(5, 7)), + ( + Instruction::load_boolean(Destination::Register(1), false, false), + Span(8, 13) + ), + (Instruction::r#return(true), Span(13, 13)), + ], + vec![], + vec![] + )) + ); + + assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true)))); +}