hash_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package html // import "github.com/tdewolff/parse/html"
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tdewolff/test"
  6. )
  7. func TestHashTable(t *testing.T) {
  8. test.T(t, ToHash([]byte("address")), Address, "'address' must resolve to Address")
  9. test.T(t, Address.String(), "address")
  10. test.T(t, Accept_Charset.String(), "accept-charset")
  11. test.T(t, ToHash([]byte("")), Hash(0), "empty string must resolve to zero")
  12. test.T(t, Hash(0xffffff).String(), "")
  13. test.T(t, ToHash([]byte("iter")), Hash(0), "'iter' must resolve to zero")
  14. test.T(t, ToHash([]byte("test")), Hash(0), "'test' must resolve to zero")
  15. }
  16. ////////////////////////////////////////////////////////////////
  17. var result int
  18. // naive scenario
  19. func BenchmarkCompareBytes(b *testing.B) {
  20. var r int
  21. val := []byte("span")
  22. for n := 0; n < b.N; n++ {
  23. if bytes.Equal(val, []byte("span")) {
  24. r++
  25. }
  26. }
  27. result = r
  28. }
  29. // using-atoms scenario
  30. func BenchmarkFindAndCompareAtom(b *testing.B) {
  31. var r int
  32. val := []byte("span")
  33. for n := 0; n < b.N; n++ {
  34. if ToHash(val) == Span {
  35. r++
  36. }
  37. }
  38. result = r
  39. }
  40. // using-atoms worst-case scenario
  41. func BenchmarkFindAtomCompareBytes(b *testing.B) {
  42. var r int
  43. val := []byte("zzzz")
  44. for n := 0; n < b.N; n++ {
  45. if h := ToHash(val); h == 0 && bytes.Equal(val, []byte("zzzz")) {
  46. r++
  47. }
  48. }
  49. result = r
  50. }