oxifft 0.4.2

Pure Rust implementation of FFTW - the Fastest Fourier Transform in the West
Documentation
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
//! Unit tests for `api::wisdom`.
//!
//! Split out of `wisdom.rs` (via `#[path = "wisdom_tests.rs"] mod tests;`,
//! the same convention already used by `dft/codelets/codegen_tests.rs`) to
//! keep the implementation file under the workspace's 2000-line limit. This
//! file is included as the body of `wisdom`'s private `tests` submodule, so
//! everything here has the same access to `wisdom`'s private items as if it
//! were still inlined via `use super::*;`.

use super::*;

// Tests that access the global wisdom cache must not run concurrently with
// each other — they share `GLOBAL_WISDOM` state.  This mutex is used as a
// cooperative semaphore so that only one such test holds the lock at a
// time.  The lock is intentionally acquired for the duration of the whole
// test body and released (via `_guard` drop) when the test returns.
static GLOBAL_WISDOM_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

// ── Basic cache operations ────────────────────────────────────────────────

#[test]
fn test_wisdom_cache_basic() {
    let mut cache = WisdomCache::new();
    assert!(cache.is_empty());

    let entry = WisdomEntry {
        problem_hash: 12345,
        solver_name: "ct-dit".to_string(),
        cost: 100.0,
    };
    cache.store(entry);

    assert_eq!(cache.len(), 1);
    assert!(!cache.is_empty());

    let looked_up = cache.lookup(12345).expect("entry not found");
    assert_eq!(looked_up.solver_name, "ct-dit");
    assert!((looked_up.cost - 100.0).abs() < f64::EPSILON);
}

// ── Export / import round-trip (v1 format) ────────────────────────────────

#[test]
fn test_wisdom_export_import_v1() {
    let mut cache = WisdomCache::new();
    cache.store(WisdomEntry {
        problem_hash: 111,
        solver_name: "rader".to_string(),
        cost: 50.0,
    });
    cache.store(WisdomEntry {
        problem_hash: 222,
        solver_name: "bluestein".to_string(),
        cost: 75.0,
    });

    let exported = cache.export_string();
    assert!(exported.contains(WISDOM_MARKER));
    assert!(exported.contains("format_version"));
    assert!(exported.contains("111"));
    assert!(exported.contains("rader"));

    let mut cache2 = WisdomCache::new();
    let result = cache2.import_string(&exported).expect("import failed");
    assert_eq!(result.imported, 2);
    assert_eq!(result.skipped_invalid, 0);
    assert_eq!(result.format_version, WISDOM_FORMAT_VERSION);
    assert_eq!(cache2.len(), 2);

    let entry = cache2.lookup(111).expect("entry not found");
    assert_eq!(entry.solver_name, "rader");
}

// ── Legacy format (v0) accepted ───────────────────────────────────────────

#[test]
fn test_wisdom_legacy_format_accepted() {
    let legacy = "(oxifft-wisdom-1.0\n  (111 \"rader\" 50)\n  (222 \"bluestein\" 75)\n)";
    let mut cache = WisdomCache::new();
    let result = cache.import_string(legacy).expect("legacy import failed");
    assert_eq!(result.format_version, 0);
    assert_eq!(result.imported, 2);
    assert_eq!(cache.len(), 2);
}

// ── Incompatible (future) version rejected ────────────────────────────────

#[test]
fn test_wisdom_incompatible_version_rejected() {
    let future_version = WISDOM_FORMAT_VERSION + 1;
    let future_wisdom =
        format!("({WISDOM_MARKER}\n  (format_version {future_version})\n  (111 \"rader\" 50)\n)");
    let mut cache = WisdomCache::new();
    let err = cache
        .import_string(&future_wisdom)
        .expect_err("should have rejected future version");
    assert!(
        matches!(
            err,
            WisdomError::IncompatibleVersion {
                found,
                expected
            } if found == future_version && expected == WISDOM_FORMAT_VERSION
        ),
        "unexpected error: {err}"
    );
}

// ── Import validation: invalid entries are skipped ────────────────────────

