diff --git a/stdlib/test/builtin/test_string_literal.mojo b/stdlib/test/builtin/test_string_literal.mojo index bec6a72b04..34d3085d7a 100644 --- a/stdlib/test/builtin/test_string_literal.mojo +++ b/stdlib/test/builtin/test_string_literal.mojo @@ -369,87 +369,85 @@ def test_center(): def test_split(): - fn st(value: StringLiteral) -> StringLiteral: - return value - + alias L = List[StringSlice[StaticConstantOrigin]] # Should add all whitespace-like chars as one # test all unicode separators # 0 is to build a String with null terminator - # alias next_line = List[UInt8](0xC2, 0x85, 0) - # """TODO: \\x85""" - # alias unicode_line_sep = List[UInt8](0xE2, 0x80, 0xA8, 0) - # """TODO: \\u2028""" - # alias unicode_paragraph_sep = List[UInt8](0xE2, 0x80, 0xA9, 0) - # """TODO: \\u2029""" + alias next_line = List[UInt8](0xC2, 0x85, 0) + """TODO: \\x85""" + alias unicode_line_sep = List[UInt8](0xE2, 0x80, 0xA8, 0) + """TODO: \\u2028""" + alias unicode_paragraph_sep = List[UInt8](0xE2, 0x80, 0xA9, 0) + """TODO: \\u2029""" # TODO add line and paragraph separator as StringLiteral once unicode # escape secuences are accepted univ_sep_var = ( - st(" ") - + st("\t") - + st("\n") - + st("\r") - + st("\v") - + st("\f") - + st("\x1c") - + st("\x1d") - + st("\x1e") - # + st(next_line) - # + st(unicode_line_sep) - # + st(unicode_paragraph_sep) + " " + + "\t" + + "\n" + + "\r" + + "\v" + + "\f" + + "\x1c" + + "\x1d" + + "\x1e" + + String(next_line) + + String(unicode_line_sep) + + String(unicode_paragraph_sep) ) s = univ_sep_var + "hello" + univ_sep_var + "world" + univ_sep_var - assert_equal(s.split(), List[String]("hello", "world")) + assert_equal(s.split(), L("hello", "world")) # should split into empty strings between separators - assert_equal(st("1,,,3").split(","), List[String]("1", "", "", "3")) - assert_equal(st(",,,").split(","), List[String]("", "", "", "")) - assert_equal(st(" a b ").split(" "), List[String]("", "a", "b", "")) - assert_equal(st("abababaaba").split("aba"), List[String]("", "b", "", "")) - assert_true(len(st("").split()) == 0) - assert_true(len(st(" ").split()) == 0) - assert_true(len(st("").split(" ")) == 1) - assert_true(len(st(",").split(",")) == 2) - assert_true(len(st(" ").split(" ")) == 2) - assert_true(len(st("").split("")) == 2) - assert_true(len(st(" ").split(" ")) == 3) - assert_true(len(st(" ").split(" ")) == 4) + assert_equal("1,,,3".split(","), L("1", "", "", "3")) + assert_equal(",,,".split(","), L("", "", "", "")) + assert_equal(" a b ".split(" "), L("", "a", "b", "")) + assert_equal("abababaaba".split("aba"), L("", "b", "", "")) + assert_true(len("".split()) == 0) + assert_true(len(" ".split()) == 0) + assert_true(len("".split(" ")) == 1) + assert_true(len(",".split(",")) == 2) + assert_true(len(" ".split(" ")) == 2) + assert_true(len("".split("")) == 2) + assert_true(len(" ".split(" ")) == 3) + assert_true(len(" ".split(" ")) == 4) # should split into maxsplit + 1 items - assert_equal(st("1,2,3").split(",", 0), List[String]("1,2,3")) - assert_equal(st("1,2,3").split(",", 1), List[String]("1", "2,3")) + assert_equal("1,2,3".split(",", 0), L("1,2,3")) + assert_equal("1,2,3".split(",", 1), L("1", "2,3")) # Split in middle - assert_equal(st("faang").split(st("n")), List[String]("faa", "g")) + assert_equal("faang".split("n"), L("faa", "g")) # No match from the delimiter - assert_equal(st("hello world").split(st("x")), List[String]("hello world")) + assert_equal("hello world".split("x"), L("hello world")) # Multiple character delimiter - assert_equal(st("hello").split(st("ll")), List[String]("he", "o")) + assert_equal("hello".split("ll"), L("he", "o")) - res = List[String]("", "bb", "", "", "", "bbb", "") - assert_equal(st("abbaaaabbba").split("a"), res) - assert_equal(st("abbaaaabbba").split("a", 8), res) + res = L("", "bb", "", "", "", "bbb", "") + assert_equal("abbaaaabbba".split("a"), res) + assert_equal("abbaaaabbba".split("a", 8), res) s1 = st("abbaaaabbba").split("a", 5) - assert_equal(s1, List[String]("", "bb", "", "", "", "bbba")) - assert_equal(st("aaa").split("a", 0), List[String]("aaa")) - assert_equal(st("a").split("a"), List[String]("", "")) - assert_equal(st("1,2,3").split("3", 0), List[String]("1,2,3")) - assert_equal(st("1,2,3").split("3", 1), List[String]("1,2,", "")) - assert_equal(st("1,2,3,3").split("3", 2), List[String]("1,2,", ",", "")) - assert_equal(st("1,2,3,3,3").split("3", 2), List[String]("1,2,", ",", ",3")) - - assert_equal(st("Hello πŸ”₯!").split(), List[String]("Hello", "πŸ”₯!")) - - s2 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split(" ") - assert_equal(s2, List[String]("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) - s3 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split("ΠΌ") - assert_equal(s3, List[String]("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) - - assert_equal(st("123").split(""), List[String]("", "1", "2", "3", "")) - assert_equal("".join(st("123").split("")), "123") - assert_equal(st(",1,2,3,").split(","), st("123").split("")) - assert_equal(",".join(st("123").split("")), ",1,2,3,") + assert_equal(s1, L("", "bb", "", "", "", "bbba")) + assert_equal("aaa".split("a", 0), L("aaa")) + assert_equal("a".split("a"), L("", "")) + assert_equal("1,2,3".split("3", 0), L("1,2,3")) + assert_equal("1,2,3".split("3", 1), L("1,2,", "")) + assert_equal("1,2,3,3".split("3", 2), L("1,2,", ",", "")) + assert_equal("1,2,3,3,3".split("3", 2), L("1,2,", ",", ",3")) + + assert_equal("Hello πŸ”₯!".split(), L("Hello", "πŸ”₯!")) + + s2 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split(" ") + assert_equal(s2, L("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) + s3 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split("ΠΌ") + assert_equal(s3, L("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) + + assert_equal("123".split(""), L("", "1", "2", "3", "")) + assert_equal("".join("123".split("")), "123") + assert_equal(",1,2,3,".split(","), "123".split("")) + assert_equal(",".join("123".split("")), ",1,2,3,") def test_splitlines(): diff --git a/stdlib/test/collections/test_string.mojo b/stdlib/test/collections/test_string.mojo index b6e83ccc74..f9a80bf24f 100644 --- a/stdlib/test/collections/test_string.mojo +++ b/stdlib/test/collections/test_string.mojo @@ -28,7 +28,8 @@ from testing import ( assert_true, ) -from utils import StringRef, StringSlice +from utils import StringRef +from utils.string_slice import StringSlice @value @@ -702,9 +703,7 @@ def test_rfind(): def test_split(): - fn st(value: StringLiteral) -> String: - return value - + alias L = List[StringSlice[StaticConstantOrigin]] # Should add all whitespace-like chars as one # test all unicode separators # 0 is to build a String with null terminator @@ -717,72 +716,72 @@ def test_split(): # TODO add line and paragraph separator as StringLiteral once unicode # escape secuences are accepted univ_sep_var = ( - st(" ") - + st("\t") - + st("\n") - + st("\r") - + st("\v") - + st("\f") - + st("\x1c") - + st("\x1d") - + st("\x1e") + " " + + "\t" + + "\n" + + "\r" + + "\v" + + "\f" + + "\x1c" + + "\x1d" + + "\x1e" + String(next_line) + String(unicode_line_sep) + String(unicode_paragraph_sep) ) s = univ_sep_var + "hello" + univ_sep_var + "world" + univ_sep_var - assert_equal(s.split(), List[String]("hello", "world")) + assert_equal(s.split(), L("hello", "world")) # should split into empty strings between separators - assert_equal(st("1,,,3").split(","), List[String]("1", "", "", "3")) - assert_equal(st(",,,").split(","), List[String]("", "", "", "")) - assert_equal(st(" a b ").split(" "), List[String]("", "a", "b", "")) - assert_equal(st("abababaaba").split("aba"), List[String]("", "b", "", "")) - assert_true(len(st("").split()) == 0) - assert_true(len(st(" ").split()) == 0) - assert_true(len(st("").split(" ")) == 1) - assert_true(len(st(",").split(",")) == 2) - assert_true(len(st(" ").split(" ")) == 2) - assert_true(len(st("").split("")) == 2) - assert_true(len(st(" ").split(" ")) == 3) - assert_true(len(st(" ").split(" ")) == 4) + assert_equal("1,,,3".split(","), L("1", "", "", "3")) + assert_equal(",,,".split(","), L("", "", "", "")) + assert_equal(" a b ".split(" "), L("", "a", "b", "")) + assert_equal("abababaaba".split("aba"), L("", "b", "", "")) + assert_true(len("".split()) == 0) + assert_true(len(" ".split()) == 0) + assert_true(len("".split(" ")) == 1) + assert_true(len(",".split(",")) == 2) + assert_true(len(" ".split(" ")) == 2) + assert_true(len("".split("")) == 2) + assert_true(len(" ".split(" ")) == 3) + assert_true(len(" ".split(" ")) == 4) # should split into maxsplit + 1 items - assert_equal(st("1,2,3").split(",", 0), List[String]("1,2,3")) - assert_equal(st("1,2,3").split(",", 1), List[String]("1", "2,3")) + assert_equal("1,2,3".split(",", 0), L("1,2,3")) + assert_equal("1,2,3".split(",", 1), L("1", "2,3")) # Split in middle - assert_equal(st("faang").split(st("n")), List[String]("faa", "g")) + assert_equal("faang".split("n"), L("faa", "g")) # No match from the delimiter - assert_equal(st("hello world").split(st("x")), List[String]("hello world")) + assert_equal("hello world".split("x"), L("hello world")) # Multiple character delimiter - assert_equal(st("hello").split(st("ll")), List[String]("he", "o")) + assert_equal("hello".split("ll"), L("he", "o")) - res = List[String]("", "bb", "", "", "", "bbb", "") - assert_equal(st("abbaaaabbba").split("a"), res) - assert_equal(st("abbaaaabbba").split("a", 8), res) + res = L("", "bb", "", "", "", "bbb", "") + assert_equal("abbaaaabbba".split("a"), res) + assert_equal("abbaaaabbba".split("a", 8), res) s1 = st("abbaaaabbba").split("a", 5) - assert_equal(s1, List[String]("", "bb", "", "", "", "bbba")) - assert_equal(st("aaa").split("a", 0), List[String]("aaa")) - assert_equal(st("a").split("a"), List[String]("", "")) - assert_equal(st("1,2,3").split("3", 0), List[String]("1,2,3")) - assert_equal(st("1,2,3").split("3", 1), List[String]("1,2,", "")) - assert_equal(st("1,2,3,3").split("3", 2), List[String]("1,2,", ",", "")) - assert_equal(st("1,2,3,3,3").split("3", 2), List[String]("1,2,", ",", ",3")) - - assert_equal(st("Hello πŸ”₯!").split(), List[String]("Hello", "πŸ”₯!")) - - s2 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split(" ") - assert_equal(s2, List[String]("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) - s3 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split("ΠΌ") - assert_equal(s3, List[String]("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) - - assert_equal(st("123").split(""), List[String]("", "1", "2", "3", "")) - assert_equal("".join(st("123").split("")), "123") - assert_equal(st(",1,2,3,").split(","), st("123").split("")) - assert_equal(",".join(st("123").split("")), ",1,2,3,") + assert_equal(s1, L("", "bb", "", "", "", "bbba")) + assert_equal("aaa".split("a", 0), L("aaa")) + assert_equal("a".split("a"), L("", "")) + assert_equal("1,2,3".split("3", 0), L("1,2,3")) + assert_equal("1,2,3".split("3", 1), L("1,2,", "")) + assert_equal("1,2,3,3".split("3", 2), L("1,2,", ",", "")) + assert_equal("1,2,3,3,3".split("3", 2), L("1,2,", ",", ",3")) + + assert_equal("Hello πŸ”₯!".split(), L("Hello", "πŸ”₯!")) + + s2 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split(" ") + assert_equal(s2, L("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) + s3 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split("ΠΌ") + assert_equal(s3, L("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) + + assert_equal("123".split(""), L("", "1", "2", "3", "")) + assert_equal("".join("123".split("")), "123") + assert_equal(",1,2,3,".split(","), "123".split("")) + assert_equal(",".join("123".split("")), ",1,2,3,") def test_splitlines(): diff --git a/stdlib/test/utils/test_string_slice.mojo b/stdlib/test/utils/test_string_slice.mojo index 6efc6dc500..982d4b165d 100644 --- a/stdlib/test/utils/test_string_slice.mojo +++ b/stdlib/test/utils/test_string_slice.mojo @@ -489,19 +489,7 @@ def test_splitlines(): def test_split(): - alias S = StringSlice[StaticConstantOrigin] - - fn st(value: StringLiteral) -> StringSlice[ImmutableAnyOrigin]: - return rebind[StringSlice[ImmutableAnyOrigin]](StringSlice(value)) - - # FIXME: remove once StringSlice conforms to TestableCollectionElement - fn _assert_equal[ - O: ImmutableOrigin - ](l1: List[StringSlice[O]], l2: List[S]) raises: - assert_equal(len(l1), len(l2)) - for i in range(len(l1)): - assert_equal(str(l1[i]), str(l2[i])) - + alias L = List[StringSlice[StaticConstantOrigin]] # Should add all whitespace-like chars as one # test all unicode separators # 0 is to build a String with null terminator @@ -514,75 +502,72 @@ def test_split(): # TODO add line and paragraph separator as StringLiteral once unicode # escape secuences are accepted univ_sep_var = ( - String(" ") - + String("\t") - + String("\n") - + String("\r") - + String("\v") - + String("\f") - + String("\x1c") - + String("\x1d") - + String("\x1e") - + String(next_line).as_string_slice() - + String(unicode_line_sep).as_string_slice() - + String(unicode_paragraph_sep).as_string_slice() + " " + + "\t" + + "\n" + + "\r" + + "\v" + + "\f" + + "\x1c" + + "\x1d" + + "\x1e" + + String(next_line) + + String(unicode_line_sep) + + String(unicode_paragraph_sep) ) s = univ_sep_var + "hello" + univ_sep_var + "world" + univ_sep_var - sl = rebind[S](s.as_string_slice()) - _assert_equal(sl.split(), List[S]("hello", "world")) + assert_equal(s.split(), L("hello", "world")) # should split into empty strings between separators - _assert_equal(st("1,,,3").split(","), List[S]("1", "", "", "3")) - _assert_equal(st(",,,").split(","), List[S]("", "", "", "")) - _assert_equal(st(" a b ").split(" "), List[S]("", "a", "b", "")) - _assert_equal(st("abababaaba").split("aba"), List[S]("", "b", "", "")) - assert_true(len(st("").split()) == 0) - assert_true(len(st(" ").split()) == 0) - assert_true(len(st("").split(" ")) == 1) - assert_true(len(st(",").split(",")) == 2) - assert_true(len(st(" ").split(" ")) == 2) - assert_true(len(st("").split("")) == 2) - assert_true(len(st(" ").split(" ")) == 3) - assert_true(len(st(" ").split(" ")) == 4) + assert_equal("1,,,3".split(","), L("1", "", "", "3")) + assert_equal(",,,".split(","), L("", "", "", "")) + assert_equal(" a b ".split(" "), L("", "a", "b", "")) + assert_equal("abababaaba".split("aba"), L("", "b", "", "")) + assert_true(len("".split()) == 0) + assert_true(len(" ".split()) == 0) + assert_true(len("".split(" ")) == 1) + assert_true(len(",".split(",")) == 2) + assert_true(len(" ".split(" ")) == 2) + assert_true(len("".split("")) == 2) + assert_true(len(" ".split(" ")) == 3) + assert_true(len(" ".split(" ")) == 4) # should split into maxsplit + 1 items - _assert_equal(st("1,2,3").split(",", 0), List[S]("1,2,3")) - _assert_equal(st("1,2,3").split(",", 1), List[S]("1", "2,3")) + assert_equal("1,2,3".split(",", 0), L("1,2,3")) + assert_equal("1,2,3".split(",", 1), L("1", "2,3")) # Split in middle - _assert_equal(st("faang").split(st("n")), List[S]("faa", "g")) + assert_equal("faang".split("n"), L("faa", "g")) # No match from the delimiter - _assert_equal(st("hello world").split(st("x")), List[S]("hello world")) + assert_equal("hello world".split("x"), L("hello world")) # Multiple character delimiter - _assert_equal(st("hello").split(st("ll")), List[S]("he", "o")) + assert_equal("hello".split("ll"), L("he", "o")) - res = List[S]("", "bb", "", "", "", "bbb", "") - _assert_equal(st("abbaaaabbba").split("a"), res) - _assert_equal(st("abbaaaabbba").split("a", 8), res) + res = L("", "bb", "", "", "", "bbb", "") + assert_equal("abbaaaabbba".split("a"), res) + assert_equal("abbaaaabbba".split("a", 8), res) s1 = st("abbaaaabbba").split("a", 5) - _assert_equal(s1, List[S]("", "bb", "", "", "", "bbba")) - _assert_equal(st("aaa").split("a", 0), List[S]("aaa")) - _assert_equal(st("a").split("a"), List[S]("", "")) - _assert_equal(st("1,2,3").split("3", 0), List[S]("1,2,3")) - _assert_equal(st("1,2,3").split("3", 1), List[S]("1,2,", "")) - _assert_equal(st("1,2,3,3").split("3", 2), List[S]("1,2,", ",", "")) - _assert_equal(st("1,2,3,3,3").split("3", 2), List[S]("1,2,", ",", ",3")) - - _assert_equal(st("Hello πŸ”₯!").split(), List[S]("Hello", "πŸ”₯!")) - - s2 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split(" ") - _assert_equal(s2, List[S]("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) - s3 = st("Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚").split("ΠΌ") - _assert_equal(s3, List[S]("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) - - _assert_equal(st("123").split(""), List[S]("", "1", "2", "3", "")) - assert_equal("".join(st("123").split("")), "123") - _assert_equal( - st(",1,2,3,").split(","), rebind[List[S]](st("123").split("")) - ) - assert_equal(",".join(st("123").split("")), ",1,2,3,") + assert_equal(s1, L("", "bb", "", "", "", "bbba")) + assert_equal("aaa".split("a", 0), L("aaa")) + assert_equal("a".split("a"), L("", "")) + assert_equal("1,2,3".split("3", 0), L("1,2,3")) + assert_equal("1,2,3".split("3", 1), L("1,2,", "")) + assert_equal("1,2,3,3".split("3", 2), L("1,2,", ",", "")) + assert_equal("1,2,3,3,3".split("3", 2), L("1,2,", ",", ",3")) + + assert_equal("Hello πŸ”₯!".split(), L("Hello", "πŸ”₯!")) + + s2 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split(" ") + assert_equal(s2, L("Π›ΠΎΡ€Π΅ΠΌ", "ипсум", "Π΄ΠΎΠ»ΠΎΡ€", "сит", "Π°ΠΌΠ΅Ρ‚")) + s3 = "Π›ΠΎΡ€Π΅ΠΌ ипсум Π΄ΠΎΠ»ΠΎΡ€ сит Π°ΠΌΠ΅Ρ‚".split("ΠΌ") + assert_equal(s3, L("Π›ΠΎΡ€Π΅", " ипсу", " Π΄ΠΎΠ»ΠΎΡ€ сит Π°", "Π΅Ρ‚")) + + assert_equal("123".split(""), L("", "1", "2", "3", "")) + assert_equal("".join("123".split("")), "123") + assert_equal(",1,2,3,".split(","), "123".split("")) + assert_equal(",".join("123".split("")), ",1,2,3,") def test_rstrip():