Skip to content

Commit c2f1c61

Browse files
committed
fix: resolve lint findings in streaming tests and example
Extract the per-plan chunked run in TestStreamingEquivalence_Float64 into a helper to drop cognitive complexity below 50; replace append([]float64(nil), x...) with slices.Clone(x) in the streaming and flush-lifecycle tests. In the streaming example, name the loop count and tone constants, use range-over-int and max(), and start the FIFO zero-length with capacity so the priming zeros are appended (satisfies makezero).
1 parent e414938 commit c2f1c61

3 files changed

Lines changed: 59 additions & 48 deletions

File tree

examples/streaming/main.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ import (
2121

2222
func main() {
2323
const (
24-
inRate = 44100.0
25-
outRate = 48000.0
26-
outFrames = 512
24+
inRate = 44100.0
25+
outRate = 48000.0
26+
outFrames = 512
27+
callbacks = 100
28+
toneAmplitude = 0.5
29+
toneHz = 997.0
2730
)
2831

2932
rs, err := resampler.NewEngineFloat32(inRate, outRate, resampler.QualityHigh)
@@ -34,12 +37,14 @@ func main() {
3437

3538
// Prime the FIFO with the startup deficit so the first callbacks are
3639
// fed. This trades Latency() samples of leading silence for a steady
37-
// pipeline.
38-
fifo := make([]float32, rs.Latency())
40+
// pipeline. The FIFO starts empty with headroom, then the priming zeros
41+
// are appended so later appends grow a zero-length-origin slice.
42+
fifo := make([]float32, 0, rs.Latency()+2*outFrames)
43+
fifo = append(fifo, make([]float32, rs.Latency())...)
3944

4045
phase := 0.0
4146
firstCall := true
42-
for callback := 0; callback < 100; callback++ {
47+
for callback := range callbacks {
4348
// Size the input chunk from the FIFO's current deficit rather than
4449
// a fixed count. A fixed input size drifts against a fixed output
4550
// size whenever ratio does not divide outFrames evenly: truncating
@@ -64,10 +69,7 @@ func main() {
6469
// below tolerates that warmup.
6570
need := outFrames
6671
if !firstCall {
67-
need = outFrames - len(fifo)
68-
if need < 0 {
69-
need = 0
70-
}
72+
need = max(outFrames-len(fifo), 0)
7173
}
7274
firstCall = false
7375
inFrames := int(math.Ceil(float64(need) / ratio))
@@ -76,8 +78,8 @@ func main() {
7678
// reuse a single scratch buffer instead of allocating each call.
7779
in := make([]float32, inFrames)
7880
for i := range in {
79-
in[i] = float32(0.5 * math.Sin(phase))
80-
phase += 2 * math.Pi * 997 / inRate
81+
in[i] = float32(toneAmplitude * math.Sin(phase))
82+
phase += 2 * math.Pi * toneHz / inRate
8183
}
8284
out, err := rs.Process(in)
8385
if err != nil {

flush_lifecycle_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package resampler
55

66
import (
77
"math"
8+
"slices"
89
"testing"
910
)
1011

@@ -56,11 +57,11 @@ func TestFlushLifecycle(t *testing.T) {
5657
t.Fatal(err)
5758
}
5859
chunk := sineChunk(4410, c.in)
59-
gotAfterFlush, err := r.Process(append([]float64(nil), chunk...))
60+
gotAfterFlush, err := r.Process(slices.Clone(chunk))
6061
if err != nil {
6162
t.Fatal(err)
6263
}
63-
gotFresh, err := fresh.Process(append([]float64(nil), chunk...))
64+
gotFresh, err := fresh.Process(slices.Clone(chunk))
6465
if err != nil {
6566
t.Fatal(err)
6667
}

streaming_equivalence_test.go

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestStreamingEquivalence_Float64(t *testing.T) {
4141
if err != nil {
4242
t.Fatalf("%s: NewEngine: %v", rr.name, err)
4343
}
44-
ref, err := oneShot.Process(append([]float64(nil), input...))
44+
ref, err := oneShot.Process(slices.Clone(input))
4545
if err != nil {
4646
t.Fatalf("%s: Process: %v", rr.name, err)
4747
}
@@ -52,44 +52,52 @@ func TestStreamingEquivalence_Float64(t *testing.T) {
5252
ref = append(ref, refTail...)
5353

5454
for pi, plan := range chunkPlans {
55-
chunked, err := NewEngine(rr.in, rr.out, q)
56-
if err != nil {
57-
t.Fatalf("%s: NewEngine: %v", rr.name, err)
58-
}
59-
var got []float64
60-
rng := rand.New(rand.NewSource(int64(pi) + 1))
61-
pos := 0
62-
for pos < n {
63-
size := plan[rng.Intn(len(plan))]
64-
if pos+size > n {
65-
size = n - pos
66-
}
67-
out, err := chunked.Process(append([]float64(nil), input[pos:pos+size]...))
68-
if err != nil {
69-
t.Fatalf("%s plan %d: Process: %v", rr.name, pi, err)
70-
}
71-
got = append(got, out...)
72-
pos += size
73-
}
74-
tail, err := chunked.Flush()
75-
if err != nil {
76-
t.Fatalf("%s plan %d: Flush: %v", rr.name, pi, err)
77-
}
78-
got = append(got, tail...)
79-
80-
if len(got) != len(ref) {
81-
t.Fatalf("%s q=%v plan %d: length %d != one-shot %d", rr.name, q, pi, len(got), len(ref))
82-
}
83-
for i := range got {
84-
if got[i] != ref[i] {
85-
t.Fatalf("%s q=%v plan %d: sample %d differs: %g != %g", rr.name, q, pi, i, got[i], ref[i])
86-
}
87-
}
55+
assertChunkedPlanEqualsFloat64(t, rr.name, rr.in, rr.out, q, pi, plan, input, ref)
8856
}
8957
}
9058
}
9159
}
9260

61+
// assertChunkedPlanEqualsFloat64 feeds input through a fresh engine in
62+
// pseudo-random chunk sizes drawn from plan (seeded by pi for reproducibility),
63+
// Flushes once, and asserts the result is bit-exact with ref.
64+
func assertChunkedPlanEqualsFloat64(t *testing.T, name string, in, out float64, q QualityPreset, pi int, plan []int, input, ref []float64) {
65+
t.Helper()
66+
67+
chunked, err := NewEngine(in, out, q)
68+
if err != nil {
69+
t.Fatalf("%s: NewEngine: %v", name, err)
70+
}
71+
var got []float64
72+
rng := rand.New(rand.NewSource(int64(pi) + 1))
73+
for pos := 0; pos < len(input); {
74+
size := plan[rng.Intn(len(plan))]
75+
if pos+size > len(input) {
76+
size = len(input) - pos
77+
}
78+
outChunk, err := chunked.Process(slices.Clone(input[pos : pos+size]))
79+
if err != nil {
80+
t.Fatalf("%s plan %d: Process: %v", name, pi, err)
81+
}
82+
got = append(got, outChunk...)
83+
pos += size
84+
}
85+
tail, err := chunked.Flush()
86+
if err != nil {
87+
t.Fatalf("%s plan %d: Flush: %v", name, pi, err)
88+
}
89+
got = append(got, tail...)
90+
91+
if len(got) != len(ref) {
92+
t.Fatalf("%s q=%v plan %d: length %d != one-shot %d", name, q, pi, len(got), len(ref))
93+
}
94+
for i := range got {
95+
if got[i] != ref[i] {
96+
t.Fatalf("%s q=%v plan %d: sample %d differs: %g != %g", name, q, pi, i, got[i], ref[i])
97+
}
98+
}
99+
}
100+
93101
func TestStreamingEquivalence_Float32(t *testing.T) {
94102
// Broadened beyond the single issue #51 configuration: an upsample and a
95103
// downsample ratio, two qualities, and two fixed chunk sizes. Chunked

0 commit comments

Comments
 (0)