-
Notifications
You must be signed in to change notification settings - Fork 15
/
usbcommon_test.go
109 lines (83 loc) · 1.75 KB
/
usbcommon_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
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Tests for usbcommon.go
*/
package main
import (
"testing"
)
// Check if two UsbAddrList are equal
func equalUsbAddrList(l1, l2 UsbAddrList) bool {
if len(l1) != len(l2) {
return false
}
for i := range l1 {
if l1[i] != l2[i] {
return false
}
}
return true
}
// Make UsbAddrList from individual addresses
func makeUsbAddrList(addrs ...UsbAddr) UsbAddrList {
l := UsbAddrList{}
for _, a := range addrs {
l.Add(a)
}
return l
}
// Test (*UsbAddrList)Add() against (*UsbAddrList)Find()
func TestUsbAddrListAddFind(t *testing.T) {
a1 := UsbAddr{0, 1}
a2 := UsbAddr{0, 2}
a3 := UsbAddr{0, 3}
l1 := makeUsbAddrList(a1, a2)
if l1.Find(a1) < 0 {
t.Fail()
}
if l1.Find(a2) < 0 {
t.Fail()
}
if l1.Find(a3) >= 0 {
t.Fail()
}
}
// Test that (*UsbAddrList)Add() is commutative operation
func TestUsbAddrListAddCommutative(t *testing.T) {
a1 := UsbAddr{0, 1}
a2 := UsbAddr{0, 2}
l1 := UsbAddrList{}
l2 := UsbAddrList{}
l1.Add(a1)
l1.Add(a2)
l2.Add(a2)
l2.Add(a1)
if !equalUsbAddrList(l1, l2) {
t.Fail()
}
}
// Test (*UsbAddrList) Diff()
func TestUsbAddrListDiff(t *testing.T) {
a1 := UsbAddr{0, 1}
a2 := UsbAddr{0, 2}
a3 := UsbAddr{0, 3}
l1 := makeUsbAddrList(a2, a3)
l2 := makeUsbAddrList(a1, a3)
added, removed := l1.Diff(l2)
if !equalUsbAddrList(added, makeUsbAddrList(a1)) {
t.Fail()
}
if !equalUsbAddrList(removed, makeUsbAddrList(a2)) {
t.Fail()
}
added, removed = l2.Diff(l1)
if !equalUsbAddrList(removed, makeUsbAddrList(a1)) {
t.Fail()
}
if !equalUsbAddrList(added, makeUsbAddrList(a2)) {
t.Fail()
}
}