#[test]
fn test_wisdom_import_skips_invalid_entries() {
    // Mix of valid entries and various forms of invalid data.
    // Entry with hash 0 — invalid.
    // Entry with empty solver — invalid.
    // Entry with NaN cost — invalid.
    // Entry with negative cost — invalid.
    // Entry (999, "ct-dit", 42.0) — valid.
    let wisdom_str = format!(
        "({WISDOM_MARKER}\n\
           (format_version {WISDOM_FORMAT_VERSION})\n\
           (0 \"ct-dit\" 1.0)\n\
           (333 \"\" 1.0)\n\
           (444 \"ct-dit\" NaN)\n\
           (555 \"ct-dit\" -1.0)\n\
           (999 \"ct-dit\" 42.0)\n\
         )"
    );

    let mut cache = WisdomCache::new();
    let result = cache
        .import_string(&wisdom_str)
        .expect("import should succeed");
    assert_eq!(
        result.imported, 1,
        "only the valid entry should be imported"
    );
    assert_eq!(
        result.skipped_invalid, 4,
        "four invalid entries should be skipped"
    );
    assert!(cache.lookup(999).is_some());
    assert!(cache.lookup(0).is_none());
    assert!(cache.lookup(333).is_none());
}

// ── Merge: new entries are added ──────────────────────────────────────────

#[test]
fn test_wisdom_merge_adds_new_entries() {
    let mut cache_a = WisdomCache::new();
    cache_a.store(WisdomEntry {
        problem_hash: 100,
        solver_name: "ct-dit".to_string(),
        cost: 10.0,
    });

    let mut cache_b = WisdomCache::new();
    cache_b.store(WisdomEntry {
        problem_hash: 200,
        solver_name: "bluestein".to_string(),
        cost: 20.0,
    });

    let b_str = cache_b.export_string();
    let merge = cache_a.merge_string(&b_str).expect("merge failed");

    assert_eq!(merge.added, 1);
    assert_eq!(merge.replaced, 0);
    assert_eq!(merge.kept_existing, 0);
    assert_eq!(merge.skipped_invalid, 0);
    assert_eq!(cache_a.len(), 2);
}

// ── Merge: lower cost wins ────────────────────────────────────────────────

#[test]
fn test_wisdom_merge_lower_cost_wins() {
    let mut cache_a = WisdomCache::new();
    cache_a.store(WisdomEntry {
        problem_hash: 100,
        solver_name: "ct-dit".to_string(),
        cost: 50.0,
    });

    // Incoming: same hash, lower cost.
    let incoming = format!(
        "({WISDOM_MARKER}\n\
           (format_version {WISDOM_FORMAT_VERSION})\n\
           (100 \"stockham\" 20.0)\n\
         )"
    );
    let merge = cache_a.merge_string(&incoming).expect("merge failed");

    assert_eq!(merge.replaced, 1);
    assert_eq!(merge.added, 0);
    assert_eq!(merge.kept_existing, 0);
    let entry = cache_a.lookup(100).expect("entry must still exist");
    assert_eq!(entry.solver_name, "stockham");
    assert!((entry.cost - 20.0).abs() < f64::EPSILON);
}

// ── Merge: higher cost in incoming — existing kept ────────────────────────

#[test]
fn test_wisdom_merge_keeps_existing_if_better() {
    let mut cache_a = WisdomCache::new();
    cache_a.store(WisdomEntry {
        problem_hash: 100,
        solver_name: "ct-dit".to_string(),
        cost: 10.0,
    });

    // Incoming: same hash, higher cost — should be ignored.
    let incoming = format!(
        "({WISDOM_MARKER}\n\
           (format_version {WISDOM_FORMAT_VERSION})\n\
           (100 \"rader\" 99.0)\n\
         )"
    );
    let merge = cache_a.merge_string(&incoming).expect("merge failed");

    assert_eq!(merge.kept_existing, 1);
    assert_eq!(merge.replaced, 0);
    let entry = cache_a.lookup(100).expect("entry must still exist");
    assert_eq!(entry.solver_name, "ct-dit"); // unchanged
}

// ── Merge rejects future format version ───────────────────────────────────

#[test]
fn test_wisdom_merge_rejects_future_version() {
    let future_version = WISDOM_FORMAT_VERSION + 5;
    let future_wisdom =
        format!("({WISDOM_MARKER}\n  (format_version {future_version})\n  (100 \"rader\" 1.0)\n)");
    let mut cache = WisdomCache::new();
    let err = cache
        .merge_string(&future_wisdom)
        .expect_err("should have rejected future version");
    assert!(matches!(
        err,
        WisdomError::IncompatibleVersion { found, .. } if found == future_version
    ));
}

