test: cover Event.Int32, Fixed and Array decoding
- commit
- b340c3890618a9c49be14c9a6c63d5ee941cb8f8
- author
- brice · · 2026-10-01 05:55 UTC
- parent
- d57c6e5
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +72 -0
A event_test.go +72 -0
| @@ -0,0 +1,72 @@ | |||
| 1 | + | package wlturbo | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "bytes" | |
| 5 | + | "testing" | |
| 6 | + | ) | |
| 7 | + | ||
| 8 | + | func TestEventDecoders(t *testing.T) { | |
| 9 | + | tests := []struct { | |
| 10 | + | name string | |
| 11 | + | data []byte | |
| 12 | + | offset int | |
| 13 | + | read func(*Event) any | |
| 14 | + | want any | |
| 15 | + | wantOffset int | |
| 16 | + | }{ | |
| 17 | + | {"int32 negative", []byte{0xfe, 0xff, 0xff, 0xff}, 0, | |
| 18 | + | func(e *Event) any { return e.Int32() }, int32(-2), 4}, | |
| 19 | + | {"int32 min", []byte{0, 0, 0, 0x80}, 0, | |
| 20 | + | func(e *Event) any { return e.Int32() }, int32(-1 << 31), 4}, | |
| 21 | + | {"int32 at offset", []byte{9, 9, 9, 9, 7, 0, 0, 0}, 4, | |
| 22 | + | func(e *Event) any { return e.Int32() }, int32(7), 8}, | |
| 23 | + | {"int32 truncated", []byte{1, 2, 3}, 0, | |
| 24 | + | func(e *Event) any { return e.Int32() }, int32(0), 0}, | |
| 25 | + | {"fixed positive", []byte{0x80, 0x01, 0, 0}, 0, // 384/256 = 1.5 | |
| 26 | + | func(e *Event) any { return e.Fixed() }, Fixed(384), 4}, | |
| 27 | + | {"fixed negative", []byte{0x00, 0xff, 0xff, 0xff}, 0, // -256/256 = -1 | |
| 28 | + | func(e *Event) any { return e.Fixed() }, Fixed(-256), 4}, | |
| 29 | + | {"fixed zero", []byte{0, 0, 0, 0}, 0, | |
| 30 | + | func(e *Event) any { return e.Fixed() }, Fixed(0), 4}, | |
| 31 | + | {"fixed truncated", []byte{1}, 0, | |
| 32 | + | func(e *Event) any { return e.Fixed() }, Fixed(0), 0}, | |
| 33 | + | {"array aligned", []byte{4, 0, 0, 0, 1, 2, 3, 4, 0xee}, 0, | |
| 34 | + | func(e *Event) any { return e.Array() }, []byte{1, 2, 3, 4}, 8}, | |
| 35 | + | {"array padded", []byte{5, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0xee}, 0, | |
| 36 | + | func(e *Event) any { return e.Array() }, []byte{1, 2, 3, 4, 5}, 12}, | |
| 37 | + | {"array zero length", []byte{0, 0, 0, 0, 0xee}, 0, | |
| 38 | + | func(e *Event) any { return e.Array() }, []byte(nil), 4}, | |
| 39 | + | {"array length past end", []byte{9, 0, 0, 0, 1, 2, 3, 4}, 0, | |
| 40 | + | func(e *Event) any { return e.Array() }, []byte(nil), 4}, | |
| 41 | + | {"array truncated header", []byte{4, 0, 0}, 0, | |
| 42 | + | func(e *Event) any { return e.Array() }, []byte(nil), 0}, | |
| 43 | + | {"array at offset", []byte{0xaa, 0xaa, 0xaa, 0xaa, 2, 0, 0, 0, 7, 8, 0, 0}, 4, | |
| 44 | + | func(e *Event) any { return e.Array() }, []byte{7, 8}, 12}, | |
| 45 | + | } | |
| 46 | + | for _, tt := range tests { | |
| 47 | + | t.Run(tt.name, func(t *testing.T) { | |
| 48 | + | e := &Event{data: tt.data, offset: tt.offset} | |
| 49 | + | got := tt.read(e) | |
| 50 | + | if b, ok := tt.want.([]byte); ok { | |
| 51 | + | if !bytes.Equal(got.([]byte), b) || (got.([]byte) == nil) != (b == nil) { | |
| 52 | + | t.Fatalf("got %v, want %v", got, b) | |
| 53 | + | } | |
| 54 | + | } else if got != tt.want { | |
| 55 | + | t.Fatalf("got %v (%T), want %v (%T)", got, got, tt.want, tt.want) | |
| 56 | + | } | |
| 57 | + | if e.offset != tt.wantOffset { | |
| 58 | + | t.Fatalf("offset %d, want %d", e.offset, tt.wantOffset) | |
| 59 | + | } | |
| 60 | + | }) | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | t.Run("array is a copy", func(t *testing.T) { | |
| 64 | + | data := []byte{4, 0, 0, 0, 1, 2, 3, 4} | |
| 65 | + | e := &Event{data: data} | |
| 66 | + | got := e.Array() | |
| 67 | + | data[4] = 0xff | |
| 68 | + | if got[0] != 1 { | |
| 69 | + | t.Fatal("Array aliases the receive buffer") | |
| 70 | + | } | |
| 71 | + | }) | |
| 72 | + | } | |
| @@ -0,0 +1,72 @@ | |||||
| 1 | + | package wlturbo | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "bytes" | |||
| 5 | + | "testing" | |||
| 6 | + | ) | |||
| 7 | + | ||||
| 8 | + | func TestEventDecoders(t *testing.T) { | |||
| 9 | + | tests := []struct { | |||
| 10 | + | name string | |||
| 11 | + | data []byte | |||
| 12 | + | offset int | |||
| 13 | + | read func(*Event) any | |||
| 14 | + | want any | |||
| 15 | + | wantOffset int | |||
| 16 | + | }{ | |||
| 17 | + | {"int32 negative", []byte{0xfe, 0xff, 0xff, 0xff}, 0, | |||
| 18 | + | func(e *Event) any { return e.Int32() }, int32(-2), 4}, | |||
| 19 | + | {"int32 min", []byte{0, 0, 0, 0x80}, 0, | |||
| 20 | + | func(e *Event) any { return e.Int32() }, int32(-1 << 31), 4}, | |||
| 21 | + | {"int32 at offset", []byte{9, 9, 9, 9, 7, 0, 0, 0}, 4, | |||
| 22 | + | func(e *Event) any { return e.Int32() }, int32(7), 8}, | |||
| 23 | + | {"int32 truncated", []byte{1, 2, 3}, 0, | |||
| 24 | + | func(e *Event) any { return e.Int32() }, int32(0), 0}, | |||
| 25 | + | {"fixed positive", []byte{0x80, 0x01, 0, 0}, 0, // 384/256 = 1.5 | |||
| 26 | + | func(e *Event) any { return e.Fixed() }, Fixed(384), 4}, | |||
| 27 | + | {"fixed negative", []byte{0x00, 0xff, 0xff, 0xff}, 0, // -256/256 = -1 | |||
| 28 | + | func(e *Event) any { return e.Fixed() }, Fixed(-256), 4}, | |||
| 29 | + | {"fixed zero", []byte{0, 0, 0, 0}, 0, | |||
| 30 | + | func(e *Event) any { return e.Fixed() }, Fixed(0), 4}, | |||
| 31 | + | {"fixed truncated", []byte{1}, 0, | |||
| 32 | + | func(e *Event) any { return e.Fixed() }, Fixed(0), 0}, | |||
| 33 | + | {"array aligned", []byte{4, 0, 0, 0, 1, 2, 3, 4, 0xee}, 0, | |||
| 34 | + | func(e *Event) any { return e.Array() }, []byte{1, 2, 3, 4}, 8}, | |||
| 35 | + | {"array padded", []byte{5, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0xee}, 0, | |||
| 36 | + | func(e *Event) any { return e.Array() }, []byte{1, 2, 3, 4, 5}, 12}, | |||
| 37 | + | {"array zero length", []byte{0, 0, 0, 0, 0xee}, 0, | |||
| 38 | + | func(e *Event) any { return e.Array() }, []byte(nil), 4}, | |||
| 39 | + | {"array length past end", []byte{9, 0, 0, 0, 1, 2, 3, 4}, 0, | |||
| 40 | + | func(e *Event) any { return e.Array() }, []byte(nil), 4}, | |||
| 41 | + | {"array truncated header", []byte{4, 0, 0}, 0, | |||
| 42 | + | func(e *Event) any { return e.Array() }, []byte(nil), 0}, | |||
| 43 | + | {"array at offset", []byte{0xaa, 0xaa, 0xaa, 0xaa, 2, 0, 0, 0, 7, 8, 0, 0}, 4, | |||
| 44 | + | func(e *Event) any { return e.Array() }, []byte{7, 8}, 12}, | |||
| 45 | + | } | |||
| 46 | + | for _, tt := range tests { | |||
| 47 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 48 | + | e := &Event{data: tt.data, offset: tt.offset} | |||
| 49 | + | got := tt.read(e) | |||
| 50 | + | if b, ok := tt.want.([]byte); ok { | |||
| 51 | + | if !bytes.Equal(got.([]byte), b) || (got.([]byte) == nil) != (b == nil) { | |||
| 52 | + | t.Fatalf("got %v, want %v", got, b) | |||
| 53 | + | } | |||
| 54 | + | } else if got != tt.want { | |||
| 55 | + | t.Fatalf("got %v (%T), want %v (%T)", got, got, tt.want, tt.want) | |||
| 56 | + | } | |||
| 57 | + | if e.offset != tt.wantOffset { | |||
| 58 | + | t.Fatalf("offset %d, want %d", e.offset, tt.wantOffset) | |||
| 59 | + | } | |||
| 60 | + | }) | |||
| 61 | + | } | |||
| 62 | + | ||||
| 63 | + | t.Run("array is a copy", func(t *testing.T) { | |||
| 64 | + | data := []byte{4, 0, 0, 0, 1, 2, 3, 4} | |||
| 65 | + | e := &Event{data: data} | |||
| 66 | + | got := e.Array() | |||
| 67 | + | data[4] = 0xff | |||
| 68 | + | if got[0] != 1 { | |||
| 69 | + | t.Fatal("Array aliases the receive buffer") | |||
| 70 | + | } | |||
| 71 | + | }) | |||
| 72 | + | } | |||