-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathautotune_test.go
More file actions
583 lines (492 loc) · 17.7 KB
/
autotune_test.go
File metadata and controls
583 lines (492 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// The MIT License (MIT)
//
// Copyright (c) 2015 xtaci
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package kcp
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAutoTune(t *testing.T) {
// Group1
signals := []uint32{0, 0, 0, 0, 0, 0}
testGroup(t, 1, signals, -1, -1)
// Group2
signals = []uint32{0, 1, 0, 1, 0, 1}
testGroup(t, 2, signals, 1, 1)
// Group3
signals = []uint32{1, 0, 1, 0, 0, 1}
testGroup(t, 3, signals, 1, 1)
// Group4
signals = []uint32{1, 0, 0, 0, 0, 1}
testGroup(t, 4, signals, 4, -1)
// Group5
signals = []uint32{1, 1, 1, 1, 1, 1}
testGroup(t, 5, signals, -1, -1)
// Group6
signals = []uint32{1, 1, 0, 1, 1, 0}
testGroup(t, 6, signals, 1, 2)
// Group7
signals = []uint32{0, 1, 1, 1, 0, 1}
testGroup(t, 7, signals, 1, 3)
// Group8
signals = []uint32{1, 1, 1, 1, 1, 1}
testGroup(t, 8, signals, -1, -1)
// Group9
signals = []uint32{0, 1, 1, 1, 1, 0}
testGroup(t, 9, signals, -1, 4)
// Group10
signals = []uint32{0, 0, 1, 1, 0, 0}
testGroup(t, 10, signals, -1, 2)
// Group11
signals = []uint32{0, 0, 0, 1, 1, 1}
testGroup(t, 11, signals, -1, -1)
// Group12
signals = []uint32{0, 0, 0, 0, 0, 1}
testGroup(t, 12, signals, -1, -1)
// Group13
signals = []uint32{1, 0, 0, 0, 0, 1}
testGroup(t, 13, signals, 4, -1)
// Group14
signals = []uint32{1, 0, 0, 0, 0, 0}
testGroup(t, 14, signals, -1, -1)
}
func TestAutoTuneEdge(t *testing.T) {
// Edge Case0: Empty signals
signals := []uint32{}
testGroup(t, 0, signals, -1, -1)
// Edge Case: 1 signal
signals = []uint32{1}
testGroup(t, 2, signals, -1, -1)
// Edge Case: 2 signals
signals = []uint32{1, 0}
testGroup(t, 3, signals, -1, -1)
// Edge Case: 3 signals
signals = []uint32{1, 0, 1}
testGroup(t, 4, signals, 1, -1)
}
func testGroup(t *testing.T, gid int, signals []uint32, expectedFalse, expectedTrue int) {
tune := autoTune{}
for i, signal := range signals {
if signal == 0 {
tune.Sample(false, uint32(i))
} else {
tune.Sample(true, uint32(i))
}
}
t.Log("Group#", gid, signals, tune.FindPeriod(false), tune.FindPeriod(true))
assert.Equal(t, expectedTrue, tune.FindPeriod(true))
assert.Equal(t, expectedFalse, tune.FindPeriod(false))
}
func TestAutoTuneOverflow(t *testing.T) {
// minimal test
tune := autoTune{}
for i := range 1024 {
if i%maxAutoTuneSamples == 0 {
tune.Sample(false, uint32(i))
} else {
tune.Sample(true, uint32(i))
}
assert.LessOrEqual(t, tune.count, maxAutoTuneSamples)
}
assert.Equal(t, maxAutoTuneSamples, tune.count)
}
func TestAutoTunePop(t *testing.T) {
signals := []uint32{0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0}
tune := autoTune{}
for i, signal := range signals {
if signal == 0 {
tune.Sample(false, uint32(i))
} else {
tune.Sample(true, uint32(i))
}
}
assert.Equal(t, 2, tune.FindPeriod(false))
assert.Equal(t, 2, tune.FindPeriod(true))
// Simulate pop by advancing head (removes oldest element)
tune.head = (tune.head + 1) % maxAutoTuneSamples
tune.count--
assert.Equal(t, 2, tune.FindPeriod(false))
assert.Equal(t, 1, tune.FindPeriod(true))
// after popping more
tune.head = (tune.head + 1) % maxAutoTuneSamples
tune.count--
tune.head = (tune.head + 1) % maxAutoTuneSamples
tune.count--
tune.head = (tune.head + 1) % maxAutoTuneSamples
tune.count--
assert.Equal(t, 1, tune.FindPeriod(false))
assert.Equal(t, 1, tune.FindPeriod(true))
}
// TestAutoTuneRingBufferWrapAround tests that the ring buffer correctly handles
// wrap-around scenarios and still finds the correct period after many overwrites.
func TestAutoTuneRingBufferWrapAround(t *testing.T) {
// Test with various periods after buffer wrap-around
testCases := []struct {
name string
period int // period of the signal
totalSamples int // total samples to generate (should exceed maxAutoTuneSamples)
expectedPeriod int // expected period to be found
bit bool // which bit's period to find
}{
{"Period3_2xBuffer", 3, maxAutoTuneSamples * 2, 3, true},
{"Period5_3xBuffer", 5, maxAutoTuneSamples * 3, 5, true},
{"Period7_4xBuffer", 7, maxAutoTuneSamples * 4, 7, true},
{"Period10_2xBuffer", 10, maxAutoTuneSamples * 2, 10, true},
{"Period3_2xBuffer_False", 3, maxAutoTuneSamples * 2, 3, false},
{"Period5_3xBuffer_False", 5, maxAutoTuneSamples * 3, 5, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tune := autoTune{}
period := tc.period
halfPeriod := period / 2
if halfPeriod == 0 {
halfPeriod = 1
}
// Generate periodic signal: N samples of 1, then N samples of 0
for i := 0; i < tc.totalSamples; i++ {
posInPeriod := i % period
bit := posInPeriod < halfPeriod
tune.Sample(bit, uint32(i))
}
// Verify buffer is at maximum capacity
assert.Equal(t, maxAutoTuneSamples, tune.count)
// Find period and verify
foundPeriod := tune.FindPeriod(tc.bit)
t.Logf("Period=%d, TotalSamples=%d, Found=%d, Expected=%d",
tc.period, tc.totalSamples, foundPeriod, tc.expectedPeriod)
// The found period should match the expected period
// Note: Due to how period is calculated (right edge - left edge),
// the actual value depends on the signal pattern
assert.True(t, foundPeriod > 0, "Should find a valid period")
})
}
}
// TestAutoTuneStablePeriodAfterOverwrite tests that the period remains stable
// even after the ring buffer has been overwritten multiple times.
func TestAutoTuneStablePeriodAfterOverwrite(t *testing.T) {
tune := autoTune{}
period := 6 // total period
trueDuration := 3
// Track periods found during continuous sampling
var periodsFound []int
// Generate a lot of samples with a consistent period
totalSamples := maxAutoTuneSamples * 5
for i := range totalSamples {
posInPeriod := i % period
bit := posInPeriod < trueDuration
tune.Sample(bit, uint32(i))
// After buffer is full, periodically check the found period
if tune.count == maxAutoTuneSamples && i%50 == 0 {
p := tune.FindPeriod(true)
if p > 0 {
periodsFound = append(periodsFound, p)
}
}
}
// All found periods should be consistent (same value)
assert.NotEmpty(t, periodsFound, "Should have found periods during sampling")
// Check that periods are stable (all the same or very close)
if len(periodsFound) > 1 {
firstPeriod := periodsFound[0]
for i, p := range periodsFound {
assert.Equal(t, firstPeriod, p,
"Period at sample %d should match first period", i)
}
t.Logf("Found stable period %d across %d checks after buffer overwrite",
firstPeriod, len(periodsFound))
}
}
// TestAutoTuneVariousPeriodsFull tests various period lengths when buffer is full
func TestAutoTuneVariousPeriodsFull(t *testing.T) {
// Test different period values
periods := []int{2, 4, 6, 8, 10, 12, 16, 20, 32, 50}
for _, period := range periods {
t.Run("Period"+string(rune('0'+period/10))+string(rune('0'+period%10)), func(t *testing.T) {
tune := autoTune{}
trueDuration := period / 2
if trueDuration == 0 {
trueDuration = 1
}
// Fill buffer completely and then some more
totalSamples := maxAutoTuneSamples + 100
for i := range totalSamples {
posInPeriod := i % period
bit := posInPeriod < trueDuration
tune.Sample(bit, uint32(i))
}
assert.Equal(t, maxAutoTuneSamples, tune.count)
periodTrue := tune.FindPeriod(true)
periodFalse := tune.FindPeriod(false)
t.Logf("Period=%d, TrueDuration=%d, FoundTrue=%d, FoundFalse=%d",
period, trueDuration, periodTrue, periodFalse)
// Should find valid periods
assert.True(t, periodTrue > 0 || periodFalse > 0,
"Should find at least one valid period")
})
}
}
// TestAutoTuneContinuousOverwrite tests continuous overwrite scenarios
// to ensure the ring buffer correctly maintains period detection capability
func TestAutoTuneContinuousOverwrite(t *testing.T) {
tune := autoTune{}
// First phase: fill with period 4 signal
period1 := 4
for i := range maxAutoTuneSamples {
bit := (i % period1) < (period1 / 2)
tune.Sample(bit, uint32(i))
}
assert.Equal(t, maxAutoTuneSamples, tune.count)
p1 := tune.FindPeriod(true)
t.Logf("Phase 1: Period 4 signal, found period=%d", p1)
assert.True(t, p1 > 0, "Should find period in phase 1")
// Second phase: continue with period 6 signal
// After enough samples, the old period 4 data should be overwritten
period2 := 6
baseSeq := uint32(maxAutoTuneSamples)
for i := range maxAutoTuneSamples {
bit := (i % period2) < (period2 / 2)
tune.Sample(bit, baseSeq+uint32(i))
}
p2 := tune.FindPeriod(true)
t.Logf("Phase 2: Period 6 signal, found period=%d", p2)
assert.True(t, p2 > 0, "Should find period in phase 2")
// Third phase: continue with period 8 signal
period3 := 8
baseSeq = uint32(maxAutoTuneSamples * 2)
for i := range maxAutoTuneSamples {
bit := (i % period3) < (period3 / 2)
tune.Sample(bit, baseSeq+uint32(i))
}
p3 := tune.FindPeriod(true)
t.Logf("Phase 3: Period 8 signal, found period=%d", p3)
assert.True(t, p3 > 0, "Should find period in phase 3")
}
// TestAutoTunePeriodChangeAfterOverwrite tests that when signal pattern changes,
// the new period can be correctly detected after old data is fully overwritten
func TestAutoTunePeriodChangeAfterOverwrite(t *testing.T) {
testCases := []struct {
name string
oldTrueDuration int
oldFalseDuration int
newTrueDuration int
newFalseDuration int
expectedNewTrue int
expectedNewFalse int
}{
{"Period_2to4", 1, 1, 2, 2, 2, 2},
{"Period_4to2", 2, 2, 1, 1, 1, 1},
{"Period_4to8", 2, 2, 4, 4, 4, 4},
{"Period_8to4", 4, 4, 2, 2, 2, 2},
{"Period_6to10", 3, 3, 5, 5, 5, 5},
{"Period_10to6", 5, 5, 3, 3, 3, 3},
{"Period_3to7", 1, 2, 3, 4, 3, 4},
{"Period_Asym2T4F_to_5T3F", 2, 4, 5, 3, 5, 3},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tune := autoTune{}
oldPeriod := tc.oldTrueDuration + tc.oldFalseDuration
newPeriod := tc.newTrueDuration + tc.newFalseDuration
// Phase 1: Fill buffer completely with old period signal
for i := range maxAutoTuneSamples {
posInPeriod := i % oldPeriod
bit := posInPeriod < tc.oldTrueDuration
tune.Sample(bit, uint32(i))
}
oldFoundTrue := tune.FindPeriod(true)
oldFoundFalse := tune.FindPeriod(false)
t.Logf("Old period: True=%d (expected %d), False=%d (expected %d)",
oldFoundTrue, tc.oldTrueDuration, oldFoundFalse, tc.oldFalseDuration)
assert.Equal(t, tc.oldTrueDuration, oldFoundTrue, "Old true period should match")
assert.Equal(t, tc.oldFalseDuration, oldFoundFalse, "Old false period should match")
// Phase 2: Overwrite with new period signal
// Fill buffer completely to ensure all old data is replaced
baseSeq := uint32(maxAutoTuneSamples)
for i := range maxAutoTuneSamples {
posInPeriod := i % newPeriod
bit := posInPeriod < tc.newTrueDuration
tune.Sample(bit, baseSeq+uint32(i))
}
newFoundTrue := tune.FindPeriod(true)
newFoundFalse := tune.FindPeriod(false)
t.Logf("New period: True=%d (expected %d), False=%d (expected %d)",
newFoundTrue, tc.expectedNewTrue, newFoundFalse, tc.expectedNewFalse)
assert.Equal(t, tc.expectedNewTrue, newFoundTrue,
"New true period should match after overwrite")
assert.Equal(t, tc.expectedNewFalse, newFoundFalse,
"New false period should match after overwrite")
})
}
}
// TestAutoTuneMultiplePeriodChanges tests multiple consecutive period changes
func TestAutoTuneMultiplePeriodChanges(t *testing.T) {
tune := autoTune{}
// Define a sequence of periods to test
periodSequence := []struct {
trueDuration int
falseDuration int
}{
{2, 2}, // Period 4
{3, 3}, // Period 6
{5, 5}, // Period 10
{1, 1}, // Period 2
{4, 4}, // Period 8
{2, 6}, // Period 8 (asymmetric)
{7, 3}, // Period 10 (asymmetric)
}
seq := uint32(0)
for phase, p := range periodSequence {
period := p.trueDuration + p.falseDuration
// Fill buffer completely with current period
for i := range maxAutoTuneSamples {
posInPeriod := i % period
bit := posInPeriod < p.trueDuration
tune.Sample(bit, seq)
seq++
}
foundTrue := tune.FindPeriod(true)
foundFalse := tune.FindPeriod(false)
t.Logf("Phase %d: Period=%d (%dT+%dF), FoundTrue=%d, FoundFalse=%d",
phase+1, period, p.trueDuration, p.falseDuration, foundTrue, foundFalse)
assert.Equal(t, p.trueDuration, foundTrue,
"Phase %d: True period should match", phase+1)
assert.Equal(t, p.falseDuration, foundFalse,
"Phase %d: False period should match", phase+1)
}
}
// TestAutoTuneGradualPeriodTransition tests the transition period
// when old data is being gradually replaced by new data
func TestAutoTuneGradualPeriodTransition(t *testing.T) {
tune := autoTune{}
// First: fill with period 4 (2T+2F)
oldTrueDuration := 2
oldFalseDuration := 2
oldPeriod := 4
for i := range maxAutoTuneSamples {
bit := (i % oldPeriod) < oldTrueDuration
tune.Sample(bit, uint32(i))
}
// Verify old period for both true and false
assert.Equal(t, oldTrueDuration, tune.FindPeriod(true))
assert.Equal(t, oldFalseDuration, tune.FindPeriod(false))
t.Logf("Initial period 4 (2T+2F) established: true=%d, false=%d",
tune.FindPeriod(true), tune.FindPeriod(false))
// Now gradually replace with period 6 (3T+3F)
newTrueDuration := 3
newFalseDuration := 3
newPeriod := 6
baseSeq := uint32(maxAutoTuneSamples)
// Track when the new period becomes stable
transitionCompleteTrue := -1
transitionCompleteFalse := -1
for i := range maxAutoTuneSamples {
bit := (i % newPeriod) < newTrueDuration
tune.Sample(bit, baseSeq+uint32(i))
// Check every 10 samples
if i%10 == 0 && i > 0 {
foundTrue := tune.FindPeriod(true)
foundFalse := tune.FindPeriod(false)
// Once we find the new period, mark transition complete
if foundTrue == newTrueDuration && transitionCompleteTrue == -1 {
transitionCompleteTrue = i
t.Logf("True transition complete at sample %d, found new period=%d", i, foundTrue)
}
if foundFalse == newFalseDuration && transitionCompleteFalse == -1 {
transitionCompleteFalse = i
t.Logf("False transition complete at sample %d, found new period=%d", i, foundFalse)
}
}
}
// After full overwrite, should definitely find new period for both
finalPeriodTrue := tune.FindPeriod(true)
finalPeriodFalse := tune.FindPeriod(false)
assert.Equal(t, newTrueDuration, finalPeriodTrue,
"After full overwrite, should find new true period")
assert.Equal(t, newFalseDuration, finalPeriodFalse,
"After full overwrite, should find new false period")
t.Logf("Final period after full overwrite: true=%d, false=%d", finalPeriodTrue, finalPeriodFalse)
}
// TestAutoTuneExactPeriodAfterWrap tests that exact periods can be detected
// after the ring buffer has wrapped around
func TestAutoTuneExactPeriodAfterWrap(t *testing.T) {
// Test specific patterns where we know the exact expected output
testCases := []struct {
name string
trueDuration int
falseDuration int
expectedTrue int
expectedFalse int
}{
{"1T_1F", 1, 1, 1, 1},
{"2T_2F", 2, 2, 2, 2},
{"3T_3F", 3, 3, 3, 3},
{"4T_4F", 4, 4, 4, 4},
{"5T_5F", 5, 5, 5, 5},
{"2T_4F", 2, 4, 2, 4},
{"4T_2F", 4, 2, 4, 2},
{"3T_6F", 3, 6, 3, 6},
{"6T_3F", 6, 3, 6, 3},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tune := autoTune{}
period := tc.trueDuration + tc.falseDuration
// Generate enough samples to wrap around multiple times
totalSamples := maxAutoTuneSamples * 3
for i := range totalSamples {
posInPeriod := i % period
bit := posInPeriod < tc.trueDuration
tune.Sample(bit, uint32(i))
}
assert.Equal(t, maxAutoTuneSamples, tune.count)
periodTrue := tune.FindPeriod(true)
periodFalse := tune.FindPeriod(false)
t.Logf("Pattern %s: FoundTrue=%d (expected %d), FoundFalse=%d (expected %d)",
tc.name, periodTrue, tc.expectedTrue, periodFalse, tc.expectedFalse)
assert.Equal(t, tc.expectedTrue, periodTrue,
"True period should match expected")
assert.Equal(t, tc.expectedFalse, periodFalse,
"False period should match expected")
})
}
}
// TestAutoTuneSequenceWrapAround tests period detection when sequence numbers wrap around
func TestAutoTuneSequenceWrapAround(t *testing.T) {
tune := autoTune{}
period := 4
trueDuration := 2
// Start from a high sequence number close to uint32 max
startSeq := uint32(0xFFFFFFFF - 100)
// Generate samples that will cause sequence wrap-around
for i := range maxAutoTuneSamples + 50 {
posInPeriod := i % period
bit := posInPeriod < trueDuration
tune.Sample(bit, startSeq+uint32(i))
}
// Note: With sequence wrap-around, FindPeriod might return -1
// because it checks for continuous sequences (lastPulse.seq+1 == sorted[idx].seq)
// This is expected behavior - the test documents this edge case
periodTrue := tune.FindPeriod(true)
periodFalse := tune.FindPeriod(false)
t.Logf("Sequence wrap-around test: FoundTrue=%d, FoundFalse=%d", periodTrue, periodFalse)
// We don't assert specific values here as this is an edge case documentation
}