// ── Global API ────────────────────────────────────────────────────────────

#[test]
fn test_wisdom_version_mismatch_unknown_header() {
    let mut cache = WisdomCache::new();
    let result = cache.import_string("(totally-unknown-header\n)");
    assert!(matches!(result, Err(WisdomError::ParseError(_))));
}

#[test]
fn test_global_wisdom_functions() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    // Clear any existing wisdom
    forget();
    assert_eq!(wisdom_count(), 0);

    // Store an entry
    store_wisdom(WisdomEntry {
        problem_hash: 999,
        solver_name: "generic".to_string(),
        cost: 200.0,
    });
    assert_eq!(wisdom_count(), 1);

    // Look it up
    let entry = lookup_wisdom(999).expect("entry not found");
    assert_eq!(entry.solver_name, "generic");

    // Export and reimport
    let exported = export_to_string();
    forget();
    assert_eq!(wisdom_count(), 0);

    let result = import_from_string(&exported).expect("import failed");
    assert_eq!(result.imported, 1);
    assert_eq!(wisdom_count(), 1);

    // Cleanup
    forget();
}

#[test]
fn test_global_merge_from_string() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    forget();

    // Seed the global cache.
    store_wisdom(WisdomEntry {
        problem_hash: 1,
        solver_name: "ct-dit".to_string(),
        cost: 30.0,
    });

    // Merge with data carrying a better entry for hash 1 and a new entry
    // for hash 2.
    let incoming = format!(
        "({WISDOM_MARKER}\n\
           (format_version {WISDOM_FORMAT_VERSION})\n\
           (1 \"stockham\" 5.0)\n\
           (2 \"rader\" 10.0)\n\
         )"
    );
    let merge = merge_from_string(&incoming).expect("merge failed");
    assert_eq!(merge.replaced, 1);
    assert_eq!(merge.added, 1);
    assert_eq!(merge.kept_existing, 0);
    assert_eq!(wisdom_count(), 2);

    forget();
}

// ── File-backed API ───────────────────────────────────────────────────────

#[cfg(feature = "std")]
#[test]
// File I/O requires `-Zmiri-disable-isolation`; excluded from default MIRI runs.
// The underlying `export_to_file`/`import_from_file` logic is tested in native mode.
#[cfg_attr(miri, ignore)]
fn test_import_export_file_roundtrip() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    let dir = std::env::temp_dir();
    let path = dir.join("oxifft_wisdom_test_roundtrip.txt");

    forget();
    store_wisdom(WisdomEntry {
        problem_hash: 42,
        solver_name: "bluestein".to_string(),
        cost: 7.5,
    });

    export_to_file(&path).expect("export failed");
    forget();
    assert_eq!(wisdom_count(), 0);

    let result = import_from_file(&path).expect("import failed");
    assert_eq!(result.imported, 1);
    assert_eq!(wisdom_count(), 1);
    assert_eq!(
        lookup_wisdom(42).expect("entry missing").solver_name,
        "bluestein"
    );

    let _ = std::fs::remove_file(&path);
    forget();
}

#[cfg(feature = "std")]
#[test]
// File I/O requires `-Zmiri-disable-isolation`; excluded from default MIRI runs.
// The underlying `merge_from_file` logic is tested in native mode.
#[cfg_attr(miri, ignore)]
fn test_merge_from_file() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    let dir = std::env::temp_dir();
    let path = dir.join("oxifft_wisdom_test_merge.txt");

    forget();
    store_wisdom(WisdomEntry {
        problem_hash: 7,
        solver_name: "ct-dit".to_string(),
        cost: 100.0,
    });

    // Write a file with a better entry for hash 7.
    let content = format!(
        "({WISDOM_MARKER}\n\
           (format_version {WISDOM_FORMAT_VERSION})\n\
           (7 \"stockham\" 25.0)\n\
         )"
    );
    std::fs::write(&path, &content).expect("write failed");

    let merge = merge_from_file(&path).expect("merge failed");
    assert_eq!(merge.replaced, 1);
    assert_eq!(
        lookup_wisdom(7).expect("entry missing").solver_name,
        "stockham"
    );

    let _ = std::fs::remove_file(&path);
    forget();
}

