From d1bece719b46aa3720927a38004e545a415e26d3 Mon Sep 17 00:00:00 2001 From: Vedat Can Keklik Date: Tue, 26 Nov 2024 15:47:14 +0300 Subject: [PATCH] fix: Stmt hash/partialeq tests --- lykiadb-lang/src/ast/expr.rs | 5 +- lykiadb-lang/src/ast/stmt.rs | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/lykiadb-lang/src/ast/expr.rs b/lykiadb-lang/src/ast/expr.rs index 5f3526e..14af938 100644 --- a/lykiadb-lang/src/ast/expr.rs +++ b/lykiadb-lang/src/ast/expr.rs @@ -584,12 +584,12 @@ impl Expr { } #[cfg(test)] -mod test { +pub mod test { use std::collections::HashSet; use crate::{ast::expr::Expr, Literal, Span}; - fn create_simple_add_expr(id: usize, left: f64, right: f64) -> Expr { + pub fn create_simple_add_expr(id: usize, left: f64, right: f64) -> Expr { Expr::Binary { left: Box::new(Expr::Literal { value: Literal::Num(left), @@ -608,6 +608,7 @@ mod test { id, } } + #[test] fn identical_exprs_should_be_equal_when_ids_are_different() { let e0 = create_simple_add_expr(0, 1.0, 2.0); diff --git a/lykiadb-lang/src/ast/stmt.rs b/lykiadb-lang/src/ast/stmt.rs index f109aea..e8e895b 100644 --- a/lykiadb-lang/src/ast/stmt.rs +++ b/lykiadb-lang/src/ast/stmt.rs @@ -101,3 +101,91 @@ impl Spanned for Stmt { } } } + +#[cfg(test)] +mod test { + use std::collections::HashSet; + + use crate::{ast::{expr::{test::create_simple_add_expr, Expr}, stmt::Stmt}, Span}; + + pub fn create_simple_block_stmt(a: Expr, b: Expr) -> Stmt { + Stmt::Block { + body: vec![ + Stmt::Expression { + expr: Box::new(a), span: Span::default() + }, + Stmt::Expression { + expr: Box::new(b), span: Span::default() + } + ], + span: Span::default() + } + } + + #[test] + fn identical_stmts_should_be_equal() { + + let s0 = create_simple_block_stmt( + create_simple_add_expr(0, 1.0, 2.0), + create_simple_add_expr(1, 1.0, 2.0) + ); + + let s1 = create_simple_block_stmt( + create_simple_add_expr(0, 1.0, 2.0), + create_simple_add_expr(1, 1.0, 2.0) + ); + + assert_eq!(s0, s1); + + let mut set: HashSet = HashSet::new(); + + set.insert(s0); + + assert!(set.contains(&s1)); + } + + #[test] + fn different_stmts_should_not_be_equal_0() { + + let s0 = create_simple_block_stmt( + create_simple_add_expr(0, 1.0, 2.0), + create_simple_add_expr(1, 1.0, 2.0) + ); + + let s1 = create_simple_block_stmt( + create_simple_add_expr(0, 2.0, 1.0), + create_simple_add_expr(1, 1.0, 2.0) + ); + + assert_ne!(s0, s1); + + let mut set: HashSet = HashSet::new(); + + set.insert(s0); + + assert!(!set.contains(&s1)); + } + + #[test] + fn different_stmts_should_not_be_equa_1() { + + let s0 = create_simple_block_stmt( + create_simple_add_expr(0, 1.0, 2.0), + create_simple_add_expr(0, 10.0, 20.0) + ); + + let s1 = create_simple_block_stmt( + create_simple_add_expr(0, 10.0, 20.0), + create_simple_add_expr(0, 1.0, 2.0), + ); + + assert_ne!(s0, s1); + + let mut set: HashSet = HashSet::new(); + + set.insert(s0); + + assert!(!set.contains(&s1)); + } + +} \ No newline at end of file