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