alice-edge 0.1.0

Embedded Model Generator - Don't send data, send the law
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
//! Integration tests spanning multiple modules.

#![allow(
    clippy::float_cmp,
    clippy::unreadable_literal,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::too_many_lines,
    clippy::needless_range_loop,
    clippy::explicit_iter_loop,
    clippy::bool_to_int_with_if,
    clippy::approx_constant,
    clippy::cast_lossless,
    clippy::redundant_clone,
    clippy::format_collect,
    clippy::similar_names,
    clippy::needless_collect,
    clippy::iter_cloned_collect,
    clippy::suboptimal_flops,
    clippy::should_panic_without_expect,
    clippy::manual_range_contains
)]

use crate::adaptive_polyfit::*;
use crate::constant_fit::*;
#[cfg(feature = "std")]
use crate::delta::*;
// `fit_piecewise_linear` is used by tests that only compile under `feature = "std"`;
// cargo fix removed this import under default features, breaking the std-feature build.
#[cfg(feature = "std")]
use crate::piecewise::*;
use crate::q16_linear::*;
#[cfg(feature = "std")]
use crate::robust::*;
use crate::simd_fit::*;

#[test]
fn test_fit_linear_perfect() {
    // y = 100x + 50
    let data: [i32; 5] = [50, 150, 250, 350, 450];
    let (slope, intercept) = fit_linear_fixed(&data);

    // slope should be ~100 in Q16.16 = 100 * 65536 = 6_553_600
    assert!((slope - 6_553_600).abs() < 100);
    // intercept should be ~50 in Q16.16 = 50 * 65536 = 3_276_800
    assert!((intercept - 3_276_800).abs() < 100);
}

#[test]
fn test_fit_constant() {
    let data: [i32; 4] = [100, 100, 100, 100];
    let (slope, intercept) = fit_linear_fixed(&data);

    // slope should be ~0
    assert!(slope.abs() < 100);
    // intercept should be ~100 in Q16.16
    assert!((intercept - 6_553_600).abs() < 100);
}

#[test]
fn test_evaluate() {
    let slope = 6_553_600; // 100.0 in Q16.16
    let intercept = 3_276_800; // 50.0 in Q16.16

    let y = evaluate_linear_fixed(slope, intercept, 3);
    // y = 100 * 3 + 50 = 350 in Q16.16 = 22_937_600
    assert!((y - 22_937_600).abs() < 1000);
}

#[test]
fn test_q16_conversion() {
    assert_eq!(int_to_q16(100), 6_553_600);
    assert_eq!(q16_to_int(6_553_600), 100);
}

#[test]
fn test_single_point() {
    let data: [i32; 1] = [42];
    let (slope, intercept) = fit_linear_fixed(&data);

    assert_eq!(slope, 0);
    assert_eq!(q16_to_int(intercept), 42);
}

#[test]
fn test_empty() {
    let data: [i32; 0] = [];
    let (slope, intercept) = fit_linear_fixed(&data);

    assert_eq!(slope, 0);
    assert_eq!(intercept, 0);
}

#[test]
fn test_loop_unrolling_boundary() {
    // Test with exactly 4 elements (perfect unroll)
    let data4: [i32; 4] = [10, 20, 30, 40];
    let (slope4, _) = fit_linear_fixed(&data4);
    assert!((slope4 - 655_360).abs() < 100); // slope = 10

    // Test with 5 elements (4 + 1 remainder)
    let data5: [i32; 5] = [10, 20, 30, 40, 50];
    let (slope5, _) = fit_linear_fixed(&data5);
    assert!((slope5 - 655_360).abs() < 100); // slope = 10

    // Test with 7 elements (4 + 3 remainder)
    let data7: [i32; 7] = [10, 20, 30, 40, 50, 60, 70];
    let (slope7, _) = fit_linear_fixed(&data7);
    assert!((slope7 - 655_360).abs() < 100); // slope = 10
}

// ── New tests ─────────────────────────────────────────────

#[test]
fn test_two_element_data() {
    // Minimal valid input for fit_linear_fixed
    let data: [i32; 2] = [0, 100];
    let (slope, intercept) = fit_linear_fixed(&data);
    // slope = 100, intercept = 0
    assert!((slope - int_to_q16(100)).abs() < 200);
    assert!(intercept.abs() < 200);
}

#[test]
fn test_negative_slope() {
    // y = -50x + 1000  → data: 1000, 950, 900, 850, 800
    let data: [i32; 5] = [1000, 950, 900, 850, 800];
    let (slope, _intercept) = fit_linear_fixed(&data);
    // slope should be negative: -50 in Q16.16 = -3_276_800
    assert!(slope < 0);
    assert!((slope + 3_276_800).abs() < 500);
}

