From d67d94b15cbc7d32683ce1d3f66a3ee5f9a3c3a1 Mon Sep 17 00:00:00 2001 From: shua Date: Wed, 22 Jan 2025 22:15:32 +0100 Subject: [PATCH] merge wasm_str tests into gen_str This change merges the wasm_str.rs and gen_str.rs tests. Any tests that could be run on all three backends now are, in addition some tests were added that just fail whenever some unimplemented things get implemented so the implementer will be forced to start testing the new code. --- crates/compiler/test_gen/src/gen_str.rs | 395 +++++-- crates/compiler/test_gen/src/tests.rs | 1 - crates/compiler/test_gen/src/wasm_str.rs | 1336 ---------------------- 3 files changed, 291 insertions(+), 1441 deletions(-) delete mode 100644 crates/compiler/test_gen/src/wasm_str.rs diff --git a/crates/compiler/test_gen/src/gen_str.rs b/crates/compiler/test_gen/src/gen_str.rs index da27017878d..1b0b6f571df 100644 --- a/crates/compiler/test_gen/src/gen_str.rs +++ b/crates/compiler/test_gen/src/gen_str.rs @@ -1,5 +1,3 @@ -#![cfg(not(feature = "gen-wasm"))] - #[cfg(feature = "gen-llvm")] use crate::helpers::llvm::assert_evals_to; #[cfg(feature = "gen-llvm")] @@ -10,6 +8,9 @@ use crate::helpers::dev::assert_evals_to; #[cfg(feature = "gen-dev")] use crate::helpers::dev::assert_evals_to as assert_llvm_evals_to; +#[cfg(feature = "gen-wasm")] +use crate::helpers::wasm::assert_evals_to; + #[allow(unused_imports)] use indoc::indoc; #[allow(unused_imports)] @@ -50,7 +51,7 @@ fn string_neq() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_empty_delimiter() { assert_evals_to!( indoc!( @@ -80,7 +81,7 @@ fn str_split_on_empty_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_bigger_delimiter_small_str() { assert_evals_to!( indoc!( @@ -110,7 +111,7 @@ fn str_split_on_bigger_delimiter_small_str() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_str_concat_repeated() { assert_evals_to!( indoc!( @@ -134,7 +135,7 @@ fn str_split_on_str_concat_repeated() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_small_str_bigger_delimiter() { assert_evals_to!( indoc!(r#"Str.split_on "JJJ" "0123456789abcdefghi""#), @@ -144,7 +145,7 @@ fn str_split_on_small_str_bigger_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_big_str_small_delimiter() { assert_evals_to!( indoc!( @@ -174,7 +175,7 @@ fn str_split_on_big_str_small_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_small_str_small_delimiter() { assert_evals_to!( indoc!( @@ -188,7 +189,7 @@ fn str_split_on_small_str_small_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_bigger_delimiter_big_strs() { assert_evals_to!( indoc!( @@ -204,7 +205,7 @@ fn str_split_on_bigger_delimiter_big_strs() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_empty_strs() { assert_evals_to!( indoc!( @@ -218,7 +219,7 @@ fn str_split_on_empty_strs() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_minimal_example() { assert_evals_to!( indoc!( @@ -232,7 +233,7 @@ fn str_split_on_minimal_example() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_small_str_big_delimiter() { assert_evals_to!( indoc!( @@ -261,7 +262,7 @@ fn str_split_on_small_str_big_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_small_str_20_char_delimiter() { assert_evals_to!( indoc!( @@ -277,7 +278,7 @@ fn str_split_on_small_str_20_char_delimiter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_concat_big_to_big() { assert_evals_to!( indoc!( @@ -292,6 +293,16 @@ fn str_concat_big_to_big() { ); } +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] +fn long_str_literal() { + assert_evals_to!( + "\"0123456789 123456789 123456789\"", + RocStr::from("0123456789 123456789 123456789"), + RocStr + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] fn small_str_literal() { @@ -327,6 +338,16 @@ fn small_str_literal() { ); } +#[test] +#[cfg(feature = "gen-wasm")] +fn small_str_literal() { + assert_evals_to!( + "\"01234567890\"", + [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], + [u8; 12] + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] fn small_str_zeroed_literal() { @@ -365,6 +386,59 @@ fn small_str_zeroed_literal() { ); } +#[test] +#[cfg(feature = "gen-wasm")] +fn small_str_zeroed_literal() { + // Verifies that we zero out unused bytes in the string. + // This is important so that string equality tests don't randomly + // fail due to unused memory being there! + // Note: Ensure the memory is non-zero beforehand, or it's not a real test! + // (It's trickier than it sounds!) + assert_evals_to!( + indoc!( + r#" + app "test" provides [main] to "./platform" + + create_str = \is_for_real_this_time -> + if is_for_real_this_time then + "J" + else + "xxxxxxx" + + function_with_reused_space = \is_for_real_this_time -> + # Different string value on each call, at the same memory location + # (Can't inline create_str without refcounting, which isn't implemented) + reused_space = create_str is_for_real_this_time + + # Unoptimised 'if' ensures that we don't just allocate in the caller's frame + if Bool.true then + reused_space + else + reused_space + + main = + garbage = function_with_reused_space Bool.false + function_with_reused_space Bool.true + "# + ), + [ + 0x4a, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0b1000_0001 + ], + [u8; 12] + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] fn small_str_concat_empty_first_arg() { @@ -400,6 +474,16 @@ fn small_str_concat_empty_first_arg() { ); } +#[test] +#[cfg(feature = "gen-wasm")] +fn small_str_concat_empty_first_arg() { + assert_evals_to!( + r#"Str.concat "" "01234567890""#, + [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], + [u8; 12] + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] fn small_str_concat_empty_second_arg() { @@ -436,7 +520,17 @@ fn small_str_concat_empty_second_arg() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(feature = "gen-wasm")] +fn small_str_concat_empty_second_arg() { + assert_evals_to!( + r#"Str.concat "01234567890" """#, + [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], + [u8; 12] + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn small_str_concat_small_to_big() { assert_evals_to!( r#"Str.concat "abc" " this is longer than 15 chars""#, @@ -481,7 +575,17 @@ fn small_str_concat_small_to_small_staying_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(feature = "gen-wasm")] +fn small_str_concat_small_to_small_staying_small() { + assert_evals_to!( + r#"Str.concat "0" "1234567890""#, + [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], + [u8; 12] + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn small_str_concat_small_to_small_overflow_to_big() { assert_evals_to!( r#"Str.concat "abcdefghijklm" "nopqrstuvwxyz""#, @@ -491,19 +595,19 @@ fn small_str_concat_small_to_small_overflow_to_big() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_concat_empty() { assert_evals_to!(r#"Str.concat "" """#, RocStr::default(), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn small_str_is_empty() { assert_evals_to!(r#"Str.is_empty "abc""#, false, bool); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn big_str_is_empty() { assert_evals_to!( r#"Str.is_empty "this is more than 23 chars long""#, @@ -513,13 +617,13 @@ fn big_str_is_empty() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn empty_str_is_empty() { assert_evals_to!(r#"Str.is_empty """#, true, bool); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with() { assert_evals_to!(r#"Str.starts_with "hello world" "hell""#, true, bool); assert_evals_to!(r#"Str.starts_with "hello world" """#, true, bool); @@ -529,7 +633,7 @@ fn str_starts_with() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_ends_with() { assert_evals_to!(r#"Str.ends_with "hello world" "world""#, true, bool); assert_evals_to!(r#"Str.ends_with "nope" "hello world""#, false, bool); @@ -537,7 +641,7 @@ fn str_ends_with() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with_same_big_str() { assert_evals_to!( r#"Str.starts_with "123456789123456789" "123456789123456789""#, @@ -547,7 +651,7 @@ fn str_starts_with_same_big_str() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with_different_big_str() { assert_evals_to!( r#"Str.starts_with "12345678912345678910" "123456789123456789""#, @@ -557,24 +661,24 @@ fn str_starts_with_different_big_str() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with_same_small_str() { assert_evals_to!(r#"Str.starts_with "1234" "1234""#, true, bool); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with_different_small_str() { assert_evals_to!(r#"Str.starts_with "1234" "12""#, true, bool); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_starts_with_false_small_str() { assert_evals_to!(r#"Str.starts_with "1234" "23""#, false, bool); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_single_ascii() { assert_evals_to!( indoc!( @@ -590,7 +694,7 @@ fn str_from_utf8_pass_single_ascii() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_many_ascii() { assert_evals_to!( indoc!( @@ -606,7 +710,7 @@ fn str_from_utf8_pass_many_ascii() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_single_unicode() { assert_evals_to!( indoc!( @@ -622,7 +726,7 @@ fn str_from_utf8_pass_single_unicode() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_many_unicode() { assert_evals_to!( indoc!( @@ -638,7 +742,7 @@ fn str_from_utf8_pass_many_unicode() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_single_grapheme() { assert_evals_to!( indoc!( @@ -654,7 +758,7 @@ fn str_from_utf8_pass_single_grapheme() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_many_grapheme() { assert_evals_to!( indoc!( @@ -670,7 +774,7 @@ fn str_from_utf8_pass_many_grapheme() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_pass_all() { assert_evals_to!( indoc!( @@ -686,7 +790,7 @@ fn str_from_utf8_pass_all() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_invalid_start_byte() { assert_evals_to!( indoc!( @@ -706,7 +810,7 @@ fn str_from_utf8_fail_invalid_start_byte() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_unexpected_end_of_sequence() { assert_evals_to!( indoc!( @@ -726,7 +830,7 @@ fn str_from_utf8_fail_unexpected_end_of_sequence() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_expected_continuation() { assert_evals_to!( indoc!( @@ -746,7 +850,7 @@ fn str_from_utf8_fail_expected_continuation() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_overlong_encoding() { assert_evals_to!( indoc!( @@ -766,7 +870,7 @@ fn str_from_utf8_fail_overlong_encoding() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_codepoint_too_large() { assert_evals_to!( indoc!( @@ -786,7 +890,7 @@ fn str_from_utf8_fail_codepoint_too_large() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_fail_surrogate_half() { assert_evals_to!( indoc!( @@ -806,7 +910,7 @@ fn str_from_utf8_fail_surrogate_half() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_lossy_expected_continuation() { assert_evals_to!( r#"Str.from_utf8_lossy [97, 98, 0xC2, 99]"#, @@ -816,7 +920,7 @@ fn str_from_utf8_lossy_expected_continuation() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf16() { assert_evals_to!( indoc!( @@ -847,8 +951,27 @@ fn str_from_utf16_emoji() { ) } +// Marking this as should_panic, because it *does* panic and it is not clear why? +// If some change magically fixes this, great, merge with above. #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(feature = "gen-wasm")] +#[should_panic(expected = r#"Roc failed with message: "Integer multiplication overflowed!"#)] +fn str_from_utf16_emoji() { + assert_evals_to!( + indoc!( + r#" + when Str.from_utf16 [0x72, 0xd83d, 0xdc96, 0x63] is + Ok val -> val + _ -> "" + "# + ), + roc_std::RocStr::from("r💖c"), + roc_std::RocStr + ) +} + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf16_err_expected_second_surrogate_half() { assert_evals_to!( indoc!( @@ -864,7 +987,7 @@ fn str_from_utf16_err_expected_second_surrogate_half() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf16_err_unexpected_second_surrogate_half() { assert_evals_to!( indoc!( @@ -880,7 +1003,7 @@ fn str_from_utf16_err_unexpected_second_surrogate_half() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf16_lossy() { assert_evals_to!( r#"Str.from_utf16_lossy [0x72, 0xdc96, 0x63]"#, @@ -890,7 +1013,7 @@ fn str_from_utf16_lossy() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf32() { assert_evals_to!( indoc!( @@ -906,7 +1029,7 @@ fn str_from_utf32() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf32_emoji() { assert_evals_to!( indoc!( @@ -922,7 +1045,7 @@ fn str_from_utf32_emoji() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf32_err_codepoint_too_large() { assert_evals_to!( indoc!( @@ -938,7 +1061,7 @@ fn str_from_utf32_err_codepoint_too_large() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf32_err_utf8_cannot_encode_surrogate_half() { assert_evals_to!( indoc!( @@ -954,7 +1077,7 @@ fn str_from_utf32_err_utf8_cannot_encode_surrogate_half() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf32_lossy() { assert_evals_to!( r#"Str.from_utf32_lossy [0x72, 0x123456, 0x63]"#, @@ -964,7 +1087,7 @@ fn str_from_utf32_lossy() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_equality() { assert_evals_to!(r#""a" == "a""#, true, bool); assert_evals_to!( @@ -1020,7 +1143,7 @@ fn nested_recursive_literal() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_join_comma_small() { assert_evals_to!( r#"Str.join_with ["1", "2"] ", " "#, @@ -1030,7 +1153,7 @@ fn str_join_comma_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_join_comma_big() { assert_evals_to!( r#"Str.join_with ["10000000", "2000000", "30000000"] ", " "#, @@ -1040,13 +1163,13 @@ fn str_join_comma_big() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_join_comma_single() { assert_evals_to!(r#"Str.join_with ["1"] ", " "#, RocStr::from("1"), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_to_utf8() { assert_evals_to!( r#"Str.to_utf8 "hello""#, @@ -1064,7 +1187,7 @@ fn str_to_utf8() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8() { assert_evals_to!( indoc!( @@ -1083,7 +1206,7 @@ fn str_from_utf8() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_slice() { assert_evals_to!( indoc!( @@ -1103,7 +1226,7 @@ fn str_from_utf8_slice() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_slice_not_end() { assert_evals_to!( indoc!( @@ -1123,7 +1246,7 @@ fn str_from_utf8_slice_not_end() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_from_utf8_order_does_not_matter() { assert_evals_to!( indoc!( @@ -1143,7 +1266,7 @@ fn str_from_utf8_order_does_not_matter() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_repeat_small_stays_small() { assert_evals_to!( indoc!(r#"Str.repeat "Roc" 3"#), @@ -1153,7 +1276,7 @@ fn str_repeat_small_stays_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_repeat_small_becomes_big() { assert_evals_to!( indoc!(r#"Str.repeat "less than 23 characters" 2"#), @@ -1163,7 +1286,17 @@ fn str_repeat_small_becomes_big() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] +fn str_repeat_small() { + assert_evals_to!( + indoc!(r#"Str.repeat "Roc" 2"#), + RocStr::from("RocRoc"), + RocStr + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_repeat_big() { assert_evals_to!( indoc!(r#"Str.repeat "more than 23 characters now" 2"#), @@ -1173,26 +1306,26 @@ fn str_repeat_big() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_repeat_empty_string() { let a = indoc!(r#"Str.repeat "" 3"#); assert_evals_to!(a, RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_repeat_zero_times() { assert_evals_to!(indoc!(r#"Str.repeat "Roc" 0"#), RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_empty_string() { assert_evals_to!(indoc!(r#"Str.trim """#), RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_null_byte() { assert_evals_to!( indoc!(r#"Str.trim (Str.reserve "\u(0000)" 40)"#), @@ -1202,13 +1335,13 @@ fn str_trim_null_byte() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_small_blank_string() { assert_evals_to!(indoc!(r#"Str.trim " ""#), RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_small_to_small() { assert_evals_to!( indoc!(r#"Str.trim " hello world ""#), @@ -1218,7 +1351,7 @@ fn str_trim_small_to_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_large_to_large_unique() { assert_evals_to!( indoc!(r#"Str.trim (Str.concat " " "hello world from a large string ")"#), @@ -1228,7 +1361,7 @@ fn str_trim_large_to_large_unique() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_large_to_small_unique() { assert_evals_to!( indoc!(r#"Str.trim (Str.concat " " "hello world ")"#), @@ -1295,13 +1428,13 @@ fn str_trim_small_to_small_shared() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_start_small_blank_string() { assert_evals_to!(indoc!(r#"Str.trim_start " ""#), RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_start_small_to_small() { assert_evals_to!( indoc!(r#"Str.trim_start " hello world ""#), @@ -1311,7 +1444,7 @@ fn str_trim_start_small_to_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_start_large_to_large_unique() { assert_evals_to!( indoc!(r#"Str.trim_start (Str.concat " " "hello world from a large string ")"#), @@ -1321,7 +1454,7 @@ fn str_trim_start_large_to_large_unique() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_start_large_to_small_unique() { assert_evals_to!( indoc!(r#"Str.trim_start (Str.concat " " "hello world ")"#), @@ -1388,13 +1521,13 @@ fn str_trim_start_small_to_small_shared() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_end_small_blank_string() { assert_evals_to!(indoc!(r#"Str.trim_end " ""#), RocStr::from(""), RocStr); } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_end_small_to_small() { assert_evals_to!( indoc!(r#"Str.trim_end " hello world ""#), @@ -1404,7 +1537,7 @@ fn str_trim_end_small_to_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_end_large_to_large_unique() { assert_evals_to!( indoc!(r#"Str.trim_end (Str.concat " hello world from a large string" " ")"#), @@ -1414,7 +1547,7 @@ fn str_trim_end_large_to_large_unique() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_trim_end_large_to_small_unique() { assert_evals_to!( indoc!(r#"Str.trim_end (Str.concat " hello world" " ")"#), @@ -1481,7 +1614,7 @@ fn str_trim_end_small_to_small_shared() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_to_nat() { assert_evals_to!( indoc!( @@ -1524,7 +1657,7 @@ fn str_to_u128() { // TODO add alignment check between i64 and I64 somewhere #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_to_i64() { assert_evals_to!( indoc!( @@ -1538,7 +1671,7 @@ fn str_to_i64() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_to_u64() { assert_evals_to!( indoc!( @@ -1689,7 +1822,7 @@ fn str_to_dec() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn issue_2811() { assert_evals_to!( indoc!( @@ -1705,7 +1838,7 @@ fn issue_2811() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_first_one_char() { assert_evals_to!( indoc!( @@ -1721,7 +1854,7 @@ fn str_split_on_first_one_char() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_first_multiple_chars() { assert_evals_to!( indoc!( @@ -1735,7 +1868,7 @@ fn str_split_on_first_multiple_chars() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_first_entire_input() { assert_evals_to!( indoc!( @@ -1749,7 +1882,7 @@ fn str_split_on_first_entire_input() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_first_not_found() { assert_evals_to!( indoc!( @@ -1763,7 +1896,7 @@ fn str_split_on_first_not_found() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_last_one_char() { assert_evals_to!( indoc!( @@ -1777,7 +1910,7 @@ fn str_split_on_last_one_char() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_last_multiple_chars() { assert_evals_to!( indoc!( @@ -1791,7 +1924,7 @@ fn str_split_on_last_multiple_chars() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_last_entire_input() { assert_evals_to!( indoc!( @@ -1805,7 +1938,7 @@ fn str_split_on_last_entire_input() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_last_not_found() { assert_evals_to!( indoc!( @@ -1819,7 +1952,7 @@ fn str_split_on_last_not_found() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_overlapping_substring_1() { assert_evals_to!( r#"Str.split_on "aaa" "aa""#, @@ -1829,7 +1962,7 @@ fn str_split_on_overlapping_substring_1() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_split_on_overlapping_substring_2() { assert_evals_to!( r#"Str.split_on "aaaa" "aa""#, @@ -1839,7 +1972,7 @@ fn str_split_on_overlapping_substring_2() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_walk_utf8() { #[cfg(not(feature = "gen-llvm-wasm"))] assert_evals_to!( @@ -1866,7 +1999,7 @@ fn str_walk_utf8() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_walk_utf8_with_index() { assert_evals_to!( indoc!( @@ -1912,7 +2045,7 @@ fn llvm_wasm_str_layout_small() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn when_on_strings() { assert_evals_to!( indoc!( @@ -2055,6 +2188,24 @@ fn release_excess_capacity() { ); } +// if this gets implemented, then add gen-dev to positive case above +#[test] +#[cfg(feature = "gen-dev")] +#[should_panic(expected = "not yet implemented: low level, StrReleaseExcessCapacity")] +fn release_excess_capacity() { + assert_evals_to!( + indoc!( + r#" + Str.reserve "" 50 + |> Str.release_excess_capacity + "# + ), + (RocStr::empty().capacity(), RocStr::empty()), + RocStr, + |value: RocStr| (value.capacity(), value) + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] fn release_excess_capacity_with_len() { @@ -2072,6 +2223,25 @@ fn release_excess_capacity_with_len() { ); } +// if this gets implemented, then add gen-dev to positive case above +#[test] +#[cfg(feature = "gen-dev")] +#[should_panic(expected = "not yet implemented: low level, StrReleaseExcessCapacity")] +fn release_excess_capacity_with_len() { + assert_evals_to!( + indoc!( + r#" + "123456789012345678901234567890" + |> Str.reserve 50 + |> Str.release_excess_capacity + "# + ), + (30, "123456789012345678901234567890".into()), + RocStr, + |value: RocStr| (value.capacity(), value) + ); +} + #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] fn release_excess_capacity_empty() { @@ -2087,8 +2257,25 @@ fn release_excess_capacity_empty() { ); } +// if this gets implemented, then add gen-dev to positive case above #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(feature = "gen-dev")] +#[should_panic(expected = "not yet implemented: low level, StrReleaseExcessCapacity")] +fn release_excess_capacity_empty() { + assert_evals_to!( + indoc!( + r#" + Str.release_excess_capacity "" + "# + ), + (RocStr::empty().capacity(), RocStr::empty()), + RocStr, + |value: RocStr| (value.capacity(), value) + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_contains_positive() { assert_evals_to!( r#" @@ -2100,7 +2287,7 @@ fn str_contains_positive() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_contains_negative() { assert_evals_to!( r#" @@ -2112,7 +2299,7 @@ fn str_contains_negative() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_contains_empty_positive() { assert_evals_to!( r#" @@ -2124,7 +2311,7 @@ fn str_contains_empty_positive() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_contains_empty_negative() { assert_evals_to!( r#" @@ -2136,7 +2323,7 @@ fn str_contains_empty_negative() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_contains_self() { assert_evals_to!( r#" @@ -2148,7 +2335,7 @@ fn str_contains_self() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_drop_prefix() { assert_evals_to!( r#" @@ -2184,7 +2371,7 @@ fn str_drop_prefix() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn str_drop_suffix() { assert_evals_to!( r#" @@ -2220,7 +2407,7 @@ fn str_drop_suffix() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn with_ascii_lowercased() { assert_evals_to!( r#" @@ -2232,7 +2419,7 @@ fn with_ascii_lowercased() { } #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] fn with_ascii_lowercased_non_zero_refcount() { assert_evals_to!( r#" diff --git a/crates/compiler/test_gen/src/tests.rs b/crates/compiler/test_gen/src/tests.rs index 41fd4988943..ece31152639 100644 --- a/crates/compiler/test_gen/src/tests.rs +++ b/crates/compiler/test_gen/src/tests.rs @@ -22,7 +22,6 @@ pub mod gen_str; pub mod gen_tags; pub mod gen_tuples; mod helpers; -pub mod wasm_str; #[cfg(feature = "gen-wasm")] pub mod wasm_linking; diff --git a/crates/compiler/test_gen/src/wasm_str.rs b/crates/compiler/test_gen/src/wasm_str.rs deleted file mode 100644 index 52320cd2708..00000000000 --- a/crates/compiler/test_gen/src/wasm_str.rs +++ /dev/null @@ -1,1336 +0,0 @@ -// Wasm pointers are only 32bit. This effects RocStr. -// These are versions of the str tests assuming 32bit pointers. -#![cfg(feature = "gen-wasm")] - -// TODO: We need to make these tests work with the llvm wasm backend. - -// #[cfg(feature = "gen-llvm")] -// use crate::helpers::llvm::assert_wasm_evals_to as assert_evals_to; - -#[cfg(feature = "gen-wasm")] -use crate::helpers::wasm::assert_evals_to; - -#[allow(unused_imports)] -use indoc::indoc; -use roc_std::{RocList, RocStr, I128, U128}; - -#[test] -fn str_split_on_empty_delimiter() { - assert_evals_to!( - indoc!( - r#" - List.len (Str.split_on "hello" "") - "# - ), - 1, - u64 - ); -} - -#[test] -fn str_split_on_bigger_delimiter_small_str() { - assert_evals_to!( - indoc!( - r#" - List.len (Str.split_on "hello" "JJJJ there") - "# - ), - 1, - u64 - ); -} - -#[test] -fn str_split_on_str_concat_repeated() { - assert_evals_to!( - indoc!( - r#" - when List.first (Str.split_on "JJJJJ" "JJJJ there") is - Ok str -> - str - |> Str.concat str - |> Str.concat str - |> Str.concat str - |> Str.concat str - - _ -> - "Not Str!" - - "# - ), - RocStr::from("JJJJJJJJJJJJJJJJJJJJJJJJJ"), - RocStr - ); -} - -#[test] -fn str_split_on_small_str_bigger_delimiter() { - assert_evals_to!( - indoc!( - r#" - when - List.first - (Str.split_on "JJJ" "0123456789abcdefghi") - is - Ok str -> str - _ -> "" - "# - ), - RocStr::from("JJJ"), - RocStr - ); -} - -#[test] -fn str_split_on_big_str_small_delimiter() { - assert_evals_to!( - indoc!( - r#" - Str.split_on "01234567789abcdefghi?01234567789abcdefghi" "?" - "# - ), - RocList::from_slice(&[ - RocStr::from("01234567789abcdefghi"), - RocStr::from("01234567789abcdefghi") - ]), - RocList - ); - - assert_evals_to!( - indoc!( - r#" - Str.split_on "01234567789abcdefghi 3ch 01234567789abcdefghi" "3ch" - "# - ), - RocList::from_slice(&[ - RocStr::from("01234567789abcdefghi "), - RocStr::from(" 01234567789abcdefghi") - ]), - RocList - ); -} - -#[test] -fn str_split_on_small_str_small_delimiter() { - assert_evals_to!( - indoc!( - r#" - Str.split_on "J!J!J" "!" - "# - ), - RocList::from_slice(&[RocStr::from("J"), RocStr::from("J"), RocStr::from("J")]), - RocList - ); -} - -#[test] -fn str_split_on_bigger_delimiter_big_strs() { - assert_evals_to!( - indoc!( - r#" - Str.split_on - "string to split is shorter" - "than the delimiter which happens to be very very long" - "# - ), - RocList::from_slice(&[RocStr::from("string to split is shorter")]), - RocList - ); -} - -#[test] -fn str_split_on_empty_strs() { - assert_evals_to!( - indoc!( - r#" - Str.split_on "" "" - "# - ), - RocList::from_slice(&[RocStr::from("")]), - RocList - ); -} - -#[test] -fn str_split_on_minimal_example() { - assert_evals_to!( - indoc!( - r#" - Str.split_on "a," "," - "# - ), - RocList::from_slice(&[RocStr::from("a"), RocStr::from("")]), - RocList - ) -} - -#[test] -fn str_split_on_small_str_big_delimiter() { - assert_evals_to!( - indoc!( - r#" - Str.split_on - "1---- ---- ---- ---- ----2---- ---- ---- ---- ----" - "---- ---- ---- ---- ----" - |> List.len - "# - ), - 3, - u64 - ); - - assert_evals_to!( - indoc!( - r#" - Str.split_on - "1---- ---- ---- ---- ----2---- ---- ---- ---- ----" - "---- ---- ---- ---- ----" - "# - ), - RocList::from_slice(&[RocStr::from("1"), RocStr::from("2"), RocStr::from("")]), - RocList - ); -} - -#[test] -fn str_split_on_small_str_20_char_delimiter() { - assert_evals_to!( - indoc!( - r#" - Str.split_on - "3|-- -- -- -- -- -- |4|-- -- -- -- -- -- |" - "|-- -- -- -- -- -- |" - "# - ), - RocList::from_slice(&[RocStr::from("3"), RocStr::from("4"), RocStr::from("")]), - RocList - ); -} - -#[test] -fn str_concat_big_to_big() { - assert_evals_to!( - indoc!( - r#" - Str.concat - "First string that is fairly long. Longer strings make for different errors. " - "Second string that is also fairly long. Two long strings test things that might not appear with short strings." - "# - ), - RocStr::from("First string that is fairly long. Longer strings make for different errors. Second string that is also fairly long. Two long strings test things that might not appear with short strings."), - RocStr - ); -} - -#[test] -#[cfg(feature = "gen-wasm")] -fn small_str_literal() { - assert_evals_to!( - "\"01234567890\"", - [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], - [u8; 12] - ); -} - -#[test] -#[cfg(feature = "gen-wasm")] -fn small_str_zeroed_literal() { - // Verifies that we zero out unused bytes in the string. - // This is important so that string equality tests don't randomly - // fail due to unused memory being there! - // Note: Ensure the memory is non-zero beforehand, or it's not a real test! - // (It's trickier than it sounds!) - assert_evals_to!( - indoc!( - r#" - app "test" provides [main] to "./platform" - - create_str = \is_for_real_this_time -> - if is_for_real_this_time then - "J" - else - "xxxxxxx" - - function_with_reused_space = \is_for_real_this_time -> - # Different string value on each call, at the same memory location - # (Can't inline create_str without refcounting, which isn't implemented) - reused_space = create_str is_for_real_this_time - - # Unoptimised 'if' ensures that we don't just allocate in the caller's frame - if Bool.true then - reused_space - else - reused_space - - main = - garbage = function_with_reused_space Bool.false - function_with_reused_space Bool.true - "# - ), - [ - 0x4a, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0b1000_0001 - ], - [u8; 12] - ); -} - -#[test] -fn long_str_literal() { - assert_evals_to!( - "\"0123456789 123456789 123456789\"", - RocStr::from("0123456789 123456789 123456789"), - RocStr - ); -} - -#[test] -fn small_str_concat_empty_first_arg() { - assert_evals_to!( - r#"Str.concat "" "01234567890""#, - [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], - [u8; 12] - ); -} - -#[test] -fn small_str_concat_empty_second_arg() { - assert_evals_to!( - r#"Str.concat "01234567890" """#, - [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], - [u8; 12] - ); -} - -#[test] -fn small_str_concat_small_to_big() { - assert_evals_to!( - r#"Str.concat "abc" " this is longer than 7 chars""#, - RocStr::from("abc this is longer than 7 chars"), - RocStr - ); -} - -#[test] -fn small_str_concat_small_to_small_staying_small() { - assert_evals_to!( - r#"Str.concat "0" "1234567890""#, - [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x8b], - [u8; 12] - ); -} - -#[test] -fn small_str_concat_small_to_small_overflow_to_big() { - assert_evals_to!( - r#"Str.concat "abcdefg" "hijklmn""#, - RocStr::from("abcdefghijklmn"), - RocStr - ); -} - -#[test] -fn str_concat_empty() { - assert_evals_to!(r#"Str.concat "" """#, RocStr::default(), RocStr); -} - -#[test] -fn small_str_is_empty() { - assert_evals_to!(r#"Str.is_empty "abc""#, false, bool); -} - -#[test] -fn big_str_is_empty() { - assert_evals_to!( - r#"Str.is_empty "this is more than 15 chars long""#, - false, - bool - ); -} - -#[test] -fn empty_str_is_empty() { - assert_evals_to!(r#"Str.is_empty """#, true, bool); -} - -#[test] -fn str_starts_with() { - assert_evals_to!(r#"Str.starts_with "hello world" "hell""#, true, bool); - assert_evals_to!(r#"Str.starts_with "hello world" """#, true, bool); - assert_evals_to!(r#"Str.starts_with "nope" "hello world""#, false, bool); - assert_evals_to!(r#"Str.starts_with "hell" "hello world""#, false, bool); - assert_evals_to!(r#"Str.starts_with "" "hello world""#, false, bool); -} - -#[test] -fn str_ends_with() { - assert_evals_to!(r#"Str.ends_with "hello world" "world""#, true, bool); - assert_evals_to!(r#"Str.ends_with "nope" "hello world""#, false, bool); - assert_evals_to!(r#"Str.ends_with "" "hello world""#, false, bool); -} - -#[test] -fn str_starts_with_same_big_str() { - assert_evals_to!( - r#"Str.starts_with "123456789123456789" "123456789123456789""#, - true, - bool - ); -} - -#[test] -fn str_starts_with_different_big_str() { - assert_evals_to!( - r#"Str.starts_with "12345678912345678910" "123456789123456789""#, - true, - bool - ); -} - -#[test] -fn str_starts_with_same_small_str() { - assert_evals_to!(r#"Str.starts_with "1234" "1234""#, true, bool); -} - -#[test] -fn str_starts_with_different_small_str() { - assert_evals_to!(r#"Str.starts_with "1234" "12""#, true, bool); -} -#[test] -fn str_starts_with_false_small_str() { - assert_evals_to!(r#"Str.starts_with "1234" "23""#, false, bool); -} - -#[test] -fn str_from_utf8_pass_single_ascii() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_many_ascii() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 98, 99, 0x7E] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("abc~"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_single_unicode() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [0xE2, 0x88, 0x86] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("∆"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_many_unicode() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [0xE2, 0x88, 0x86, 0xC5, 0x93, 0xC2, 0xAC] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("∆œ¬"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_single_grapheme() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [0xF0, 0x9F, 0x92, 0x96] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("💖"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_many_grapheme() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [0xF0, 0x9F, 0x92, 0x96, 0xF0, 0x9F, 0xA4, 0xA0, 0xF0, 0x9F, 0x9A, 0x80] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("💖🤠🚀"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_pass_all() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [0xF0, 0x9F, 0x92, 0x96, 98, 0xE2, 0x88, 0x86] is - Ok val -> val - Err _ -> "" - "# - ), - roc_std::RocStr::from("💖b∆"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_invalid_start_byte() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 98, 0x80, 99] is - Err (BadUtf8 {problem: InvalidStartByte, index: byte_index}) -> - if byte_index == 2 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_unexpected_end_of_sequence() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 98, 99, 0xC2] is - Err (BadUtf8 {problem: UnexpectedEndOfSequence, index: byte_index}) -> - if byte_index == 3 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_expected_continuation() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 98, 99, 0xC2, 0x00] is - Err (BadUtf8 {problem: ExpectedContinuation, index: byte_index}) -> - if byte_index == 3 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_overlong_encoding() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 0xF0, 0x80, 0x80, 0x80] is - Err (BadUtf8 {problem: OverlongEncoding, index: byte_index}) -> - if byte_index == 1 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_codepoint_too_large() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 0xF4, 0x90, 0x80, 0x80] is - Err (BadUtf8 {problem: CodepointTooLarge, index: byte_index}) -> - if byte_index == 1 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_fail_surrogate_half() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf8 [97, 98, 0xED, 0xA0, 0x80] is - Err (BadUtf8 {problem: EncodesSurrogateHalf, index: byte_index}) -> - if byte_index == 2 then - "a" - else - "b" - _ -> "" - "# - ), - roc_std::RocStr::from("a"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf8_lossy_expected_continuation() { - assert_evals_to!( - r#"Str.from_utf8_lossy [97, 98, 0xC2, 99]"#, - roc_std::RocStr::from("ab�c"), - roc_std::RocStr - ); -} - -#[test] -fn str_from_utf16() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf16 [0x72, 0x6f, 0x63] is - Ok val -> val - _ -> "" - "# - ), - roc_std::RocStr::from("roc"), - roc_std::RocStr - ) -} - -// Marking this as should_panic, because it *does* panic and it is not clear why? -// If some change magically fixes this, great, remove the should_panic attribute. -#[test] -#[should_panic(expected = r#"Roc failed with message: "Integer multiplication overflowed!"#)] -fn str_from_utf16_emoji() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf16 [0x72, 0xd83d, 0xdc96, 0x63] is - Ok val -> val - _ -> "" - "# - ), - roc_std::RocStr::from("r💖c"), - roc_std::RocStr - ) -} - -#[test] -fn str_from_utf16_err_expected_second_surrogate_half() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf16 [0x72, 0xd83d, 0x63] is - Err (BadUtf16 {problem: EncodesSurrogateHalf, index: index }) -> index - _ -> 42 - "# - ), - 1u64, - u64 - ) -} - -#[test] -fn str_from_utf16_err_unexpected_second_surrogate_half() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf16 [0x72, 0xdc96, 0x63] is - Err (BadUtf16 {problem: EncodesSurrogateHalf, index: index }) -> index - _ -> 42 - "# - ), - 1u64, - u64 - ) -} - -#[test] -fn str_from_utf16_lossy() { - assert_evals_to!( - r#"Str.from_utf16_lossy [0x72, 0xdc96, 0x63]"#, - roc_std::RocStr::from("r�c"), - roc_std::RocStr - ) -} - -#[test] -fn str_from_utf32() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf32 [0x72, 0x6f, 0x63] is - Ok val -> val - _ -> "" - "# - ), - roc_std::RocStr::from("roc"), - roc_std::RocStr - ) -} - -#[test] -fn str_from_utf32_emoji() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf32 [0x72, 0x1f496, 0x63] is - Ok val -> val - _ -> "" - "# - ), - roc_std::RocStr::from("r💖c"), - roc_std::RocStr - ) -} - -#[test] -fn str_from_utf32_err_codepoint_too_large() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf32 [0x72, 0x123456, 0x63] is - Err (BadUtf32 {problem: CodepointTooLarge, index: index }) -> index - _ -> 42 - "# - ), - 1u64, - u64 - ) -} - -#[test] -fn str_from_utf32_err_utf8_cannot_encode_surrogate_half() { - assert_evals_to!( - indoc!( - r#" - when Str.from_utf32 [0x72, 0xd83d, 0x63] is - Err (BadUtf32 {problem: EncodesSurrogateHalf, index: index }) -> index - _ -> 42 - "# - ), - 1u64, - u64 - ) -} - -#[test] -fn str_from_utf32_lossy() { - assert_evals_to!( - r#"Str.from_utf32_lossy [0x72, 0x123456, 0x63]"#, - roc_std::RocStr::from("r�c"), - roc_std::RocStr - ) -} - -#[test] -fn str_equality() { - assert_evals_to!(r#""a" == "a""#, true, bool); - assert_evals_to!( - r#""loremipsumdolarsitamet" == "loremipsumdolarsitamet""#, - true, - bool - ); - assert_evals_to!(r#""a" != "b""#, true, bool); - assert_evals_to!(r#""a" == "b""#, false, bool); -} - -#[test] -fn str_join_comma_small() { - assert_evals_to!( - r#"Str.join_with ["1", "2"] ", " "#, - RocStr::from("1, 2"), - RocStr - ); -} - -#[test] -fn str_join_comma_big() { - assert_evals_to!( - r#"Str.join_with ["10000000", "2000000", "30000000"] ", " "#, - RocStr::from("10000000, 2000000, 30000000"), - RocStr - ); -} - -#[test] -fn str_join_comma_single() { - assert_evals_to!(r#"Str.join_with ["1"] ", " "#, RocStr::from("1"), RocStr); -} - -#[test] -fn str_to_utf8() { - assert_evals_to!( - r#"Str.to_utf8 "hello""#, - RocList::from_slice(&[104, 101, 108, 108, 111]), - RocList - ); - assert_evals_to!( - r#"Str.to_utf8 "this is a long string""#, - RocList::from_slice(&[ - 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 108, 111, 110, 103, 32, 115, 116, 114, - 105, 110, 103 - ]), - RocList - ); -} - -#[test] -fn str_from_utf8() { - assert_evals_to!( - indoc!( - r#" - bytes = - Str.to_utf8 "hello" - - when Str.from_utf8 bytes is - Ok utf8_string -> utf8_string - _ -> "" - "# - ), - RocStr::from("hello"), - RocStr - ); -} - -#[test] -fn str_from_utf8_slice() { - assert_evals_to!( - indoc!( - r#" - bytes = - Str.to_utf8 "hello" - |> List.sublist { start: 1, len: 4 } - - when Str.from_utf8 bytes is - Ok utf8_string -> utf8_string - _ -> "" - "# - ), - RocStr::from("ello"), - RocStr - ); -} - -#[test] -fn str_from_utf8_slice_not_end() { - assert_evals_to!( - indoc!( - r#" - bytes = - Str.to_utf8 "hello" - |> List.sublist { start: 1, len: 3 } - - when Str.from_utf8 bytes is - Ok utf8_string -> utf8_string - _ -> "" - "# - ), - RocStr::from("ell"), - RocStr - ); -} - -#[test] -fn str_from_utf8_order_does_not_matter() { - assert_evals_to!( - indoc!( - r#" - bytes = - Str.to_utf8 "hello" - |> List.sublist { start: 1, len: 3 } - - when Str.from_utf8 bytes is - Ok utf8_string -> utf8_string - _ -> "" - "# - ), - RocStr::from("ell"), - RocStr - ); -} - -#[test] -fn str_repeat_small() { - assert_evals_to!( - indoc!(r#"Str.repeat "Roc" 2"#), - RocStr::from("RocRoc"), - RocStr - ); -} - -#[test] -fn str_repeat_big() { - assert_evals_to!( - indoc!(r#"Str.repeat "more than 16 characters" 2"#), - RocStr::from("more than 16 charactersmore than 16 characters"), - RocStr - ); -} - -#[test] -fn str_repeat_empty_string() { - assert_evals_to!(indoc!(r#"Str.repeat "" 3"#), RocStr::from(""), RocStr); -} - -#[test] -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_small_blank_string() { - assert_evals_to!(indoc!(r#"Str.trim " ""#), RocStr::from(""), RocStr); -} - -#[test] -fn str_trim_small_to_small() { - assert_evals_to!( - indoc!(r#"Str.trim " hello ""#), - RocStr::from("hello"), - RocStr - ); -} - -#[test] -fn str_trim_large_to_large_unique() { - assert_evals_to!( - indoc!(r#"Str.trim (Str.concat " " "hello world from a large string ")"#), - RocStr::from("hello world from a large string"), - RocStr - ); -} - -#[test] -fn str_trim_large_to_small_unique() { - assert_evals_to!( - indoc!(r#"Str.trim (Str.concat " " "hello ")"#), - RocStr::from("hello"), - RocStr - ); -} - -#[test] -fn str_trim_large_to_large_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello world world " - - { trimmed: Str.trim original, original: original } - "# - ), - ( - RocStr::from(" hello world world "), - RocStr::from("hello world world"), - ), - (RocStr, RocStr) - ); -} - -#[test] -fn str_trim_large_to_small_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello " - - { trimmed: Str.trim original, original: original } - "# - ), - (RocStr::from(" hello "), RocStr::from("hello"),), - (RocStr, RocStr) - ); -} - -#[test] -fn str_trim_small_to_small_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello " - - { trimmed: Str.trim original, original: original } - "# - ), - (RocStr::from(" hello "), RocStr::from("hello"),), - (RocStr, RocStr) - ); -} - -#[test] -fn str_trim_start_small_blank_string() { - assert_evals_to!(indoc!(r#"Str.trim_start " ""#), RocStr::from(""), RocStr); -} - -#[test] -fn str_trim_start_small_to_small() { - assert_evals_to!( - indoc!(r#"Str.trim_start " hello ""#), - RocStr::from("hello "), - RocStr - ); -} - -#[test] -fn str_trim_start_large_to_large_unique() { - assert_evals_to!( - indoc!(r#"Str.trim_start (Str.concat " " "hello world from a large string ")"#), - RocStr::from("hello world from a large string "), - RocStr - ); -} - -#[test] -fn str_trim_start_large_to_small_unique() { - assert_evals_to!( - indoc!(r#"Str.trim_start (Str.concat " " "hello ")"#), - RocStr::from("hello "), - RocStr - ); -} - -#[test] -fn str_trim_end_small_blank_string() { - assert_evals_to!(indoc!(r#"Str.trim_end " ""#), RocStr::from(""), RocStr); -} - -#[test] -fn str_trim_end_small_to_small() { - assert_evals_to!( - indoc!(r#"Str.trim_end " hello ""#), - RocStr::from(" hello"), - RocStr - ); -} - -#[test] -fn str_trim_end_large_to_large_unique() { - assert_evals_to!( - indoc!(r#"Str.trim_end (Str.concat " hello world from a large string" " ")"#), - RocStr::from(" hello world from a large string"), - RocStr - ); -} - -#[test] -fn str_trim_end_large_to_small_unique() { - assert_evals_to!( - indoc!(r#"Str.trim_end (Str.concat " hello" " ")"#), - RocStr::from(" hello"), - RocStr - ); -} - -#[test] -fn str_trim_end_large_to_large_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello world world " - - { trimmed: Str.trim_end original, original: original } - "# - ), - ( - RocStr::from(" hello world world "), - RocStr::from(" hello world world"), - ), - (RocStr, RocStr) - ); -} - -#[test] -fn str_trim_end_large_to_small_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello " - - { trimmed: Str.trim_end original, original: original } - "# - ), - (RocStr::from(" hello "), RocStr::from(" hello"),), - (RocStr, RocStr) - ); -} - -#[test] -fn str_trim_end_small_to_small_shared() { - assert_evals_to!( - indoc!( - r#" - original : Str - original = " hello " - - { trimmed: Str.trim_end original, original: original } - "# - ), - (RocStr::from(" hello "), RocStr::from(" hello"),), - (RocStr, RocStr) - ); -} - -#[test] -fn str_to_i128() { - assert_evals_to!( - indoc!( - r#" - when Str.to_i128 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - I128::from(1), - I128 - ); -} - -#[test] -fn str_to_u128() { - assert_evals_to!( - indoc!( - r#" - when Str.to_u128 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - U128::from(1), - U128 - ); -} - -#[test] -fn str_to_i64() { - assert_evals_to!( - indoc!( - r#" - when Str.to_i64 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - i64 - ); -} - -#[test] -fn str_to_u64() { - assert_evals_to!( - indoc!( - r#" - when Str.to_u64 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - u64 - ); -} - -#[test] -fn str_to_i32() { - assert_evals_to!( - indoc!( - r#" - when Str.to_i32 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - i32 - ); -} - -#[test] -fn str_to_u32() { - assert_evals_to!( - indoc!( - r#" - when Str.to_u32 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - u32 - ); -} - -#[test] -fn str_to_i16() { - assert_evals_to!( - indoc!( - r#" - when Str.to_i16 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - i16 - ); -} - -#[test] -fn str_to_u16() { - assert_evals_to!( - indoc!( - r#" - when Str.to_u16 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - u16 - ); -} - -#[test] -fn str_to_i8() { - assert_evals_to!( - indoc!( - r#" - when Str.to_i8 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - i8 - ); -} - -#[test] -fn str_to_u8() { - assert_evals_to!( - indoc!( - r#" - when Str.to_u8 "1" is - Ok n -> n - Err _ -> 0 - "# - ), - 1, - u8 - ); -} - -#[test] -fn str_to_f64() { - assert_evals_to!( - indoc!( - r#" - when Str.to_f64 "1.0" is - Ok n -> n - Err _ -> 0 - "# - ), - 1.0, - f64 - ); -} - -#[test] -fn str_to_f32() { - assert_evals_to!( - indoc!( - r#" - when Str.to_f32 "1.0" is - Ok n -> n - Err _ -> 0 - "# - ), - 1.0, - f32 - ); -} - -#[test] -fn str_to_dec() { - use roc_std::RocDec; - - assert_evals_to!( - indoc!( - r#" - when Str.to_dec "1.0" is - Ok n -> n - Err _ -> 0 - "# - ), - RocDec::from_str("1.0").unwrap(), - RocDec - ); -}