dymokomi/luce-browser-foundation
Source of release 0.6.0
0.6.0 / src / web_infra / tests_strings.lucb
#==============================================================================================
#
# tests_strings - tests of web_infra
#
# DESCRIPTION:
# Port of Tests/LibWeb/TestStrings.cpp (Ladybird 47c82b38d0; is_code_unit_prefix), and
# tests of the other Infra helpers, pinned to the reference build's results.
#
#==============================================================================================
## A StringView over bytes that need not be UTF-8.
func sv_bytes(bytes: const u8[]) -> ak.StringView:
return ak.literalsv_u8(bytes.data, bytes.length)
## The text of a String.
func text_of(string: ak.String) -> str:
# A short String keeps its bytes inline, in this frame's copy: copy them out.
let view = ak.string_bytes_as_string_view(&string)
let bytes = ak.string_view_bytes(&view)
let copy = new u8[bytes.length] --- else trap("out of memory")
for i in 0..<bytes.length:
copy[i] = bytes[i]
return (str)copy
## The text of a Utf16String, converted to UTF-8.
func text_of_utf16(string: ak.Utf16String) -> str:
return text_of(ak.utf16_string_base_to_utf8(&string.utf16_string_base))
## A String of a string literal.
func string_of(text: str) -> ak.String:
return ak.string_from_utf8(ak.sv(text)) else trap("MUST: String::from_utf8")
# mark: Without other regions ===================================================================
test "is_ascii_whitespace":
let whitespace: u32[5] = [0x09, 0x0a, 0x0c, 0x0d, 0x20]
for code_point in whitespace:
assert(is_ascii_whitespace(code_point))
# U+000B VT is not ASCII whitespace in Infra (it is in AK::is_ascii_space).
let not_whitespace: u32[5] = [0x0b, 0x00, 0x41, 0xa0, 0x3000]
for code_point in not_whitespace:
assert(not is_ascii_whitespace(code_point))
assert(ascii_whitespace == "\t\n\u{C}\r ")
for code_point in ascii_whitespace_code_points:
assert(is_ascii_whitespace(code_point))
test "byte_sequence_prefix_and_order":
assert(is_prefix_of("ab".bytes, "abc".bytes))
assert(not is_prefix_of("abc".bytes, "ab".bytes))
assert(is_prefix_of("".bytes, "abc".bytes))
assert(is_byte_less_than("abc".bytes, "abd".bytes))
assert(is_byte_less_than("ab".bytes, "abc".bytes))
assert(not is_byte_less_than("abc".bytes, "ab".bytes))
assert(not is_byte_less_than("abc".bytes, "abc".bytes))
# mark: TestStrings.cpp =========================================================================
test "is_code_unit_prefix":
# Basic prefix match
assert(is_code_unit_prefix(ak.sv("abc"), ak.sv("abcde")))
# Exact match
assert(is_code_unit_prefix(ak.sv("abc"), ak.sv("abc")))
# Empty prefix
assert(is_code_unit_prefix(ak.sv(""), ak.sv("abc")))
# Empty input string
assert(not is_code_unit_prefix(ak.sv("abc"), ak.sv("")))
# Both strings empty
assert(is_code_unit_prefix(ak.sv(""), ak.sv("")))
# Prefix longer than input string
assert(not is_code_unit_prefix(ak.sv("abcdef"), ak.sv("abc")))
# Non-ASCII characters
assert(is_code_unit_prefix(ak.sv("こんにちは"), ak.sv("こんにちは世界")))
assert(not is_code_unit_prefix(ak.sv("世界"), ak.sv("こんにちは世界")))
assert(is_code_unit_prefix(ak.sv("こ"), ak.sv("こん")))
assert(not is_code_unit_prefix(ak.sv("こん"), ak.sv("こ")))
# Special characters
assert(is_code_unit_prefix(ak.sv("!@#"), ak.sv("!@#$%^")))
assert(not is_code_unit_prefix(ak.sv("!@#$"), ak.sv("!@#")))
# Case sensitivity
assert(not is_code_unit_prefix(ak.sv("abc"), ak.sv("ABC")))
assert(not is_code_unit_prefix(ak.sv("ABC"), ak.sv("abc")))
# mark: Reference-build cases ===================================================================
test "normalize_newlines":
assert(text_of(normalize_newlines_string(string_of("a\r\nb\rc\n\r\r\nd"))) == "a\nb\nc\n\n\nd")
assert(text_of(normalize_newlines_string(string_of("abc"))) == "abc")
let utf16 = ak.utf16_string_from_utf8_string_view(ak.sv("x\r\ny\rz\r"))
assert(text_of_utf16(normalize_newlines_utf16_string(utf16)) == "x\ny\nz\n")
test "strip_and_collapse_whitespace":
let collapsed = strip_and_collapse_whitespace_string_view(ak.sv(" a \t\n b\u{C}\u{C}c ")) else trap("MUST")
assert(text_of(collapsed) == "a b c")
# U+000B VT is not ASCII whitespace.
let vertical_tab = strip_and_collapse_whitespace_string_view(ak.sv("\u{B} a\u{B}b ")) else trap("MUST")
assert(text_of(vertical_tab) == "\u{B} a\u{B}b")
let utf16 = ak.utf16_string_from_utf8_string_view(ak.sv(" a \t\n b\u{C}\u{C}c "))
assert(text_of_utf16(strip_and_collapse_whitespace_utf16_string(utf16)) == "a b c")
let unchanged = ak.utf16_string_from_utf8_string_view(ak.sv("abc"))
assert(text_of_utf16(strip_and_collapse_whitespace_utf16_string(unchanged)) == "abc")
test "convert_to_scalar_value_string":
# An encoded lone surrogate (U+D800) becomes U+FFFD.
let converted = convert_to_scalar_value_string(sv_bytes(b"a\xed\xa0\x80z")) else trap("MUST")
assert(text_of(converted) == "a\u{FFFD}z")
test "code_unit_less_than":
assert(code_unit_less_than(ak.sv("abc"), ak.sv("abd")))
assert(not code_unit_less_than(ak.sv("abd"), ak.sv("abc")))
assert(code_unit_less_than(ak.sv("ab"), ak.sv("abc")))
# U+FFFF is one code unit, U+10000 two starting with 0xD800: code-unit order differs from
# code-point order.
assert(not code_unit_less_than(ak.sv("\u{FFFF}"), ak.sv("\u{10000}")))
assert(code_unit_less_than(ak.sv("\u{10000}"), ak.sv("\u{FFFF}")))
test "byte_lowercase_and_uppercase":
var buffer = ak.byte_buffer_copy_span_u8("Hello, WORLD 123".bytes) else trap("MUST")
byte_lowercase(&buffer)
assert((str)ak.byte_buffer_bytes_const(&buffer) == "hello, world 123")
byte_uppercase(&buffer)
assert((str)ak.byte_buffer_bytes_const(&buffer) == "HELLO, WORLD 123")