diff options
Diffstat (limited to 'test/util/UtilTest.mc')
| -rw-r--r-- | test/util/UtilTest.mc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/test/util/UtilTest.mc b/test/util/UtilTest.mc index ea689a6..c19d6d0 100644 --- a/test/util/UtilTest.mc +++ b/test/util/UtilTest.mc @@ -64,3 +64,33 @@ function jobsRunToCompletion(logger as Test.Logger) as Boolean { Jobs.runToCompletion(job, 3); return job.calls == 4 && job.done == 12; } + +(:test) +function bytesUtf8Validation(logger as Test.Logger) as Boolean { + var valid = [[0x41]b, [0xC3, 0xA9]b, [0xE2, 0x82, 0xAC]b, [0xF0, 0x9F, 0x98, 0x80]b, [0xF4, 0x8F, 0xBF, 0xBF]b]; + var invalid = [ + [0x00]b, // NUL + [0xC0, 0x80]b, // overlong NUL + [0xE0, 0x80, 0x80]b, // overlong + [0xED, 0xA0, 0x80]b, // surrogate + [0xF4, 0x90, 0x80, 0x80]b, // above U+10FFFF + [0xF5, 0x80, 0x80, 0x80]b, // invalid lead byte + [0xE2, 0x82]b, // truncated + [0x80]b, // lone continuation + [0xC3, 0x28]b // bad continuation + ]; + for (var i = 0; i < valid.size(); i++) { + var text = Bytes.toUtf8(valid[i] as ByteArray); + if (text == null || !Bytes.equal(Bytes.fromUtf8(text), valid[i] as ByteArray)) { + logger.error("valid UTF-8 case " + i + " rejected or changed"); + return false; + } + } + for (var i = 0; i < invalid.size(); i++) { + if (Bytes.toUtf8(invalid[i] as ByteArray) != null) { + logger.error("invalid UTF-8 case " + i + " accepted"); + return false; + } + } + return true; +} |
