diff options
author | EuAndreh <eu@euandre.org> | 2024-11-03 16:32:13 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-11-03 16:44:52 -0300 |
commit | 92c31e2987eb8b4c5602f4048039b1e55645427b (patch) | |
tree | c5f42889445e7737b85c5aac2393295000309732 /tests/tre.go | |
parent | Setup Makefile project structure with empty src/tre.go source (diff) | |
download | tre-92c31e2987eb8b4c5602f4048039b1e55645427b.tar.gz tre-92c31e2987eb8b4c5602f4048039b1e55645427b.tar.xz |
Absorb utf8/ code
Diffstat (limited to 'tests/tre.go')
-rw-r--r-- | tests/tre.go | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/tests/tre.go b/tests/tre.go index 7495c4b..1f3cfed 100644 --- a/tests/tre.go +++ b/tests/tre.go @@ -1,6 +1,257 @@ package tre +import ( + "fmt" + "os" + "testing" + "testing/internal/testdeps" +) + + + +func TestGenCharBlocksWellFormed(t *testing.T) { + cBlk := func(from []byte, to []byte) *CharBlock { + return &CharBlock{ + From: from, + To: to, + } + } + + seq := func(b ...byte) []byte { + return b + } + + tests := []struct { + from rune + to rune + blocks []*CharBlock + }{ + { + from: '\u0000', + to: '\u007f', + blocks: []*CharBlock{ + cBlk(seq(0x00), seq(0x7f)), + }, + }, + { + from: '\u0080', + to: '\u07ff', + blocks: []*CharBlock{ + cBlk(seq(0xc2, 0x80), seq(0xdf, 0xbf)), + }, + }, + { + from: '\u0800', + to: '\u0fff', + blocks: []*CharBlock{ + cBlk( + seq(0xe0, 0xa0, 0x80), + seq(0xe0, 0xbf, 0xbf), + ), + }, + }, + { + from: '\u1000', + to: '\ucfff', + blocks: []*CharBlock{ + cBlk( + seq(0xe1, 0x80, 0x80), + seq(0xec, 0xbf, 0xbf), + ), + }, + }, + { + from: '\ud000', + to: '\ud7ff', + blocks: []*CharBlock{ + cBlk( + seq(0xed, 0x80, 0x80), + seq(0xed, 0x9f, 0xbf), + ), + }, + }, + { + from: '\ue000', + to: '\uffff', + blocks: []*CharBlock{ + cBlk( + seq(0xee, 0x80, 0x80), + seq(0xef, 0xbf, 0xbf), + ), + }, + }, + { + from: '\U00010000', + to: '\U0003ffff', + blocks: []*CharBlock{ + cBlk( + seq(0xf0, 0x90, 0x80, 0x80), + seq(0xf0, 0xbf, 0xbf, 0xbf), + ), + }, + }, + { + from: '\U00040000', + to: '\U000fffff', + blocks: []*CharBlock{ + cBlk( + seq(0xf1, 0x80, 0x80, 0x80), + seq(0xf3, 0xbf, 0xbf, 0xbf), + ), + }, + }, + { + from: '\U00100000', + to: '\U0010ffff', + blocks: []*CharBlock{ + cBlk( + seq(0xf4, 0x80, 0x80, 0x80), + seq(0xf4, 0x8f, 0xbf, 0xbf), + ), + }, + }, + { + from: '\u0000', + to: '\U0010ffff', + blocks: []*CharBlock{ + cBlk(seq(0x00), seq(0x7f)), + cBlk(seq(0xc2, 0x80), seq(0xdf, 0xbf)), + cBlk( + seq(0xe0, 0xa0, 0x80), + seq(0xe0, 0xbf, 0xbf), + ), + cBlk( + seq(0xe1, 0x80, 0x80), + seq(0xec, 0xbf, 0xbf), + ), + cBlk( + seq(0xed, 0x80, 0x80), + seq(0xed, 0x9f, 0xbf), + ), + cBlk( + seq(0xee, 0x80, 0x80), + seq(0xef, 0xbf, 0xbf), + ), + cBlk( + seq(0xf0, 0x90, 0x80, 0x80), + seq(0xf0, 0xbf, 0xbf, 0xbf), + ), + cBlk( + seq(0xf1, 0x80, 0x80, 0x80), + seq(0xf3, 0xbf, 0xbf, 0xbf), + ), + cBlk( + seq(0xf4, 0x80, 0x80, 0x80), + seq(0xf4, 0x8f, 0xbf, 0xbf), + ), + }, + }, + } + for _, tt := range tests { + const errmsg = "unexpected character block: want: %+v, got: %+v" + tts := fmt.Sprintf("%v..%v", tt.from, tt.to) + t.Run(tts, func(t *testing.T) { + blks, err := GenCharBlocks(tt.from, tt.to) + if err != nil { + t.Fatal(err) + } + if len(blks) != len(tt.blocks) { + t.Fatalf(errmsg, tt.blocks, blks) + } + for i, blk := range blks { + expected := tt.blocks[i] + neqFrom := len(blk.From) != len(expected.From) + neqTo := len(blk.To) != len(expected.To) + if neqFrom || neqTo { + t.Fatalf(errmsg, tt.blocks, blks) + } + for j := 0; j < len(blk.From); j++ { + neqFrom := blk.From[j] != + expected.From[j] + neqTo := blk.To[j] != + expected.To[j] + if neqFrom || neqTo { + t.Fatalf( + errmsg, + tt.blocks, + blks, + ) + } + } + } + }) + } +} + +func TestGenCharBlocksIllFormed(t *testing.T) { + tests := []struct { + from rune + to rune + }{ + { + // from > to + from: '\u0001', + to: '\u0000', + }, + { + from: -1, // <U+0000 + to: '\u0000', + }, + { + from: '\u0000', + to: -1, // <U+0000 + }, + { + from: 0x110000, // >U+10FFFF + to: '\u0000', + }, + { + from: '\u0000', + to: 0x110000, // >U+10FFFF + }, + { + from: 0xd800, // U+D800 (surrogate code point) + to: '\ue000', + }, + { + from: 0xdfff, // U+DFFF (surrogate code point) + to: '\ue000', + }, + { + from: '\ucfff', + to: 0xd800, // U+D800 (surrogate code point) + }, + { + from: '\ucfff', + to: 0xdfff, // U+DFFF (surrogate code point) + }, + } + for _, tt := range tests { + tts := fmt.Sprintf("%v..%v", tt.from, tt.to) + t.Run(tts, func(t *testing.T) { + blks, err := GenCharBlocks(tt.from, tt.to) + if err == nil { + t.Fatal("expected error didn't occur") + } + if blks != nil { + t.Fatal("character blocks must be nil") + } + }) + } +} + func MainTest() { + tests := []testing.InternalTest{ + { "TestGenCharBlocksWellFormed", TestGenCharBlocksWellFormed }, + { "TestGenCharBlocksIllFormed", TestGenCharBlocksIllFormed }, + } + + deps := testdeps.TestDeps{} + benchmarks := []testing.InternalBenchmark {} + fuzzTargets := []testing.InternalFuzzTarget{} + examples := []testing.InternalExample {} + m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples) + os.Exit(m.Run()) } |