traverse_test.go 7.54 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7
package traverse

import (
	"bytes"
	"fmt"
	"testing"

8
	mdag "github.com/ipfs/go-ipfs/merkledag"
9
	mdagtest "github.com/ipfs/go-ipfs/merkledag/test"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12
)

func TestDFSPreNoSkip(t *testing.T) {
13 14
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPre, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15

16
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18 19 20 21 22 23
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

24
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27 28 29 30 31
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

32
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36 37 38 39 40 41
0 /a
1 /a/aa
2 /a/aa/aaa
2 /a/aa/aab
1 /a/ab
2 /a/ab/aba
2 /a/ab/abb
`))

42
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
`))
}

func TestDFSPreSkip(t *testing.T) {
78 79
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPre, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80

81
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84 85 86 87 88
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

89
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92 93 94 95 96
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

97
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99 100 101 102 103 104 105 106
0 /a
1 /a/aa
2 /a/aa/aaa
2 /a/aa/aab
1 /a/ab
2 /a/ab/aba
2 /a/ab/abb
`))

107
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112 113 114 115 116
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))
}

func TestDFSPostNoSkip(t *testing.T) {
117 118
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPost, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119

120
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121 122 123 124 125 126 127
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
0 /a
`))

128
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129 130 131 132 133 134 135
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))

136
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137 138 139 140 141 142 143 144 145
2 /a/aa/aaa
2 /a/aa/aab
1 /a/aa
2 /a/ab/aba
2 /a/ab/abb
1 /a/ab
0 /a
`))

146
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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 177 178 179 180 181
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))
}

func TestDFSPostSkip(t *testing.T) {
182 183
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPost, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
184

185
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186 187 188 189 190 191 192
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
0 /a
`))

193
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194 195 196 197 198 199 200
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))

201
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202 203 204 205 206 207 208 209 210
2 /a/aa/aaa
2 /a/aa/aab
1 /a/aa
2 /a/ab/aba
2 /a/ab/abb
1 /a/ab
0 /a
`))

211
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212 213 214 215 216 217 218 219 220
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))
}

func TestBFSNoSkip(t *testing.T) {
221 222
	ds := mdagtest.Mock()
	opts := Options{Order: BFS, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223

224
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225 226 227 228 229 230 231
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

232
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
233 234 235 236 237 238 239
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

240
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
241 242 243 244 245 246 247 248 249
0 /a
1 /a/aa
1 /a/ab
2 /a/aa/aaa
2 /a/aa/aab
2 /a/ab/aba
2 /a/ab/abb
`))

250
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
0 /a
1 /a/aa
1 /a/aa
2 /a/aa/aaa
2 /a/aa/aaa
2 /a/aa/aaa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
`))
}

func TestBFSSkip(t *testing.T) {
286 287
	ds := mdagtest.Mock()
	opts := Options{Order: BFS, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
288

289
	testWalkOutputs(t, newFan(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
290 291 292 293 294 295 296
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

297
	testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
298 299 300 301 302 303 304
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

305
	testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
306 307 308 309 310 311 312 313 314
0 /a
1 /a/aa
1 /a/ab
2 /a/aa/aaa
2 /a/aa/aab
2 /a/ab/aba
2 /a/ab/abb
`))

315
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
316 317 318 319 320 321 322 323 324 325 326
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))
}

func testWalkOutputs(t *testing.T, root *mdag.Node, opts Options, expect []byte) {
	expect = bytes.TrimLeft(expect, "\n")

327
	buf := new(bytes.Buffer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
328
	walk := func(current State) error {
329
		s := fmt.Sprintf("%d %s\n", current.Depth, current.Node.Data())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		t.Logf("walk: %s", s)
		buf.Write([]byte(s))
		return nil
	}

	opts.Func = walk
	if err := Traverse(root, opts); err != nil {
		t.Error(err)
		return
	}

	actual := buf.Bytes()
	if !bytes.Equal(actual, expect) {
		t.Error("error: outputs differ")
		t.Logf("expect:\n%s", expect)
		t.Logf("actual:\n%s", actual)
	} else {
		t.Logf("expect matches actual:\n%s", expect)
	}
}

351
func newFan(t *testing.T, ds mdag.DAGService) *mdag.Node {
352
	a := mdag.NodeWithData([]byte("/a"))
353 354 355 356
	addLink(t, ds, a, child(t, ds, a, "aa"))
	addLink(t, ds, a, child(t, ds, a, "ab"))
	addLink(t, ds, a, child(t, ds, a, "ac"))
	addLink(t, ds, a, child(t, ds, a, "ad"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
357 358 359
	return a
}

360
func newLinkedList(t *testing.T, ds mdag.DAGService) *mdag.Node {
361
	a := mdag.NodeWithData([]byte("/a"))
362 363 364 365 366 367 368 369
	aa := child(t, ds, a, "aa")
	aaa := child(t, ds, aa, "aaa")
	aaaa := child(t, ds, aaa, "aaaa")
	aaaaa := child(t, ds, aaaa, "aaaaa")
	addLink(t, ds, aaaa, aaaaa)
	addLink(t, ds, aaa, aaaa)
	addLink(t, ds, aa, aaa)
	addLink(t, ds, a, aa)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
370 371 372
	return a
}

373
func newBinaryTree(t *testing.T, ds mdag.DAGService) *mdag.Node {
374
	a := mdag.NodeWithData([]byte("/a"))
375 376 377 378 379 380 381 382
	aa := child(t, ds, a, "aa")
	ab := child(t, ds, a, "ab")
	addLink(t, ds, aa, child(t, ds, aa, "aaa"))
	addLink(t, ds, aa, child(t, ds, aa, "aab"))
	addLink(t, ds, ab, child(t, ds, ab, "aba"))
	addLink(t, ds, ab, child(t, ds, ab, "abb"))
	addLink(t, ds, a, aa)
	addLink(t, ds, a, ab)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
383 384 385
	return a
}

386
func newBinaryDAG(t *testing.T, ds mdag.DAGService) *mdag.Node {
387
	a := mdag.NodeWithData([]byte("/a"))
388 389 390 391 392 393 394 395 396 397 398 399
	aa := child(t, ds, a, "aa")
	aaa := child(t, ds, aa, "aaa")
	aaaa := child(t, ds, aaa, "aaaa")
	aaaaa := child(t, ds, aaaa, "aaaaa")
	addLink(t, ds, aaaa, aaaaa)
	addLink(t, ds, aaaa, aaaaa)
	addLink(t, ds, aaa, aaaa)
	addLink(t, ds, aaa, aaaa)
	addLink(t, ds, aa, aaa)
	addLink(t, ds, aa, aaa)
	addLink(t, ds, a, aa)
	addLink(t, ds, a, aa)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
400 401 402
	return a
}

403
func addLink(t *testing.T, ds mdag.DAGService, a, b *mdag.Node) {
404
	to := string(a.Data()) + "2" + string(b.Data())
405 406 407
	if _, err := ds.Add(b); err != nil {
		t.Error(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
408 409 410 411 412
	if err := a.AddNodeLink(to, b); err != nil {
		t.Error(err)
	}
}

413
func child(t *testing.T, ds mdag.DAGService, a *mdag.Node, name string) *mdag.Node {
414
	return mdag.NodeWithData([]byte(string(a.Data()) + "/" + name))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
415
}