-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_simple_test.go
105 lines (85 loc) · 2.15 KB
/
example_simple_test.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 by Fabian Kohn
//
// This source code is licensed under the Apache License, Version 2.0, found in
// the LICENSE file in the root directory of this source tree.
////////////////////////////////////////////////////////////////////////////////
package topo
import "testing"
func testStringType(t *testing.T) []string {
// List of all simple strings (to be sorted)
var allStrings = []string{
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
}
// All string dependencies
var stringDependencies = []Dependency{
{Child: "B", Parent: "A"},
{Child: "B", Parent: "C"},
{Child: "B", Parent: "D"},
{Child: "A", Parent: "E"},
{Child: "D", Parent: "C"},
}
// Getter function to convert original elements to a generic type
getter := func(i int) Type {
return allStrings[i]
}
// Setter function to restore the original type of the data
setter := func(i int, val Type) {
allStrings[i] = val.(string)
}
// Perform topological sort
if err := Sort(allStrings, stringDependencies, getter, setter); err != nil {
t.Fatal(err)
}
// Check if all StrDependencies are fulfilled
for _, dependency := range stringDependencies {
posFrom, posTo := -1, -1
for j := 0; j < len(allStrings); j++ {
if allStrings[j] == dependency.Child {
posFrom = j
}
if allStrings[j] == dependency.Parent {
posTo = j
}
}
if posTo >= posFrom {
t.Fatalf("Unexpected order, want pos(%v) < pos(%v) for %v / %v", posTo, posFrom, dependency.Child, dependency.Parent)
}
}
return allStrings
}
func TestStringType(t *testing.T) {
testStringType(t)
}
func TestStringTypeStability(t *testing.T) {
expected := testStringType(t)
for run := 0; run < nRunsConsistency; run++ {
if res := testStringType(t); !testEqString(res, expected) {
t.Fatalf("API stability violation, want %s, have %s", expected, res)
}
}
}
func testEqString(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}