1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
package uniseg
import "testing"
// Test all official Unicode test cases for line breaks using the byte slice
// function.
func TestLineCasesBytes(t *testing.T) {
for testNum, testCase := range lineBreakTestCases {
/*t.Logf(`Test case %d %q: Expecting %x, getting %x, code points %x"`,
testNum,
strings.TrimSpace(testCase.original),
testCase.expected,
decomposed(testCase.original),
[]rune(testCase.original))*/
var (
segment []byte
index int
)
state := -1
b := []byte(testCase.original)
WordLoop:
for index = 0; len(b) > 0; index++ {
if index >= len(testCase.expected) {
t.Errorf(`Test case %d %q failed: More segments %d returned than expected %d`,
testNum,
testCase.original,
index,
len(testCase.expected))
break
}
segment, b, _, state = FirstLineSegment(b, state)
cluster := []rune(string(segment))
if len(cluster) != len(testCase.expected[index]) {
t.Errorf(`Test case %d %q failed: Segment at index %d has %d codepoints %x, %d expected %x`,
testNum,
testCase.original,
index,
len(cluster),
cluster,
len(testCase.expected[index]),
testCase.expected[index])
break
}
for i, r := range cluster {
if r != testCase.expected[index][i] {
t.Errorf(`Test case %d %q failed: Segment at index %d is %x, expected %x`,
testNum,
testCase.original,
index,
cluster,
testCase.expected[index])
break WordLoop
}
}
}
if index < len(testCase.expected) {
t.Errorf(`Test case %d %q failed: Fewer segments returned (%d) than expected (%d)`,
testNum,
testCase.original,
index,
len(testCase.expected))
}
}
segment, rest, mustBreak, newState := FirstLineSegment([]byte{}, -1)
if len(segment) > 0 {
t.Errorf(`Expected segment to be empty byte slice, got %q`, segment)
}
if len(rest) > 0 {
t.Errorf(`Expected rest to be empty byte slice, got %q`, rest)
}
if mustBreak {
t.Error(`Expected mustBreak to be false, got true`)
}
if newState != 0 {
t.Errorf(`Expected newState to be 0, got %d`, newState)
}
}
// Test all official Unicode test cases for line breaks using the string
// function.
func TestLineCasesString(t *testing.T) {
for testNum, testCase := range lineBreakTestCases {
/*t.Logf(`Test case %d %q: Expecting %x, getting %x, code points %x"`,
testNum,
strings.TrimSpace(testCase.original),
testCase.expected,
decomposed(testCase.original),
[]rune(testCase.original))*/
var (
segment string
index int
)
state := -1
str := testCase.original
WordLoop:
for index = 0; len(str) > 0; index++ {
if index >= len(testCase.expected) {
t.Errorf(`Test case %d %q failed: More segments %d returned than expected %d`,
testNum,
testCase.original,
index,
len(testCase.expected))
break
}
segment, str, _, state = FirstLineSegmentInString(str, state)
cluster := []rune(string(segment))
if len(cluster) != len(testCase.expected[index]) {
t.Errorf(`Test case %d %q failed: Segment at index %d has %d codepoints %x, %d expected %x`,
testNum,
testCase.original,
index,
len(cluster),
cluster,
len(testCase.expected[index]),
testCase.expected[index])
break
}
for i, r := range cluster {
if r != testCase.expected[index][i] {
t.Errorf(`Test case %d %q failed: Segment at index %d is %x, expected %x`,
testNum,
testCase.original,
index,
cluster,
testCase.expected[index])
break WordLoop
}
}
}
if index < len(testCase.expected) {
t.Errorf(`Test case %d %q failed: Fewer segments returned (%d) than expected (%d)`,
testNum,
testCase.original,
index,
len(testCase.expected))
}
}
segment, rest, mustBreak, newState := FirstLineSegmentInString("", -1)
if len(segment) > 0 {
t.Errorf(`Expected segment to be empty string, got %q`, segment)
}
if len(rest) > 0 {
t.Errorf(`Expected rest to be empty string, got %q`, rest)
}
if mustBreak {
t.Error(`Expected mustBreak to be false, got true`)
}
if newState != 0 {
t.Errorf(`Expected newState to be 0, got %d`, newState)
}
}
var hasTrailingLineBreakTestCases = []struct {
input string
want bool
}{
{"\v", true}, // prBK
{"\r", true}, // prCR
{"\n", true}, // prLF
{"\u0085", true}, // prNL
{" ", false},
{"A", false},
{"", false},
}
func TestHasTrailingLineBreak(t *testing.T) {
for _, tt := range hasTrailingLineBreakTestCases {
got := HasTrailingLineBreak([]byte(tt.input))
if got != tt.want {
t.Errorf("HasTrailingLineBreak(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestHasTrailingLineBreakInString(t *testing.T) {
for _, tt := range hasTrailingLineBreakTestCases {
got := HasTrailingLineBreakInString(tt.input)
if got != tt.want {
t.Errorf("HasTrailingLineBreak(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
// Benchmark the use of the line break function for byte slices.
func BenchmarkLineFunctionBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
var c []byte
state := -1
str := benchmarkBytes
for len(str) > 0 {
c, str, _, state = FirstLineSegment(str, state)
resultRunes = []rune(string(c))
}
}
}
// Benchmark the use of the line break function for strings.
func BenchmarkLineFunctionString(b *testing.B) {
for i := 0; i < b.N; i++ {
var c string
state := -1
str := benchmarkStr
for len(str) > 0 {
c, str, _, state = FirstLineSegmentInString(str, state)
resultRunes = []rune(c)
}
}
}
|