| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/models" |
| 9 |
) |
| 10 |
|
| 11 |
func genFakeTree() *Thread { |
| 12 |
tree := new(Thread) |
| 13 |
var prevChild *Thread |
| 14 |
for i := uint32(1); i < uint32(3); i++ { |
| 15 |
child := &Thread{ |
| 16 |
Uid: models.UID(fmt.Sprintf("%d", i*10)), |
| 17 |
Parent: tree, |
| 18 |
PrevSibling: prevChild, |
| 19 |
} |
| 20 |
if prevChild != nil { |
| 21 |
prevChild.NextSibling = child |
| 22 |
} else if tree.FirstChild == nil { |
| 23 |
tree.FirstChild = child |
| 24 |
} else { |
| 25 |
panic("unreachable") |
| 26 |
} |
| 27 |
prevChild = child |
| 28 |
var prevSecond *Thread |
| 29 |
for j := uint32(1); j < uint32(3); j++ { |
| 30 |
second := &Thread{ |
| 31 |
Uid: models.UID(fmt.Sprintf("%d", i*10+j)), |
| 32 |
Parent: child, |
| 33 |
PrevSibling: prevSecond, |
| 34 |
} |
| 35 |
if prevSecond != nil { |
| 36 |
prevSecond.NextSibling = second |
| 37 |
} else if child.FirstChild == nil { |
| 38 |
child.FirstChild = second |
| 39 |
} else { |
| 40 |
panic("unreachable") |
| 41 |
} |
| 42 |
prevSecond = second |
| 43 |
var prevThird *Thread |
| 44 |
limit := uint32(3) |
| 45 |
if j == 2 { |
| 46 |
limit = 8 |
| 47 |
} |
| 48 |
for k := uint32(1); k < limit; k++ { |
| 49 |
third := &Thread{ |
| 50 |
Uid: models.UID(fmt.Sprintf("%d", (i*10+j)*10+j)), |
| 51 |
Parent: second, |
| 52 |
PrevSibling: prevThird, |
| 53 |
} |
| 54 |
if prevThird != nil { |
| 55 |
prevThird.NextSibling = third |
| 56 |
} else if second.FirstChild == nil { |
| 57 |
second.FirstChild = third |
| 58 |
} else { |
| 59 |
panic("unreachable") |
| 60 |
} |
| 61 |
prevThird = third |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
return tree |
| 66 |
} |
| 67 |
|
| 68 |
func TestNewWalk(t *testing.T) { |
| 69 |
tree := genFakeTree() |
| 70 |
var prefix []string |
| 71 |
lastLevel := 0 |
| 72 |
tree.Walk(func(t *Thread, lvl int, e error) error { |
| 73 |
if e != nil { |
| 74 |
fmt.Printf("ERROR: %v\n", e) |
| 75 |
} |
| 76 |
if lvl > lastLevel && lvl > 1 { |
| 77 |
// we actually just descended... so figure out what connector we need |
| 78 |
// level 1 is flush to the root, so we avoid the indentation there |
| 79 |
if t.Parent.NextSibling != nil { |
| 80 |
prefix = append(prefix, "│ ") |
| 81 |
} else { |
| 82 |
prefix = append(prefix, " ") |
| 83 |
} |
| 84 |
} else if lvl < lastLevel { |
| 85 |
// ascended, need to trim the prefix layers |
| 86 |
diff := lastLevel - lvl |
| 87 |
prefix = prefix[:len(prefix)-diff] |
| 88 |
} |
| 89 |
|
| 90 |
var arrow string |
| 91 |
if t.Parent != nil { |
| 92 |
if t.NextSibling != nil { |
| 93 |
arrow = "├─>" |
| 94 |
} else { |
| 95 |
arrow = "└─>" |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// format |
| 100 |
fmt.Printf("%s%s%s\n", strings.Join(prefix, ""), arrow, t) |
| 101 |
|
| 102 |
lastLevel = lvl |
| 103 |
return nil |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
func uidSeq(tree *Thread) string { |
| 108 |
var seq []string |
| 109 |
tree.Walk(func(t *Thread, _ int, _ error) error { |
| 110 |
seq = append(seq, string(t.Uid)) |
| 111 |
return nil |
| 112 |
}) |
| 113 |
return strings.Join(seq, ".") |
| 114 |
} |
| 115 |
|
| 116 |
func TestThread_AddChild(t *testing.T) { |
| 117 |
tests := []struct { |
| 118 |
name string |
| 119 |
seq []models.UID |
| 120 |
want string |
| 121 |
}{ |
| 122 |
{ |
| 123 |
name: "ascending", |
| 124 |
seq: []models.UID{"1", "2", "3", "4", "5", "6"}, |
| 125 |
want: ".1.2.3.4.5.6", |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "descending", |
| 129 |
seq: []models.UID{"6", "5", "4", "3", "2", "1"}, |
| 130 |
want: ".6.5.4.3.2.1", |
| 131 |
}, |
| 132 |
} |
| 133 |
for _, test := range tests { |
| 134 |
t.Run(test.name, func(t *testing.T) { |
| 135 |
tree := new(Thread) |
| 136 |
for _, i := range test.seq { |
| 137 |
tree.AddChild(&Thread{Uid: i}) |
| 138 |
} |
| 139 |
if got := uidSeq(tree); got != test.want { |
| 140 |
t.Errorf("got: %s, but wanted: %s", got, |
| 141 |
test.want) |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestThread_OrderedInsert(t *testing.T) { |
| 148 |
tests := []struct { |
| 149 |
name string |
| 150 |
seq []models.UID |
| 151 |
want string |
| 152 |
}{ |
| 153 |
{ |
| 154 |
name: "ascending", |
| 155 |
seq: []models.UID{"1", "2", "3", "4", "5", "6"}, |
| 156 |
want: ".1.2.3.4.5.6", |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "descending", |
| 160 |
seq: []models.UID{"6", "5", "4", "3", "2", "1"}, |
| 161 |
want: ".1.2.3.4.5.6", |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "mixed", |
| 165 |
seq: []models.UID{"2", "1", "6", "3", "4", "5"}, |
| 166 |
want: ".1.2.3.4.5.6", |
| 167 |
}, |
| 168 |
} |
| 169 |
for _, test := range tests { |
| 170 |
t.Run(test.name, func(t *testing.T) { |
| 171 |
tree := new(Thread) |
| 172 |
for _, i := range test.seq { |
| 173 |
tree.OrderedInsert(&Thread{Uid: i}) |
| 174 |
} |
| 175 |
if got := uidSeq(tree); got != test.want { |
| 176 |
t.Errorf("got: %s, but wanted: %s", got, |
| 177 |
test.want) |
| 178 |
} |
| 179 |
}) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestThread_InsertCmd(t *testing.T) { |
| 184 |
tests := []struct { |
| 185 |
name string |
| 186 |
seq []models.UID |
| 187 |
want string |
| 188 |
}{ |
| 189 |
{ |
| 190 |
name: "ascending", |
| 191 |
seq: []models.UID{"1", "2", "3", "4", "5", "6"}, |
| 192 |
want: ".6.4.2.1.3.5", |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "descending", |
| 196 |
seq: []models.UID{"6", "5", "4", "3", "2", "1"}, |
| 197 |
want: ".6.4.2.1.3.5", |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: "mixed", |
| 201 |
seq: []models.UID{"2", "1", "6", "3", "4", "5"}, |
| 202 |
want: ".6.4.2.1.3.5", |
| 203 |
}, |
| 204 |
} |
| 205 |
sortMap := map[models.UID]int{ |
| 206 |
"6": 1, |
| 207 |
"4": 2, |
| 208 |
"2": 3, |
| 209 |
"1": 4, |
| 210 |
"3": 5, |
| 211 |
"5": 6, |
| 212 |
} |
| 213 |
|
| 214 |
// bigger compares the new child with the next node and returns true if |
| 215 |
// the child node is bigger and false otherwise. |
| 216 |
bigger := func(newNode, nextChild *Thread) bool { |
| 217 |
return sortMap[newNode.Uid] > sortMap[nextChild.Uid] |
| 218 |
} |
| 219 |
|
| 220 |
for _, test := range tests { |
| 221 |
t.Run(test.name, func(t *testing.T) { |
| 222 |
tree := new(Thread) |
| 223 |
for _, i := range test.seq { |
| 224 |
tree.InsertCmp(&Thread{Uid: i}, bigger) |
| 225 |
} |
| 226 |
if got := uidSeq(tree); got != test.want { |
| 227 |
t.Errorf("got: %s, but wanted: %s", got, |
| 228 |
test.want) |
| 229 |
} |
| 230 |
}) |
| 231 |
} |
| 232 |
} |