// ── Poison recovery ───────────────────────────────────────────────────────

#[test]
fn test_global_wisdom_recovers_from_poisoning() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    forget();

    // Poison GLOBAL_WISDOM's RwLock by panicking while a write guard
    // (obtained via the same private `with_wisdom_mut` path every public
    // mutating wisdom function uses) is held.
    let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        with_wisdom_mut(|_cache| {
            panic!("intentional panic to poison GLOBAL_WISDOM for testing recovery");
        });
    }));
    assert!(panicked.is_err(), "the closure should have panicked");

    // The lock is now poisoned. Earlier versions of this module treated
    // that as fatal (`.expect("Global wisdom lock poisoned")`), so every
    // subsequent call would panic too. The fix recovers the guard
    // instead, so ordinary wisdom operations must keep working.
    store_wisdom(WisdomEntry {
        problem_hash: 424_242,
        solver_name: "ct-dit".to_string(),
        cost: 1.0,
    });
    assert_eq!(
        wisdom_count(),
        1,
        "wisdom operations must survive lock poisoning"
    );
    let entry = lookup_wisdom(424_242).expect("entry not found after poison recovery");
    assert_eq!(entry.solver_name, "ct-dit");

    forget();
}

// ── Untrusted-input ceilings ──────────────────────────────────────────────

#[test]
fn test_import_string_rejects_oversized_input() {
    let huge = "x".repeat(MAX_WISDOM_TEXT_BYTES + 1);
    let mut cache = WisdomCache::new();
    let err = cache
        .import_string(&huge)
        .expect_err("oversized input must be rejected");
    assert!(matches!(err, WisdomError::TooLarge { kind: "bytes", .. }));
}

#[test]
fn test_merge_string_rejects_oversized_input() {
    let huge = "x".repeat(MAX_WISDOM_TEXT_BYTES + 1);
    let mut cache = WisdomCache::new();
    let err = cache
        .merge_string(&huge)
        .expect_err("oversized input must be rejected");
    assert!(matches!(err, WisdomError::TooLarge { kind: "bytes", .. }));
}

#[test]
fn test_import_string_rejects_too_many_entries() {
    // Build a small-in-bytes-but-many-entries wisdom string exceeding
    // MAX_WISDOM_TEXT_ENTRIES, using the shortest possible entry lines.
    let mut s = format!("({WISDOM_MARKER}\n  (format_version {WISDOM_FORMAT_VERSION})\n");
    for i in 1..=(MAX_WISDOM_TEXT_ENTRIES + 10) {
        s.push_str(&format!("  ({i} \"a\" 1)\n"));
    }
    s.push(')');

    let mut cache = WisdomCache::new();
    let err = cache
        .import_string(&s)
        .expect_err("too many entries must be rejected");
    assert!(matches!(
        err,
        WisdomError::TooLarge {
            kind: "entries",
            ..
        }
    ));
}

// ── import_system_wisdom error propagation ────────────────────────────────

#[cfg(feature = "std")]
#[test]
fn test_import_from_first_existing_propagates_real_error() {
    let dir = std::env::temp_dir();
    let corrupt_path = dir.join("oxifft_wisdom_test_corrupt_system_wisdom.txt");
    let missing_path = dir.join("oxifft_wisdom_test_definitely_missing_file_xyz.txt");

    // A candidate path that EXISTS but is not valid wisdom data at all.
    std::fs::write(&corrupt_path, b"not a wisdom file").expect("write failed");

    // Only the corrupt (existing) path is a candidate: the specific parse
    // error must be propagated, not masked as "not found".
    let err = import_from_first_existing(std::slice::from_ref(&corrupt_path))
        .expect_err("corrupt existing file must fail to import");
    assert!(
        matches!(err, WisdomError::ParseError(_)),
        "expected a ParseError from the corrupt file, got: {err}"
    );

    // A nonexistent-only candidate list still yields the generic NotFound.
    let err =
        import_from_first_existing(&[missing_path]).expect_err("no candidate existing must fail");
    assert!(matches!(err, WisdomError::IoError(_)));

    let _ = std::fs::remove_file(&corrupt_path);
}

