Skip to content

Commit

Permalink
add tests that use stringslice assert_equal from modularml#3898
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Dec 19, 2024
1 parent ba2c415 commit 79a6745
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 182 deletions.
120 changes: 59 additions & 61 deletions stdlib/test/builtin/test_string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
105 changes: 52 additions & 53 deletions stdlib/test/collections/test_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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():
Expand Down
Loading

0 comments on commit 79a6745

Please sign in to comment.