#[test]
fn test_negative_intercept() {
    // y = 10x - 100  → data: -100, -90, -80, -70, -60
    let data: [i32; 5] = [-100, -90, -80, -70, -60];
    let (slope, intercept) = fit_linear_fixed(&data);
    // slope ≈ 10, intercept ≈ -100
    assert!((slope - int_to_q16(10)).abs() < 500);
    assert!(intercept < 0);
    assert!((intercept + int_to_q16(100)).abs() < 500);
}

#[test]
fn test_three_element_data() {
    // y = 5x + 0  → data: 0, 5, 10
    let data: [i32; 3] = [0, 5, 10];
    let (slope, intercept) = fit_linear_fixed(&data);
    assert!((slope - int_to_q16(5)).abs() < 200);
    assert!(intercept.abs() < 200);
}

#[test]
fn test_evaluate_at_zero() {
    // At x=0, result = intercept
    let slope = int_to_q16(50);
    let intercept = int_to_q16(200);
    let y = evaluate_linear_fixed(slope, intercept, 0);
    assert_eq!(y, intercept);
}

#[test]
fn test_evaluate_negative_x() {
    // slope=10, intercept=100, x=-2 → y = -20 + 100 = 80 (in Q16)
    let slope = int_to_q16(10);
    let intercept = int_to_q16(100);
    let y = evaluate_linear_fixed(slope, intercept, -2);
    let expected = int_to_q16(80);
    assert!((y - expected).abs() < 10);
}

#[test]
fn test_q16_to_int_negative() {
    // -100 in Q16.16
    let q = int_to_q16(-100);
    assert_eq!(q16_to_int(q), -100);
}

#[test]
fn test_q16_to_int_zero() {
    assert_eq!(q16_to_int(0), 0);
    assert_eq!(int_to_q16(0), 0);
}

#[test]
fn test_fit_constant_fixed_empty() {
    let result = fit_constant_fixed(&[]);
    assert_eq!(result, 0);
}

#[test]
fn test_fit_constant_fixed_single() {
    let data = [500];
    let mean = fit_constant_fixed(&data);
    assert_eq!(q16_to_int(mean), 500);
}

#[test]
fn test_fit_constant_fixed_multiple() {
    // Mean of [100, 200, 300] = 200
    let data = [100, 200, 300];
    let mean = fit_constant_fixed(&data);
    assert!((q16_to_int(mean) - 200).abs() <= 1);
}

#[test]
fn test_fit_constant_fixed_unrolled() {
    // 8 elements: exercises the 4x unrolled path + remainder
    let data = [10, 20, 30, 40, 50, 60, 70, 80];
    let mean = fit_constant_fixed(&data);
    // mean = 45
    assert!((q16_to_int(mean) - 45).abs() <= 1);
}

#[test]
fn test_compute_residual_error_perfect_fit() {
    // Perfect linear data → residual should be zero (or very small)
    let data = [0, 10, 20, 30, 40];
    let (slope, intercept) = fit_linear_fixed(&data);
    let err = compute_residual_error(&data, slope, intercept);
    assert!(err < 1000, "residual error for perfect fit: {err}");
}

#[test]
fn test_compute_residual_error_constant_vs_linear() {
    // For linear data, constant model has higher residual than linear model
    let data = [0, 100, 200, 300, 400];
    let constant_mean = fit_constant_fixed(&data);
    let (slope, intercept) = fit_linear_fixed(&data);

    let err_const = compute_residual_error(&data, 0, constant_mean);
    let err_linear = compute_residual_error(&data, slope, intercept);
    assert!(err_linear < err_const);
}

#[test]
fn test_should_use_linear_for_trending_data() {
    // Strongly linear data should prefer linear model
    let data = [0, 100, 200, 300, 400, 500, 600];
    assert!(should_use_linear(&data));
}

#[test]
fn test_should_use_linear_for_constant_data() {
    // Constant data should not prefer linear model
    let data = [100, 100, 100, 100, 100, 100];
    assert!(!should_use_linear(&data));
}

#[test]
fn test_should_use_linear_too_short() {
    // Less than 3 elements always returns false
    assert!(!should_use_linear(&[]));
    assert!(!should_use_linear(&[1]));
    assert!(!should_use_linear(&[1, 2]));
}

#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_positive() {
    // 1.0 in Q16.16 = 65536 → q16_to_f32 should give 1.0
    let q = Q16_ONE;
    let f = q16_to_f32(q);
    assert!((f - 1.0).abs() < 1e-5);
}

#[allow(clippy::float_cmp)]
#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_zero() {
    assert_eq!(q16_to_f32(0), 0.0);
}

#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_half() {
    // 0.5 in Q16.16 = 32768
    let q = Q16_ONE >> 1;
    let f = q16_to_f32(q);
    assert!((f - 0.5).abs() < 1e-5);
}

#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_negative() {
    // -1.0 in Q16.16 = -65536
    let q = -Q16_ONE;
    let f = q16_to_f32(q);
    assert!((f + 1.0).abs() < 1e-5);
}

#[cfg(feature = "std")]
#[test]
fn test_large_dataset_consistency() {
    // 100-element linear dataset: slope=1, intercept=0
    let data: Vec<i32> = (0..100).collect();
    let (slope, intercept) = fit_linear_fixed(&data);
    // slope ≈ 1.0 in Q16.16 = 65536
    assert!((slope - 65536).abs() < 500);
    assert!(intercept.abs() < 500);
}

#[test]
fn test_fit_linear_all_zeros() {
    let data = [0i32; 8];
    let (slope, intercept) = fit_linear_fixed(&data);
    assert_eq!(slope, 0);
    assert_eq!(intercept, 0);
}

#[test]
fn test_evaluate_linear_fixed_large_x() {
    // Verify no panic/crash with a larger x value
    let slope = int_to_q16(1);
    let intercept = int_to_q16(0);
    let y = evaluate_linear_fixed(slope, intercept, 1000);
    // y ≈ 1000 in Q16.16
    assert!((q16_to_int(y) - 1000).abs() <= 1);
}

// ── E1: 多項式フィット テスト ─────────────────────────────────────

#[test]
fn test_fit_quadratic_perfect() {
    // y = x² → data: 0, 1, 4, 9, 16, 25
    let data = [0, 1, 4, 9, 16, 25];
    let (a, b, c) = fit_quadratic_fixed(&data);
    // a ≈ 1.0 (Q16.16 = 65536)
    assert!((a - int_to_q16(1)).abs() < 500, "a = {a} (expected ~65536)");
    // b ≈ 0
    assert!(b.abs() < 500, "b = {b} (expected ~0)");
    // c ≈ 0
    assert!(c.abs() < 500, "c = {c} (expected ~0)");
}

#[test]
fn test_fit_quadratic_with_linear() {
    // y = 2x² + 3x + 10  → data: 10, 15, 24, 37, 54
    let data = [10, 15, 24, 37, 54];
    let (a, b, c) = fit_quadratic_fixed(&data);
    assert!((a - int_to_q16(2)).abs() < 1000, "a = {a}");
    assert!((b - int_to_q16(3)).abs() < 1000, "b = {b}");
    assert!((c - int_to_q16(10)).abs() < 1000, "c = {c}");
}

#[test]
fn test_evaluate_quadratic() {
    let a = int_to_q16(1); //    let b = int_to_q16(2); // 2x
    let c = int_to_q16(3); // +3
                           // At x=3: 9 + 6 + 3 = 18
    let y = evaluate_quadratic_fixed(a, b, c, 3);
    assert!((q16_to_int(y) - 18).abs() <= 1);
}

#[test]
fn test_fit_quadratic_fallback_2pts() {
    let data = [10, 20];
    let (a, b, _c) = fit_quadratic_fixed(&data);
    assert_eq!(a, 0); // 2次係数は0に退化
    assert!((b - int_to_q16(10)).abs() < 500);
}

#[test]
fn test_fit_quadratic_empty() {
    let data: [i32; 0] = [];
    let (a, b, c) = fit_quadratic_fixed(&data);
    assert_eq!(a, 0);
    assert_eq!(b, 0);
    assert_eq!(c, 0);
}

#[test]
fn test_fit_cubic_perfect() {
    // y = x³ → data: 0, 1, 8, 27, 64, 125
    let data = [0, 1, 8, 27, 64, 125];
    let (a, b, _c, _d) = fit_cubic_fixed(&data);
    // a ≈ 1.0 (x³ 係数)
    assert!(
        (a - int_to_q16(1)).abs() < 2000,
        "a = {a} (expected ~65536)"
    );
    // b ≈ 0 (x² 係数)
    assert!(b.abs() < 2000, "b = {b} (expected ~0)");
}

#[test]
fn test_fit_cubic_fallback_3pts() {
    // 3点 → 2次退化
    let data = [0, 1, 4];
    let (a, _b, _c, _d) = fit_cubic_fixed(&data);
    assert_eq!(a, 0);
}

#[test]
fn test_evaluate_cubic() {
    let a = int_to_q16(1);
    let b = 0;
    let c = 0;
    let d = int_to_q16(5);
    // At x=2: 8 + 0 + 0 + 5 = 13
    let y = evaluate_cubic_fixed(a, b, c, d, 2);
    assert!((q16_to_int(y) - 13).abs() <= 1);
}