#[cfg(feature = "std")]
#[test]
fn test_import_from_first_existing_incompatible_version_propagated() {
    let dir = std::env::temp_dir();
    let path = dir.join("oxifft_wisdom_test_future_version_system_wisdom.txt");

    let future_version = WISDOM_FORMAT_VERSION + 1;
    let content =
        format!("({WISDOM_MARKER}\n  (format_version {future_version})\n  (1 \"x\" 1.0)\n)");
    std::fs::write(&path, &content).expect("write failed");

    let err = import_from_first_existing(std::slice::from_ref(&path))
        .expect_err("future format version must fail to import");
    assert!(
        matches!(err, WisdomError::IncompatibleVersion { found, .. } if found == future_version),
        "expected IncompatibleVersion, got: {err}"
    );

    let _ = std::fs::remove_file(&path);
}

// ── Binary format ─────────────────────────────────────────────────────────

#[test]
fn test_binary_round_trip_seven_factor_mixed_radix() {
    // n = 2187 = 3^7 needs 7 mixed-radix stages — one more than the old
    // fixed `[u16; 6]` binary layout could hold. This must survive a
    // to_binary/from_binary round trip exactly under the v2 format.
    let mut cache = WisdomCache::new();
    cache.store(WisdomEntry {
        problem_hash: 2187,
        solver_name: "mixed-radix-3-3-3-3-3-3-3".to_string(),
        cost: 123.0,
    });

    let bytes = cache.to_binary();
    let restored = WisdomCache::from_binary(&bytes).expect("round-trip must succeed");
    let entry = restored
        .lookup(2187)
        .expect("entry for n=2187 must survive round-trip");
    assert_eq!(
        entry.solver_name, "mixed-radix-3-3-3-3-3-3-3",
        "all 7 factors must be preserved, not truncated to 6"
    );
}

#[test]
fn test_binary_round_trip_preserves_many_entries() {
    // Regression for the old `entry_count.min(u16::MAX)` truncation:
    // deliberately exceed u16::MAX (65_535) entries, which the old
    // format would have silently dropped down to. The new u32
    // entry-count field has no trouble representing this.
    let n: u64 = u64::from(u16::MAX) + 1000;
    let mut cache = WisdomCache::new();
    for i in 1..=n {
        cache.store(WisdomEntry {
            problem_hash: i,
            solver_name: "ct-dit".to_string(),
            cost: i as f64,
        });
    }
    let bytes = cache.to_binary();
    let restored = WisdomCache::from_binary(&bytes).expect("round-trip must succeed");
    assert_eq!(restored.entry_count(), n as usize);
}

#[test]
fn test_from_binary_rejects_bad_magic() {
    let mut bytes = WisdomCache::new().to_binary();
    bytes[0] = b'X';
    let err = WisdomCache::from_binary(&bytes).expect_err("bad magic must be rejected");
    assert!(matches!(err, WisdomError::ParseError(_)));
}

#[test]
fn test_from_binary_rejects_future_version() {
    let mut bytes = WisdomCache::new().to_binary();
    // Byte offset 8..10 is the format_version field.
    bytes[8] = 99;
    bytes[9] = 0;
    let err = WisdomCache::from_binary(&bytes).expect_err("future version must be rejected");
    assert!(matches!(
        err,
        WisdomError::IncompatibleVersion { found: 99, .. }
    ));
}

#[test]
fn test_from_binary_rejects_unrecognised_old_version() {
    let mut bytes = WisdomCache::new().to_binary();
    // Version 0 was never a valid binary format version (only the text
    // format has a legacy version 0).
    bytes[8] = 0;
    bytes[9] = 0;
    let err = WisdomCache::from_binary(&bytes).expect_err("version 0 must be rejected");
    assert!(matches!(err, WisdomError::ParseError(_)));
}

#[test]
fn test_from_binary_rejects_truncated_header() {
    let err = WisdomCache::from_binary(&[0u8; 10])
        .expect_err("data shorter than the 16-byte header must be rejected");
    assert!(matches!(err, WisdomError::ParseError(_)));
}

#[test]
fn test_from_binary_rejects_truncated_entries() {
    let mut cache = WisdomCache::new();
    cache.store(WisdomEntry {
        problem_hash: 16,
        solver_name: "ct-dit".to_string(),
        cost: 1.0,
    });
    let mut bytes = cache.to_binary();
    bytes.truncate(bytes.len() - 4); // chop off part of the last entry
    let err = WisdomCache::from_binary(&bytes).expect_err("truncated entry data must be rejected");
    assert!(matches!(err, WisdomError::ParseError(_)));
}

