Skip to content

Commit

Permalink
fix: Stmt hash/partialeq tests
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Nov 26, 2024
1 parent e846bf3 commit d1bece7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
Expand Down
88 changes: 88 additions & 0 deletions lykiadb-lang/src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stmt> = 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<Stmt> = 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<Stmt> = HashSet::new();

set.insert(s0);

assert!(!set.contains(&s1));
}

}

0 comments on commit d1bece7

Please sign in to comment.