diff --git a/.golangci.yml b/.golangci.yml index 88cb4fb..120faf2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,12 +19,16 @@ linters-settings: recommendations: - errors forbidigo: + analyze-types: true forbid: - ^fmt.Print(f|ln)?$ - ^log.(Panic|Fatal|Print)(f|ln)?$ - ^os.Exit$ - ^panic$ - ^print(ln)?$ + - p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$ + pkg: ^testing$ + msg: "use testify/assert instead" varnamelen: max-distance: 12 min-name-length: 2 @@ -127,9 +131,12 @@ issues: exclude-dirs-use-default: false exclude-rules: # Allow complex tests and examples, better to be self contained - - path: (examples|main\.go|_test\.go) + - path: (examples|main\.go) linters: + - gocognit - forbidigo + - path: _test\.go + linters: - gocognit # Allow forbidden identifiers in CLI commands diff --git a/decoder_test.go b/decoder_test.go index b8abcd1..528b5b8 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/pion/opus/pkg/oggreader" + "github.com/stretchr/testify/assert" ) // nolint: gochecknoglobals @@ -89,9 +90,7 @@ func TestTinyOgg(t *testing.T) { var out [1920]byte ogg, _, err := oggreader.NewWith(bytes.NewReader(tinyogg)) - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) decoder := NewDecoder() for { @@ -101,9 +100,7 @@ func TestTinyOgg(t *testing.T) { } else if bytes.HasPrefix(segments[0], []byte("OpusTags")) { continue } - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) for i := range segments { if _, _, err = decoder.Decode(segments[i], out[:]); err != nil { diff --git a/go.mod b/go.mod index 208f198..de23166 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/pion/opus go 1.20 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/bitdepth/bitdepth_test.go b/internal/bitdepth/bitdepth_test.go index 1fe8ceb..c2d6bb3 100644 --- a/internal/bitdepth/bitdepth_test.go +++ b/internal/bitdepth/bitdepth_test.go @@ -4,20 +4,15 @@ package bitdepth import ( - "bytes" "testing" + + "github.com/stretchr/testify/assert" ) func TestConvertFloat32LittleEndianToSigned16LittleEndian(t *testing.T) { in := []float32{0.3, 0, .55, .72, -.05} out := make([]byte, len(in)*2) - err := ConvertFloat32LittleEndianToSigned16LittleEndian(in, out, 1) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal([]byte{0x66, 0x26, 0x00, 0x00, 0x65, 0x46, 0x28, 0x5c, 0x99, 0xf9}, out) { - t.Fatal("buffer mismatch") - } + assert.NoError(t, ConvertFloat32LittleEndianToSigned16LittleEndian(in, out, 1)) + assert.Equal(t, []byte{0x66, 0x26, 0x00, 0x00, 0x65, 0x46, 0x28, 0x5c, 0x99, 0xf9}, out) } diff --git a/internal/rangecoding/decoder_test.go b/internal/rangecoding/decoder_test.go index 5028fdc..f14a784 100644 --- a/internal/rangecoding/decoder_test.go +++ b/internal/rangecoding/decoder_test.go @@ -5,6 +5,8 @@ package rangecoding import ( "testing" + + "github.com/stretchr/testify/assert" ) // nolint: gochecknoglobals @@ -132,148 +134,62 @@ func TestDecoder(t *testing.T) { // nolint:cyclop,gocyclo decoder := &Decoder{} decoder.Init([]byte{0x0b, 0xe4, 0xc1, 0x36, 0xec, 0xc5, 0x80}) - if result := decoder.DecodeSymbolLogP(0x1); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolLogP(0x1); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelFrameTypeInactive); result != 1 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelGainHighbits[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelGainLowbits); result != 6 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelGainDelta); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelGainDelta); result != 3 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelGainDelta); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS1[1][0]); result != 9 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[10]); result != 5 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[9]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfS2[8]); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLsfInterpolationOffset); result != 4 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelLcgSeed); result != 2 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelExcRate[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } - if result := decoder.DecodeSymbolWithICDF(silkModelPulseCount[0]); result != 0 { - t.Fatal("") - } + assert.Equal(t, uint32(0), decoder.DecodeSymbolLogP(0x1)) + assert.Equal(t, uint32(0), decoder.DecodeSymbolLogP(0x1)) + + assert.Equal(t, uint32(1), decoder.DecodeSymbolWithICDF(silkModelFrameTypeInactive)) + + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelGainHighbits[0])) + + assert.Equal(t, uint32(6), decoder.DecodeSymbolWithICDF(silkModelGainLowbits)) + + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelGainDelta)) + assert.Equal(t, uint32(3), decoder.DecodeSymbolWithICDF(silkModelGainDelta)) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelGainDelta)) + + assert.Equal(t, uint32(9), decoder.DecodeSymbolWithICDF(silkModelLsfS1[1][0])) + assert.Equal(t, uint32(5), decoder.DecodeSymbolWithICDF(silkModelLsfS2[10])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[9])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfS2[8])) + + assert.Equal(t, uint32(4), decoder.DecodeSymbolWithICDF(silkModelLsfInterpolationOffset)) + + assert.Equal(t, uint32(2), decoder.DecodeSymbolWithICDF(silkModelLcgSeed)) + + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelExcRate[0])) + + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) + assert.Equal(t, uint32(0), decoder.DecodeSymbolWithICDF(silkModelPulseCount[0])) } diff --git a/internal/silk/decoder_test.go b/internal/silk/decoder_test.go index 9d9f164..049cab8 100644 --- a/internal/silk/decoder_test.go +++ b/internal/silk/decoder_test.go @@ -4,11 +4,10 @@ package silk import ( - "errors" - "reflect" "testing" "github.com/pion/opus/internal/rangecoding" + "github.com/stretchr/testify/assert" ) const floatEqualityThreshold = 0.000001 @@ -41,56 +40,38 @@ func createRangeDecoder( func TestDecode20MsOnly(t *testing.T) { d := &Decoder{} - err := d.Decode(testSilkFrame(), []float32{}, false, 1, BandwidthWideband) - if !errors.Is(err, errUnsupportedSilkFrameDuration) { - t.Fatal(err) - } + assert.ErrorIs(t, errUnsupportedSilkFrameDuration, d.Decode(testSilkFrame(), []float32{}, false, 1, BandwidthWideband)) } func TestDecodeStereoTODO(t *testing.T) { d := &Decoder{} err := d.Decode(testSilkFrame(), []float32{}, true, nanoseconds20Ms, BandwidthWideband) - if !errors.Is(err, errUnsupportedSilkStereo) { - t.Fatal(err) - } + assert.ErrorIs(t, errUnsupportedSilkStereo, err) } func TestDecodeFrameType(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(testSilkFrame(), 31, 536870912, 437100388)} signalType, quantizationOffsetType := d.determineFrameType(false) - if signalType != frameSignalTypeInactive { - t.Fatal() - } - if quantizationOffsetType != frameQuantizationOffsetTypeHigh { - t.Fatal() - } + assert.Equal(t, frameSignalTypeInactive, signalType) + assert.Equal(t, frameQuantizationOffsetTypeHigh, quantizationOffsetType) } func TestDecodeSubframeQuantizations(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(testSilkFrame(), 31, 482344960, 437100388)} - - gainQ16 := d.decodeSubframeQuantizations(frameSignalTypeInactive) - if !reflect.DeepEqual(gainQ16, []float32{210944, 112640, 96256, 96256}) { - t.Fatal() - } + assert.Equal(t, []float32{210944, 112640, 96256, 96256}, d.decodeSubframeQuantizations(frameSignalTypeInactive)) } func TestDecodeBufferSize(t *testing.T) { d := NewDecoder() err := d.Decode([]byte{}, make([]float32, 50), false, nanoseconds20Ms, BandwidthWideband) - if !errors.Is(err, errOutBufferTooSmall) { - t.Fatal() - } + assert.Equal(t, errOutBufferTooSmall, err) } func TestNormalizeLineSpectralFrequencyStageOne(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(testSilkFrame(), 47, 722810880, 387065757)} - I1 := d.normalizeLineSpectralFrequencyStageOne(false, BandwidthWideband) - if I1 != 9 { - t.Fatal() - } + assert.Equal(t, uint32(9), d.normalizeLineSpectralFrequencyStageOne(false, BandwidthWideband)) } func TestNormalizeLSFStabilization(t *testing.T) { @@ -111,9 +92,7 @@ func TestNormalizeLSFStabilization(t *testing.T) { } decoder.normalizeLSFStabilization(in, 16, BandwidthWideband) - if !reflect.DeepEqual(in, expectedOut) { - t.Fatal() - } + assert.Equal(t, in, expectedOut) in = []int16{ 1533, 1674, 2506, 4374, 6630, @@ -126,29 +105,22 @@ func TestNormalizeLSFStabilization(t *testing.T) { 19355, 21645, 25228, 26972, 30360, 30363, } decoder.normalizeLSFStabilization(in, 16, BandwidthWideband) - if !reflect.DeepEqual(in, expectedOut) { - t.Fatal() - } + assert.Equal(t, in, expectedOut) } func TestNormalizeLineSpectralFrequencyStageTwo(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(testSilkFrame(), 47, 50822640, 5895957)} dLPC, resQ10 := d.normalizeLineSpectralFrequencyStageTwo(BandwidthWideband, 9) - if !reflect.DeepEqual(resQ10, testResQ10()) { - t.Fatal() - } else if dLPC != 16 { - t.Fatal() - } + assert.Equal(t, testResQ10(), resQ10) + assert.Equal(t, 16, dLPC) } func TestNormalizeLineSpectralFrequencyCoefficients(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(testSilkFrame(), 55, 493249168, 174371199)} nlsfQ1 := d.normalizeLineSpectralFrequencyCoefficients(16, BandwidthWideband, testResQ10(), 9) - if !reflect.DeepEqual(nlsfQ1, testNlsfQ1()) { - t.Fatal() - } + assert.Equal(t, testNlsfQ1(), nlsfQ1) } func TestNormalizeLSFInterpolation(t *testing.T) { @@ -157,9 +129,7 @@ func TestNormalizeLSFInterpolation(t *testing.T) { var expectedN1Q15 []int16 actualN1Q15, _ := d.normalizeLSFInterpolation(expectedN1Q15) - if !reflect.DeepEqual(actualN1Q15, expectedN1Q15) { - t.Fatal() - } + assert.Equal(t, expectedN1Q15, actualN1Q15) }) t.Run("wQ2 == 1", func(t *testing.T) { @@ -185,10 +155,7 @@ func TestNormalizeLSFInterpolation(t *testing.T) { } actualN2Q15, _ := decoder.normalizeLSFInterpolation(n2Q15) - - if !reflect.DeepEqual(actualN2Q15, expectedN1Q15) { - t.Fatal() - } + assert.Equal(t, expectedN1Q15, actualN2Q15) }) } @@ -206,9 +173,7 @@ func TestConvertNormalizedLSFsToLPCCoefficients(t *testing.T) { -3441, -3848, -4493, -1614, -1960, -3112, -2153, -2898, } - if !reflect.DeepEqual(decoder.convertNormalizedLSFsToLPCCoefficients(nlsfQ15, BandwidthWideband), expectedA32Q17) { - t.Fatal() - } + assert.Equal(t, expectedA32Q17, decoder.convertNormalizedLSFsToLPCCoefficients(nlsfQ15, BandwidthWideband)) } func TestLimitLPCCoefficientsRange(t *testing.T) { @@ -221,9 +186,7 @@ func TestLimitLPCCoefficientsRange(t *testing.T) { d.limitLPCCoefficientsRange(A32Q17) - if !reflect.DeepEqual(A32Q17, expectedLimited) { - t.Fatal() - } + assert.Equal(t, expectedLimited, A32Q17) } func TestExcitation(t *testing.T) { @@ -261,9 +224,7 @@ func TestExcitation(t *testing.T) { pulsecounts, lsbcounts := d.decodePulseAndLSBCounts(shellblocks, rateLevel) eRaw := d.decodeExcitation(frameSignalTypeUnvoiced, frameQuantizationOffsetTypeLow, lcgSeed, pulsecounts, lsbcounts) - if !reflect.DeepEqual(expected, eRaw) { - t.Fatal() - } + assert.Equal(t, expected, eRaw) } func TestLimitLPCFilterPredictionGain(t *testing.T) { @@ -280,9 +241,7 @@ func TestLimitLPCFilterPredictionGain(t *testing.T) { } aQ12 := d.limitLPCFilterPredictionGain(a32Q17) - if !reflect.DeepEqual(aQ12, expectedAQ12) { - t.Fatal() - } + assert.Equal(t, aQ12, expectedAQ12) } func TestLPCSynthesis(t *testing.T) { //nolint:lll @@ -446,9 +405,7 @@ func TestLPCSynthesis(t *testing.T) { //nolint:lll out := make([]float32, 80) decoder.lpcSynthesis(out, decoder.samplesInSubframe(BandwidthWideband), i, dLPC, aQ12, res, gainQ16, lpc) for j := range out { - if out[j]-expectedOut[i][j] > floatEqualityThreshold { - t.Fatalf("run(%d) index(%d) (%f) != (%f)", i, j, out[j], expectedOut[i][j]) - } + assert.Less(t, out[j]-expectedOut[i][j], float32(floatEqualityThreshold)) } } } @@ -462,13 +419,8 @@ func TestDecodePitchLags(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(silkFrame, 73, 30770362, 1380489)} lagMax, pitchLags, _ := d.decodePitchLags(frameSignalTypeVoiced, BandwidthWideband) - if lagMax != 288 { - t.Fatal() - } - - if !reflect.DeepEqual(pitchLags, []int{206, 206, 206, 206}) { - t.Fatal() - } + assert.Equal(t, uint32(288), lagMax) + assert.Equal(t, []int{206, 206, 206, 206}, pitchLags) } func TestDecodeLTPFilterCoefficients(t *testing.T) { @@ -480,14 +432,12 @@ func TestDecodeLTPFilterCoefficients(t *testing.T) { d := &Decoder{rangeDecoder: createRangeDecoder(silkFrame, 89, 253853952, 138203876)} bQ7 := d.decodeLTPFilterCoefficients(frameSignalTypeVoiced) - if !reflect.DeepEqual(bQ7, [][]int8{ + assert.Equal(t, [][]int8{ {1, 1, 8, 1, 1}, {2, 0, 77, 11, 9}, {1, 1, 8, 1, 1}, {-1, 36, 64, 27, -6}, - }) { - t.Fatal() - } + }, bQ7) } func TestDecodeLTPScalingParameter(t *testing.T) { @@ -498,17 +448,12 @@ func TestDecodeLTPScalingParameter(t *testing.T) { 0x6b, 0x71, 0xa4, 0x03, 0x70, 0xbf, } d := &Decoder{rangeDecoder: createRangeDecoder(silkFrame, 105, 160412192, 164623240)} - - if d.decodeLTPScalingParamater(frameSignalTypeVoiced) != 15565.0 { - t.Fatal() - } + assert.Equal(t, float32(15565.0), d.decodeLTPScalingParamater(frameSignalTypeVoiced)) }) t.Run("Unvoiced", func(t *testing.T) { d := &Decoder{} - if d.decodeLTPScalingParamater(frameSignalTypeUnvoiced) != 15565.0 { - t.Fatal() - } + assert.Equal(t, float32(15565.0), d.decodeLTPScalingParamater(frameSignalTypeUnvoiced)) }) } @@ -520,16 +465,12 @@ func TestDecode(t *testing.T) { t.Helper() for i := range expectedOut { - if out[i]-expectedOut[i] > floatEqualityThreshold { - t.Fatalf("%d (%f) != (%f)", i, out[i], expectedOut[i]) - } + assert.Less(t, out[i]-expectedOut[i], float32(floatEqualityThreshold)) } } t.Run("Unvoiced Single Frame", func(t *testing.T) { - if err := decoder.Decode(testSilkFrame(), out, false, nanoseconds20Ms, BandwidthWideband); err != nil { - t.Fatal(err) - } + assert.NoError(t, decoder.Decode(testSilkFrame(), out, false, nanoseconds20Ms, BandwidthWideband)) // nolint: dupl expectedOut := []float32{ @@ -602,15 +543,13 @@ func TestDecode(t *testing.T) { }) t.Run("Unvoiced Subsequent Frame", func(t *testing.T) { - if err := decoder.Decode( + assert.NoError(t, decoder.Decode( []byte{0x07, 0xc9, 0x72, 0x27, 0xe1, 0x44, 0xea, 0x50}, out, false, nanoseconds20Ms, BandwidthWideband, - ); err != nil { - t.Fatal(err) - } + )) expectedOut := []float32{ 0.000011, -0.000009, -0.000011, -0.000012, 0.000009, diff --git a/internal/silk/silk_test.go b/internal/silk/silk_test.go index 003f5b1..25022fb 100644 --- a/internal/silk/silk_test.go +++ b/internal/silk/silk_test.go @@ -3,34 +3,18 @@ package silk -import "testing" +import ( + "testing" -func TestIlog(t *testing.T) { - if ilog(-1) != 0 { - t.Fatal() - } - - if ilog(0) != 0 { - t.Fatal() - } - - if ilog(1) != 1 { - t.Fatal() - } - - if ilog(2) != 2 { - t.Fatal() - } + "github.com/stretchr/testify/assert" +) - if ilog(3) != 2 { - t.Fatal() - } - - if ilog(4) != 3 { - t.Fatal() - } - - if ilog(7) != 3 { - t.Fatal() - } +func TestIlog(t *testing.T) { + assert.Equal(t, 0, ilog(-1)) + assert.Equal(t, 0, ilog(0)) + assert.Equal(t, 1, ilog(1)) + assert.Equal(t, 2, ilog(2)) + assert.Equal(t, 2, ilog(3)) + assert.Equal(t, 3, ilog(4)) + assert.Equal(t, 3, ilog(7)) } diff --git a/pkg/oggreader/oggreader_test.go b/pkg/oggreader/oggreader_test.go index 1e5e3e4..fdcfff2 100644 --- a/pkg/oggreader/oggreader_test.go +++ b/pkg/oggreader/oggreader_test.go @@ -5,10 +5,10 @@ package oggreader import ( "bytes" - "errors" "io" - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) // buildOggFile generates a valid oggfile that can @@ -28,58 +28,38 @@ func buildOggContainer() []byte { func TestOggReader_ParseValidHeader(t *testing.T) { reader, header, err := NewWith(bytes.NewReader(buildOggContainer())) - switch { - case err != nil: - t.Fatal() - case reader == nil: - t.Fatal() - case header == nil: - t.Fatal() - case !reflect.DeepEqual(header.ChannelMap, uint8(0)): - t.Fatal() - case !reflect.DeepEqual(header.Channels, uint8(2)): - t.Fatal() - case !reflect.DeepEqual(header.OutputGain, uint16(0)): - t.Fatal() - case !reflect.DeepEqual(header.PreSkip, uint16(0xf00)): - t.Fatal() - case !reflect.DeepEqual(header.SampleRate, uint32(48000)): - t.Fatal() - case !reflect.DeepEqual(header.Version, uint8(1)): - t.Fatal() - } + assert.NoError(t, err) + assert.NotNil(t, reader) + + assert.Equal(t, &OggHeader{ + ChannelMap: 0x0, + Channels: 0x2, + OutputGain: 0x0, + PreSkip: 0xf00, + SampleRate: 0xbb80, + Version: 0x1, + }, + header) } func TestOggReader_ParseNextPage(t *testing.T) { ogg := bytes.NewReader(buildOggContainer()) reader, _, err := NewWith(ogg) - switch { - case err != nil: - t.Fatal() - case reader == nil: - t.Fatal() - } + assert.NoError(t, err) + assert.NotNil(t, reader) payload, _, err := reader.ParseNextPage() - switch { - case err != nil: - t.Fatal() - case !reflect.DeepEqual([][]byte{{0x98, 0x36, 0xbe, 0x88, 0x9e}}, payload): - t.Fatal() - } + assert.NoError(t, err) + assert.Equal(t, [][]byte{{0x98, 0x36, 0xbe, 0x88, 0x9e}}, payload) _, _, err = reader.ParseNextPage() - if !errors.Is(err, io.EOF) { - t.Fatal() - } + assert.ErrorIs(t, io.EOF, err) } func TestOggReader_ParseErrors(t *testing.T) { t.Run("Assert that Reader isn't nil", func(t *testing.T) { _, _, err := NewWith(nil) - if !errors.Is(err, errNilStream) { - t.Fatal() - } + assert.ErrorIs(t, errNilStream, err) }) t.Run("Invalid ID Page Header Signature", func(t *testing.T) { @@ -87,9 +67,7 @@ func TestOggReader_ParseErrors(t *testing.T) { ogg[0] = 0 _, _, err := newWith(bytes.NewReader(ogg), false) - if !errors.Is(err, errBadIDPageSignature) { - t.Fatal() - } + assert.ErrorIs(t, errBadIDPageSignature, err) }) t.Run("Invalid ID Page Header Type", func(t *testing.T) { @@ -97,9 +75,7 @@ func TestOggReader_ParseErrors(t *testing.T) { ogg[5] = 0 _, _, err := newWith(bytes.NewReader(ogg), false) - if !errors.Is(err, errBadIDPageType) { - t.Fatal() - } + assert.ErrorIs(t, errBadIDPageType, err) }) t.Run("Invalid ID Page Payload Length", func(t *testing.T) { @@ -107,9 +83,7 @@ func TestOggReader_ParseErrors(t *testing.T) { ogg[27] = 0 _, _, err := newWith(bytes.NewReader(ogg), false) - if !errors.Is(err, errBadIDPageLength) { - t.Fatal() - } + assert.ErrorIs(t, errBadIDPageLength, err) }) t.Run("Invalid ID Page Payload Length", func(t *testing.T) { @@ -117,9 +91,7 @@ func TestOggReader_ParseErrors(t *testing.T) { ogg[35] = 0 _, _, err := newWith(bytes.NewReader(ogg), false) - if !errors.Is(err, errBadIDPagePayloadSignature) { - t.Fatal() - } + assert.ErrorIs(t, errBadIDPagePayloadSignature, err) }) t.Run("Invalid Page Checksum", func(t *testing.T) { @@ -127,8 +99,6 @@ func TestOggReader_ParseErrors(t *testing.T) { ogg[22] = 0 _, _, err := NewWith(bytes.NewReader(ogg)) - if !errors.Is(err, errChecksumMismatch) { - t.Fatal() - } + assert.ErrorIs(t, errChecksumMismatch, err) }) }