forked from someonegg/pathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinode_parents_test.go
More file actions
89 lines (79 loc) · 1.98 KB
/
inode_parents_test.go
File metadata and controls
89 lines (79 loc) · 1.98 KB
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
package pathfs
import (
"testing"
)
func sameParentEntry(e1, e2 parentEntry) bool {
if e1.name != e2.name {
return false
}
if e1.node != nil && e2.node != nil {
return e1.node.ino == e2.node.ino
}
return e1.node == e2.node
}
func TestInodeParents(t *testing.T) {
var p inodeParents
var ino1, ino2, ino3 inode
// empty store should be empty without panicing
if count := p.count(); count != 0 {
t.Error(count)
}
if p.all() != nil {
t.Error("empty store should return nil but did not")
}
// non-dupes should be stored
all := []parentEntry{
parentEntry{"foo", &ino1},
parentEntry{"foo2", &ino1},
parentEntry{"foo3", &ino1},
parentEntry{"foo", &ino2},
parentEntry{"foo", &ino3},
}
for i, v := range all {
p.add(v)
if count := p.count(); count != i+1 {
t.Errorf("want=%d have=%d", i+1, count)
}
last := p.get()
if last != v {
t.Error("get did not give us last-known parent")
}
}
// adding dupes should not cause the count to increase, but
// must cause get() to return the most recently added dupe.
for _, v := range all {
p.add(v)
if count := p.count(); count != len(all) {
t.Errorf("want=%d have=%d", len(all), count)
}
last := p.get()
if last != v {
t.Error("get did not give us last-known parent")
}
}
all2 := p.all()
if len(all) != len(all2) {
t.Errorf("want=%d have=%d", len(all), len(all2))
}
// test sortParents
sorted := sortParents(&p)
n := p.count()
if sorted[n-1] != p.newest {
t.Errorf("want inode %d to be newest parents, have inode %d", p.newest.node.ino, sorted[n-1].node.ino)
}
for i := 1; i < n-1; i++ {
prev, cur := sorted[i-1], sorted[i]
if !p.other[cur].After(p.other[prev]) {
t.Errorf("want parent inode %d to be newer than %d", cur.node.ino, prev.node.ino)
}
}
// test delete
// swap entry 2 and 3 to cover all statements
all[1], all[2] = all[2], all[1]
for i := n - 1; i >= 0; i-- {
p.delete(all[i])
if count := p.count(); count != i {
t.Errorf("want=%d have=%d", len(all)-i-1, count)
}
}
}