| 1 |
747 |
jeremybenn |
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package norm
|
| 6 |
|
|
|
| 7 |
|
|
import "testing"
|
| 8 |
|
|
|
| 9 |
|
|
// TestCase is used for most tests.
|
| 10 |
|
|
type TestCase struct {
|
| 11 |
|
|
in []rune
|
| 12 |
|
|
out []rune
|
| 13 |
|
|
}
|
| 14 |
|
|
|
| 15 |
|
|
type insertFunc func(rb *reorderBuffer, r rune) bool
|
| 16 |
|
|
|
| 17 |
|
|
func insert(rb *reorderBuffer, r rune) bool {
|
| 18 |
|
|
src := inputString(string(r))
|
| 19 |
|
|
return rb.insert(src, 0, rb.f.info(src, 0))
|
| 20 |
|
|
}
|
| 21 |
|
|
|
| 22 |
|
|
func runTests(t *testing.T, name string, fm Form, f insertFunc, tests []TestCase) {
|
| 23 |
|
|
rb := reorderBuffer{}
|
| 24 |
|
|
rb.init(fm, nil)
|
| 25 |
|
|
for i, test := range tests {
|
| 26 |
|
|
rb.reset()
|
| 27 |
|
|
for j, rune := range test.in {
|
| 28 |
|
|
b := []byte(string(rune))
|
| 29 |
|
|
src := inputBytes(b)
|
| 30 |
|
|
if !rb.insert(src, 0, rb.f.info(src, 0)) {
|
| 31 |
|
|
t.Errorf("%s:%d: insert failed for rune %d", name, i, j)
|
| 32 |
|
|
}
|
| 33 |
|
|
}
|
| 34 |
|
|
if rb.f.composing {
|
| 35 |
|
|
rb.compose()
|
| 36 |
|
|
}
|
| 37 |
|
|
if rb.nrune != len(test.out) {
|
| 38 |
|
|
t.Errorf("%s:%d: length = %d; want %d", name, i, rb.nrune, len(test.out))
|
| 39 |
|
|
continue
|
| 40 |
|
|
}
|
| 41 |
|
|
for j, want := range test.out {
|
| 42 |
|
|
found := rune(rb.runeAt(j))
|
| 43 |
|
|
if found != want {
|
| 44 |
|
|
t.Errorf("%s:%d: runeAt(%d) = %U; want %U", name, i, j, found, want)
|
| 45 |
|
|
}
|
| 46 |
|
|
}
|
| 47 |
|
|
}
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
func TestFlush(t *testing.T) {
|
| 51 |
|
|
rb := reorderBuffer{}
|
| 52 |
|
|
rb.init(NFC, nil)
|
| 53 |
|
|
out := make([]byte, 0)
|
| 54 |
|
|
|
| 55 |
|
|
out = rb.flush(out)
|
| 56 |
|
|
if len(out) != 0 {
|
| 57 |
|
|
t.Errorf("wrote bytes on flush of empty buffer. (len(out) = %d)", len(out))
|
| 58 |
|
|
}
|
| 59 |
|
|
|
| 60 |
|
|
for _, r := range []rune("world!") {
|
| 61 |
|
|
insert(&rb, r)
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
out = []byte("Hello ")
|
| 65 |
|
|
out = rb.flush(out)
|
| 66 |
|
|
want := "Hello world!"
|
| 67 |
|
|
if string(out) != want {
|
| 68 |
|
|
t.Errorf(`output after flush was "%s"; want "%s"`, string(out), want)
|
| 69 |
|
|
}
|
| 70 |
|
|
if rb.nrune != 0 {
|
| 71 |
|
|
t.Errorf("flush: non-null size of info buffer (rb.nrune == %d)", rb.nrune)
|
| 72 |
|
|
}
|
| 73 |
|
|
if rb.nbyte != 0 {
|
| 74 |
|
|
t.Errorf("flush: non-null size of byte buffer (rb.nbyte == %d)", rb.nbyte)
|
| 75 |
|
|
}
|
| 76 |
|
|
}
|
| 77 |
|
|
|
| 78 |
|
|
var insertTests = []TestCase{
|
| 79 |
|
|
{[]rune{'a'}, []rune{'a'}},
|
| 80 |
|
|
{[]rune{0x300}, []rune{0x300}},
|
| 81 |
|
|
{[]rune{0x300, 0x316}, []rune{0x316, 0x300}}, // CCC(0x300)==230; CCC(0x316)==220
|
| 82 |
|
|
{[]rune{0x316, 0x300}, []rune{0x316, 0x300}},
|
| 83 |
|
|
{[]rune{0x41, 0x316, 0x300}, []rune{0x41, 0x316, 0x300}},
|
| 84 |
|
|
{[]rune{0x41, 0x300, 0x316}, []rune{0x41, 0x316, 0x300}},
|
| 85 |
|
|
{[]rune{0x300, 0x316, 0x41}, []rune{0x316, 0x300, 0x41}},
|
| 86 |
|
|
{[]rune{0x41, 0x300, 0x40, 0x316}, []rune{0x41, 0x300, 0x40, 0x316}},
|
| 87 |
|
|
}
|
| 88 |
|
|
|
| 89 |
|
|
func TestInsert(t *testing.T) {
|
| 90 |
|
|
runTests(t, "TestInsert", NFD, insert, insertTests)
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
var decompositionNFDTest = []TestCase{
|
| 94 |
|
|
{[]rune{0xC0}, []rune{0x41, 0x300}},
|
| 95 |
|
|
{[]rune{0xAC00}, []rune{0x1100, 0x1161}},
|
| 96 |
|
|
{[]rune{0x01C4}, []rune{0x01C4}},
|
| 97 |
|
|
{[]rune{0x320E}, []rune{0x320E}},
|
| 98 |
|
|
{[]rune("음ẻ과"), []rune{0x110B, 0x1173, 0x11B7, 0x65, 0x309, 0x1100, 0x116A}},
|
| 99 |
|
|
}
|
| 100 |
|
|
|
| 101 |
|
|
var decompositionNFKDTest = []TestCase{
|
| 102 |
|
|
{[]rune{0xC0}, []rune{0x41, 0x300}},
|
| 103 |
|
|
{[]rune{0xAC00}, []rune{0x1100, 0x1161}},
|
| 104 |
|
|
{[]rune{0x01C4}, []rune{0x44, 0x5A, 0x030C}},
|
| 105 |
|
|
{[]rune{0x320E}, []rune{0x28, 0x1100, 0x1161, 0x29}},
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
func TestDecomposition(t *testing.T) {
|
| 109 |
|
|
runTests(t, "TestDecompositionNFD", NFD, insert, decompositionNFDTest)
|
| 110 |
|
|
runTests(t, "TestDecompositionNFKD", NFKD, insert, decompositionNFKDTest)
|
| 111 |
|
|
}
|
| 112 |
|
|
|
| 113 |
|
|
var compositionTest = []TestCase{
|
| 114 |
|
|
{[]rune{0x41, 0x300}, []rune{0xC0}},
|
| 115 |
|
|
{[]rune{0x41, 0x316}, []rune{0x41, 0x316}},
|
| 116 |
|
|
{[]rune{0x41, 0x300, 0x35D}, []rune{0xC0, 0x35D}},
|
| 117 |
|
|
{[]rune{0x41, 0x316, 0x300}, []rune{0xC0, 0x316}},
|
| 118 |
|
|
// blocking starter
|
| 119 |
|
|
{[]rune{0x41, 0x316, 0x40, 0x300}, []rune{0x41, 0x316, 0x40, 0x300}},
|
| 120 |
|
|
{[]rune{0x1100, 0x1161}, []rune{0xAC00}},
|
| 121 |
|
|
// parenthesized Hangul, alternate between ASCII and Hangul.
|
| 122 |
|
|
{[]rune{0x28, 0x1100, 0x1161, 0x29}, []rune{0x28, 0xAC00, 0x29}},
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
func TestComposition(t *testing.T) {
|
| 126 |
|
|
runTests(t, "TestComposition", NFC, insert, compositionTest)
|
| 127 |
|
|
}
|