#[test]
fn test_from_binary_rejects_oversized_blob() {
    let huge = vec![0u8; MAX_WISDOM_BINARY_BYTES + 1];
    let err = WisdomCache::from_binary(&huge).expect_err("oversized blob must be rejected");
    assert!(matches!(err, WisdomError::TooLarge { kind: "bytes", .. }));
}

#[test]
fn test_from_binary_rejects_oversized_entry_count_header() {
    // A tiny blob whose header claims far more entries than
    // MAX_WISDOM_BINARY_ENTRIES must be rejected before any per-entry
    // decoding is attempted (no huge bounds-checked scan).
    let mut bytes = WisdomCache::new().to_binary();
    let bogus_count = (MAX_WISDOM_BINARY_ENTRIES as u32) + 1;
    bytes[12..16].copy_from_slice(&bogus_count.to_le_bytes());
    let err = WisdomCache::from_binary(&bytes)
        .expect_err("declared entry count over the ceiling must be rejected");
    assert!(matches!(
        err,
        WisdomError::TooLarge {
            kind: "entries",
            ..
        }
    ));
}

#[test]
fn test_from_binary_skips_degenerate_mixed_radix_factor() {
    // Hand-craft a v2 blob with one entry whose "mixed-radix" factor is
    // 0 — this is exactly the shape that used to reach
    // `algorithm_from_solver_name` and panic (integer divide-by-zero)
    // when replayed. It must now be silently skipped instead.
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&2u16.to_le_bytes()); // format_version = 2
    bytes.extend_from_slice(&0u16.to_le_bytes()); // reserved
    bytes.extend_from_slice(&1u32.to_le_bytes()); // entry_count = 1

    bytes.extend_from_slice(&42u64.to_le_bytes()); // size_key
    bytes.push(ALGO_TAG_MIXED_RADIX);
    bytes.push(1); // factors_len
    bytes.extend_from_slice(&0u16.to_le_bytes()); // factor = 0 (degenerate)
    bytes.extend_from_slice(&1000u64.to_le_bytes()); // elapsed_ns

    let cache = WisdomCache::from_binary(&bytes)
        .expect("structurally well-formed data must decode without a hard error");
    assert!(
        cache.lookup(42).is_none(),
        "the degenerate mixed-radix entry must be skipped, not stored"
    );
}

#[test]
fn test_from_binary_skips_unsupported_radix_and_product_mismatch() {
    // Radix 6 is not in {2,3,4,5,7,8,16}; even though 6*7=42 matches the
    // size, the radix itself is invalid and must be rejected.
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&1u32.to_le_bytes());
    bytes.extend_from_slice(&42u64.to_le_bytes());
    bytes.push(ALGO_TAG_MIXED_RADIX);
    bytes.push(2);
    bytes.extend_from_slice(&6u16.to_le_bytes());
    bytes.extend_from_slice(&7u16.to_le_bytes());
    bytes.extend_from_slice(&1000u64.to_le_bytes());
    let cache = WisdomCache::from_binary(&bytes).expect("must not hard-error");
    assert!(
        cache.lookup(42).is_none(),
        "unsupported radix 6 must be rejected"
    );

    // Valid radices {2,7} but their product (14) does not match the
    // declared size (100) — must also be rejected.
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&1u32.to_le_bytes());
    bytes.extend_from_slice(&100u64.to_le_bytes());
    bytes.push(ALGO_TAG_MIXED_RADIX);
    bytes.push(2);
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&7u16.to_le_bytes());
    bytes.extend_from_slice(&1000u64.to_le_bytes());
    let cache = WisdomCache::from_binary(&bytes).expect("must not hard-error");
    assert!(
        cache.lookup(100).is_none(),
        "factor product (14) mismatching the declared size (100) must be rejected"
    );
}

