Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.0

0.7.0 / tests / unit / utf8_test.lucb

## The `utf8` module against its archive: scalars encode, decode and are validated.
import utf8

test "scalars encode, decode and are validated":
    var room: u8[4]
    assert(utf8.encode(0x41, room[0..]) == 1 and room[0] == 0x41)
    assert(utf8.encode(0x20AC, room[0..]) == 3 and room[0] == 0xE2 and room[1] == 0x82 and room[2] == 0xAC)
    var wide: u8[4]
    assert(utf8.encode(0x1F600, wide[0..]) == 4 and utf8.encode(0xD800, wide[0..]) == none)
    assert(utf8.encoded_length(0x7F) == 1 and utf8.encoded_length(0x7FF) == 2 and utf8.encoded_length(0x110000) == none)
    let euro = utf8.decode(room[..<3]) else trap("no scalar")
    assert((u32)euro.scalar == 0x20AC and euro.width == 3)
    assert(utf8.valid("plain ascii".bytes) and utf8.valid(room[..<3]))
    let bad: u8[2] = [0xC0, 0x80]
    assert(not utf8.valid(bad[0..]) and utf8.invalid_offset(bad[0..]) == 0)