add failing tests

* SIGSEGV for non-empty strings
This commit is contained in:
Dan Knutson 2021-10-23 10:16:30 -05:00
parent e7523ad41d
commit 46365da73a
3 changed files with 46 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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
);
}