#[test]
fn test_from_binary_skips_zero_hash_and_unknown_tag() {
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&2u32.to_le_bytes()); // entry_count = 2

    // Entry 1: hash == 0 (invalid, matches text-format rule).
    bytes.extend_from_slice(&0u64.to_le_bytes());
    bytes.push(ALGO_TAG_COOLEY_TUKEY);
    bytes.push(0);
    bytes.extend_from_slice(&1u64.to_le_bytes());

    // Entry 2: unrecognised algo_tag (255 = Unknown).
    bytes.extend_from_slice(&7u64.to_le_bytes());
    bytes.push(ALGO_TAG_UNKNOWN);
    bytes.push(0);
    bytes.extend_from_slice(&1u64.to_le_bytes());

    let cache = WisdomCache::from_binary(&bytes).expect("must not hard-error");
    assert_eq!(cache.entry_count(), 0, "both entries must be skipped");
}

#[test]
fn test_import_binary_reports_skipped_invalid() {
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&2u32.to_le_bytes());

    // Entry 1: valid ct-dit entry.
    bytes.extend_from_slice(&16u64.to_le_bytes());
    bytes.push(ALGO_TAG_COOLEY_TUKEY);
    bytes.push(0);
    bytes.extend_from_slice(&500u64.to_le_bytes());

    // Entry 2: invalid (hash == 0).
    bytes.extend_from_slice(&0u64.to_le_bytes());
    bytes.push(ALGO_TAG_COOLEY_TUKEY);
    bytes.push(0);
    bytes.extend_from_slice(&500u64.to_le_bytes());

    let mut cache = WisdomCache::new();
    let result = cache.import_binary(&bytes).expect("import must succeed");
    assert_eq!(result.imported, 1);
    assert_eq!(result.skipped_invalid, 1);
    assert_eq!(result.format_version, 2);
    assert!(cache.lookup(16).is_some());
}

#[test]
fn test_from_binary_reads_legacy_v1_format() {
    // Hand-craft a version-1 blob (fixed [u16; 6] factors, u16 entry
    // count) to verify backward-compat reading of files written by
    // OxiFFT <= 0.3.x still works under the new decoder.
    let mut bytes = Vec::new();
    bytes.extend_from_slice(BINARY_MAGIC);
    bytes.extend_from_slice(&1u16.to_le_bytes()); // format_version = 1
    bytes.extend_from_slice(&1u16.to_le_bytes()); // entry_count = 1
    bytes.extend_from_slice(&0u32.to_le_bytes()); // reserved

    bytes.extend_from_slice(&6u64.to_le_bytes()); // size_key = 6 = 2*3
    bytes.push(ALGO_TAG_MIXED_RADIX);
    bytes.push(2); // factors_len
    bytes.extend_from_slice(&2u16.to_le_bytes());
    bytes.extend_from_slice(&3u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&0u16.to_le_bytes());
    bytes.extend_from_slice(&12345u64.to_le_bytes()); // elapsed_ns

    let cache = WisdomCache::from_binary(&bytes).expect("legacy v1 blob must decode");
    let entry = cache.lookup(6).expect("entry must be present");
    assert_eq!(entry.solver_name, "mixed-radix-2-3");
    assert!((entry.cost - 12345.0).abs() < f64::EPSILON);
}

#[test]
fn test_to_binary_checked_rejects_too_many_factors() {
    // Craft a solver name with more dash-separated numeric factors than
    // MAX_BINARY_FACTORS can represent; `to_binary_checked` must return
    // an error rather than silently truncating (which is exactly the
    // v1 bug this module fixes, just pushed to a much larger bound).
    let factors: Vec<String> = (0..=MAX_BINARY_FACTORS).map(|_| "2".to_string()).collect();
    let mut cache = WisdomCache::new();
    cache.store(WisdomEntry {
        problem_hash: 1,
        solver_name: format!("mixed-radix-{}", factors.join("-")),
        cost: 1.0,
    });

    let err = cache
        .to_binary_checked()
        .expect_err("factor list beyond MAX_BINARY_FACTORS must be rejected");
    assert!(matches!(
        err,
        WisdomError::TooLarge {
            kind: "factors",
            ..
        }
    ));

    // The infallible convenience form must still never panic: it caps
    // instead of erroring.
    let bytes = cache.to_binary();
    assert!(!bytes.is_empty());
}

