From 46365da73a61bdb6789b189eafa8ec91c273ceee Mon Sep 17 00:00:00 2001 From: Dan Knutson Date: Sat, 23 Oct 2021 10:16:30 -0500 Subject: [PATCH] add failing tests * SIGSEGV for non-empty strings --- compiler/builtins/bitcode/src/str.zig | 15 ++++++-------- compiler/solve/tests/solve_expr.rs | 12 ++++++++++++ compiler/test_gen/src/gen_str.rs | 28 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/compiler/builtins/bitcode/src/str.zig b/compiler/builtins/bitcode/src/str.zig index 81d61f0781..1cd2ba8a2d 100644 --- a/compiler/builtins/bitcode/src/str.zig +++ b/compiler/builtins/bitcode/src/str.zig @@ -1504,14 +1504,12 @@ test "isWhitespace" { try expect(!isWhitespace('x')); } -pub fn strTrim(string: RocStr) callconv(.C) RocStr { - return @call(.{ .modifier = always_inline }, trim, .{string}); -} - // TODO GIESCH // ask & read about small & large strings -fn trim(string: RocStr) RocStr { - if (string.isEmpty()) return RocStr.empty(); +pub fn strTrim(string: RocStr) callconv(.C) RocStr { + if (string.isEmpty()) { + return RocStr.empty(); + } const leading_bytes = countLeadingWhitespaceBytes(string); const trailing_bytes = countTrailingWhitespaceBytes(string); @@ -1525,6 +1523,8 @@ fn trim(string: RocStr) RocStr { // should this just use isUnique? (are all small strings safe for mutation?) // should we rename isUnique to isUnleakable or something? // could also just inline the unsafe reallocate call + + // SIGSEGV is not from this branch if (string.isRefcountOne()) { const dest = string.str_bytes orelse unreachable; const source = dest + leading_bytes; @@ -1645,6 +1645,3 @@ test "strTrim: unique hello world" { try expect(trimmed.eq(expected)); } - -// TODO GIESCH -// add top level roc tests diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index cd05ede55d..f6b4106379 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3733,6 +3733,18 @@ mod solve_expr { ); } + #[test] + fn str_trim() { + infer_eq_without_problem( + indoc!( + r#" + Str.trim + "# + ), + "Str -> Str", + ); + } + #[test] fn function_that_captures_nothing_is_not_captured() { // we should make sure that a function that doesn't capture anything it not itself captured diff --git a/compiler/test_gen/src/gen_str.rs b/compiler/test_gen/src/gen_str.rs index 292946403e..1172f62cdb 100644 --- a/compiler/test_gen/src/gen_str.rs +++ b/compiler/test_gen/src/gen_str.rs @@ -977,3 +977,31 @@ fn str_repeat_empty_string() { fn str_repeat_zero_times() { assert_evals_to!(indoc!(r#"Str.repeat "Roc" 0"#), RocStr::from(""), RocStr); } + +#[test] +fn str_trim_empty_string() { + assert_evals_to!(indoc!(r#"Str.trim """#), RocStr::from(""), RocStr); +} + +#[test] +fn str_trim_blank_string() { + assert_evals_to!(indoc!(r#"Str.trim " ""#), RocStr::from(""), RocStr); +} + +#[test] +fn str_trim_blank_string_large() { + assert_evals_to!( + indoc!(r#"Str.trim " " "#), + RocStr::from(""), + RocStr + ); +} + +#[test] +fn str_trim_hello_world() { + assert_evals_to!( + indoc!(r#"Str.trim " hello world ""#), + RocStr::from("hello world"), + RocStr + ); +}