atom_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2012 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 atom
  5. import (
  6. "sort"
  7. "testing"
  8. )
  9. func TestKnown(t *testing.T) {
  10. for _, s := range testAtomList {
  11. if atom := Lookup([]byte(s)); atom.String() != s {
  12. t.Errorf("Lookup(%q) = %#x (%q)", s, uint32(atom), atom.String())
  13. }
  14. }
  15. }
  16. func TestHits(t *testing.T) {
  17. for _, a := range table {
  18. if a == 0 {
  19. continue
  20. }
  21. got := Lookup([]byte(a.String()))
  22. if got != a {
  23. t.Errorf("Lookup(%q) = %#x, want %#x", a.String(), uint32(got), uint32(a))
  24. }
  25. }
  26. }
  27. func TestMisses(t *testing.T) {
  28. testCases := []string{
  29. "",
  30. "\x00",
  31. "\xff",
  32. "A",
  33. "DIV",
  34. "Div",
  35. "dIV",
  36. "aa",
  37. "a\x00",
  38. "ab",
  39. "abb",
  40. "abbr0",
  41. "abbr ",
  42. " abbr",
  43. " a",
  44. "acceptcharset",
  45. "acceptCharset",
  46. "accept_charset",
  47. "h0",
  48. "h1h2",
  49. "h7",
  50. "onClick",
  51. "λ",
  52. // The following string has the same hash (0xa1d7fab7) as "onmouseover".
  53. "\x00\x00\x00\x00\x00\x50\x18\xae\x38\xd0\xb7",
  54. }
  55. for _, tc := range testCases {
  56. got := Lookup([]byte(tc))
  57. if got != 0 {
  58. t.Errorf("Lookup(%q): got %d, want 0", tc, got)
  59. }
  60. }
  61. }
  62. func TestForeignObject(t *testing.T) {
  63. const (
  64. afo = Foreignobject
  65. afO = ForeignObject
  66. sfo = "foreignobject"
  67. sfO = "foreignObject"
  68. )
  69. if got := Lookup([]byte(sfo)); got != afo {
  70. t.Errorf("Lookup(%q): got %#v, want %#v", sfo, got, afo)
  71. }
  72. if got := Lookup([]byte(sfO)); got != afO {
  73. t.Errorf("Lookup(%q): got %#v, want %#v", sfO, got, afO)
  74. }
  75. if got := afo.String(); got != sfo {
  76. t.Errorf("Atom(%#v).String(): got %q, want %q", afo, got, sfo)
  77. }
  78. if got := afO.String(); got != sfO {
  79. t.Errorf("Atom(%#v).String(): got %q, want %q", afO, got, sfO)
  80. }
  81. }
  82. func BenchmarkLookup(b *testing.B) {
  83. sortedTable := make([]string, 0, len(table))
  84. for _, a := range table {
  85. if a != 0 {
  86. sortedTable = append(sortedTable, a.String())
  87. }
  88. }
  89. sort.Strings(sortedTable)
  90. x := make([][]byte, 1000)
  91. for i := range x {
  92. x[i] = []byte(sortedTable[i%len(sortedTable)])
  93. }
  94. b.ResetTimer()
  95. for i := 0; i < b.N; i++ {
  96. for _, s := range x {
  97. Lookup(s)
  98. }
  99. }
  100. }