#[test]
fn test_is_valid_mixed_radix_factors() {
    assert!(is_valid_mixed_radix_factors(&[2, 3], 6));
    assert!(is_valid_mixed_radix_factors(&[3, 3, 3, 3, 3, 3, 3], 2187));
    assert!(
        !is_valid_mixed_radix_factors(&[], 6),
        "empty factors invalid"
    );
    assert!(
        !is_valid_mixed_radix_factors(&[0], 0),
        "zero factor invalid"
    );
    assert!(
        !is_valid_mixed_radix_factors(&[6], 6),
        "radix 6 is not supported"
    );
    assert!(
        !is_valid_mixed_radix_factors(&[2, 3], 7),
        "product must match size exactly"
    );
    // Must not panic even with values chosen to stress the overflow guard.
    assert!(!is_valid_mixed_radix_factors(&[16; 20], u64::MAX));
}

// ── Utility ───────────────────────────────────────────────────────────────

#[test]
fn test_wisdom_clear() {
    let mut cache = WisdomCache::new();
    cache.store(WisdomEntry {
        problem_hash: 1,
        solver_name: "test".to_string(),
        cost: 1.0,
    });
    assert!(!cache.is_empty());

    cache.clear();
    assert!(cache.is_empty());
}

#[cfg(feature = "std")]
#[test]
fn test_user_wisdom_path() {
    // Just verify it doesn't panic
    let _path = get_user_wisdom_path();
}

// ── Hostile wisdom: solver names must be re-validated against the size ─────

/// End-to-end version of the `algorithm_from_solver_name` gating regression.
///
/// A wisdom line `(64 "nop" 1.0)` is entirely well-formed — the hash is
/// non-zero, the name is non-empty, the cost is finite — so `is_valid_entry`
/// accepts it and it lands in the global cache.  Before the fix, planning a
/// 64-point transform in a wisdom-backed mode reconstructed `Algorithm::Nop`
/// from it, and `execute_inplace` (a no-op for Nop) returned the caller's
/// input unchanged as an "FFT result".
#[test]
fn test_planted_nop_wisdom_cannot_forge_an_identity_fft() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    forget();

    let hostile = format!("({WISDOM_MARKER}\n  (format_version 1)\n  (64 \"nop\" 1.0)\n)");
    let result = import_from_string(&hostile).expect("hostile wisdom is structurally valid");
    assert_eq!(result.imported, 1, "the entry itself is well-formed");

    let plan = crate::api::Plan::<f64>::dft_1d(
        64,
        crate::api::Direction::Forward,
        crate::api::Flags::MEASURE,
    )
    .expect("planning must succeed despite the hostile entry");

    let mut data: Vec<crate::Complex<f64>> = (0..64)
        .map(|i| crate::Complex::new(i as f64, 0.0))
        .collect();
    let original = data.clone();
    plan.execute_inplace(&mut data);

    // DC bin of 0..63 is 63*64/2 = 2016 — i.e. a real transform ran.
    assert!(
        (data[0].re - 2016.0).abs() < 1e-6,
        "expected a real 64-point FFT, got {:?}",
        data[0]
    );
    assert!(
        data.iter()
            .zip(&original)
            .any(|(a, b)| (a.re - b.re).abs() > 1e-9),
        "the buffer must not have been returned unchanged"
    );

    forget();
}

/// The same shape for a Cooley-Tukey name at a non-power-of-two size:
/// `(6 "ct-dit" 1.0)` must not drive the radix-2 engine (whose applicability
/// guard is `debug_assert!` only, hence absent in release builds).
#[test]
fn test_planted_ct_wisdom_at_non_power_of_two_is_ignored() {
    let _guard = GLOBAL_WISDOM_TEST_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    forget();

    let hostile = format!("({WISDOM_MARKER}\n  (format_version 1)\n  (6 \"ct-dit\" 1.0)\n)");
    let imported = import_from_string(&hostile).expect("hostile wisdom is structurally valid");
    assert_eq!(imported.imported, 1, "the entry itself is well-formed");

    let plan = crate::api::Plan::<f64>::dft_1d(
        6,
        crate::api::Direction::Forward,
        crate::api::Flags::MEASURE,
    )
    .expect("planning must succeed despite the hostile entry");

    let input: Vec<crate::Complex<f64>> =
        (0..6).map(|i| crate::Complex::new(i as f64, 0.0)).collect();
    let mut output = vec![crate::Complex::<f64>::zero(); 6];
    plan.execute(&input, &mut output);

    // DC bin = 0+1+2+3+4+5 = 15.
    assert!(
        (output[0].re - 15.0).abs() < 1e-9,
        "expected a real 6-point FFT, got {:?}",
        output[0]
    );

    forget();
}