-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive_test.go
178 lines (130 loc) · 4.33 KB
/
drive_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
package roomba_api
import (
"testing"
rt "github.com/xa4a/go-roomba/testing"
)
func TestDriveOk(t *testing.T) {
StartTestServer()
defer StopTestServer()
defer rt.ClearTestRoomba()
client := NewTestClient(t)
defer client.Close()
conn_req := AcquireConnectionRequest{Port: DUMMY_PORT_NAME}
conn_resp := AcquireConnectionResponse{}
err := client.Call("RoombaServer.AcquireConnection", conn_req, &conn_resp)
if err != nil {
t.Fatalf("failed acquiring dummy connection: %s", err)
}
port_name := conn_resp.Port
drive_req := DriveRequest{port_name, -200, 500}
err = client.Call("RoombaServer.Drive", drive_req, &DriveResponse{})
if err != nil {
t.Fatalf("rpc call failed unexpectedly: %s", err)
}
expected := []byte{128, 131, 137, 255, 56, 1, 244}
rt.VerifyWritten(testRoombaServer.Connections[port_name].Roomba, expected, t)
// Special cases radius.
for _, radius := range []int16{-2000, 2000, 0, 10, -10, 32767, -32768} {
drive_req = DriveRequest{port_name, -200, radius}
err = client.Call("RoombaServer.Drive", drive_req, &DriveResponse{})
if err != nil {
t.Fatalf("rpc call failed unexpectedly for radius %d: %s", radius, err)
}
}
}
func TestDriveWrongConnId(t *testing.T) {
StartTestServer()
defer StopTestServer()
client := NewTestClient(t)
defer client.Close()
drive_req := DriveRequest{"foo", -200, 500}
err := client.Call("RoombaServer.Drive", drive_req, &DriveResponse{})
if err == nil {
t.Fatalf("rpc call succeeded unexpectedly")
}
}
func TestDriveWrongVelocityRadius(t *testing.T) {
StartTestServer()
defer StopTestServer()
defer rt.ClearTestRoomba()
client := NewTestClient(t)
defer client.Close()
conn_req := AcquireConnectionRequest{Port: DUMMY_PORT_NAME}
conn_resp := AcquireConnectionResponse{}
err := client.Call("RoombaServer.AcquireConnection", conn_req, &conn_resp)
if err != nil {
t.Fatalf("failed acquiring dummy connection: %s", err)
}
port_name := conn_resp.Port
for _, velocity := range []int16{-501, -1000, 501, 1001} {
drive_req := DriveRequest{port_name, velocity, 500}
err = client.Call("RoombaServer.Drive", drive_req, &DriveResponse{})
if err == nil {
t.Errorf("rpc call succeeded unexpectedly")
}
}
for _, radius := range []int16{-2001, -10000, 2001, 10000} {
drive_req := DriveRequest{port_name, 315, radius}
err = client.Call("RoombaServer.Drive", drive_req, &DriveResponse{})
if err == nil {
t.Errorf("rpc call succeeded unexpectedly")
}
}
}
func TestDirectDriveOk(t *testing.T) {
StartTestServer()
defer StopTestServer()
defer rt.ClearTestRoomba()
client := NewTestClient(t)
defer client.Close()
conn_req := AcquireConnectionRequest{Port: DUMMY_PORT_NAME}
conn_resp := AcquireConnectionResponse{}
err := client.Call("RoombaServer.AcquireConnection", conn_req, &conn_resp)
if err != nil {
t.Errorf("failed acquiring dummy connection: %s", err)
}
port_name := conn_resp.Port
drive_req := DirectDriveRequest{port_name, 127, 256}
err = client.Call("RoombaServer.DirectDrive", drive_req, &DirectDriveResponse{})
if err != nil {
t.Fatalf("rpc call failed unexpectedly: %s", err)
}
expected := []byte{128, 131, 145, 0, 127, 1, 0}
rt.VerifyWritten(testRoombaServer.Connections[port_name].Roomba, expected, t)
}
func TestDirectDriveWrongConnId(t *testing.T) {
StartTestServer()
defer StopTestServer()
client := NewTestClient(t)
defer client.Close()
drive_req := DirectDriveRequest{"foo", 127, 256}
err := client.Call("RoombaServer.DirectDrive", drive_req, &DirectDriveResponse{})
if err == nil {
t.Fatalf("rpc call succeeded unexpectedly")
}
}
func TestDirectDriveWrongVelocity(t *testing.T) {
StartTestServer()
defer StopTestServer()
defer rt.ClearTestRoomba()
client := NewTestClient(t)
defer client.Close()
conn_req := AcquireConnectionRequest{Port: DUMMY_PORT_NAME}
conn_resp := AcquireConnectionResponse{}
err := client.Call("RoombaServer.AcquireConnection", conn_req, &conn_resp)
if err != nil {
t.Fatalf("failed acquiring dummy connection: %s", err)
}
port_name := conn_resp.Port
for _, velocities := range [][2]int16{{-501, 500},
{-1000, 501},
{35, 1002}} {
left := velocities[0]
right := velocities[1]
drive_req := DirectDriveRequest{port_name, left, right}
err = client.Call("RoombaServer.DirectDrive", drive_req, &DirectDriveResponse{})
if err == nil {
t.Errorf("rpc call succeeded unexpectedly")
}
}
}