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

import (
	"bytes"
5
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7 8
	"fmt"
	"testing"

9
	mdag "github.com/ipfs/go-ipfs/merkledag"
10
	mdagtest "github.com/ipfs/go-ipfs/merkledag/test"
Jeromy's avatar
Jeromy committed
11

12
	ipld "gx/ipfs/QmWi2BYBL5gJ3CiAiQchg6rn1A8iBsrWy51EYxvHVjFvLb/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15
)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

318
	testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
`))
}

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

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

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

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

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

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

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

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