PageRenderTime 104ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/tst/tst_test.go

http://github.com/badgerodon/collections
Go | 83 lines | 71 code | 11 blank | 1 comment | 18 complexity | facf0dc9f67d55de47d90ecddce516b2 MD5 | raw file
  1. package tst
  2. import (
  3. //"fmt"
  4. "math/rand"
  5. "testing"
  6. )
  7. func randomString() string {
  8. n := 3 + rand.Intn(10)
  9. bs := make([]byte, n)
  10. for i := 0; i<n; i++ {
  11. bs[i] = byte(97 + rand.Intn(25))
  12. }
  13. return string(bs)
  14. }
  15. func Test(t *testing.T) {
  16. tree := New()
  17. tree.Insert("test", 1)
  18. if tree.Len() != 1 {
  19. t.Errorf("expecting len 1")
  20. }
  21. if !tree.Has("test") {
  22. t.Errorf("expecting to find key=test")
  23. }
  24. tree.Insert("testing", 2)
  25. tree.Insert("abcd", 0)
  26. found := false
  27. tree.Do(func(key string, val interface{})bool {
  28. if key == "test" && val.(int) == 1 {
  29. found = true
  30. }
  31. return true
  32. })
  33. if !found {
  34. t.Errorf("expecting iterator to find test")
  35. }
  36. tree.Remove("testing")
  37. tree.Remove("abcd")
  38. v := tree.Remove("test")
  39. if tree.Len() != 0 {
  40. t.Errorf("expecting len 0")
  41. }
  42. if tree.Has("test") {
  43. t.Errorf("expecting not to find key=test")
  44. }
  45. if v.(int) != 1 {
  46. t.Errorf("expecting value=1")
  47. }
  48. }
  49. func BenchmarkInsert(b *testing.B) {
  50. b.StopTimer()
  51. strs := make([]string, b.N)
  52. for i := 0; i<b.N; i++ {
  53. strs[i] = randomString()
  54. }
  55. b.StartTimer()
  56. tree := New()
  57. for i, str := range strs {
  58. tree.Insert(str, i)
  59. }
  60. }
  61. func BenchmarkMapInsert(b *testing.B) {
  62. b.StopTimer()
  63. strs := make([]string, b.N)
  64. for i := 0; i<b.N; i++ {
  65. strs[i] = randomString()
  66. }
  67. b.StartTimer()
  68. m := make(map[string]int)
  69. for i, str := range strs {
  70. m[str] = i
  71. }
  72. }