File: tst_test.go

package info (click to toggle)
golang-github-badgerodon-collections 0.0~git20130729.604e922-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 164 kB
  • sloc: makefile: 5
file content (83 lines) | stat: -rw-r--r-- 1,462 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package tst

import (
	//"fmt"
	"math/rand"
	"testing"
)

func randomString() string {
	n := 3 + rand.Intn(10)
	bs := make([]byte, n)
	for i := 0; i<n; i++ {
		bs[i] = byte(97 + rand.Intn(25))
	}
	return string(bs)
}

func Test(t *testing.T) {
	tree := New()
	tree.Insert("test", 1)
	if tree.Len() != 1 {
		t.Errorf("expecting len 1")
	}
	if !tree.Has("test") {
		t.Errorf("expecting to find key=test")
	}
	
	tree.Insert("testing", 2)
	tree.Insert("abcd", 0)
		
	found := false
	tree.Do(func(key string, val interface{})bool {
		if key == "test" && val.(int) == 1 {
			found = true
		}
		return true
	})
	if !found {
		t.Errorf("expecting iterator to find test")
	}
	
	tree.Remove("testing")
	tree.Remove("abcd")
	
	v := tree.Remove("test")
	if tree.Len() != 0 {
		t.Errorf("expecting len 0")
	}
	if tree.Has("test") {
		t.Errorf("expecting not to find key=test")
	}
	if v.(int) != 1 {
		t.Errorf("expecting value=1")
	}
}

func BenchmarkInsert(b *testing.B) {
	b.StopTimer()
	strs := make([]string, b.N)
	for i := 0; i<b.N; i++ {
		strs[i] = randomString()
	}
	b.StartTimer()
	
	tree := New()
	for i, str := range strs {
		tree.Insert(str, i)
	}
}

func BenchmarkMapInsert(b *testing.B) {
	b.StopTimer()
	strs := make([]string, b.N)
	for i := 0; i<b.N; i++ {
		strs[i] = randomString()
	}
	b.StartTimer()
	
	m := make(map[string]int)
	for i, str := range strs {
		m[str] = i
	}
}