-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle_purchase_test.go
182 lines (172 loc) · 4.15 KB
/
vehicle_purchase_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package purchase
import (
"math"
"testing"
)
const floatEqualityThreshold = 1e-5
func TestNeedsLicense(t *testing.T) {
tests := []struct {
name string
kind string
expected bool
}{
{
name: "need a license for a car",
kind: "car",
expected: true,
},
{
name: "need a license for a truck",
kind: "truck",
expected: true,
},
{
name: "does not need a license for a bike",
kind: "bike",
expected: false,
},
{
name: "does not need a license for a stroller",
kind: "stroller",
expected: false,
},
{
name: "does not need a license for a e-scooter",
kind: "e-scooter",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := NeedsLicense(test.kind)
if actual != test.expected {
t.Errorf(
"NeedsLicense(%q) = %t, want %t",
test.kind,
actual,
test.expected)
}
})
}
}
func TestChooseVehicle(t *testing.T) {
tests := []struct {
name string
choice1 string
choice2 string
expected string
}{
{
name: "chooses Bugatti over Ford",
choice1: "Bugatti Veyron",
choice2: "Ford Pinto",
expected: "Bugatti Veyron is clearly the better choice.",
}, {
name: "chooses Chery over Kia",
choice1: "Chery EQ",
choice2: "Kia Niro Elektro",
expected: "Chery EQ is clearly the better choice.",
}, {
name: "chooses Ford Focus over Ford Pinto",
choice1: "Ford Focus",
choice2: "Ford Pinto",
expected: "Ford Focus is clearly the better choice.",
}, {
name: "chooses 2018 over 2020",
choice1: "2018 Bergamont City",
choice2: "2020 Gazelle Medeo",
expected: "2018 Bergamont City is clearly the better choice.",
}, {
name: "chooses Bugatti over ford",
choice1: "Bugatti Veyron",
choice2: "ford",
expected: "Bugatti Veyron is clearly the better choice.",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := ChooseVehicle(test.choice1, test.choice2)
if actual != test.expected {
t.Errorf(
"ChooseVehicle(%q, %q) = %q, want %q",
test.choice1,
test.choice2,
actual,
test.expected)
}
// The result should be independent of the order in which the choices are given.
actual = ChooseVehicle(test.choice2, test.choice1)
if actual != test.expected {
t.Errorf(
"ChooseVehicle(%q, %q) = %q, want %q",
test.choice2,
test.choice1,
actual,
test.expected)
}
})
}
}
func TestCalculateResellPrice(t *testing.T) {
tests := []struct {
name string
originalPrice float64
age float64
expected float64
}{
{
name: "price is reduced to 80%% for age below 3",
originalPrice: 40000,
age: 2,
expected: 32000,
},
{
name: "price is reduced to 80%% for age below 3",
originalPrice: 40000,
age: 2.5,
expected: 32000,
},
{
name: "price is reduced to 70%% for age 7",
originalPrice: 40000,
age: 7,
expected: 28000,
},
{
name: "price is reduced to 50%% for age 10",
originalPrice: 25000,
age: 10,
expected: 12500,
},
{
name: "price is reduced to 50%% for age 11",
originalPrice: 50000,
age: 11,
expected: 25000,
},
{
name: "float price is reduced to 70%% for age 8,",
originalPrice: 39000.000001,
age: 8,
expected: 27300.0000007,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := CalculateResellPrice(test.originalPrice, test.age)
if !floatingPointEquals(actual, test.expected) {
t.Errorf(
"CalculateResellPrice(%v, %v) = %v, want %v",
test.originalPrice,
test.age,
actual,
test.expected)
}
})
}
}
func floatingPointEquals(got, want float64) bool {
absoluteDifferenceBelowThreshold := math.Abs(got-want) <= floatEqualityThreshold
relativeDifferenceBelowThreshold := math.Abs(got-want)/(math.Abs(got)+math.Abs(want)) <= floatEqualityThreshold
return absoluteDifferenceBelowThreshold || relativeDifferenceBelowThreshold
}