Skip to content

Commit acbd689

Browse files
committed
Test CreateLUT
1 parent 3eff2ad commit acbd689

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

runewidth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ func handleEnv() {
3434
EastAsianWidth = env == "1"
3535
}
3636
// update DefaultCondition
37-
DefaultCondition.EastAsianWidth = EastAsianWidth
37+
if DefaultCondition.EastAsianWidth != EastAsianWidth {
38+
DefaultCondition.EastAsianWidth = EastAsianWidth
39+
if len(DefaultCondition.combinedLut) > 0 {
40+
DefaultCondition.combinedLut = DefaultCondition.combinedLut[:0]
41+
CreateLUT()
42+
}
43+
}
3844
}
3945

4046
type interval struct {

runewidth_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !js && !appengine
12
// +build !js,!appengine
23

34
package runewidth
@@ -87,7 +88,56 @@ func TestRuneWidthChecksums(t *testing.T) {
8788
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s",
8889
testcase.name, gotSHA, testcase.wantSHA)
8990
}
91+
92+
// Test with LUT
93+
c.CreateLUT()
94+
for r := rune(0); r <= utf8.MaxRune; r++ {
95+
buf[r] = byte(c.RuneWidth(r))
96+
}
97+
gotSHA = fmt.Sprintf("%x", sha256.Sum256(buf))
98+
if gotSHA != testcase.wantSHA {
99+
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s",
100+
testcase.name, gotSHA, testcase.wantSHA)
101+
}
102+
}
103+
}
104+
105+
func TestDefaultLUT(t *testing.T) {
106+
var testcases = []struct {
107+
name string
108+
eastAsianWidth bool
109+
wantSHA string
110+
}{
111+
{"ea-no", false, "4eb632b105d3b2c800dda9141381d0b8a95250a3a5c7f1a5ca2c4d4daaa85234"},
112+
{"ea-yes", true, "c2ddc3bdf42d81d4c23050e21eda46eb639b38b15322d35e8eb6c26f3b83ce92"},
113+
}
114+
115+
old := os.Getenv("RUNEWIDTH_EASTASIAN")
116+
defer os.Setenv("RUNEWIDTH_EASTASIAN", old)
117+
118+
CreateLUT()
119+
for _, testcase := range testcases {
120+
c := DefaultCondition
121+
122+
if testcase.eastAsianWidth {
123+
os.Setenv("RUNEWIDTH_EASTASIAN", "1")
124+
} else {
125+
os.Setenv("RUNEWIDTH_EASTASIAN", "0")
126+
}
127+
handleEnv()
128+
129+
buf := make([]byte, utf8.MaxRune+1)
130+
for r := rune(0); r <= utf8.MaxRune; r++ {
131+
buf[r] = byte(c.RuneWidth(r))
132+
}
133+
gotSHA := fmt.Sprintf("%x", sha256.Sum256(buf))
134+
if gotSHA != testcase.wantSHA {
135+
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s",
136+
testcase.name, gotSHA, testcase.wantSHA)
137+
}
90138
}
139+
// Remove for other tests.
140+
DefaultCondition.combinedLut = nil
91141
}
92142

93143
func checkInterval(first, last rune) bool {

0 commit comments

Comments
 (0)