utf8_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // Copyright 2009 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. package utf8_test
  5. import (
  6. "bytes"
  7. "testing"
  8. "unicode"
  9. . "unicode/utf8"
  10. )
  11. // Validate the constants redefined from unicode.
  12. func init() {
  13. if MaxRune != unicode.MaxRune {
  14. panic("utf8.MaxRune is wrong")
  15. }
  16. if RuneError != unicode.ReplacementChar {
  17. panic("utf8.RuneError is wrong")
  18. }
  19. }
  20. // Validate the constants redefined from unicode.
  21. func TestConstants(t *testing.T) {
  22. if MaxRune != unicode.MaxRune {
  23. t.Errorf("utf8.MaxRune is wrong: %x should be %x", MaxRune, unicode.MaxRune)
  24. }
  25. if RuneError != unicode.ReplacementChar {
  26. t.Errorf("utf8.RuneError is wrong: %x should be %x", RuneError, unicode.ReplacementChar)
  27. }
  28. }
  29. type Utf8Map struct {
  30. r rune
  31. str string
  32. }
  33. var utf8map = []Utf8Map{
  34. {0x0000, "\x00"},
  35. {0x0001, "\x01"},
  36. {0x007e, "\x7e"},
  37. {0x007f, "\x7f"},
  38. {0x0080, "\xc2\x80"},
  39. {0x0081, "\xc2\x81"},
  40. {0x00bf, "\xc2\xbf"},
  41. {0x00c0, "\xc3\x80"},
  42. {0x00c1, "\xc3\x81"},
  43. {0x00c8, "\xc3\x88"},
  44. {0x00d0, "\xc3\x90"},
  45. {0x00e0, "\xc3\xa0"},
  46. {0x00f0, "\xc3\xb0"},
  47. {0x00f8, "\xc3\xb8"},
  48. {0x00ff, "\xc3\xbf"},
  49. {0x0100, "\xc4\x80"},
  50. {0x07ff, "\xdf\xbf"},
  51. {0x0800, "\xe0\xa0\x80"},
  52. {0x0801, "\xe0\xa0\x81"},
  53. {0xd7ff, "\xed\x9f\xbf"}, // last code point before surrogate half.
  54. {0xe000, "\xee\x80\x80"}, // first code point after surrogate half.
  55. {0xfffe, "\xef\xbf\xbe"},
  56. {0xffff, "\xef\xbf\xbf"},
  57. {0x10000, "\xf0\x90\x80\x80"},
  58. {0x10001, "\xf0\x90\x80\x81"},
  59. {0x10fffe, "\xf4\x8f\xbf\xbe"},
  60. {0x10ffff, "\xf4\x8f\xbf\xbf"},
  61. {0xFFFD, "\xef\xbf\xbd"},
  62. }
  63. var surrogateMap = []Utf8Map{
  64. {0xd800, "\xed\xa0\x80"}, // surrogate min decodes to (RuneError, 1)
  65. {0xdfff, "\xed\xbf\xbf"}, // surrogate max decodes to (RuneError, 1)
  66. }
  67. var testStrings = []string{
  68. "",
  69. "abcd",
  70. "☺☻☹",
  71. "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
  72. "日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©",
  73. "\x80\x80\x80\x80",
  74. }
  75. func TestFullRune(t *testing.T) {
  76. for _, m := range utf8map {
  77. b := []byte(m.str)
  78. if !FullRune(b) {
  79. t.Errorf("FullRune(%q) (%U) = false, want true", b, m.r)
  80. }
  81. s := m.str
  82. if !FullRuneInString(s) {
  83. t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.r)
  84. }
  85. b1 := b[0 : len(b)-1]
  86. if FullRune(b1) {
  87. t.Errorf("FullRune(%q) = true, want false", b1)
  88. }
  89. s1 := string(b1)
  90. if FullRuneInString(s1) {
  91. t.Errorf("FullRune(%q) = true, want false", s1)
  92. }
  93. }
  94. }
  95. func TestEncodeRune(t *testing.T) {
  96. for _, m := range utf8map {
  97. b := []byte(m.str)
  98. var buf [10]byte
  99. n := EncodeRune(buf[0:], m.r)
  100. b1 := buf[0:n]
  101. if !bytes.Equal(b, b1) {
  102. t.Errorf("EncodeRune(%#04x) = %q want %q", m.r, b1, b)
  103. }
  104. }
  105. }
  106. func TestDecodeRune(t *testing.T) {
  107. for _, m := range utf8map {
  108. b := []byte(m.str)
  109. r, size := DecodeRune(b)
  110. if r != m.r || size != len(b) {
  111. t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b))
  112. }
  113. s := m.str
  114. r, size = DecodeRuneInString(s)
  115. if r != m.r || size != len(b) {
  116. t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b))
  117. }
  118. // there's an extra byte that bytes left behind - make sure trailing byte works
  119. r, size = DecodeRune(b[0:cap(b)])
  120. if r != m.r || size != len(b) {
  121. t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b))
  122. }
  123. s = m.str + "\x00"
  124. r, size = DecodeRuneInString(s)
  125. if r != m.r || size != len(b) {
  126. t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b))
  127. }
  128. // make sure missing bytes fail
  129. wantsize := 1
  130. if wantsize >= len(b) {
  131. wantsize = 0
  132. }
  133. r, size = DecodeRune(b[0 : len(b)-1])
  134. if r != RuneError || size != wantsize {
  135. t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], r, size, RuneError, wantsize)
  136. }
  137. s = m.str[0 : len(m.str)-1]
  138. r, size = DecodeRuneInString(s)
  139. if r != RuneError || size != wantsize {
  140. t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, wantsize)
  141. }
  142. // make sure bad sequences fail
  143. if len(b) == 1 {
  144. b[0] = 0x80
  145. } else {
  146. b[len(b)-1] = 0x7F
  147. }
  148. r, size = DecodeRune(b)
  149. if r != RuneError || size != 1 {
  150. t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, RuneError, 1)
  151. }
  152. s = string(b)
  153. r, size = DecodeRuneInString(s)
  154. if r != RuneError || size != 1 {
  155. t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, 1)
  156. }
  157. }
  158. }
  159. func TestDecodeSurrogateRune(t *testing.T) {
  160. for _, m := range surrogateMap {
  161. b := []byte(m.str)
  162. r, size := DecodeRune(b)
  163. if r != RuneError || size != 1 {
  164. t.Errorf("DecodeRune(%q) = %x, %d want %x, %d", b, r, size, RuneError, 1)
  165. }
  166. s := m.str
  167. r, size = DecodeRuneInString(s)
  168. if r != RuneError || size != 1 {
  169. t.Errorf("DecodeRuneInString(%q) = %x, %d want %x, %d", b, r, size, RuneError, 1)
  170. }
  171. }
  172. }
  173. // Check that DecodeRune and DecodeLastRune correspond to
  174. // the equivalent range loop.
  175. func TestSequencing(t *testing.T) {
  176. for _, ts := range testStrings {
  177. for _, m := range utf8map {
  178. for _, s := range []string{ts + m.str, m.str + ts, ts + m.str + ts} {
  179. testSequence(t, s)
  180. }
  181. }
  182. }
  183. }
  184. // Check that a range loop and a []int conversion visit the same runes.
  185. // Not really a test of this package, but the assumption is used here and
  186. // it's good to verify
  187. func TestIntConversion(t *testing.T) {
  188. for _, ts := range testStrings {
  189. runes := []rune(ts)
  190. if RuneCountInString(ts) != len(runes) {
  191. t.Errorf("%q: expected %d runes; got %d", ts, len(runes), RuneCountInString(ts))
  192. break
  193. }
  194. i := 0
  195. for _, r := range ts {
  196. if r != runes[i] {
  197. t.Errorf("%q[%d]: expected %c (%U); got %c (%U)", ts, i, runes[i], runes[i], r, r)
  198. }
  199. i++
  200. }
  201. }
  202. }
  203. func testSequence(t *testing.T, s string) {
  204. type info struct {
  205. index int
  206. r rune
  207. }
  208. index := make([]info, len(s))
  209. b := []byte(s)
  210. si := 0
  211. j := 0
  212. for i, r := range s {
  213. if si != i {
  214. t.Errorf("Sequence(%q) mismatched index %d, want %d", s, si, i)
  215. return
  216. }
  217. index[j] = info{i, r}
  218. j++
  219. r1, size1 := DecodeRune(b[i:])
  220. if r != r1 {
  221. t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], r1, r)
  222. return
  223. }
  224. r2, size2 := DecodeRuneInString(s[i:])
  225. if r != r2 {
  226. t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], r2, r)
  227. return
  228. }
  229. if size1 != size2 {
  230. t.Errorf("DecodeRune/DecodeRuneInString(%q) size mismatch %d/%d", s[i:], size1, size2)
  231. return
  232. }
  233. si += size1
  234. }
  235. j--
  236. for si = len(s); si > 0; {
  237. r1, size1 := DecodeLastRune(b[0:si])
  238. r2, size2 := DecodeLastRuneInString(s[0:si])
  239. if size1 != size2 {
  240. t.Errorf("DecodeLastRune/DecodeLastRuneInString(%q, %d) size mismatch %d/%d", s, si, size1, size2)
  241. return
  242. }
  243. if r1 != index[j].r {
  244. t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, r1, index[j].r)
  245. return
  246. }
  247. if r2 != index[j].r {
  248. t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, r2, index[j].r)
  249. return
  250. }
  251. si -= size1
  252. if si != index[j].index {
  253. t.Errorf("DecodeLastRune(%q) index mismatch at %d, want %d", s, si, index[j].index)
  254. return
  255. }
  256. j--
  257. }
  258. if si != 0 {
  259. t.Errorf("DecodeLastRune(%q) finished at %d, not 0", s, si)
  260. }
  261. }
  262. // Check that negative runes encode as U+FFFD.
  263. func TestNegativeRune(t *testing.T) {
  264. errorbuf := make([]byte, UTFMax)
  265. errorbuf = errorbuf[0:EncodeRune(errorbuf, RuneError)]
  266. buf := make([]byte, UTFMax)
  267. buf = buf[0:EncodeRune(buf, -1)]
  268. if !bytes.Equal(buf, errorbuf) {
  269. t.Errorf("incorrect encoding [% x] for -1; expected [% x]", buf, errorbuf)
  270. }
  271. }
  272. type RuneCountTest struct {
  273. in string
  274. out int
  275. }
  276. var runecounttests = []RuneCountTest{
  277. {"abcd", 4},
  278. {"☺☻☹", 3},
  279. {"1,2,3,4", 7},
  280. {"\xe2\x00", 2},
  281. }
  282. func TestRuneCount(t *testing.T) {
  283. for _, tt := range runecounttests {
  284. if out := RuneCountInString(tt.in); out != tt.out {
  285. t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out)
  286. }
  287. if out := RuneCount([]byte(tt.in)); out != tt.out {
  288. t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out)
  289. }
  290. }
  291. }
  292. type RuneLenTest struct {
  293. r rune
  294. size int
  295. }
  296. var runelentests = []RuneLenTest{
  297. {0, 1},
  298. {'e', 1},
  299. {'é', 2},
  300. {'☺', 3},
  301. {RuneError, 3},
  302. {MaxRune, 4},
  303. {0xD800, -1},
  304. {0xDFFF, -1},
  305. {MaxRune + 1, -1},
  306. {-1, -1},
  307. }
  308. func TestRuneLen(t *testing.T) {
  309. for _, tt := range runelentests {
  310. if size := RuneLen(tt.r); size != tt.size {
  311. t.Errorf("RuneLen(%#U) = %d, want %d", tt.r, size, tt.size)
  312. }
  313. }
  314. }
  315. type ValidTest struct {
  316. in string
  317. out bool
  318. }
  319. var validTests = []ValidTest{
  320. {"", true},
  321. {"a", true},
  322. {"abc", true},
  323. {"Ж", true},
  324. {"ЖЖ", true},
  325. {"брэд-ЛГТМ", true},
  326. {"☺☻☹", true},
  327. {string([]byte{66, 250}), false},
  328. {string([]byte{66, 250, 67}), false},
  329. {"a\uFFFDb", true},
  330. {string("\xF4\x8F\xBF\xBF"), true}, // U+10FFFF
  331. {string("\xF4\x90\x80\x80"), false}, // U+10FFFF+1; out of range
  332. {string("\xF7\xBF\xBF\xBF"), false}, // 0x1FFFFF; out of range
  333. {string("\xFB\xBF\xBF\xBF\xBF"), false}, // 0x3FFFFFF; out of range
  334. {string("\xc0\x80"), false}, // U+0000 encoded in two bytes: incorrect
  335. {string("\xed\xa0\x80"), false}, // U+D800 high surrogate (sic)
  336. {string("\xed\xbf\xbf"), false}, // U+DFFF low surrogate (sic)
  337. }
  338. func TestValid(t *testing.T) {
  339. for _, tt := range validTests {
  340. if Valid([]byte(tt.in)) != tt.out {
  341. t.Errorf("Valid(%q) = %v; want %v", tt.in, !tt.out, tt.out)
  342. }
  343. if ValidString(tt.in) != tt.out {
  344. t.Errorf("ValidString(%q) = %v; want %v", tt.in, !tt.out, tt.out)
  345. }
  346. }
  347. }
  348. type ValidRuneTest struct {
  349. r rune
  350. ok bool
  351. }
  352. var validrunetests = []ValidRuneTest{
  353. {0, true},
  354. {'e', true},
  355. {'é', true},
  356. {'☺', true},
  357. {RuneError, true},
  358. {MaxRune, true},
  359. {0xD7FF, true},
  360. {0xD800, false},
  361. {0xDFFF, false},
  362. {0xE000, true},
  363. {MaxRune + 1, false},
  364. {-1, false},
  365. }
  366. func TestValidRune(t *testing.T) {
  367. for _, tt := range validrunetests {
  368. if ok := ValidRune(tt.r); ok != tt.ok {
  369. t.Errorf("ValidRune(%#U) = %t, want %t", tt.r, ok, tt.ok)
  370. }
  371. }
  372. }
  373. func BenchmarkRuneCountTenASCIIChars(b *testing.B) {
  374. for i := 0; i < b.N; i++ {
  375. RuneCountInString("0123456789")
  376. }
  377. }
  378. func BenchmarkRuneCountTenJapaneseChars(b *testing.B) {
  379. for i := 0; i < b.N; i++ {
  380. RuneCountInString("日本語日本語日本語日")
  381. }
  382. }
  383. func BenchmarkEncodeASCIIRune(b *testing.B) {
  384. buf := make([]byte, UTFMax)
  385. for i := 0; i < b.N; i++ {
  386. EncodeRune(buf, 'a')
  387. }
  388. }
  389. func BenchmarkEncodeJapaneseRune(b *testing.B) {
  390. buf := make([]byte, UTFMax)
  391. for i := 0; i < b.N; i++ {
  392. EncodeRune(buf, '本')
  393. }
  394. }
  395. func BenchmarkDecodeASCIIRune(b *testing.B) {
  396. a := []byte{'a'}
  397. for i := 0; i < b.N; i++ {
  398. DecodeRune(a)
  399. }
  400. }
  401. func BenchmarkDecodeJapaneseRune(b *testing.B) {
  402. nihon := []byte("本")
  403. for i := 0; i < b.N; i++ {
  404. DecodeRune(nihon)
  405. }
  406. }