Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.1

0.6.1 / src / ak / tests_byte_string.lucb

#==============================================================================================
#
#   tests_byte_string - ByteString tests
#
#   DESCRIPTION:
#       Port of Tests/AK/TestByteString.cpp (lines 1-260). to_int is in tests_to_number
#       (to_number<T>); move_string's "moved-from is empty" check has no counterpart (moves
#       are copies, DESIGN.md ยง3.3).
#
#==============================================================================================

## `ByteString("text")`.
func tc_bs(text: str) -> ByteString:
    return byte_string_from_str(text)

## `byte_string == "text"` (operator==(char const*)).
func tc_bs_is(string: const ByteString*, text: str) -> bool:
    return byte_string_eq_u8(string, text.bytes.data)

## strlen of a NUL-terminated pointer.
func tc_strlen(characters: const u8*) -> usize:
    var length: usize = 0
    while characters[length] != 0:
        length += 1
    return length

test "construct_empty":
    let empty = byte_string_empty()
    assert(byte_string_is_empty(&empty))

    var constructed = byte_string_empty()
    byte_string_construct(&constructed)
    assert(byte_string_is_empty(&constructed))

    let from_literal = tc_bs("")
    assert(byte_string_is_empty(&from_literal))

    assert(byte_string_impl(&from_literal) == byte_string_impl(&empty))
    assert(byte_string_characters(&from_literal)[0] == 0)

test "construct_contents":
    let test_string = tc_bs("ABCDEF")
    assert(not byte_string_is_empty(&test_string))
    assert(byte_string_length(&test_string) == 6)
    assert(byte_string_length(&test_string) == tc_strlen(byte_string_characters(&test_string)))
    assert(byte_string_bytes(&test_string) == "ABCDEF".bytes)

    assert(tc_bs_is(&test_string, "ABCDEF"))
    assert(not tc_bs_is(&test_string, "ABCDE"))
    assert(not tc_bs_is(&test_string, "ABCDEFG"))

test "equal":
    var constructed = byte_string_empty()
    byte_string_construct(&constructed)
    let empty = byte_string_empty()
    assert(byte_string_eq_byte_string(&empty, &constructed))

test "compare":
    let a = tc_bs("a")
    let b = tc_bs("b")
    let empty = byte_string_empty()
    let sv_a = sv("a")
    let sv_b = sv("b")

    assert(string_view_lt(&sv_a, byte_string_view(&b)))
    assert(not string_view_gt(&sv_a, byte_string_view(&b)))
    assert(string_view_gt(&sv_b, byte_string_view(&a)))
    assert(not string_view_lt(&sv_b, byte_string_view(&b)))
    assert(string_view_ge(&sv_a, byte_string_view(&a)))
    assert(not string_view_ge(&sv_a, byte_string_view(&b)))
    assert(string_view_le(&sv_a, byte_string_view(&a)))
    assert(not string_view_le(&sv_b, byte_string_view(&a)))

    assert(byte_string_gt(&a, &empty))
    assert(not byte_string_gt(&empty, &a))
    assert(byte_string_lt(&empty, &a))
    assert(not byte_string_lt(&a, &empty))
    assert(byte_string_ge_byte_string(&a, &empty))
    assert(not byte_string_ge_byte_string(&empty, &a))
    assert(byte_string_le_byte_string(&empty, &a))
    assert(not byte_string_le_byte_string(&a, &empty))

    assert(not byte_string_gt(&empty, &empty))
    assert(not byte_string_lt(&empty, &empty))
    assert(byte_string_ge_byte_string(&empty, &empty))
    assert(byte_string_le_byte_string(&empty, &empty))

test "index_access":
    let test_string = tc_bs("ABCDEF")
    assert(byte_string_at(&test_string, 0) == 'A')
    assert(byte_string_at(&test_string, 1) == 'B')

