Commit 219eaf6a authored by Andreas Bigger's avatar Andreas Bigger

table-driven tests for the input threshold function

parent f208b022
...@@ -7,33 +7,96 @@ import ( ...@@ -7,33 +7,96 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// TestInputThreshold tests the [ChannelConfig.InputThreshold] function. // TestInputThreshold tests the [ChannelConfig.InputThreshold]
// function using a table-driven testing approach.
func TestInputThreshold(t *testing.T) { func TestInputThreshold(t *testing.T) {
// Construct an empty channel config type testInput struct {
config := batcher.ChannelConfig{ TargetFrameSize uint64
SeqWindowSize: 15, TargetNumFrames int
ChannelTimeout: 40, ApproxComprRatio float64
MaxChannelDuration: 1, }
SubSafetyMargin: 4, type test struct {
MaxFrameSize: 120000, input testInput
want uint64
}
// Construct test cases that test the boundary conditions
tests := []test{
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
},
want: 2,
},
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 1,
ApproxComprRatio: 1,
},
want: 1,
},
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 1,
ApproxComprRatio: 2,
},
want: 0,
},
{
input: testInput{
TargetFrameSize: 100000, TargetFrameSize: 100000,
TargetNumFrames: 1, TargetNumFrames: 1,
ApproxComprRatio: 0.4, ApproxComprRatio: 0.4,
},
want: 250_000,
},
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 100000,
ApproxComprRatio: 0.4,
},
want: 250_000,
},
{
input: testInput{
TargetFrameSize: 100000,
TargetNumFrames: 100000,
ApproxComprRatio: 0.4,
},
want: 25_000_000_000,
},
// A compression ratio of 0 means there is no input threshold
{
input: testInput{
TargetFrameSize: 100000,
TargetNumFrames: 100000,
ApproxComprRatio: 0,
},
want: uint64(0xffffffffffffffff),
},
{
input: testInput{
TargetFrameSize: 0,
TargetNumFrames: 0,
ApproxComprRatio: 0,
},
want: 0,
},
} }
// The input threshold is calculated as: (targetNumFrames * targetFrameSize) / approxComprRatio // Validate each test case
// Here we see that 100,000 / 0.4 = 100,000 * 2.5 = 250,000 for _, tt := range tests {
inputThreshold := config.InputThreshold() config := batcher.ChannelConfig{
require.Equal(t, uint64(250_000), inputThreshold) TargetFrameSize: tt.input.TargetFrameSize,
TargetNumFrames: tt.input.TargetNumFrames,
// Set the approximate compression ratio to 0 ApproxComprRatio: tt.input.ApproxComprRatio,
// Logically, this represents infinite compression, }
// so there is no threshold on the size of the input. got := config.InputThreshold()
// In practice, this should never be set to 0. require.Equal(t, tt.want, got)
config.ApproxComprRatio = 0 }
// The input threshold will overflow to the max uint64 value
receivedThreshold := config.InputThreshold()
max := config.TargetNumFrames * int(config.TargetFrameSize)
require.True(t, receivedThreshold > uint64(max))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment