aboutsummaryrefslogtreecommitdiffstats
path: root/difftree/internal/merkletrie/iter_test.go
blob: 65116e1c31a392697d3aa5d5a9d875b9459feab8 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package merkletrie

import . "gopkg.in/check.v1"

type IterSuite struct{}

var _ = Suite(&IterSuite{})

// we don't care about hashes for iterating the tree, so
// use this hash for every object
var hash = []byte{}

// leafs have no children, use this empty list.
var empty = []*node{}

// test a defined as an operation to run on an iterator and the key of
// the node expected to be returned by the operation.  Use "" as the
// expected key for when there are no more objects in the tree.
type test struct {
	operation int    // next or step
	expected  string // key of the expected node, "" for nil node
}

// test.operation
const (
	next = iota
	step
)

// goes over a list of tests, calling each operation on the iter and
// checking that the obtained result is equal to the expected result
func runTests(c *C, description string, iter *Iter, list []test) {
	var obtained Noder
	var ok bool
	var comment CommentInterface

	for i, t := range list {
		comment = Commentf("description %q, operation #%d",
			description, i+1)

		switch t.operation {
		case next:
			obtained, ok = iter.Next()
		case step:
			obtained, ok = iter.Step()
		default:
			c.Fatalf("invalid operation %d", t.operation)
		}

		if t.expected == "" {
			c.Assert(ok, Equals, false, comment)
			c.Assert(obtained, IsNil, comment)
		} else {
			c.Assert(ok, Equals, true, comment)
			c.Assert(obtained.Key(), Equals, t.expected, comment)
		}
	}
}

// a simple tree consisting on just a leaf
func (s *IterSuite) TestLeaf(c *C) {
	for description, tests := range runs0 {
		runTests(c, description, iterLeaf(), tests)
	}
}

//     root
//       |
//       a
func (s *IterSuite) TestOneChild(c *C) {
	for description, tests := range runs1 {
		runTests(c, description, iter1(), tests)
	}
}

//     root
//      / \
//     a   b
func (s *IterSuite) Test2HorizontalSorted(c *C) {
	for description, tests := range runs2Horizontal {
		runTests(c, description, iter2HorizontalSorted(), tests)
	}
}

//     root
//      / \
//     b   a
func (s *IterSuite) Test2HorizontalReverse(c *C) {
	for description, tests := range runs2Horizontal {
		runTests(c, description, iter2HorizontalReverse(), tests)
	}
}

//     root
//      |
//      a
//      |
//      b
func (s *IterSuite) Test2VerticalSorted(c *C) {
	for description, tests := range runs2VerticalSorted {
		runTests(c, description, iter2VerticalSorted(), tests)
	}
}

//     root
//      |
//      b
//      |
//      a
func (s *IterSuite) Test2VerticalReverse(c *C) {
	for description, tests := range runs2VerticalReverse {
		runTests(c, description, iter2VerticalReverse(), tests)
	}
}

//     root
//      /|\
//     c a b
func (s *IterSuite) Test3Horizontal(c *C) {
	for description, tests := range runs3Horizontal {
		runTests(c, description, iter3Horizontal(), tests)
	}
}

//     root
//      |
//      b
//      |
//      c
//      |
//      a
func (s *IterSuite) Test3Vertical(c *C) {
	for description, tests := range runs3Vertical {
		runTests(c, description, iter3Vertical(), tests)
	}
}

//     root
//      / \
//     c   a
//     |
//     b
func (s *IterSuite) Test3Mix1(c *C) {
	for description, tests := range runs3Mix1 {
		runTests(c, description, iter3Mix1(), tests)
	}
}

//     root
//      / \
//     b   a
//         |
//         c
func (s *IterSuite) Test3Mix2(c *C) {
	for description, tests := range runs3Mix2 {
		runTests(c, description, iter3Mix2(), tests)
	}
}

//      root
//      / | \
//     /  |  ----
//    f   d      h --------
//   /\         /  \      |
//  e   a      j   b      g
//  |  / \     |
//  l  n  k    icm
//     |
//     o
//     |
//     p
func (s *IterSuite) TestCrazy(c *C) {
	for description, tests := range runsCrazy {
		runTests(c, description, iterCrazy(), tests)
	}
}