This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
154 lines (135 loc) · 3.7 KB
/
main_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import "testing"
var Employees = Employee{
Id: 1,
Name: "Claire",
Subordinates: []*Employee{
{
Id: 2,
Name: "Roger",
Subordinates: []*Employee{
{
Id: 3,
Name: "George",
},
{
Id: 4,
Name: "Suzie",
},
{
Id: 5,
Name: "Lola",
Subordinates: []*Employee{
{
Id: 6,
Name: "Foo",
},
},
},
},
},
{
Id: 7,
Name: "Bar",
},
{
Id: 8,
Name: "Paul",
Subordinates: []*Employee{
{
Id: 9,
Name: "Jen",
},
{
Id: 10,
Name: "Ringo",
},
},
},
},
}
func TestSearcherEmptyDirectory(t *testing.T) {
managers := Searcher(nil, 1)
if managers != nil {
t.Error("Searcher shouldn't return managers when searching through an empty employee directory")
}
}
func TestSearcherNonexistentEmployee(t *testing.T) {
managers := Searcher(&Employees, 9001)
if managers != nil {
t.Error("Searcher shoudn't return managers for a nonexistent employee")
}
}
func TestSearcherCEO(t *testing.T) {
managers := Searcher(&Employees, 1)
if len(managers) > 1 && managers[0] != 1 {
t.Error("Searcher should return the CEO as its own manager")
}
}
func TestSearcherAnyEmployee(t *testing.T) {
// First level...
expectedManagers := []uint{1}
managers := Searcher(&Employees, 8)
for i, manager := range managers {
if manager != expectedManagers[i] {
t.Error("Searcher should return the managers of any employee, including himself/herself")
}
}
// Second level employee...
expectedManagers = []uint{8, 1}
managers = Searcher(&Employees, 10)
for i, manager := range managers {
if manager != expectedManagers[i] {
t.Error("Searcher should return the managers of any employee, including himself/herself")
}
}
// Third level employee...
expectedManagers = []uint{5, 2, 1}
managers = Searcher(&Employees, 6)
for i, manager := range managers {
if manager != expectedManagers[i] {
t.Error("Searcher should return the managers of any employee, including himself/herself")
}
}
}
func TestFindCommonManagerEmptyDirectory(t *testing.T) {
if FindCommonManager(nil, 1, 2) != 0 {
t.Error("FindCommonManager shouldn't return a manager when searching through an empty employee directory")
}
}
func TestFindCommonManagerOneNonexistentEmployee(t *testing.T) {
if FindCommonManager(&Employees, 9001, 2) != 0 {
t.Error("FindCommonManager shouldn't return a manager for 2 employees, when one of them is nonexistent")
}
}
func TestFindCommonManagerOneEmployeeIsCEO(t *testing.T) {
var expectedManager uint = 1
manager := FindCommonManager(&Employees, 1, 10)
if manager != expectedManager {
t.Error("FindCommonManager should return the CEO as the closest common manager for 2 employees, when one of them is the CEO")
}
}
func TestFindCommonManagerSameEmployee(t *testing.T) {
var expectedManager uint = 8
manager := FindCommonManager(&Employees, 9, 9)
if manager != expectedManager {
t.Error("FindCommandManager should return the closest common manager for twice the same employee")
}
}
func TestFindCommonManagerDifferentEmployees(t *testing.T) {
var expectedManager uint = 8
manager := FindCommonManager(&Employees, 9, 10)
if manager != expectedManager {
t.Error("FindCommandManager should return the closest common manager for 2 different employees")
}
expectedManager = 2
manager = FindCommonManager(&Employees, 3, 6)
if manager != expectedManager {
t.Error("FindCommandManager should return the closest common manager for 2 different employees")
}
expectedManager = 1
manager = FindCommonManager(&Employees, 6, 9)
if manager != expectedManager {
t.Error("FindCommandManager should return the closest common manager for 2 different employees")
}
}