Skip to content

Commit

Permalink
merkledag/traverse: Fix tests after node pointer removal
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>
  • Loading branch information
mildred authored and Shanti Bouchez-Mongardé committed Mar 4, 2016
1 parent b1f77e9 commit 683b817
Showing 1 changed file with 79 additions and 61 deletions.
140 changes: 79 additions & 61 deletions merkledag/traverse/traverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@ import (
"testing"

mdag "github.com/ipfs/go-ipfs/merkledag"
mdagtest "github.com/ipfs/go-ipfs/merkledag/test"
)

func TestDFSPreNoSkip(t *testing.T) {
opts := Options{Order: DFSPre}
ds := mdagtest.Mock()
opts := Options{Order: DFSPre, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
Expand All @@ -37,7 +39,7 @@ func TestDFSPreNoSkip(t *testing.T) {
2 /a/ab/abb
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
Expand Down Expand Up @@ -73,25 +75,26 @@ func TestDFSPreNoSkip(t *testing.T) {
}

func TestDFSPreSkip(t *testing.T) {
opts := Options{Order: DFSPre, SkipDuplicates: true}
ds := mdagtest.Mock()
opts := Options{Order: DFSPre, SkipDuplicates: true, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
Expand All @@ -101,7 +104,7 @@ func TestDFSPreSkip(t *testing.T) {
2 /a/ab/abb
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
Expand All @@ -111,25 +114,26 @@ func TestDFSPreSkip(t *testing.T) {
}

func TestDFSPostNoSkip(t *testing.T) {
opts := Options{Order: DFSPost}
ds := mdagtest.Mock()
opts := Options{Order: DFSPost, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
0 /a
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
2 /a/aa/aaa
2 /a/aa/aab
1 /a/aa
Expand All @@ -139,7 +143,7 @@ func TestDFSPostNoSkip(t *testing.T) {
0 /a
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
4 /a/aa/aaa/aaaa/aaaaa
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
Expand Down Expand Up @@ -175,25 +179,26 @@ func TestDFSPostNoSkip(t *testing.T) {
}

func TestDFSPostSkip(t *testing.T) {
opts := Options{Order: DFSPost, SkipDuplicates: true}
ds := mdagtest.Mock()
opts := Options{Order: DFSPost, SkipDuplicates: true, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
0 /a
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
1 /a/aa
0 /a
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
2 /a/aa/aaa
2 /a/aa/aab
1 /a/aa
Expand All @@ -203,7 +208,7 @@ func TestDFSPostSkip(t *testing.T) {
0 /a
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
4 /a/aa/aaa/aaaa/aaaaa
3 /a/aa/aaa/aaaa
2 /a/aa/aaa
Expand All @@ -213,25 +218,26 @@ func TestDFSPostSkip(t *testing.T) {
}

func TestBFSNoSkip(t *testing.T) {
opts := Options{Order: BFS}
ds := mdagtest.Mock()
opts := Options{Order: BFS, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
Expand All @@ -241,7 +247,7 @@ func TestBFSNoSkip(t *testing.T) {
2 /a/ab/abb
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/aa
Expand Down Expand Up @@ -277,25 +283,26 @@ func TestBFSNoSkip(t *testing.T) {
}

func TestBFSSkip(t *testing.T) {
opts := Options{Order: BFS, SkipDuplicates: true}
ds := mdagtest.Mock()
opts := Options{Order: BFS, SkipDuplicates: true, DAG: ds}

testWalkOutputs(t, newFan(t), opts, []byte(`
testWalkOutputs(t, newFan(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
1 /a/ac
1 /a/ad
`))

testWalkOutputs(t, newLinkedList(t), opts, []byte(`
testWalkOutputs(t, newLinkedList(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
3 /a/aa/aaa/aaaa
4 /a/aa/aaa/aaaa/aaaaa
`))

testWalkOutputs(t, newBinaryTree(t), opts, []byte(`
testWalkOutputs(t, newBinaryTree(t, ds), opts, []byte(`
0 /a
1 /a/aa
1 /a/ab
Expand All @@ -305,7 +312,7 @@ func TestBFSSkip(t *testing.T) {
2 /a/ab/abb
`))

testWalkOutputs(t, newBinaryDAG(t), opts, []byte(`
testWalkOutputs(t, newBinaryDAG(t, ds), opts, []byte(`
0 /a
1 /a/aa
2 /a/aa/aaa
Expand Down Expand Up @@ -341,57 +348,68 @@ func testWalkOutputs(t *testing.T, root *mdag.Node, opts Options, expect []byte)
}
}

func newFan(t *testing.T) *mdag.Node {
func newFan(t *testing.T, ds mdag.DAGService) *mdag.Node {
a := &mdag.Node{Data: []byte("/a")}
addChild(t, a, "aa")
addChild(t, a, "ab")
addChild(t, a, "ac")
addChild(t, a, "ad")
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"))
return a
}

func newLinkedList(t *testing.T) *mdag.Node {
func newLinkedList(t *testing.T, ds mdag.DAGService) *mdag.Node {
a := &mdag.Node{Data: []byte("/a")}
aa := addChild(t, a, "aa")
aaa := addChild(t, aa, "aaa")
aaaa := addChild(t, aaa, "aaaa")
addChild(t, aaaa, "aaaaa")
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)
return a
}

func newBinaryTree(t *testing.T) *mdag.Node {
func newBinaryTree(t *testing.T, ds mdag.DAGService) *mdag.Node {
a := &mdag.Node{Data: []byte("/a")}
aa := addChild(t, a, "aa")
ab := addChild(t, a, "ab")
addChild(t, aa, "aaa")
addChild(t, aa, "aab")
addChild(t, ab, "aba")
addChild(t, ab, "abb")
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)
return a
}

func newBinaryDAG(t *testing.T) *mdag.Node {
func newBinaryDAG(t *testing.T, ds mdag.DAGService) *mdag.Node {
a := &mdag.Node{Data: []byte("/a")}
aa := addChild(t, a, "aa")
aaa := addChild(t, aa, "aaa")
aaaa := addChild(t, aaa, "aaaa")
aaaaa := addChild(t, aaaa, "aaaaa")
addLink(t, a, aa)
addLink(t, aa, aaa)
addLink(t, aaa, aaaa)
addLink(t, aaaa, aaaaa)
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)
return a
}

func addLink(t *testing.T, a, b *mdag.Node) {
func addLink(t *testing.T, ds mdag.DAGService, a, b *mdag.Node) {
to := string(a.Data) + "2" + string(b.Data)
if _, err := ds.Add(b); err != nil {
t.Error(err)
}
if err := a.AddNodeLink(to, b); err != nil {
t.Error(err)
}
}

func addChild(t *testing.T, a *mdag.Node, name string) *mdag.Node {
c := &mdag.Node{Data: []byte(string(a.Data) + "/" + name)}
addLink(t, a, c)
return c
func child(t *testing.T, ds mdag.DAGService, a *mdag.Node, name string) *mdag.Node {
return &mdag.Node{Data: []byte(string(a.Data) + "/" + name)}
}

0 comments on commit 683b817

Please sign in to comment.