From 402f759b74867b95ad11a8b4ceba966ad67169c6 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Wed, 22 Nov 2023 13:49:12 +0200 Subject: [PATCH] Fixed flakey test --- .github/workflows/rust.yml | 5 +--- ast_vm/src/stdlib/fns.rs | 51 +++++++++++------------------------ reggie/src/stdlib.rs | 54 +++++++++++++++++++++++++++++++++----- test_suite.sh | 18 +++++++++++++ tests/reggie/main.rs | 1 - tests/reggie/stdlib.rs | 35 ------------------------ 6 files changed, 83 insertions(+), 81 deletions(-) create mode 100755 test_suite.sh delete mode 100644 tests/reggie/stdlib.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b19a605..dffc976 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -19,9 +19,6 @@ jobs: - name: Build run: cargo build --verbose - name: Run tests - run: - cargo test --verbose && - cargo test --verbose --test engine && - cargo test --verbose --test reggie + run: ./test_suite.sh env: QUICKCHECK_GENERATOR_SIZE: 10 diff --git a/ast_vm/src/stdlib/fns.rs b/ast_vm/src/stdlib/fns.rs index 34d9b24..096a185 100644 --- a/ast_vm/src/stdlib/fns.rs +++ b/ast_vm/src/stdlib/fns.rs @@ -154,7 +154,7 @@ fn strsub_inner(str: String, start: isize, end: isize) -> Result str.len() { str.len() } else { start }; + let start = if start > str.len() { str.len() + 1 } else { start }; let end = if end < start as isize { start } else { end as usize }; let end = if end > str.len() { str.len() } else { end }; @@ -333,27 +333,6 @@ mod test { )); } - // #[test] - // fn format_errors_when_no_format_string_is_passed() { - // let res = format(&[]); - // assert!(res.is_err()); - // } - - // #[quickcheck] - // fn formatting_string_without_escapes_results_in_the_same_string(string: String) -> TestResult { - // if let Some(_) = string - // .matches("%c|%d|%E|%e|%f|%g|%i|%o|%u|%X|%x|%q|%s") - // .next() - // { - // return TestResult::discard(); - // } - // let res = format(&[LuaValue::String(string.clone())]); - // assert!(res.is_ok()); - // let value = res.unwrap(); - // assert_eq!(value, LuaValue::String(string)); - // TestResult::passed() - // } - #[quickcheck] fn strlen_returns_the_number_of_bytes_in_a_string(str: String) { let len = str.len(); @@ -379,23 +358,25 @@ mod test { #[quickcheck] fn strsub_slices_string_suffix(str: String, start: usize) { - let suffix = if str.len() < start { - Some("".to_string()) - } else if str.is_char_boundary(start) { - Some(str[start..].to_string()) + let suffix_start = if start <= 1 { + 0 + } else if start as usize >= str.len() + 1 { + str.len() } else { - None + start as usize - 1 }; - let res = strsub(&[LuaValue::String(str), LuaValue::Number(start.into())]); - if let Some(suffix) = suffix { - if let Ok(LuaValue::String(res)) = res { - assert_eq!(res, suffix); - } else { - panic!("Expected string, got {:?}", res); - } + let expected_suffix = LuaValue::string(&str[suffix_start..]); + + let res = strsub(&[ + LuaValue::String(str), + LuaValue::number(start), + LuaValue::Nil, + ]); + if let Ok(res) = res { + assert_eq!(res, expected_suffix); } else { - assert!(res.is_err()); + panic!("Expected string, got {:?}", res); } } diff --git a/reggie/src/stdlib.rs b/reggie/src/stdlib.rs index 1a1b346..8d988ee 100644 --- a/reggie/src/stdlib.rs +++ b/reggie/src/stdlib.rs @@ -1,7 +1,8 @@ use luar_error::ExpectedType; use std::{ cmp::{max, min}, - io::{self, Write}, rc::Rc, + io::{self, Write}, + rc::Rc, }; use crate::{trace_execution, EvalError, GlobalValues, LuaValue, TypeError}; @@ -165,14 +166,26 @@ pub fn define_stdlib(global_values: &mut GlobalValues) { } #[cfg(test)] -#[cfg(feature = "quickcheck")] mod test { - use std::{io::Cursor, rc::Rc}; + use std::io::Cursor; - use crate::NativeFunction; + use crate::{TableRef, TableValue}; use super::*; + #[test] + fn flooring_unconvertible_values_is_an_error() { + let unsupported = [ + LuaValue::Nil, + LuaValue::string("hello"), + LuaValue::Table(TableRef::from(TableValue::new())), + LuaValue::native_function(|| ()), + ]; + for value in unsupported { + assert!(floor(&value).is_err()) + } + } + #[test] fn printing_with_no_args_prints_newline() { let mut buf = Cursor::new(Vec::new()); @@ -188,6 +201,20 @@ mod test { assert_eq!(res, "nil\n"); } + #[cfg(feature = "quickcheck")] + #[quickcheck] + fn floor_floor_numbers(num: f64) { + use crate::eq_with_nan::eq_with_nan; + + let res = floor(&LuaValue::Float(num)).unwrap(); + + assert!(eq_with_nan(res.number_as_f64().unwrap(), num.floor())); + + let res = floor(&LuaValue::String(format!("{}", num))).unwrap(); + assert!(eq_with_nan(res.number_as_f64().unwrap(), num.floor())); + } + + #[cfg(feature = "quickcheck")] #[quickcheck] fn printing_string_prints_its_value(str: String) { let mut buf = Cursor::new(Vec::new()); @@ -196,6 +223,7 @@ mod test { assert_eq!(res, format!("{}\n", str)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn printing_int_prints_string_repr(num: i32) { let mut buf = Cursor::new(Vec::new()); @@ -204,6 +232,7 @@ mod test { assert_eq!(res, format!("{}\n", num)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn printing_float_prints_string_repr(num: f64) { let mut buf = Cursor::new(Vec::new()); @@ -212,8 +241,12 @@ mod test { assert_eq!(res, format!("{}\n", num)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn printing_function_prints_it_address() { + use std::rc::Rc; + use crate::NativeFunction; + let func = NativeFunction::new(|| ()); let addr = Rc::as_ptr(&func.0); let mut buf = Cursor::new(Vec::new()); @@ -222,6 +255,7 @@ mod test { assert_eq!(res, format!("function: {:p}\n", addr)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn multiple_values_are_printed_separated_by_tabs(values: Vec) { let mut buf = Cursor::new(Vec::new()); @@ -235,11 +269,14 @@ mod test { *expected_buf.get_mut().last_mut().unwrap() = b'\t'; print(&mut expected_buf, std::slice::from_ref(&value)).unwrap(); } + let expected_str = String::from_utf8(expected_buf.into_inner()).unwrap(); + assert_eq!(res_str, expected_str); + } else { + assert_eq!(res_str, "\n"); } - let expected_str = String::from_utf8(expected_buf.into_inner()).unwrap(); - assert_eq!(res_str, expected_str); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strlen_returns_the_number_of_bytes_in_a_string(str: String) { let len = str.len(); @@ -247,6 +284,7 @@ mod test { assert_eq!(res, LuaValue::Int(len as i32)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strlen_returns_the_number_of_bytes_in_a_stringified_int(num: i32) { let str = format!("{}", num); @@ -255,6 +293,7 @@ mod test { assert_eq!(res, LuaValue::Int(len as i32)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strlen_returns_the_number_of_bytes_in_a_stringified_float(num: f64) { let str = format!("{}", num); @@ -263,6 +302,7 @@ mod test { assert_eq!(res, LuaValue::Int(len as i32)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strlen_on_non_stringable_errors_out(value: LuaValue) { if value.is_string() || value.is_int() || value.is_float() { @@ -271,6 +311,7 @@ mod test { assert!(strlen(&value).is_err()); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strsub_slices_string_suffix(str: String, start: i32) { let suffix_start = if start <= 1 { @@ -291,6 +332,7 @@ mod test { assert_eq!(res, Ok(expected_suffix)); } + #[cfg(feature = "quickcheck")] #[quickcheck] fn strsub_errors_out_on_non_stringables(value: LuaValue, start: u16, end: Option) { if value.is_string() || value.is_int() || value.is_float() { diff --git a/test_suite.sh b/test_suite.sh new file mode 100755 index 0000000..cb893cd --- /dev/null +++ b/test_suite.sh @@ -0,0 +1,18 @@ +set -e + +cd lex +cargo t --features quickcheck +cd .. + +cd syn +cargo t --features quickcheck +cd .. + +cd reggie +cargo t --features quickcheck +cd .. + +cargo test -p ast_vm --lib +cargo test --test reggie +cargo test --test engine +cargo test --test correctness diff --git a/tests/reggie/main.rs b/tests/reggie/main.rs index d128696..1a25ec5 100644 --- a/tests/reggie/main.rs +++ b/tests/reggie/main.rs @@ -10,7 +10,6 @@ mod boolean_ops; mod comparison; mod function; mod local_decl; -mod stdlib; mod table; mod table_constructor; mod unary_op; diff --git a/tests/reggie/stdlib.rs b/tests/reggie/stdlib.rs deleted file mode 100644 index b593a6a..0000000 --- a/tests/reggie/stdlib.rs +++ /dev/null @@ -1,35 +0,0 @@ -use reggie::{stdlib::floor, LuaValue, TableRef, TableValue, NativeFunction}; - -// use crate::eq_with_nan; - -// #[quickcheck] -// fn floor_floor_numbers(num: f64) { - -// floor(&LuaValue::Float(num)) -// .unwrap() - -// assert!(eq_with_nan( -// num.as_f64().floor() -// )); - -// assert!(eq_with_nan( -// floor(&[LuaValue::string(format!("{}", num))]) -// .unwrap() -// .unwrap_number() -// .as_f64(), -// num.as_f64().floor() -// )); -// } - -#[test] -fn flooring_unconvertible_values_is_an_error() { - let unsupported = [ - LuaValue::Nil, - LuaValue::string("hello"), - LuaValue::Table(TableRef::from(TableValue::new())), - LuaValue::native_function(|| ()), - ]; - for value in unsupported { - assert!(floor(&value).is_err()) - } -}