From a3058763c24ae5a85d546806c2f6fb5342ac40d7 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Fri, 19 Jul 2024 00:44:38 +0200 Subject: [PATCH] test(fib): fix completely wrong test --- Cargo.lock | 23 ++++++++++++++++++++ Cargo.toml | 3 +++ src/builder/mod.rs | 5 ++--- src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50b2b5a..958d3a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,12 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "either" version = "1.13.0" @@ -103,6 +109,7 @@ version = "0.1.0" dependencies = [ "inkwell", "petgraph", + "pretty_assertions", ] [[package]] @@ -121,6 +128,16 @@ dependencies = [ "indexmap", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -187,3 +204,9 @@ name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index bc7c50b..4186a8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ edition = "2021" [dependencies] inkwell = {git = "https://github.com/TheDan64/inkwell", features = ["llvm18-0"]} petgraph = "0.6.5" + +[dev-dependencies] +pretty_assertions = "1.4.0" diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 8fd00a0..7388d4e 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -1,11 +1,10 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{HashMap}; use std::ffi::CStr; use std::fmt::Display; use function::{CairoFunction, CairoFunctionBuilder}; -use inkwell::values::{AnyValue, AsValueRef, BasicValueEnum, FunctionValue, InstructionOpcode, PhiValue}; +use inkwell::values::{FunctionValue, InstructionOpcode}; use inkwell::IntPredicate; -use petgraph::algo::{has_path_connecting, tarjan_scc}; pub mod function; diff --git a/src/lib.rs b/src/lib.rs index bf63583..46267bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,9 +90,13 @@ mod tests { #[test] fn test_fib() { let expected_name = "fib".to_owned(); - let expected_return_type = "i64".to_owned(); - let expected_params = vec![CairoParameter::new("left".to_owned(), "i64".to_owned())]; - let code = compile("examples/increment/increment.ll"); + let expected_return_type = "i32".to_owned(); + let expected_params = vec![ + CairoParameter::new("a".to_owned(), "i32".to_owned()), + CairoParameter::new("b".to_owned(), "i32".to_owned()), + CairoParameter::new("n".to_owned(), "i32".to_owned()), + ]; + let code = compile("examples/fib/fib.ll"); // Check number of functions generated assert_eq!(code.count_functions(), 1, "Add function should generate exactly 1 function"); @@ -104,12 +108,46 @@ mod tests { ); // Check function body - assert_eq!( + pretty_assertions::assert_eq!( function.body, CairoFunctionBody::new(vec![ - "let _0 = left + 170141183460469231731687303715884105727_i128;".to_owned(), - "return _0;".to_owned() - ]) + "let mut is_from_bb2 = false;".to_owned(), + "let mut is_from_start = false;".to_owned(), + "let result = n == 0_i32;".to_owned(), + "".to_owned(), + "is_from_bb2 = false;".to_owned(), + "is_from_start = true;".to_owned(), + "let mut ntr3 = 0_i32;".to_owned(), + "let mut btr2 = 0_i32;".to_owned(), + "let mut atr1 = 0_i32;".to_owned(), + "let mut _4 = 0_i32;".to_owned(), + "let mut _5 = 0_i32;".to_owned(), + "let mut var9 = false;".to_owned(), + "if !result {".to_owned(), + "loop {".to_owned(), + "ntr3 = if is_from_bb2 { _5 } else if is_from_start { n } else { panic!(\"There is a bug in the \ + compiler please report it\")};" + .to_owned(), + "btr2 = if is_from_bb2 { _4 } else if is_from_start { b } else { panic!(\"There is a bug in the \ + compiler please report it\")};" + .to_owned(), + "atr1 = if is_from_bb2 { btr2 } else if is_from_start { a } else { panic!(\"There is a bug in the \ + compiler please report it\")};" + .to_owned(), + "_4 = btr2 + atr1;".to_owned(), + "_5 = ntr3 + -1_i32;".to_owned(), + "var9 = _5 == 0_i32;".to_owned(), + "if var9\n{break;}".to_owned(), + "is_from_bb2 = true;".to_owned(), + "is_from_start = false;".to_owned(), + "};".to_owned(), + "let mut atrlcssa = 0_i32;".to_owned(), + "}".to_owned(), + "let atrlcssa = if is_from_start { a } else if is_from_bb2 { btr2 } else { panic!(\"There is a bug in \ + the compiler please report it\")};" + .to_owned(), + "return btr2;".to_owned() + ]), ); } }