| 1 |
package gatt |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
func TestL2capWriterChunk(t *testing.T) { |
| 11 |
cases := []struct { |
| 12 |
mtu uint16 |
| 13 |
head int |
| 14 |
chunk int |
| 15 |
ok bool |
| 16 |
}{ |
| 17 |
{mtu: 5, head: 0, chunk: 4, ok: true}, |
| 18 |
{mtu: 5, head: 0, chunk: 5, ok: true}, |
| 19 |
{mtu: 5, head: 0, chunk: 6, ok: false}, |
| 20 |
{mtu: 5, head: 1, chunk: 3, ok: true}, |
| 21 |
{mtu: 5, head: 1, chunk: 4, ok: true}, |
| 22 |
{mtu: 5, head: 1, chunk: 5, ok: false}, |
| 23 |
} |
| 24 |
|
| 25 |
for _, tt := range cases { |
| 26 |
w := newL2capWriter(tt.mtu) |
| 27 |
var want []byte |
| 28 |
for i := 0; i < tt.head; i++ { |
| 29 |
w.WriteByteFit(byte(i)) |
| 30 |
want = append(want, byte(i)) |
| 31 |
} |
| 32 |
w.Chunk() |
| 33 |
for i := 0; i < tt.chunk; i++ { |
| 34 |
w.WriteByteFit(byte(i)) |
| 35 |
if tt.ok { |
| 36 |
want = append(want, byte(i)) |
| 37 |
} |
| 38 |
} |
| 39 |
ok := w.Commit() |
| 40 |
if ok != tt.ok { |
| 41 |
t.Errorf("Chunk(%d %d %d) commit: got %t want %t", tt.mtu, tt.head, tt.chunk, ok, tt.ok) |
| 42 |
continue |
| 43 |
} |
| 44 |
if !bytes.Equal(want, w.Bytes()) { |
| 45 |
t.Errorf("Chunk(%d %d %d) write: got %x want %x", tt.mtu, tt.head, tt.chunk, w.Bytes(), want) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestL2capWriterPanicDoubleChunk(t *testing.T) { |
| 51 |
defer func() { recover() }() |
| 52 |
w := newL2capWriter(5) |
| 53 |
w.Chunk() |
| 54 |
w.Chunk() |
| 55 |
t.Errorf("l2capWriter should panic on double-chunk") |
| 56 |
} |
| 57 |
|
| 58 |
func TestL2capWriterPanicCommitBeforeChunk(t *testing.T) { |
| 59 |
defer func() { recover() }() |
| 60 |
w := newL2capWriter(5) |
| 61 |
w.Commit() |
| 62 |
t.Errorf("l2capWriter should panic on commit-before-chunk") |
| 63 |
} |
| 64 |
|
| 65 |
func TestL2capWriterPanicDoubleCommit(t *testing.T) { |
| 66 |
defer func() { recover() }() |
| 67 |
w := newL2capWriter(5) |
| 68 |
w.Chunk() |
| 69 |
w.Commit() |
| 70 |
w.Commit() |
| 71 |
t.Errorf("l2capWriter should panic on double-commit") |
| 72 |
} |
| 73 |
|
| 74 |
func BenchmarkWriteUint16(b *testing.B) { |
| 75 |
for i := 0; i < b.N; i++ { |
| 76 |
w := newL2capWriter(17) |
| 77 |
w.WriteUint16Fit(0) |
| 78 |
} |
| 79 |
} |