test "starts_with":
    let test_string = tc_bs("ABCDEF")
    assert(byte_string_starts_with_string_view(&test_string, sv("AB")))
    assert(byte_string_starts_with_u8(&test_string, 'A'))
    assert(not byte_string_starts_with_u8(&test_string, 'B'))
    assert(byte_string_starts_with_string_view(&test_string, sv("ABCDEF")))
    assert(not byte_string_starts_with_string_view(&test_string, sv("DEF")))
    assert(byte_string_starts_with_string_view(&test_string, sv("abc"), CaseSensitivity.case_insensitive))
    assert(not byte_string_starts_with_string_view(&test_string, sv("abc"), CaseSensitivity.case_sensitive))

test "ends_with":
    let test_string = tc_bs("ABCDEF")
    assert(byte_string_ends_with_string_view(&test_string, sv("EF")))
    assert(byte_string_ends_with_u8(&test_string, 'F'))
    assert(not byte_string_ends_with_u8(&test_string, 'E'))
    assert(byte_string_ends_with_string_view(&test_string, sv("ABCDEF")))
    assert(not byte_string_ends_with_string_view(&test_string, sv("ABC")))
    assert(byte_string_ends_with_string_view(&test_string, sv("def"), CaseSensitivity.case_insensitive))
    assert(not byte_string_ends_with_string_view(&test_string, sv("def"), CaseSensitivity.case_sensitive))

test "copy_string":
    let test_string = tc_bs("ABCDEF")
    let test_string_copy = test_string
    assert(byte_string_eq_byte_string(&test_string, &test_string_copy))
    assert(byte_string_characters(&test_string) == byte_string_characters(&test_string_copy))

test "move_string":
    let test_string = tc_bs("ABCDEF")
    let test_string_copy = test_string
    let test_string_move = test_string_copy
    assert(byte_string_eq_byte_string(&test_string, &test_string_move))

test "repeated":
    let zero = byte_string_repeated_u8('x', 0)
    assert(tc_bs_is(&zero, ""))
    let one = byte_string_repeated_u8('x', 1)
    assert(tc_bs_is(&one, "x"))
    let two = byte_string_repeated_u8('x', 2)
    assert(tc_bs_is(&two, "xx"))

test "to_lowercase":
    let string = tc_bs("ABC")
    let lowercase = byte_string_to_lowercase(&string)
    assert(tc_bs_is(&lowercase, "abc"))

test "to_uppercase":
    let string = tc_bs("AbC")
    let uppercase = byte_string_to_uppercase(&string)
    assert(tc_bs_is(&uppercase, "ABC"))

test "replace":
    var test_string = tc_bs("Well, hello Friends!")

    test_string = byte_string_replace(&test_string, sv("Friends"), sv("Testers"), ReplaceMode.first_only)
    assert(tc_bs_is(&test_string, "Well, hello Testers!"))

    test_string = byte_string_replace(&test_string, sv("ell"), sv("e're"), ReplaceMode.all)
    assert(tc_bs_is(&test_string, "We're, he'reo Testers!"))

    test_string = byte_string_replace(&test_string, sv("!"), sv(" :^)"), ReplaceMode.first_only)
    assert(tc_bs_is(&test_string, "We're, he'reo Testers :^)"))

    test_string = tc_bs("111._.111._.111")
    test_string = byte_string_replace(&test_string, sv("111"), sv("|||"), ReplaceMode.all)
    assert(tc_bs_is(&test_string, "|||._.|||._.|||"))

    test_string = byte_string_replace(&test_string, sv("|||"), sv("111"), ReplaceMode.first_only)
    assert(tc_bs_is(&test_string, "111._.|||._.|||"))

test "count":
    var test_string = tc_bs("Well, hello Friends!")
    var count = byte_string_count(&test_string, sv("Friends"))
    assert(count == 1)

    count = byte_string_count(&test_string, sv("ell"))
    assert(count == 2)

    count = byte_string_count(&test_string, sv("!"))
    assert(count == 1)

    test_string = tc_bs("111._.111._.111")
    count = byte_string_count(&test_string, sv("111"))
    assert(count == 3)

    count = byte_string_count(&test_string, sv("._."))
    assert(count == 2)

