Skip to content

Commit 3a7d276

Browse files
committed
Better test coverage for build
1 parent f9fc024 commit 3a7d276

File tree

8 files changed

+217
-37
lines changed

8 files changed

+217
-37
lines changed

build/build.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ func generic0(n int, edge func(v, w int) bool) *Virtual {
249249
// Generic returns a virtual graph with n vertices; its edge set consists of
250250
// all edges (v, w), v ≠ w, for which edge(v, w) returns true.
251251
func Generic(n int, edge FilterFunc) *Virtual {
252-
if edge == nil {
252+
switch {
253+
case n < 0:
254+
return nil
255+
case n == 0:
256+
return null
257+
case n == 1:
258+
return singleton()
259+
case edge == nil:
253260
return Kn(n)
254261
}
255262
return generic0(n, edge)
@@ -277,7 +284,9 @@ func Specific(g graph.Iterator) *Virtual {
277284
return res
278285
}
279286
res.cost = func(v, w int) (cost int64) {
280-
// Only called when w is a neighbor of v.
287+
if !res.edge(v, w) {
288+
return 0
289+
}
281290
h.VisitFrom(v, w, func(w int, c int64) (skip bool) {
282291
cost = c
283292
return true

build/build_test.go

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ func TestEdge(t *testing.T) {
149149
}
150150

151151
func TestLine(t *testing.T) {
152+
if mess, diff := diff(line(-1), (*Virtual)(nil)); diff {
153+
t.Errorf("line %s", mess)
154+
}
152155
if mess, diff := diff(line(0).String(), "0 []"); diff {
153156
t.Errorf("line %s", mess)
154157
}
@@ -167,7 +170,73 @@ func TestLine(t *testing.T) {
167170
}
168171

169172
func TestGeneric(t *testing.T) {
170-
g := Generic(0, func(v, w int) bool { return v != 0 })
173+
g := generic0(-1, alwaysEdge)
174+
if mess, diff := diff(g, (*Virtual)(nil)); diff {
175+
t.Errorf("generic0 %s", mess)
176+
}
177+
Consistent("generic0", t, g)
178+
179+
g = generic0(0, alwaysEdge)
180+
if mess, diff := diff(g.String(), "0 []"); diff {
181+
t.Errorf("generic0 %s", mess)
182+
}
183+
Consistent("generic0", t, g)
184+
185+
g = generic0(1, alwaysEdge)
186+
if mess, diff := diff(g.String(), "1 []"); diff {
187+
t.Errorf("generic0 %s", mess)
188+
}
189+
Consistent("generic0", t, g)
190+
191+
g = generic(-1, zero, alwaysEdge)
192+
if mess, diff := diff(g, (*Virtual)(nil)); diff {
193+
t.Errorf("generic %s", mess)
194+
}
195+
Consistent("generic", t, g)
196+
197+
g = generic(0, zero, alwaysEdge)
198+
if mess, diff := diff(g.String(), "0 []"); diff {
199+
t.Errorf("generic %s", mess)
200+
}
201+
Consistent("generic", t, g)
202+
203+
g = generic(1, zero, alwaysEdge)
204+
if mess, diff := diff(g.String(), "1 []"); diff {
205+
t.Errorf("generic %s", mess)
206+
}
207+
Consistent("generic", t, g)
208+
209+
g = generic(2, zero, alwaysEdge)
210+
if mess, diff := diff(g.String(), "2 [{0 1}]"); diff {
211+
t.Errorf("generic %s", mess)
212+
}
213+
Consistent("generic", t, g)
214+
215+
g = Generic(-1, nil)
216+
if mess, diff := diff(g, (*Virtual)(nil)); diff {
217+
t.Errorf("Generic %s", mess)
218+
}
219+
Consistent("Generic", t, g)
220+
221+
g = Generic(0, nil)
222+
if mess, diff := diff(g.String(), "0 []"); diff {
223+
t.Errorf("Generic %s", mess)
224+
}
225+
Consistent("Generic", t, g)
226+
227+
g = Generic(1, nil)
228+
if mess, diff := diff(g.String(), "1 []"); diff {
229+
t.Errorf("Generic %s", mess)
230+
}
231+
Consistent("Generic", t, g)
232+
233+
g = Generic(2, nil)
234+
if mess, diff := diff(g.String(), "2 [{0 1}]"); diff {
235+
t.Errorf("Generic %s", mess)
236+
}
237+
Consistent("Generic", t, g)
238+
239+
g = Generic(0, func(v, w int) bool { return v != 0 })
171240
if mess, diff := diff(g.String(), "0 []"); diff {
172241
t.Errorf("Generic %s", mess)
173242
}
@@ -205,9 +274,22 @@ func TestSpecific(t *testing.T) {
205274
t.Errorf("Specific %s", mess)
206275
}
207276
Consistent("Specific", t, Specific(Kn(4)))
277+
278+
if mess, diff := diff(res.cost(0, 0), int64(0)); diff {
279+
t.Errorf("Specific cost %s", mess)
280+
}
281+
if mess, diff := diff(res.cost(0, 1), int64(1)); diff {
282+
t.Errorf("Specific cost %s", mess)
283+
}
284+
if mess, diff := diff(res.cost(1, 0), int64(10)); diff {
285+
t.Errorf("Specific cost %s", mess)
286+
}
208287
}
209288

210289
func TestEmpty(t *testing.T) {
290+
if mess, diff := diff(Empty(-1), (*Virtual)(nil)); diff {
291+
t.Errorf("Empty %s", mess)
292+
}
211293
if mess, diff := diff(Empty(0).String(), "0 []"); diff {
212294
t.Errorf("Empty %s", mess)
213295
}
@@ -223,6 +305,9 @@ func TestEmpty(t *testing.T) {
223305
}
224306

225307
func TestKn(t *testing.T) {
308+
if mess, diff := diff(Kn(-1), (*Virtual)(nil)); diff {
309+
t.Errorf("Kn %s", mess)
310+
}
226311
if mess, diff := diff(Kn(0).String(), "0 []"); diff {
227312
t.Errorf("Kn %s", mess)
228313
}
@@ -289,6 +374,12 @@ func TestKeep(t *testing.T) {
289374
t.Errorf("Keep %s", mess)
290375
}
291376
Consistent("Keep", t, g)
377+
378+
g = Cycle(3).AddCost(1).Keep(nil)
379+
if mess, diff := diff(g.String(), "3 [{0 1}:1 {0 2}:1 {1 2}:1]"); diff {
380+
t.Errorf("Keep %s", mess)
381+
}
382+
Consistent("Keep", t, g)
292383
}
293384

294385
func TestAddCost(t *testing.T) {
@@ -315,10 +406,58 @@ func TestAddCostFunc(t *testing.T) {
315406
}
316407
Consistent("AccCostFunc", t, res)
317408

409+
if mess, diff := diff(res.Cost(0, 0), int64(0)); diff {
410+
t.Errorf("Cost %s", mess)
411+
}
412+
if mess, diff := diff(res.Cost(0, 1), int64(5)); diff {
413+
t.Errorf("Cost %s", mess)
414+
}
415+
if mess, diff := diff(res.Cost(-1, 0), int64(0)); diff {
416+
t.Errorf("Cost %s", mess)
417+
}
418+
419+
res = Grid(1, 2).AddCostFunc(nil)
420+
exp = "2 [{0 1}]"
421+
if mess, diff := diff(res.String(), exp); diff {
422+
t.Errorf("AddCostFunc %s", mess)
423+
}
424+
Consistent("AccCostFunc", t, res)
425+
318426
res = Empty(0).AddCostFunc(Cost(5))
319427
exp = "0 []"
320428
if mess, diff := diff(res.String(), exp); diff {
321429
t.Errorf("AddCostFunc %s", mess)
322430
}
323431
Consistent("AddCostFunc", t, res)
324432
}
433+
434+
func TestVisit(t *testing.T) {
435+
g := Grid(1, 2).AddCost(5)
436+
res := g.Visit(0, func(w int, c int64) (skip bool) {
437+
return w == 1 && c == 5
438+
})
439+
if mess, diff := diff(res, true); diff {
440+
t.Errorf("Visit %s", mess)
441+
}
442+
443+
res = g.VisitFrom(0, 0, func(w int, c int64) (skip bool) {
444+
return w == 1 && c == 5
445+
})
446+
if mess, diff := diff(res, true); diff {
447+
t.Errorf("Visit %s", mess)
448+
}
449+
450+
res = g.VisitFrom(0, -1, func(w int, c int64) (skip bool) {
451+
return w == 1 && c == 5
452+
})
453+
if mess, diff := diff(res, true); diff {
454+
t.Errorf("Visit %s", mess)
455+
}
456+
457+
res = g.VisitFrom(0, 2, func(w int, c int64) (skip bool) {
458+
return w == 1 && c == 5
459+
})
460+
if mess, diff := diff(res, false); diff {
461+
t.Errorf("Visit %s", mess)
462+
}
463+
}

build/cartesian.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import "strconv"
1313
func (g1 *Virtual) Cartesian(g2 *Virtual) *Virtual {
1414
m, n := g1.Order(), g2.Order()
1515
switch {
16-
case m < 0 || n < 0:
17-
return nil
1816
case m == 0 || n == 0:
1917
return null
2018
case m*n/m != n:

build/circulant_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ func TestCirculant(t *testing.T) {
1212
}
1313
Consistent("Circulant", t, res)
1414

15+
res = Circulant(-1)
16+
if mess, diff := diff(res, (*Virtual)(nil)); diff {
17+
t.Errorf("Circulant %s", mess)
18+
}
19+
Consistent("Circulant", t, res)
20+
21+
res = Circulant(0)
22+
if mess, diff := diff(res.String(), "0 []"); diff {
23+
t.Errorf("Circulant %s", mess)
24+
}
25+
Consistent("Circulant", t, res)
26+
1527
res = Circulant(1)
1628
if mess, diff := diff(res.String(), "1 []"); diff {
1729
t.Errorf("Circulant %s", mess)

build/cycle_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package build
33
import "testing"
44

55
func TestCycle(t *testing.T) {
6+
if mess, diff := diff(Cycle(-1), (*Virtual)(nil)); diff {
7+
t.Errorf("Cycle %s", mess)
8+
}
9+
610
if mess, diff := diff(Cycle(0).String(), "0 []"); diff {
711
t.Errorf("Cycle %s", mess)
812
}

build/join_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ func TestJoin(t *testing.T) {
1818
}
1919
Consistent("Join", t, res)
2020

21+
res = Empty(0).Join(g, b)
22+
exp = "4 [{0 1} {0 2} {1 3} {2 3}]"
23+
if mess, diff := diff(res.String(), exp); diff {
24+
t.Errorf("Join %s", mess)
25+
}
26+
Consistent("Join", t, res)
27+
2128
b = EdgeSet{
2229
From: Vertex(1),
2330
To: Vertex(3),

build/subgraph_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,33 @@ func TestSubgraph(t *testing.T) {
1212
}
1313
Consistent("Subgraph1", t, res)
1414

15+
res = Kn(2).Subgraph(Range(-1, 10))
16+
exp = "2 [{0 1}]"
17+
if mess, diff := diff(res.String(), exp); diff {
18+
t.Errorf("Subgraph %s", mess)
19+
}
20+
Consistent("Subgraph2", t, res)
21+
1522
res = Kn(6).AddCostFunc(cost).Subgraph(Range(3, 6))
1623
exp = "3 [(0 1):34 (0 2):35 (1 0):43 (1 2):45 (2 0):53 (2 1):54]"
1724
if mess, diff := diff(res.String(), exp); diff {
1825
t.Errorf("Subgraph %s", mess)
1926
}
20-
Consistent("Subgraph2", t, res)
27+
Consistent("Subgraph3", t, res)
2128

2229
res = Kn(6).AddCostFunc(cost).Subgraph(Vertex(1).Or(Vertex(3)))
2330
exp = "2 [(0 1):13 (1 0):31]"
2431
if mess, diff := diff(res.String(), exp); diff {
2532
t.Errorf("Subgraph %s", mess)
2633
}
27-
Consistent("Subgraph3", t, res)
34+
Consistent("Subgraph4", t, res)
2835

2936
res = Grid(3, 3).Subgraph(Range(0, 4).Or(Range(5, 9)))
3037
exp = "8 [{0 1} {0 3} {1 2} {2 4} {3 5} {4 7} {5 6} {6 7}]"
3138
if mess, diff := diff(res.String(), exp); diff {
3239
t.Errorf("Subgraph %s", mess)
3340
}
34-
Consistent("Subgraph4", t, res)
41+
Consistent("Subgraph5", t, res)
3542

3643
for m := 0; m < 6; m++ {
3744
for n := 0; n < 6; n++ {

0 commit comments

Comments
 (0)