Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that Unmarshal doesn't do any allocations #267

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codecs/av1_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestAV1_Unmarshal_Error(t *testing.T) {
}

func TestAV1_Unmarshal(t *testing.T) {
// nolint: dupl
av1Payload := []byte{
0x68, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x2c,
0xd6, 0xd3, 0x0c, 0xd5, 0x02, 0x00, 0x80,
Expand Down
227 changes: 227 additions & 0 deletions codecs/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,230 @@ func TestCommon_Min(t *testing.T) {
t.Fatal("Error: 3 == 3")
}
}

func TestZeroAllocations(t *testing.T) {
type unmarshaller interface {
Unmarshal(data []byte) ([]byte, error)
}
type tst struct {
packet unmarshaller
data []byte
}
tests := []tst{
{
packet: &VP8Packet{},
data: []byte{
0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x90,
},
}, {
packet: &VP9Packet{},
data: []byte{0xA0, 0x02, 0x23, 0x01, 0xAA},
}, {
packet: &H264Packet{},
data: []byte{
0x78, 0x00, 0x0f, 0x67,
0x42, 0xc0, 0x1f, 0x1a,
0x32, 0x35, 0x01, 0x40,
0x7a, 0x40, 0x3c, 0x22,
0x11, 0xa8, 0x00, 0x05,
0x68, 0x1a, 0x34, 0xe3,
0xc8,
},
}, {
packet: &AV1Packet{},
// nolint: dupl
data: []byte{
0x68, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x2c,
0xd6, 0xd3, 0x0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
0xd0, 0xca, 0x5d, 0x07, 0xb9, 0xb2, 0xa9,
0xa9, 0xee, 0x42, 0x33, 0x93, 0x21, 0x30,
0x5e, 0xd2, 0x58, 0xfd, 0xdd, 0x73, 0x0d,
0xb2, 0x93, 0x58, 0x77, 0x78, 0x40, 0x69,
0xba, 0x3c, 0x95, 0x1c, 0x61, 0xc6, 0xc6,
0x97, 0x1c, 0xef, 0x4d, 0x91, 0x0a, 0x42,
0x91, 0x1d, 0x14, 0x93, 0xf5, 0x78, 0x41,
0x32, 0x8a, 0x0a, 0x43, 0xd4, 0x3e, 0x6b,
0xb0, 0xd8, 0x0e, 0x04,
},
},
}

type zeroAllocation interface {
SetZeroAllocation(zeroAllocation bool)
}

for _, test := range tests {
allocs := testing.AllocsPerRun(10, func() {
if d, ok := test.packet.(zeroAllocation); ok {
d.SetZeroAllocation(true)
}
_, err := test.packet.Unmarshal(test.data)
if err != nil {
t.Errorf("Unmarshal failed: %v", err)
}
})

if allocs != 0 {
t.Errorf("%T: %v allocs", test.packet, allocs)
}
}
}
Loading