traverse_test.go 7.72 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"
Jeromy's avatar
Jeromy committed
10

Steven Allen's avatar
Steven Allen committed
11
	node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14
)

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

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

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

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

44
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
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) {
80 81
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPre, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82

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

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

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

109
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113 114 115 116 117 118
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) {
119 120
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPost, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121

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

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

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

148
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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 182 183
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) {
184 185
	ds := mdagtest.Mock()
	opts := Options{Order: DFSPost, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186

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

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

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

213
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
214 215 216 217 218 219 220 221 222
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) {
223 224
	ds := mdagtest.Mock()
	opts := Options{Order: BFS, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225

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

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

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

252
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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 286 287
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) {
288 289
	ds := mdagtest.Mock()
	opts := Options{Order: BFS, SkipDuplicates: true, DAG: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
290

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

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

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

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

Jeromy's avatar
Jeromy committed
326
func testWalkOutputs(t *testing.T, root node.Node, opts Options, expect []byte) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
327 328
	expect = bytes.TrimLeft(expect, "\n")

329
	buf := new(bytes.Buffer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330
	walk := func(current State) error {
331
		s := fmt.Sprintf("%d %s\n", current.Depth, current.Node.(*mdag.ProtoNode).Data())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
		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)
	}
}

Jeromy's avatar
Jeromy committed
353
func newFan(t *testing.T, ds mdag.DAGService) node.Node {
354
	a := mdag.NodeWithData([]byte("/a"))
355 356 357 358
	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
359 360 361
	return a
}

Jeromy's avatar
Jeromy committed
362
func newLinkedList(t *testing.T, ds mdag.DAGService) node.Node {
363
	a := mdag.NodeWithData([]byte("/a"))
364 365 366 367 368 369 370 371
	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
372 373 374
	return a
}

Jeromy's avatar
Jeromy committed
375
func newBinaryTree(t *testing.T, ds mdag.DAGService) node.Node {
376
	a := mdag.NodeWithData([]byte("/a"))
377 378 379 380 381 382 383 384
	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
385 386 387
	return a
}

Jeromy's avatar
Jeromy committed
388
func newBinaryDAG(t *testing.T, ds mdag.DAGService) node.Node {
389
	a := mdag.NodeWithData([]byte("/a"))
390 391 392 393 394 395 396 397 398 399 400 401
	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
402 403 404
	return a
}

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

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