llm
Download
60 lines 1.6 KiBTomoModified 2026-09-22 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
test "byte literals"
    >> Byte(100)
    >> Byte(0x64)
    assert Byte(100) == Byte(0x64)
    >> Byte(0xFF)
    assert Byte(0xFF) == Byte(0xFF)

test "byte hex formatting"
    >> b := Byte(0x0F)
    >> b.hex()
    assert b.hex() == "0F"
    >> b.hex(prefix=yes)
    assert b.hex(prefix=yes) == "0x0F"
    >> b.hex(uppercase=no)
    assert b.hex(uppercase=no) == "0f"

test "byte get_bit"
    >> Byte(0x06)
    >> Byte(0x06).get_bit(1)
    assert not Byte(0x06).get_bit(1)
    >> Byte(0x06).get_bit(2)
    assert Byte(0x06).get_bit(2)
    >> Byte(0x06).get_bit(3)
    assert Byte(0x06).get_bit(3)
    >> Byte(0x06).get_bit(4)
    assert not Byte(0x06).get_bit(4)

test "byte bitwise negation"
    >> b := Byte(0x0F)
    >> not b
    assert not b == Byte(0xF0)

    # `not` flips all eight bits and nothing above them:
    assert not Byte(0x00) == Byte(0xFF)
    assert not Byte(0xFF) == Byte(0x00)

    # Flipping twice is the identity:
    assert not not b == b

    assert (b and not b) == Byte(0x00)
    assert (b or not b) == Byte(0xFF)

test "Byte from text"
    # A literal folds at compile time ('A' in the generated C) and `!` on it
    # emits no none-check:
    assert Byte("A")! == Byte(65)
    assert Byte("\t")! == Byte(9)
    assert Byte("'")! == Byte(39)

    # A runtime text converts only when it is exactly one UTF-8 byte:
    one := "A"
    two := "é"
    empty := ""
    assert Byte(one) == Byte(65)
    assert Byte(two) == none
    assert Byte(one.repeat(2)) == none
    assert Byte(empty) == none

    # A literal that could only ever be `none` is a compile error instead:
    # Byte("ab"), Byte("é"), Byte("")