-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_test.go
144 lines (135 loc) · 2.63 KB
/
segment_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
package fare
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestNewSegment(t *testing.T) {
p11 := Position{
RideID: 1,
Lat: 37.966660,
Long: 23.728308,
Timestamp: time.Unix(1405594957, 0),
}
p12 := Position{
RideID: 1,
Lat: 37.966627,
Long: 23.728263,
Timestamp: time.Unix(1405594966, 0),
}
p21 := Position{
RideID: 2,
Lat: 37.966627,
Long: 23.728263,
Timestamp: time.Unix(1405594966, 0),
}
tests := []struct {
name string
p1, p2 Position
maxSpeed float64
check func(s Segment, err error)
}{
{
name: "ok",
p1: p11,
p2: p12,
maxSpeed: 100,
check: func(s Segment, err error) {
assert.Nil(t, err)
},
},
{
name: "different rideId - error",
p1: p11,
p2: p21,
maxSpeed: 100,
check: func(s Segment, err error) {
assert.NotNil(t, err)
},
},
{
name: "exceeds max speed - error",
p1: p11,
p2: p12,
maxSpeed: 1,
check: func(s Segment, err error) {
assert.NotNil(t, err)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.check(NewSegment(test.p1, test.p2, test.maxSpeed))
})
}
}
func TestSegment_Price(t *testing.T) {
tests := []struct {
name string
segment Segment
check func(p Price)
fare Price
}{
{
name: "less than 10km/h",
segment: Segment{
speed: 5,
duration: time.Hour,
},
fare: Price(fareIdlePerHour),
},
{
name: "the minimum fare",
segment: Segment{
speed: 15,
distance: 1,
startedAt: time.Unix(1405594957, 0),
finishedAt: time.Unix(1405594965, 0),
},
fare: Price(fareMovingNormal),
},
{
name: "midnight ride",
segment: Segment{
speed: 50,
distance: 100,
startedAt: time.Unix(1593397864, 0),
finishedAt: time.Unix(1593397964, 0),
},
fare: Price(130),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.fare, test.segment.Fare())
})
}
}
func BenchmarkNewSegment(b *testing.B) {
p11 := Position{
RideID: 1,
Lat: 37.966660,
Long: 23.728308,
Timestamp: time.Unix(1405594957, 0),
}
p12 := Position{
RideID: 1,
Lat: 37.966627,
Long: 23.728263,
Timestamp: time.Unix(1405594966, 0),
}
for n := 0; n < b.N; n++ {
_, _ = NewSegment(p11, p12, 100)
}
}
func BenchmarkSegment_Fare(b *testing.B) {
segment := Segment{
speed: 50,
distance: 100,
startedAt: time.Unix(1593397864, 0),
finishedAt: time.Unix(1593397964, 0),
}
for n := 0; n < b.N; n++ {
_ = segment.Fare()
}
}