-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_struct_test.go
70 lines (57 loc) · 1.6 KB
/
example_struct_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
////////////////////////////////////////////////////////////////////////////////
// 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"
"github.com/stretchr/testify/require"
)
// StructType is a struct type
type StructType struct {
String string
Int int
Float float64
}
func testStructType(t *testing.T) []StructType {
// List of all structs (to be sorted)
var allStructs = []StructType{
{"A", 1, 1.0},
{"B", 2, 2.0},
{"C", 3, 3.0},
{"D", 4, 4.0},
{"E", 5, 5.0},
}
// List of all struct dependencies
var structDependencies = []Dependency[StructType]{
{Child: StructType{"A", 1, 1.0}, Parent: StructType{"C", 3, 3.0}},
{Child: StructType{"D", 4, 4.0}, Parent: StructType{"E", 5, 5.0}},
}
// Perform topological sort
require.Nil(t, Sort(allStructs, structDependencies))
// Check if all StrDependencies are fulfilled
for _, dependency := range structDependencies {
posFrom, posTo := -1, -1
for j := 0; j < len(allStructs); j++ {
if allStructs[j] == dependency.Child {
posFrom = j
}
if allStructs[j] == dependency.Parent {
posTo = j
}
}
require.Less(t, posTo, posFrom)
}
return allStructs
}
func TestStructType(t *testing.T) {
testStringType(t)
}
func TestStructTypeStability(t *testing.T) {
expected := testStructType(t)
for run := 0; run < nRunsConsistency; run++ {
require.EqualValues(t, expected, testStructType(t))
}
}