test "substring":
    let subject = tc_bs("abcdef")
    let whole = byte_string_substring_usize(&subject, 0, 6)
    assert(byte_string_eq_byte_string(&whole, &subject))
    let abc = byte_string_substring_usize(&subject, 0, 3)
    assert(tc_bs_is(&abc, "abc"))
    let def_part = byte_string_substring_usize(&subject, 3, 3)
    assert(tc_bs_is(&def_part, "def"))
    let empty_middle = byte_string_substring_usize(&subject, 3, 0)
    assert(tc_bs_is(&empty_middle, ""))
    let empty_end = byte_string_substring_usize(&subject, 6, 0)
    assert(tc_bs_is(&empty_end, ""))

test "split":
    var subject = tc_bs("foo bar baz")
    var parts = byte_string_split(&subject, ' ')
    assert(test_vector_size(&parts) == 3)
    let part_0 = test_vector_at(&parts, 0)
    let part_1 = test_vector_at(&parts, 1)
    let part_2 = test_vector_at(&parts, 2)
    assert(tc_bs_is(&part_0, "foo"))
    assert(tc_bs_is(&part_1, "bar"))
    assert(tc_bs_is(&part_2, "baz"))

    assert(byte_string_characters(&part_0)[3] == 0)
    assert(byte_string_characters(&part_1)[3] == 0)
    assert(byte_string_characters(&part_2)[3] == 0)

    subject = tc_bs("a    b")

    parts = byte_string_split(&subject, ' ')
    assert(test_vector_size(&parts) == 2)
    let a = test_vector_at(&parts, 0)
    let b = test_vector_at(&parts, 1)
    assert(tc_bs_is(&a, "a"))
    assert(tc_bs_is(&b, "b"))

    parts = byte_string_split(&subject, ' ', SplitBehavior.keep_empty)
    assert(test_vector_size(&parts) == 5)
    let expected: str[5] = ["a", "", "", "", "b"]
    for i: usize in 0..<5:
        let part = test_vector_at(&parts, i)
        assert(tc_bs_is(&part, expected[i]))

    subject = tc_bs("axxbx")
    let split_x = byte_string_split(&subject, 'x')
    assert(test_vector_size(&split_x) == 2)
    let split_x_empty = byte_string_split(&subject, 'x', SplitBehavior.keep_empty)
    assert(test_vector_size(&split_x_empty) == 4)
    let split_view_x = byte_string_split_view_u8(&subject, 'x')
    assert(test_vector_size(&split_view_x) == 2)
    let split_view_x_empty = byte_string_split_view_u8(&subject, 'x', SplitBehavior.keep_empty)
    assert(test_vector_size(&split_view_x_empty) == 4)

test "builder_zero_initial_capacity":
    var builder = string_builder_new_usize(0)
    string_builder_append_string_view(&builder, sv(""))
    let built = string_builder_to_byte_string(&builder)
    assert(byte_string_length(&built) == 0)

test "find":
    let a = tc_bs("foobarbar")
    assert(byte_string_find_string_view(&a, sv("bar")) == 3)
    assert(byte_string_find_string_view(&a, sv("baz")) == none)
    assert(byte_string_find_string_view(&a, sv("bar"), 4) == 6)
    assert(byte_string_find_string_view(&a, sv("bar"), 9) == none)

    assert(byte_string_find_u8(&a, 'f') == 0)
    assert(byte_string_find_u8(&a, 'x') == none)
    assert(byte_string_find_u8(&a, 'f', 1) == none)
    assert(byte_string_find_u8(&a, 'b') == 3)
    assert(byte_string_find_u8(&a, 'b', 4) == 6)
    assert(byte_string_find_u8(&a, 'b', 9) == none)

test "find_with_empty_needle":
    var string = tc_bs("")
    assert(byte_string_find_string_view(&string, sv("")) == 0)
    let all_in_empty = byte_string_find_all(&string, sv(""))
    assert(test_vector_size(&all_in_empty) == 1)
    assert(test_vector_at(&all_in_empty, 0) == 0)

    string = tc_bs("abc")
    assert(byte_string_find_string_view(&string, sv("")) == 0)
    let all_in_abc = byte_string_find_all(&string, sv(""))
    assert(test_vector_size(&all_in_abc) == 4)
    for i: usize in 0..<4:
        assert(test_vector_at(&all_in_abc, i) == i)