forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
163 lines (151 loc) · 3.81 KB
/
misc_test.go
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
package logos
import (
"fmt"
"testing"
"github.com/gnolang/gno/pkgs/random"
require "github.com/jaekwon/testify/require"
)
// Tests whether widthOf() and nextCharacter() do the same thing.
func TestStringWidthSlow(t *testing.T) {
for n := 1; n < 4; n++ {
bz := make([]byte, n)
for {
width1 := widthOf(string(bz))
width2 := widthOfSlow(string(bz))
if width1 == 0 {
if isRepeatedWZJ(bz) {
// these bytes encode one or more U+200D WZJ as UTF8.
} else {
require.Fail(t, fmt.Sprintf("unexpected zero width string for bytes %X", bz))
}
} else {
require.True(t, 0 < width1, "got zero width for bytes %X", bz)
}
require.Equal(t, width1, width2)
if !incBuffer(bz) {
break
}
}
}
}
// Same as above but for longer pseudo-random strings.
func TestStringWidthRandom(t *testing.T) {
max := 10 * 1024 * 1024
for i := 0; i < max; i++ {
if i%(max/80) == 0 {
fmt.Print(".")
}
bz := random.RandBytes(12)
width1 := widthOf(string(bz))
width2 := widthOfSlow(string(bz))
if width1 == 0 {
if isRepeatedWZJ(bz) {
// these bytes encode one or more U+200D WZJ as UTF8.
} else {
require.Fail(t, "unexpected zero width string")
}
} else {
require.True(t, 0 < width1, "got zero width for bytes %X", bz)
}
require.Equal(t, width2, width1,
"want %d but got %d the slow way: %X",
width1, width2, bz)
}
}
// For debugging.
func TestStringWidthDummy(t *testing.T) {
bz := []byte{0x0C, 0x5B, 0x0D, 0xCF, 0xC5, 0xE2, 0x80, 0x8D, 0xC1, 0x32, 0x69, 0x41}
width1 := widthOf(string(bz))
width2 := widthOfSlow(string(bz))
if width1 == 0 {
if isRepeatedWZJ(bz) {
// these bytes encode one or more U+200D WZJ as UTF8.
} else {
require.Fail(t, "unexpected zero width string")
}
} else {
require.True(t, 0 < width1, "got zero width for bytes %X", bz)
}
require.Equal(t, width2, width1,
"want %d but got %d the slow way: %X",
width1, width2, bz)
}
// For debugging.
func TestStringWidthDummy2(t *testing.T) {
// NOTE: this is broken in the OSX terminal. This should print a USA flag
// and have width 2, or possibly default to two block letters "U" and "S",
// but my terminal prints a flag of width 1.
bz := []byte("\U0001f1fa\U0001f1f8")
width1 := widthOf(string(bz))
width2 := widthOfSlow(string(bz))
require.Equal(t, width1, 1)
require.Equal(t, width2, width1,
"want %d but got %d the slow way: %X",
width1, width2, bz)
}
func isRepeatedWZJ(bz []byte) bool {
if len(bz)%3 != 0 {
return false
}
// this is U+200D is UTF8.
for i := 0; i < len(bz); i += 3 {
if bz[i] != 0xE2 {
return false
}
if bz[i+1] != 0x80 {
return false
}
if bz[i+2] != 0x8D {
return false
}
}
return true
}
// get the width of a string using nextCharacter().
func widthOfSlow(s string) (w int) {
rz := toRunes(s)
for 0 < len(rz) {
_, w2, n := nextCharacter(rz)
if n == 0 {
panic("should not happen")
}
w += w2
rz = rz[n:]
}
return
}
//----------------------------------------
// incBuffer for testing
// If overflow, bz becomes zero and returns false.
func incBuffer(bz []byte) bool {
for i := 0; i < len(bz); i++ {
if bz[i] == 0xFF {
bz[i] = 0x00
} else {
bz[i]++
return true
}
}
return false
}
func TestIncBuffer1(t *testing.T) {
bz := []byte{0x00}
for i := 0; i < (1<<(1*8))-1; i++ {
require.Equal(t, incBuffer(bz), true)
require.Equal(t, bz[0], byte(i+1))
}
require.Equal(t, incBuffer(bz), false)
require.Equal(t, bz[0], byte(0x00))
}
func TestIncBuffer2(t *testing.T) {
bz := []byte{0x00, 0x00}
for i := 0; i < (1<<(2*8))-1; i++ {
require.Equal(t, incBuffer(bz), true)
require.Equal(t, bz[0], byte(((i+1)>>0)%256))
require.Equal(t, bz[1], byte(((i+1)>>8)%256))
}
require.Equal(t, bz, []byte{0xFF, 0xFF})
require.Equal(t, incBuffer(bz), false)
require.Equal(t, bz[0], byte(0x00))
require.Equal(t, bz[1], byte(0x00))
}