// ── E2: 区間分割フィット テスト ───────────────────────────────────

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_single_segment() {
    // 完全な線形データ → 1セグメント
    let data: Vec<i32> = (0..20).map(|x| x * 10).collect();
    let segments = fit_piecewise_linear(&data, i64::MAX, 4);
    assert_eq!(segments.len(), 1);
    assert_eq!(segments[0].start, 0);
    assert_eq!(segments[0].end, 20);
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_split() {
    // ステップ関数: 0..10 → 値0, 10..20 → 値1000
    let mut data = vec![0i32; 10];
    data.extend(vec![1000i32; 10]);
    let segments = fit_piecewise_linear(&data, 100, 4);
    assert!(segments.len() >= 2, "segments: {}", segments.len());
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_empty() {
    let segments = fit_piecewise_linear(&[], 100, 4);
    assert!(segments.is_empty());
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_short() {
    let data = [1, 2, 3];
    let segments = fit_piecewise_linear(&data, 0, 4);
    assert_eq!(segments.len(), 1);
}

// ── E3: 外れ値除去テスト ──────────────────────────────────────────

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_basic() {
    // 通常データ + 1個の外れ値
    let data = [100, 101, 99, 100, 102, 100, 9999, 100, 101, 99];
    let filtered = filter_outliers_mad(&data, 3);
    assert_eq!(filtered.len(), 10);
    // 外れ値 9999 が中央値に置換されている
    assert!(filtered[6] < 200, "outlier was replaced: {}", filtered[6]);
}

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_no_outliers() {
    let data = [10, 11, 10, 11, 10];
    let filtered = filter_outliers_mad(&data, 3);
    assert_eq!(filtered, data);
}

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_short() {
    let data = [1, 2];
    let filtered = filter_outliers_mad(&data, 3);
    assert_eq!(filtered, data);
}

#[cfg(feature = "std")]
#[test]
fn test_fit_linear_robust() {
    // y = 10x + 外れ値
    let mut data: Vec<i32> = (0..20).map(|x| x * 10).collect();
    data[10] = 99999; // 外れ値
    let (slope, _intercept) = fit_linear_robust(&data, 3);
    // 外れ値除去後、slope ≈ 10
    assert!((slope - int_to_q16(10)).abs() < 20000, "slope = {slope}");
}

// ── E5: SIMD テスト ───────────────────────────────────────────────

#[test]
fn test_fit_linear_simd_matches_scalar() {
    let data = [50, 150, 250, 350, 450, 550, 650, 750, 850, 950];
    let (slope_scalar, intercept_scalar) = fit_linear_fixed(&data);
    let (slope_simd, intercept_simd) = fit_linear_simd(&data);
    assert!(
        (slope_simd - slope_scalar).abs() < 100,
        "slope: simd={slope_simd} scalar={slope_scalar}"
    );
    assert!(
        (intercept_simd - intercept_scalar).abs() < 100,
        "intercept: simd={intercept_simd} scalar={intercept_scalar}"
    );
}

#[test]
fn test_fit_linear_simd_large() {
    // 大きなデータセット
    let data: [i32; 16] = [
        10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
    ];
    let (slope_scalar, intercept_scalar) = fit_linear_fixed(&data);
    let (slope_simd, intercept_simd) = fit_linear_simd(&data);
    assert!(
        (slope_simd - slope_scalar).abs() < 100,
        "slope: simd={slope_simd} scalar={slope_scalar}"
    );
    assert!(
        (intercept_simd - intercept_scalar).abs() < 100,
        "intercept: simd={intercept_simd} scalar={intercept_scalar}"
    );
}

#[test]
fn test_fit_linear_simd_small_fallback() {
    // 少量データは scalar にフォールバック
    let data = [100, 200, 300];
    let (slope, intercept) = fit_linear_simd(&data);
    assert!((slope - int_to_q16(100)).abs() < 500);
    assert!((intercept - int_to_q16(100)).abs() < 500);
}

// ── E9: 差分エンコーディング テスト ───────────────────────────────

#[cfg(feature = "std")]
#[test]
fn test_delta_encode_decode_roundtrip() {
    let coeffs = vec![(100, 200), (110, 210), (120, 220), (130, 230)];
    let encoded = delta_encode_coefficients(&coeffs);
    let decoded = delta_decode_coefficients(&encoded);
    assert_eq!(decoded, coeffs);
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encode_first_preserved() {
    let coeffs = vec![(1000, 2000), (1005, 2010)];
    let encoded = delta_encode_coefficients(&coeffs);
    assert_eq!(encoded[0], (1000, 2000)); // 最初はそのまま
    assert_eq!(encoded[1], (5, 10)); // 差分
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encode_empty() {
    let encoded = delta_encode_coefficients(&[]);
    assert!(encoded.is_empty());
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encoding_savings() {
    // 緩やかに変化する係数 → 差分は小さく、varint サイズも小さい
    let coeffs: Vec<(i32, i32)> = (0..100)
        .map(|i| (int_to_q16(100 + i), int_to_q16(50 + i)))
        .collect();
    let (original, delta) = delta_encoding_savings(&coeffs);
    assert!(delta <= original, "delta={delta} original={original}");
}

// ── Q16 conversion tests (Python binding logic coverage) ──────

#[test]
fn test_q16_roundtrip_positive() {
    for v in [1, 10, 100, 1000, 32767] {
        let q = int_to_q16(v);
        assert_eq!(q16_to_int(q), v, "roundtrip failed for {v}");
    }
}

#[test]
fn test_q16_roundtrip_negative() {
    for v in [-1, -10, -100, -1000, -32768] {
        let q = int_to_q16(v);
        assert_eq!(q16_to_int(q), v, "roundtrip failed for {v}");
    }
}

#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_roundtrip() {
    // 整数値は f32 経由でも正確に戻る
    for v in [0, 1, -1, 100, -100] {
        let q = int_to_q16(v);
        let f = q16_to_f32(q);
        assert!(
            (f - v as f32).abs() < 1e-3,
            "f32 roundtrip failed for {v}: got {f}",
        );
    }
}

#[cfg(feature = "std")]
#[test]
fn test_q16_to_f32_fractional() {
    // 0.25 in Q16.16 = 16384
    let q = Q16_ONE / 4;
    let f = q16_to_f32(q);
    assert!((f - 0.25).abs() < 1e-5);
}

#[test]
fn test_compute_residual_error_empty() {
    let err = compute_residual_error(&[], 0, 0);
    assert_eq!(err, 0);
}

#[test]
fn test_compute_residual_error_single() {
    let data = [100];
    let slope = 0;
    let intercept = int_to_q16(100);
    let err = compute_residual_error(&data, slope, intercept);
    assert!(err < 100, "err = {err}");
}

// ── 追加テスト: Q16定数 ────────────────────────────────────────────

#[test]
fn test_q16_shift_and_one_constants() {
    assert_eq!(Q16_SHIFT, 16);
    assert_eq!(Q16_ONE, 65536);
    assert_eq!(Q16_ONE, 1 << Q16_SHIFT);
}

// ── 追加テスト: fit_linear_fixed ─────────────────────────────────

#[test]
fn test_fit_linear_exactly_8_elements() {
    // 8要素: 4xループ丁度2周回
    let data = [0i32, 10, 20, 30, 40, 50, 60, 70];
    let (slope, intercept) = fit_linear_fixed(&data);
    assert!((slope - int_to_q16(10)).abs() < 500, "slope={slope}");
    assert!(intercept.abs() < 500, "intercept={intercept}");
}

#[test]
fn test_fit_linear_exactly_9_elements() {
    // 9要素: 4x*2 + 1余り
    let data = [0i32, 5, 10, 15, 20, 25, 30, 35, 40];
    let (slope, intercept) = fit_linear_fixed(&data);
    assert!((slope - int_to_q16(5)).abs() < 300);
    assert!(intercept.abs() < 300);
}

#[test]
fn test_fit_linear_large_values() {
    // Q16.16の範囲内 (≤32767) の大きな値
    let data = [1000i32, 2000, 3000, 4000, 5000];
    let (slope, _intercept) = fit_linear_fixed(&data);
    // slope ≈ 1000 in Q16.16 > 0
    assert!(slope > 0, "slope must be positive, got {slope}");
}

#[test]
fn test_fit_linear_temperature_sensor() {
    // 温度センサー模擬: 25.00°C から0.1°Cずつ上昇 (×100 整数表現)
    let data = [2500i32, 2510, 2520, 2530, 2540, 2550, 2560, 2570];
    let (slope, intercept) = fit_linear_fixed(&data);
    // slope ≈ 10 (Q16.16)
    assert!((slope - int_to_q16(10)).abs() < 500, "slope={slope}");
    // intercept ≈ 2500 (Q16.16)
    assert!(
        (intercept - int_to_q16(2500)).abs() < 1000,
        "intercept={intercept}"
    );
}

#[test]
fn test_fit_linear_pressure_sensor() {
    // 気圧センサー: 101325 Pa から一定下降
    let data = [10132i32, 10130, 10128, 10126, 10124, 10122];
    let (slope, _intercept) = fit_linear_fixed(&data);
    assert!(slope < 0, "pressure drop: slope should be negative");
}

#[test]
fn test_fit_linear_noisy_linear() {
    // ノイズ込み線形データ: y ≈ 100x + 50 ± 5
    let data = [52i32, 148, 253, 347, 452, 548, 653, 747];
    let (slope, _intercept) = fit_linear_fixed(&data);
    // slope ≈ 100 (±10%)
    assert!(
        (slope - int_to_q16(100)).abs() < int_to_q16(15),
        "slope={slope}"
    );
}

// ── 追加テスト: evaluate_linear_fixed ────────────────────────────

#[test]
fn test_evaluate_linear_fixed_symmetry() {
    // f(x) と f(-x) の対称性チェック: slope=10, intercept=0
    let slope = int_to_q16(10);
    let intercept = 0;
    let y_pos = evaluate_linear_fixed(slope, intercept, 5);
    let y_neg = evaluate_linear_fixed(slope, intercept, -5);
    assert_eq!(y_pos, int_to_q16(50));
    assert_eq!(y_neg, -int_to_q16(50));
}

#[test]
fn test_evaluate_linear_fixed_zero_slope() {
    // slope=0 のとき、常に intercept を返す
    let intercept = int_to_q16(999);
    for x in [-10, 0, 10, 100] {
        let y = evaluate_linear_fixed(0, intercept, x);
        assert_eq!(y, intercept, "x={x}");
    }
}

// ── 追加テスト: fit_constant_fixed ────────────────────────────────

#[test]
fn test_fit_constant_fixed_four_elements() {
    // 丁度4要素: unrolled pathのみ
    let data = [10i32, 20, 30, 40];
    let mean = fit_constant_fixed(&data);
    assert!((q16_to_int(mean) - 25).abs() <= 1);
}

#[test]
fn test_fit_constant_fixed_five_elements() {
    // 5要素: 4+1
    let data = [10i32, 20, 30, 40, 50];
    let mean = fit_constant_fixed(&data);
    assert!((q16_to_int(mean) - 30).abs() <= 1);
}

#[test]
fn test_fit_constant_fixed_negative_values() {
    // 負の値を含む平均
    let data = [-100i32, -50, 0, 50, 100];
    let mean = fit_constant_fixed(&data);
    assert!(q16_to_int(mean).abs() <= 1);
}

// ── 追加テスト: fit_quadratic_fixed ──────────────────────────────

#[test]
fn test_fit_quadratic_negative_a() {
    // y = -x² + 100  → data: 100, 99, 96, 91, 84
    let data = [100i32, 99, 96, 91, 84];
    let (a, _b, c) = fit_quadratic_fixed(&data);
    assert!(a < 0, "a should be negative, got {a}");
    assert!((c - int_to_q16(100)).abs() < 1000, "c={c}");
}

#[test]
fn test_fit_quadratic_single_point() {
    let data = [42i32];
    let (a, b, c) = fit_quadratic_fixed(&data);
    assert_eq!(a, 0);
    assert_eq!(b, 0);
    assert_eq!(q16_to_int(c), 42);
}

#[test]
fn test_fit_quadratic_all_same() {
    // 定数データ → a=0, b=0, c=value
    let data = [7i32; 6];
    let (a, b, c) = fit_quadratic_fixed(&data);
    assert_eq!(a, 0, "a={a}");
    assert!(b.abs() < 500, "b={b}");
    assert!((c - int_to_q16(7)).abs() < 500, "c={c}");
}

#[test]
fn test_evaluate_quadratic_at_zero() {
    // x=0 → c
    let a = int_to_q16(5);
    let b = int_to_q16(3);
    let c = int_to_q16(7);
    let y = evaluate_quadratic_fixed(a, b, c, 0);
    assert_eq!(y, c);
}

#[test]
fn test_evaluate_quadratic_at_one() {
    // x=1: a+b+c
    let a = int_to_q16(2);
    let b = int_to_q16(3);
    let c = int_to_q16(4);
    // y = 2*1 + 3*1 + 4 = 9
    let y = evaluate_quadratic_fixed(a, b, c, 1);
    assert!((q16_to_int(y) - 9).abs() <= 1);
}

#[test]
fn test_evaluate_quadratic_negative_x() {
    // x=-2: a*4 - b*2 + c = 1*4 - 2*2 + 1 = 1
    let a = int_to_q16(1);
    let b = int_to_q16(2);
    let c = int_to_q16(1);
    let y = evaluate_quadratic_fixed(a, b, c, -2);
    // 4 - 4 + 1 = 1
    assert!((q16_to_int(y) - 1).abs() <= 1);
}

// ── 追加テスト: fit_cubic_fixed ──────────────────────────────────

#[test]
fn test_fit_cubic_fallback_1pt() {
    let data = [99i32];
    let (a, b, _c, d) = fit_cubic_fixed(&data);
    assert_eq!(a, 0);
    assert_eq!(b, 0);
    assert_eq!(q16_to_int(d), 99);
}

#[test]
fn test_fit_cubic_fallback_2pts() {
    let data = [0i32, 10];
    let (a, b, _c, _d) = fit_cubic_fixed(&data);
    assert_eq!(a, 0, "cubic term must be 0 for 2 pts");
    assert_eq!(b, 0, "quadratic term must be 0 for 2 pts");
}

#[test]
fn test_fit_cubic_constant_data() {
    let data = [5i32; 5];
    let (a, _b, _c, d) = fit_cubic_fixed(&data);
    assert_eq!(a, 0, "a={a}");
    assert!((d - int_to_q16(5)).abs() < 500, "d={d}");
}

#[test]
fn test_evaluate_cubic_at_zero() {
    let a = int_to_q16(1);
    let b = int_to_q16(2);
    let c = int_to_q16(3);
    let d = int_to_q16(10);
    // x=0: d
    let y = evaluate_cubic_fixed(a, b, c, d, 0);
    assert_eq!(y, d);
}

#[test]
fn test_evaluate_cubic_at_negative_x() {
    // y = x³: at x=-2 → -8
    let a = int_to_q16(1);
    let b = 0;
    let c = 0;
    let d = 0;
    let y = evaluate_cubic_fixed(a, b, c, d, -2);
    assert!((q16_to_int(y) - (-8)).abs() <= 1);
}

#[test]
fn test_evaluate_cubic_consistency_with_quadratic() {
    // When a=0, cubic should match quadratic
    let b = int_to_q16(2);
    let c = int_to_q16(3);
    let d = int_to_q16(5);
    for x in [0i32, 1, 2, -1, -2] {
        let yq = evaluate_quadratic_fixed(b, c, d, x);
        let yc = evaluate_cubic_fixed(0, b, c, d, x);
        assert_eq!(yc, yq, "mismatch at x={x}");
    }
}

// ── 追加テスト: should_use_linear ─────────────────────────────────

#[test]
fn test_should_use_linear_noisy_constant() {
    // ほぼ定数(±1)のデータ → linear を使わない
    let data = [100i32, 101, 100, 99, 100, 101, 100, 99];
    assert!(!should_use_linear(&data));
}

#[test]
fn test_should_use_linear_exactly_3_elements() {
    // 3要素の完全線形データ
    let data = [0i32, 100, 200];
    assert!(should_use_linear(&data));
}

// ── 追加テスト: compute_residual_error ───────────────────────────

#[test]
fn test_compute_residual_error_noisy() {
    // ノイズのある線形データ: 完全フィットよりも残差が大きい
    let perfect = [0i32, 10, 20, 30, 40];
    let noisy = [0i32, 12, 18, 33, 37];
    let (sp, ip) = fit_linear_fixed(&perfect);
    let (sn, inn) = fit_linear_fixed(&noisy);
    let err_perfect = compute_residual_error(&perfect, sp, ip);
    let err_noisy = compute_residual_error(&noisy, sn, inn);
    // ノイジーデータの残差が大きいか、同等
    assert!(err_noisy >= err_perfect);
}

// ── 追加テスト: std-only 機能 ─────────────────────────────────────

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_mad_all_same() {
    // 全要素同一 (MAD=0) → そのまま返す
    let data = [42i32; 8];
    let filtered = filter_outliers_mad(&data, 3);
    assert_eq!(filtered, data);
}

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_mad_multiple_outliers() {
    // 複数の外れ値
    let data = [100i32, 101, 9999, 99, 100, -9999, 101, 100];
    let filtered = filter_outliers_mad(&data, 3);
    assert!(filtered[2] < 200, "upper outlier replaced: {}", filtered[2]);
    assert!(
        filtered[5] > -200,
        "lower outlier replaced: {}",
        filtered[5]
    );
}

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_mad_k1_aggressive() {
    // k=1: より積極的に除去
    let data = [100i32, 101, 102, 200, 99, 100];
    let filtered = filter_outliers_mad(&data, 1);
    // 200 は外れ値として置換される
    assert!(filtered[3] < 200, "filtered[3]={}", filtered[3]);
}

#[cfg(feature = "std")]
#[test]
fn test_filter_outliers_mad_exact_3_elements() {
    // 3要素での動作
    let data = [10i32, 100, 10];
    let filtered = filter_outliers_mad(&data, 2);
    assert_eq!(filtered.len(), 3);
}

#[cfg(feature = "std")]
#[test]
fn test_fit_linear_robust_no_outliers() {
    // 外れ値なし: 通常フィットと同じ結果
    let data: Vec<i32> = (0..10).map(|x: i32| x * 50).collect();
    let (s_robust, i_robust) = fit_linear_robust(&data, 3);
    let (s_plain, i_plain) = fit_linear_fixed(&data);
    assert!((s_robust - s_plain).abs() < 200);
    assert!((i_robust - i_plain).abs() < 200);
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_coverage_fields() {
    // セグメントのフィールドが正しい
    let data: Vec<i32> = (0..10).map(|x| x * 5).collect();
    let segments = fit_piecewise_linear(&data, i64::MAX, 2);
    assert!(!segments.is_empty());
    let seg = &segments[0];
    assert_eq!(seg.start, 0);
    assert_eq!(seg.end, 10);
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_coverage_ordering() {
    // セグメントが昇順であること
    let mut data = vec![0i32; 10];
    data.extend(vec![500i32; 10]);
    let segments = fit_piecewise_linear(&data, 0, 2);
    for w in segments.windows(2) {
        assert!(w[0].end <= w[1].start, "segments overlap or unordered");
    }
}

#[cfg(feature = "std")]
#[test]
fn test_piecewise_linear_single_element() {
    let data = [42i32];
    let segments = fit_piecewise_linear(&data, 100, 4);
    // min_segment_len=4 → data.len()<4, 単一セグメントを返す
    assert_eq!(segments.len(), 1);
    assert_eq!(segments[0].start, 0);
    assert_eq!(segments[0].end, 1);
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encode_single_element() {
    let coeffs = vec![(500i32, 1000i32)];
    let encoded = delta_encode_coefficients(&coeffs);
    assert_eq!(encoded.len(), 1);
    assert_eq!(encoded[0], (500, 1000));
    let decoded = delta_decode_coefficients(&encoded);
    assert_eq!(decoded, coeffs);
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encode_negative_values() {
    let coeffs = vec![(-100i32, -200i32), (-90, -180), (-80, -160)];
    let encoded = delta_encode_coefficients(&coeffs);
    assert_eq!(encoded[0], (-100, -200));
    assert_eq!(encoded[1].0, 10); // delta = -90 - (-100) = 10
    let decoded = delta_decode_coefficients(&encoded);
    assert_eq!(decoded, coeffs);
}

#[cfg(feature = "std")]
#[test]
fn test_delta_decode_empty() {
    let decoded = delta_decode_coefficients(&[]);
    assert!(decoded.is_empty());
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encoding_savings_constant_coefficients() {
    // 変化しない係数 → delta=0 → 圧縮効率が高い
    let coeffs: Vec<(i32, i32)> = vec![(int_to_q16(10), int_to_q16(5)); 20];
    let (original, delta) = delta_encoding_savings(&coeffs);
    assert!(delta <= original, "delta={delta} original={original}");
}

#[cfg(feature = "std")]
#[test]
fn test_delta_encoding_savings_single() {
    let coeffs = vec![(int_to_q16(50), int_to_q16(25))];
    let (original, delta) = delta_encoding_savings(&coeffs);
    assert_eq!(original, delta); // 1要素はデルタ変換しない
}

#[cfg(feature = "std")]
#[test]
fn test_fit_linear_simd_zero_data() {
    let data = [0i32; 8];
    let (slope, intercept) = fit_linear_simd(&data);
    assert_eq!(slope, 0);
    assert_eq!(intercept, 0);
}

#[cfg(feature = "std")]
#[test]
fn test_large_dataset_q16_accuracy() {
    // 1000要素の完全線形データ: 精度確認
    let data: Vec<i32> = (0..1000).map(|x: i32| x * 3 + 7).collect();
    let (slope, intercept) = fit_linear_fixed(&data);
    // slope ≈ 3, intercept ≈ 7
    assert!((slope - int_to_q16(3)).abs() < 1000, "slope={slope}");
    assert!(
        (intercept - int_to_q16(7)).abs() < 1000,
        "intercept={intercept}"
    );
}

#[cfg(feature = "std")]
#[test]
fn test_model_selection_sensor_window() {
    // センサーウィンドウ: 直線的な温度変化 → linear を使う
    let window: Vec<i32> = (0..16).map(|x: i32| 2000 + x * 5).collect();
    assert!(should_use_linear(&window));
}

#[cfg(feature = "std")]
#[test]
fn test_model_selection_stable_sensor() {
    // 安定したセンサー → constant を使う
    let window: Vec<i32> = (0..16).map(|_| 2500).collect();
    assert!(!should_use_linear(&window));
}