| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bytes; |
| 5 |
use fmt; |
| 6 |
use io; |
| 7 |
use strings; |
| 8 |
|
| 9 |
|
| 10 |
fn c_checkrange(chars: []u8, f: *fn (c: u8) bool) void = { |
| 11 |
for (let i = 0z; i < 256; i += 1) { |
| 12 |
let expected = for (let j = 0z; j < len(chars); j += 1) { |
| 13 |
if (chars[j] == i: u8) { |
| 14 |
break true; |
| 15 |
}; |
| 16 |
} else false; |
| 17 |
|
| 18 |
if (f(i: u8) != expected) { |
| 19 |
fmt::println(i, expected, f(i: u8))!; |
| 20 |
}; |
| 21 |
assert(f(i: u8) == expected); |
| 22 |
}; |
| 23 |
}; |
| 24 |
|
| 25 |
@test fn c_is_num() void = { |
| 26 |
const chars: [_]u8 = [ |
| 27 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', |
| 28 |
]; |
| 29 |
c_checkrange(chars, &c_is_num); |
| 30 |
}; |
| 31 |
|
| 32 |
@test fn c_is_print() void = { |
| 33 |
const chars: [_]u8 = [ |
| 34 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 35 |
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 36 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 37 |
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 38 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '\'', |
| 39 |
'(', ')', '+', ',', '-', '.', '/', ':', '=', '?', |
| 40 |
]; |
| 41 |
c_checkrange(chars, &c_is_print); |
| 42 |
}; |
| 43 |
|
| 44 |
@test fn utf8() void = { |
| 45 |
let buf: [16]u8 = [0...]; |
| 46 |
let b: [_]u8 = [ |
| 47 |
0x55, |
| 48 |
0x56, |
| 49 |
0xd0, 0x98, |
| 50 |
0xe0, 0xa4, 0xb9, |
| 51 |
0xf0, 0x90, 0x8d, 0x88 |
| 52 |
]; |
| 53 |
const runesat: [_]size = [0, 1, 2, 2, 4, 4, 4, 7, 7, 7, 7, 8]; |
| 54 |
|
| 55 |
let expected: str = strings::fromutf8([0xf0, 0x90, 0x8d, 0x88])!; |
| 56 |
assert(read_utf8str(&d([0x0c, 0x04, 0xf0, 0x90, 0x8d, 0x88]), buf)! |
| 57 |
== expected); |
| 58 |
assert(read_utf8str(&d([0x0c, 0x03, 0xf0, 0x90, 0x8d]), buf) is invalid); |
| 59 |
|
| 60 |
bytes::zero(buf); |
| 61 |
let r = strreader(&d([0x0c, 0x04, 0xf0, 0x90, 0x8d, 0x88]), utag::UTF8_STRING)!; |
| 62 |
assert(io::read(&r, buf)! == 4); |
| 63 |
assert(bytes::equal(buf[..4], strings::toutf8(expected))); |
| 64 |
|
| 65 |
bytes::zero(buf); |
| 66 |
let expected: str = strings::fromutf8([0x55, 0x56, 0xf0, 0x90, 0x8d, 0x88])!; |
| 67 |
assert(read_utf8str(&d([0x0c, 0x06, 0x55, 0x56, 0xf0, 0x90, 0x8d, 0x88]), buf)! |
| 68 |
== expected); |
| 69 |
assert(read_utf8str(&d([0x0c, 0x05, 0x55, 0x56, 0xf0, 0x90, 0x8d]), buf) is invalid); |
| 70 |
|
| 71 |
bytes::zero(buf); |
| 72 |
let r = strreader(&d([0x0c, 0x06, 0x55, 0x56, 0xf0, 0x90, 0x8d, 0x88]), utag::UTF8_STRING)!; |
| 73 |
assert(io::read(&r, buf)! == 6); |
| 74 |
assert(bytes::equal(buf[..6], strings::toutf8(expected))); |
| 75 |
|
| 76 |
let r = strreader(&d([0x0c, 0x05, 0x55, 0x56, 0xf0, 0x90, 0x8d]), utag::UTF8_STRING)!; |
| 77 |
assert(unwrap_err(io::readall(&r, buf[2..]) as io::error) is invalid); |
| 78 |
|
| 79 |
bytes::zero(buf); |
| 80 |
let r = strreader(&d([0x0c, 0x06, 0x55, 0x56, 0xf0, 0x90, 0x8d, 0x88]), utag::UTF8_STRING)!; |
| 81 |
assert(io::read(&r, buf[..4])! == 2); |
| 82 |
assert(io::read(&r, buf[2..])! == 4); |
| 83 |
assert(bytes::equal(buf[..6], strings::toutf8(expected))); |
| 84 |
|
| 85 |
bytes::zero(buf); |
| 86 |
let r = strreader(&d([0x0c, 0x05, 0x55, 0x56, 0xf0, 0x90, 0x8d]), utag::UTF8_STRING)!; |
| 87 |
assert(io::read(&r, buf[..4])! == 2); |
| 88 |
assert(unwrap_err(io::readall(&r, buf[2..]) as io::error) is invalid); |
| 89 |
}; |
| 90 |
|
| 91 |
@test fn t61() void = { |
| 92 |
let input: [_]u8 = [ |
| 93 |
0x14, 0x29, |
| 94 |
0x42, 0xc8, 0x61, 0x72, 0x65, 0x6e, 0x20, 0x76, 0x65, 0x72, |
| 95 |
0x7a, 0x65, 0x68, 0x72, 0x65, 0x6e, 0x20, 0x67, 0x65, 0x72, |
| 96 |
0x6e, 0x65, 0x20, 0xc8, 0x75, 0x62, 0x65, 0x72, 0x6d, 0xc8, |
| 97 |
0x61, 0xfb, 0x69, 0x67, 0x20, 0x48, 0x6f, 0x6e, 0x69, 0x67, |
| 98 |
0x0a, |
| 99 |
]; |
| 100 |
|
| 101 |
const expected: [_]u8 = [ |
| 102 |
0x42, 0xc3, 0xa4, 0x72, 0x65, 0x6e, 0x20, 0x76, 0x65, 0x72, |
| 103 |
0x7a, 0x65, 0x68, 0x72, 0x65, 0x6e, 0x20, 0x67, 0x65, 0x72, |
| 104 |
0x6e, 0x65, 0x20, 0xc3, 0xbc, 0x62, 0x65, 0x72, 0x6d, 0xc3, |
| 105 |
0xa4, 0xc3, 0x9f, 0x69, 0x67, 0x20, 0x48, 0x6f, 0x6e, 0x69, |
| 106 |
0x67, 0x0a, |
| 107 |
]; |
| 108 |
|
| 109 |
let dec = d(input); |
| 110 |
let r = strreader(&dec, utag::TELETEX_STRING)!; |
| 111 |
let result = io::drain(&r)!; |
| 112 |
defer free(result); |
| 113 |
assert(bytes::equal(expected, result)); |
| 114 |
assert(trypeek(&dec) is io::EOF); |
| 115 |
|
| 116 |
// cut off multibyte char |
| 117 |
input[1] = 0x2; |
| 118 |
let r = strreader(&d(input[..4]), utag::TELETEX_STRING)!; |
| 119 |
assert(unwrap_err(io::drain(&r) as io::error) is invalid); |
| 120 |
|
| 121 |
// not enough space for multibyte char |
| 122 |
let buf: [24]u8 = [0...]; |
| 123 |
let in = input[..27]; |
| 124 |
in[1] = (len(in) - 2): u8; |
| 125 |
let dec = d(in); |
| 126 |
let r = strreader(&dec, utag::TELETEX_STRING)!; |
| 127 |
assert(io::read(&r, buf)! == 23); |
| 128 |
assert(trypeek(&dec) is badformat); |
| 129 |
|
| 130 |
let r = strreader(&d([ |
| 131 |
0x14, 0x0f, 0x63, 0x6c, 0xc2, 0x65, 0x73, 0x20, 0x70, 0x75, |
| 132 |
0x62, 0x6c, 0x69, 0x71, 0x75, 0x65, 0x73, |
| 133 |
]), utag::TELETEX_STRING)!; |
| 134 |
let b = io::drain(&r)!; |
| 135 |
defer free(b); |
| 136 |
|
| 137 |
assert(strings::fromutf8(b)! == "cl\u00e9s publiques"); |
| 138 |
}; |
| 139 |
|
| 140 |
@test fn bmp() void = { |
| 141 |
let input: [_]u8 = [ |
| 142 |
0x1e, 0x26, |
| 143 |
0x00, 0x48, 0x00, 0xe4, 0x00, 0x72, 0x00, 0x65, 0x00, 0x6c, |
| 144 |
0x00, 0x61, 0x00, 0x6e, 0x00, 0x67, 0x00, 0x20, 0x00, 0x69, |
| 145 |
0x01, 0x61, 0x00, 0x20, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x61, |
| 146 |
0x00, 0x74, 0x00, 0x6f, 0x00, 0x20, 0x27, 0x64, |
| 147 |
]; |
| 148 |
|
| 149 |
const expected: [_]u8 = [ |
| 150 |
0x48, 0xc3, 0xa4, 0x72, 0x65, 0x6c, 0x61, 0x6e, 0x67, 0x20, |
| 151 |
0x69, 0xc5, 0xa1, 0x20, 0x6e, 0x65, 0x61, 0x74, 0x6f, 0x20, |
| 152 |
0xe2, 0x9d, 0xa4, |
| 153 |
]; |
| 154 |
|
| 155 |
let dec = d(input); |
| 156 |
let r = strreader(&dec, utag::BMP_STRING)!; |
| 157 |
let result = io::drain(&r)!; |
| 158 |
defer free(result); |
| 159 |
assert(bytes::equal(expected, result)); |
| 160 |
assert(trypeek(&dec) is io::EOF); |
| 161 |
}; |