forked from currantlabs/gatt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
attr_test.go
68 lines (61 loc) · 1.9 KB
/
attr_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
package gatt
import (
"reflect"
"testing"
)
func TestHandleRangeAt(t *testing.T) {
r := &attrRange{
aa: make([]attr, 3),
base: 4,
}
r.aa[0].h = 4
r.aa[1].h = 5
r.aa[2].h = 6
for _, h := range [...]uint16{0, 2, 3, 7, 8, 100} {
if _, ok := r.At(h); ok {
t.Errorf("At(%d) should return !ok", h)
}
}
for _, h := range [...]uint16{4, 5, 6} {
if _, ok := r.At(h); !ok {
t.Errorf("At(%d) should return ok", h)
}
if a, _ := r.At(h); a.h != h {
t.Errorf("At(%d) returned wrong attr, got %d want %d", h, a.h, h)
}
}
}
func TestHandleRangeSubrange(t *testing.T) {
r := &attrRange{
aa: make([]attr, 3),
}
cases := []struct {
start, end uint16
base uint16
want []attr
}{
{start: 0, end: 3, base: 4, want: []attr{}},
{start: 0, end: 4, base: 4, want: []attr{r.aa[0]}},
{start: 0, end: 5, base: 4, want: []attr{r.aa[0], r.aa[1]}},
{start: 4, end: 5, base: 4, want: []attr{r.aa[0], r.aa[1]}},
{start: 4, end: 6, base: 4, want: []attr{r.aa[0], r.aa[1], r.aa[2]}},
{start: 4, end: 100, base: 4, want: []attr{r.aa[0], r.aa[1], r.aa[2]}},
{start: 5, end: 100, base: 4, want: []attr{r.aa[1], r.aa[2]}},
{start: 5, end: 6, base: 4, want: []attr{r.aa[1], r.aa[2]}},
{start: 5, end: 5, base: 4, want: []attr{r.aa[1]}},
{start: 6, end: 6, base: 4, want: []attr{r.aa[2]}},
{start: 6, end: 100, base: 4, want: []attr{r.aa[2]}},
{start: 7, end: 100, base: 4, want: []attr{}},
{start: 100, end: 1000, base: 4, want: []attr{}},
{start: 1000, end: 100, base: 4, want: []attr{}},
{start: 5, end: 1, base: 4, want: []attr{}},
{start: 1, end: 65535, base: 4, want: []attr{r.aa[0], r.aa[1], r.aa[2]}},
{start: 1, end: 65535, base: 0, want: []attr{r.aa[1], r.aa[2]}},
}
for _, tt := range cases {
r.base = tt.base
if got := r.Subrange(tt.start, tt.end); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Range(%d, %d): got %v want %v", tt.start, tt.end, got, tt.want)
}
}
}