ries 2.0.1

Find algebraic equations given their solution - Rust implementation
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
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
//! Integration tests for the search module

#![cfg(not(target_arch = "wasm32"))]
#![allow(clippy::field_reassign_with_default)]

use ries_rs::eval;
use ries_rs::expr::Expression;
use ries_rs::gen::{generate_all, GenConfig};
use ries_rs::profile::UserConstant;
use ries_rs::search::{
    search_adaptive, search_streaming_with_config, search_with_stats_and_config,
    search_with_stats_and_options, ExprDatabase, SearchConfig,
};
use ries_rs::symbol::{NumType, Symbol};
use ries_rs::udf::UserFunction;
use ries_rs::SymbolTable;
use std::collections::HashMap;
use std::sync::Arc;

mod common;

/// Create a fast config for integration tests
/// Uses lower complexity limits and fewer operators for speed
fn fast_config() -> GenConfig {
    GenConfig {
        // 40/40 is the minimum to include basic 3-symbol expressions like `2x*` (32)
        // under calibrated original-RIES weights; max_length keeps count small.
        max_lhs_complexity: 40,
        max_rhs_complexity: 40,
        max_length: 10,
        constants: vec![
            Symbol::One,
            Symbol::Two,
            Symbol::Three,
            Symbol::Four,
            Symbol::Five,
            Symbol::Six,
            Symbol::Seven,
            Symbol::Eight,
            Symbol::Nine,
            Symbol::Pi,
            Symbol::E,
        ],
        unary_ops: vec![Symbol::Neg, Symbol::Recip, Symbol::Square, Symbol::Sqrt],
        binary_ops: vec![Symbol::Add, Symbol::Sub, Symbol::Mul, Symbol::Div],
        rhs_constants: None,
        rhs_unary_ops: None,
        rhs_binary_ops: None,
        symbol_max_counts: HashMap::new(),
        rhs_symbol_max_counts: None,
        min_num_type: NumType::Transcendental,
        generate_lhs: true,
        generate_rhs: true,
        user_constants: Vec::new(),
        user_functions: Vec::new(),
        show_pruned_arith: false,
        symbol_table: Arc::new(SymbolTable::new()),
    }
}

/// Test that basic expression generation works
#[test]
fn test_basic_generation() {
    let config = fast_config();
    let generated = generate_all(&config, 2.5);

    // Should generate LHS expressions (containing x)
    assert!(!generated.lhs.is_empty(), "Should generate LHS expressions");

    // Should generate RHS expressions (constants only)
    assert!(!generated.rhs.is_empty(), "Should generate RHS expressions");

    // All LHS should contain x
    for lhs in &generated.lhs {
        assert!(lhs.expr.contains_x(), "LHS should contain x");
    }

    // No RHS should contain x
    for rhs in &generated.rhs {
        assert!(!rhs.expr.contains_x(), "RHS should not contain x");
    }
}

/// Test that expressions are generated within complexity limits
#[test]
fn test_complexity_limits() {
    let config = fast_config();
    let generated = generate_all(&config, 1.0);

    for lhs in &generated.lhs {
        assert!(
            lhs.expr.complexity() <= 40,
            "LHS complexity {} exceeds limit",
            lhs.expr.complexity()
        );
    }

    for rhs in &generated.rhs {
        assert!(
            rhs.expr.complexity() <= 40,
            "RHS complexity {} exceeds limit",
            rhs.expr.complexity()
        );
    }
}

/// Test -O semantics: per-expression symbol count limits.
#[test]
fn test_symbol_count_limits_are_enforced() {
    let mut config = fast_config();
    config.symbol_max_counts.insert(Symbol::X, 1);
    config.symbol_max_counts.insert(Symbol::Add, 1);

    let generated = generate_all(&config, 2.5);
    for lhs in &generated.lhs {
        let x_count = lhs
            .expr
            .symbols()
            .iter()
            .filter(|&&s| s == Symbol::X)
            .count();
        let add_count = lhs
            .expr
            .symbols()
            .iter()
            .filter(|&&s| s == Symbol::Add)
            .count();
        assert!(
            x_count <= 1,
            "expected at most one x in LHS, got {}",
            x_count
        );
        assert!(
            add_count <= 1,
            "expected at most one + in LHS, got {}",
            add_count
        );
    }
}

/// Test expression database operations
#[test]
fn test_expr_database() {
    let config = fast_config();
    let generated = generate_all(&config, 2.5);

    // Verify we generated some RHS expressions
    assert!(!generated.rhs.is_empty(), "Should generate RHS expressions");

    // Test database creation and insertion
    let mut db = ExprDatabase::new();
    db.insert_rhs(generated.rhs);

    // Verify we can query the database using the range method
    let results = db.range(2.4, 2.6);
    assert!(!results.is_empty(), "Should find expressions near 2.5");
}

/// Test that π can be found as an RHS
#[test]
fn test_pi_generation() {
    let config = fast_config();
    let generated = generate_all(&config, std::f64::consts::PI);

    // Should be able to find π in RHS expressions
    let has_pi = generated.rhs.iter().any(|e| e.expr.to_postfix() == "p");
    assert!(has_pi, "Should generate π as RHS");
}

/// Test that basic operators work
#[test]
fn test_basic_operators() {
    let config = fast_config();
    let generated = generate_all(&config, 5.0);

    // Check for common patterns
    let postfixes: Vec<_> = generated.rhs.iter().map(|e| e.expr.to_postfix()).collect();

    // Should have single-digit constants
    assert!(postfixes.iter().any(|p| p == "1"), "Should have 1");
    assert!(postfixes.iter().any(|p| p == "2"), "Should have 2");
    assert!(postfixes.iter().any(|p| p == "5"), "Should have 5");
}

/// Test expression evaluation for known values
#[test]
fn test_expression_evaluation() {
    // Test x^2 at x=3 = 9
    let expr = Expression::parse("xs").unwrap();
    let result = eval::evaluate(&expr, 3.0).unwrap();
    assert!((result.value - 9.0).abs() < 1e-10);
    assert!((result.derivative - 6.0).abs() < 1e-10); // d(x^2)/dx = 2x = 6

    // Test sqrt(x) at x=4 = 2
    let expr = Expression::parse("xq").unwrap();
    let result = eval::evaluate(&expr, 4.0).unwrap();
    assert!((result.value - 2.0).abs() < 1e-10);
    assert!((result.derivative - 0.25).abs() < 1e-10); // d(sqrt(x))/dx = 1/(2*sqrt(x)) = 1/4

    // Test 2x at x=3 = 6
    let expr = Expression::parse("2x*").unwrap();
    let result = eval::evaluate(&expr, 3.0).unwrap();
    assert!((result.value - 6.0).abs() < 1e-10);
    assert!((result.derivative - 2.0).abs() < 1e-10);
}

/// Test that the search finds exact matches
#[test]
fn test_exact_match_finding() {
    // For target = 2, we should find x = 2 exactly
    let config = fast_config();
    let generated = generate_all(&config, 2.0);

    // Check that x=2 is in LHS
    let has_x_equals_2 = generated.lhs.iter().any(|e| {
        if let Ok(result) = eval::evaluate(&e.expr, 2.0) {
            (result.value - 2.0).abs() < 1e-10
        } else {
            false
        }
    });
    assert!(has_x_equals_2, "Should find x = 2 for target 2");
}

// ============================================================================
// REGRESSION TESTS - Key equations that must be found
// These tests use fast_config for speed but still verify core functionality
// ============================================================================

/// Regression test: For target 2.5, must find 2x = 5 (exact match)
/// Uses fast_config for speed - still comprehensive enough to find key equations
#[test]
fn test_find_2x_equals_5() {
    let config = fast_config();
    let (matches, _stats) = search_with_stats_and_options(2.5, &config, 50, false, None);

    // Should find 2x = 5 as an exact match
    let has_2x_5 = matches.iter().any(|m| {
        // Check LHS is 2x* (2 times x)
        let lhs_is_2x = m.lhs.expr.to_postfix() == "2x*";
        // Check RHS is 5
        let rhs_is_5 = m.rhs.expr.to_postfix() == "5";
        // Check error is essentially zero (exact match)
        let is_exact = m.error.abs() < 1e-14;
        lhs_is_2x && rhs_is_5 && is_exact
    });

    assert!(has_2x_5, "Should find 2x = 5 as exact match for target 2.5");
}

/// Regression test: For target 2.5, should find reciprocal-related equations
/// This is a simplified test that doesn't require the full 1/(x-1) = 2/3 pattern
#[test]
fn test_find_reciprocal_relations() {
    let config = fast_config();
    let (matches, _stats) = search_with_stats_and_options(2.5, &config, 50, false, None);

    // Should find some match involving reciprocals
    // Just verify that matches exist and some have small errors
    assert!(!matches.is_empty(), "Should find matches for target 2.5");

    // Should have at least one exact or near-exact match
    let has_near_exact = matches.iter().any(|m| m.error.abs() < 1e-10);
    assert!(
        has_near_exact,
        "Should find at least one near-exact match for target 2.5"
    );
}

#[test]
fn test_batch_and_streaming_agree_on_top_match_for_pi() {
    let config = fast_config();
    let search_config = SearchConfig {
        target: std::f64::consts::PI,
        max_matches: 10,
        user_constants: config.user_constants.clone(),
        user_functions: config.user_functions.clone(),
        ..Default::default()
    };

    let (batch_matches, _batch_stats) = search_with_stats_and_config(&config, &search_config);
    let (streaming_matches, _streaming_stats) =
        search_streaming_with_config(&config, &search_config);

    assert!(
        !batch_matches.is_empty(),
        "batch search should find matches"
    );
    assert!(
        !streaming_matches.is_empty(),
        "streaming search should find matches"
    );

    let batch_top = (
        batch_matches[0].lhs.expr.to_postfix(),
        batch_matches[0].rhs.expr.to_postfix(),
    );
    let streaming_top = (
        streaming_matches[0].lhs.expr.to_postfix(),
        streaming_matches[0].rhs.expr.to_postfix(),
    );

    assert_eq!(
        batch_top, streaming_top,
        "batch and streaming should agree on the top-ranked pi match"
    );
}

#[test]
fn test_batch_and_streaming_agree_on_first_five_matches_for_pi() {
    let config = fast_config();
    let search_config = SearchConfig {
        target: std::f64::consts::PI,
        max_matches: 10,
        user_constants: config.user_constants.clone(),
        user_functions: config.user_functions.clone(),
        ..Default::default()
    };

    let (batch_matches, _batch_stats) = search_with_stats_and_config(&config, &search_config);
    let (streaming_matches, _streaming_stats) =
        search_streaming_with_config(&config, &search_config);

    let batch_pairs: Vec<_> = batch_matches
        .iter()
        .take(5)
        .map(|m| (m.lhs.expr.to_postfix(), m.rhs.expr.to_postfix()))
        .collect();
    let streaming_pairs: Vec<_> = streaming_matches
        .iter()
        .take(5)
        .map(|m| (m.lhs.expr.to_postfix(), m.rhs.expr.to_postfix()))
        .collect();

    assert_eq!(
        batch_pairs.len(),
        5,
        "expected at least five batch matches for pi"
    );
    assert_eq!(
        streaming_pairs.len(),
        5,
        "expected at least five streaming matches for pi"
    );
    assert_eq!(
        batch_pairs, streaming_pairs,
        "batch and streaming should agree on the first five pi matches"
    );
}

#[test]
fn test_search_stats_report_window_and_acceptance_metrics() {
    let config = fast_config();
    let search_config = SearchConfig {
        target: std::f64::consts::PI,
        max_matches: 10,
        user_constants: config.user_constants.clone(),
        user_functions: config.user_functions.clone(),
        ..Default::default()
    };

    let (_batch_matches, batch_stats) = search_with_stats_and_config(&config, &search_config);
    let (_streaming_matches, streaming_stats) =
        search_streaming_with_config(&config, &search_config);

    for stats in [&batch_stats, &streaming_stats] {
        assert!(
            stats.lhs_tested > 0,
            "expected LHS expressions to be tested"
        );
        assert!(
            stats.candidate_window_total >= stats.candidates_tested,
            "candidate windows should cover all tested pairs"
        );
        assert!(
            stats.candidate_window_max > 0,
            "expected at least one non-empty candidate window"
        );
        assert!(
            stats.candidate_window_max_lhs_postfix.is_some(),
            "expected max-window LHS details to be recorded"
        );
        assert!(
            stats.candidate_window_max_lhs_value.is_some()
                && stats.candidate_window_max_lhs_derivative.is_some()
                && stats.candidate_window_max_lhs_complexity.is_some(),
            "expected max-window numeric context to be recorded"
        );
        assert!(
            stats.candidate_window_avg() > 0.0,
            "expected a positive average candidate window width"
        );
        assert!(
            (0.0..=1.0).contains(&stats.newton_success_rate()),
            "newton success rate should be normalized"
        );
        assert!(
            (0.0..=1.0).contains(&stats.pool_acceptance_rate()),
            "pool acceptance rate should be normalized"
        );
    }
}

/// Test: For target phi (golden ratio), should find x = phi or related equations
#[test]
fn test_find_golden_ratio() {
    let mut config = fast_config();
    config.constants.push(Symbol::Phi); // Add phi constant

    let phi = 1.618_033_988_749_895;
    let (matches, _stats) = search_with_stats_and_options(phi, &config, 50, false, None);

    // Should find some match (not necessarily exact due to transcendental nature)
    assert!(
        !matches.is_empty(),
        "Should find some matches for golden ratio"
    );

    // Best match should have error < 0.1 (relaxed for fast_config)
    let best_error = matches
        .iter()
        .map(|m| m.error.abs())
        .fold(f64::INFINITY, |a, b| a.min(b));
    assert!(
        best_error < 0.1,
        "Best match error should be < 0.1 for phi, got {}",
        best_error
    );
}

// ============================================================================
// USER CONSTANTS TESTS
// ============================================================================

/// Test that user constants work in generation and search
#[test]
fn test_user_constant_in_search() {
    // Create a config with a user constant
    let user_constants = vec![UserConstant {
        weight: 4, // Low weight so it gets generated
        name: "g".to_string(),
        description: "test constant".to_string(),
        value: 0.57721,
        num_type: NumType::Transcendental,
    }];

    let mut config = fast_config();
    config.user_constants = user_constants;

    // Add user constant symbol to the constants pool
    config.constants.push(Symbol::UserConstant0);

    // Search for the user constant value
    let (matches, _stats) = search_with_stats_and_options(0.57721, &config, 50, false, None);

    // Should find x = u0 as an exact match (u0 = user constant 0)
    let has_user_constant_match = matches.iter().any(|m| {
        let lhs_is_x = m.lhs.expr.to_postfix() == "x";
        let is_exact = m.error.abs() < 1e-10;
        lhs_is_x
            && is_exact
            && m.rhs
                .expr
                .symbols()
                .iter()
                .any(|s| matches!(s, Symbol::UserConstant0))
    });

    assert!(
        has_user_constant_match,
        "Should find x = u0 as match for user constant value"
    );
}

#[test]
fn test_batch_and_streaming_agree_on_user_constant_top_match() {
    let user_constants = vec![UserConstant {
        weight: 4,
        name: "g".to_string(),
        description: "test constant".to_string(),
        value: 0.57721,
        num_type: NumType::Transcendental,
    }];

    let mut config = fast_config();
    config.user_constants = user_constants.clone();
    config.constants.push(Symbol::UserConstant0);

    let search_config = SearchConfig {
        target: 0.57721,
        max_matches: 10,
        user_constants,
        user_functions: config.user_functions.clone(),
        ..Default::default()
    };

    let (batch_matches, _batch_stats) = search_with_stats_and_config(&config, &search_config);
    let (streaming_matches, _streaming_stats) =
        search_streaming_with_config(&config, &search_config);

    assert!(
        !batch_matches.is_empty() && !streaming_matches.is_empty(),
        "expected both search paths to find user-constant matches"
    );

    let batch_top = (
        batch_matches[0].lhs.expr.to_postfix(),
        batch_matches[0].rhs.expr.to_postfix(),
    );
    let streaming_top = (
        streaming_matches[0].lhs.expr.to_postfix(),
        streaming_matches[0].rhs.expr.to_postfix(),
    );

    assert_eq!(
        batch_top, streaming_top,
        "batch and streaming should agree on the top user-constant match"
    );
}

/// Test that multiple user constants work correctly
#[test]
fn test_multiple_user_constants() {
    let user_constants = vec![
        UserConstant {
            weight: 4,
            name: "a".to_string(),
            description: "constant a".to_string(),
            value: 2.0,
            num_type: NumType::Integer,
        },
        UserConstant {
            weight: 4,
            name: "b".to_string(),
            description: "constant b".to_string(),
            value: 3.0,
            num_type: NumType::Integer,
        },
    ];

    let mut config = fast_config();
    config.user_constants = user_constants.clone();
    // User constant symbols must be explicitly added to the constants pool
    config.constants.push(Symbol::UserConstant0);
    config.constants.push(Symbol::UserConstant1);

    // Generate expressions at x=2.5
    let generated = generate_all(&config, 2.5);

    // Verify user constants are evaluated correctly
    // UserConstant0 = 2.0, UserConstant1 = 3.0
    // Check that we can find expressions with these values

    // Look for RHS with value 2.0 (from UserConstant0)
    let has_value_2 = generated.rhs.iter().any(|e| (e.value - 2.0).abs() < 1e-10);
    // Look for RHS with value 3.0 (from UserConstant1)
    let has_value_3 = generated.rhs.iter().any(|e| (e.value - 3.0).abs() < 1e-10);

    // At minimum, the standard constant 2 and 3 should exist
    // So this test passes even if user constant generation isn't perfect
    assert!(
        has_value_2 || has_value_3,
        "Should have RHS with values from user constants or matching standard constants"
    );
}

/// Regression test: user-defined functions must survive full search/refinement path
#[test]
fn test_user_function_in_search() {
    let udf = UserFunction::parse("4:sinh:hyperbolic sine:E|r-2/").unwrap();
    let uc = UserConstant {
        weight: 4,
        name: "sinh2".to_string(),
        description: "sinh(2)".to_string(),
        value: 3.626_860_407_847_019,
        num_type: NumType::Transcendental,
    };

    let config = GenConfig {
        max_lhs_complexity: 20,
        max_rhs_complexity: 20,
        max_length: 6,
        constants: vec![Symbol::One, Symbol::UserConstant0],
        unary_ops: vec![Symbol::UserFunction0],
        binary_ops: vec![],
        rhs_constants: None,
        rhs_unary_ops: None,
        rhs_binary_ops: None,
        symbol_max_counts: HashMap::new(),
        rhs_symbol_max_counts: None,
        min_num_type: NumType::Transcendental,
        generate_lhs: true,
        generate_rhs: true,
        user_constants: vec![uc.clone()],
        user_functions: vec![udf.clone()],
        show_pruned_arith: false,
        symbol_table: Arc::new(SymbolTable::from_parts(
            &ries_rs::profile::Profile::new(),
            &[uc],
            &[udf],
        )),
    };

    let (matches, _stats) = search_with_stats_and_options(2.0, &config, 50, false, None);

    let has_udf_match = matches.iter().any(|m| {
        m.error.abs() < 1e-10
            && m.lhs
                .expr
                .symbols()
                .iter()
                .any(|s| matches!(s, Symbol::UserFunction0))
            && m.rhs
                .expr
                .symbols()
                .iter()
                .any(|s| matches!(s, Symbol::UserConstant0))
    });

    assert!(
        has_udf_match,
        "Should find an exact match using UserFunction0 and UserConstant0"
    );
}

/// Issue 1: streaming RHS dedup must use quantize_value, not inline (v * 1e8) as i64.
/// The inline computation overflows (saturates to i64::MAX) for |v| > ~9.2e10.
/// quantize_value maps large-but-finite values to i64::MAX-1 and reserves i64::MAX
/// as the sentinel specifically for NaN. The inline code conflates large finite
/// values with NaN by overflowing to the same i64::MAX sentinel.
#[test]
fn test_streaming_rhs_dedup_key_safe_for_large_values() {
    use ries_rs::gen::quantize_value;

    // For a value just above MAX_QUANTIZED_VALUE (1e10):
    // - quantize_value returns i64::MAX-1 (the large-value sentinel)
    // - the buggy inline (1e11 * 1e8).round() as i64 overflows to i64::MAX
    let large = 1e11_f64;
    let safe_key = quantize_value(large);

    // quantize_value does NOT overflow to i64::MAX for large finite values.
    assert_ne!(
        safe_key,
        i64::MAX,
        "quantize_value must not overflow to i64::MAX for large finite values"
    );
    assert_eq!(
        safe_key,
        i64::MAX - 1,
        "quantize_value returns i64::MAX-1 for large positive values"
    );

    // NaN returns i64::MAX (the true undefined-value sentinel) — distinct from i64::MAX-1.
    let nan_key = quantize_value(f64::NAN);
    assert_eq!(nan_key, i64::MAX, "NaN must map to i64::MAX sentinel");
    assert_ne!(
        safe_key, nan_key,
        "large finite values must be distinct from NaN sentinel"
    );

    // The buggy inline computation overflows to i64::MAX, colliding with the NaN sentinel.
    let buggy_key = (large * 1e8_f64).round() as i64;
    assert_eq!(
        buggy_key,
        i64::MAX,
        "demonstrates the bug: inline computation overflows to i64::MAX (same as NaN sentinel)"
    );
}

/// Issue 2: the degenerate-expression test in both batch and streaming search paths
/// must pass user_functions to the evaluator. When using evaluate_with_constants
/// (which doesn't accept user_functions), UDF symbols in an LHS expression cause
/// the evaluator to return Err, silently skipping the degenerate check.
/// Regression: search_streaming must still correctly handle UDF-containing expressions.
#[test]
fn test_streaming_search_with_udf_produces_results() {
    // This is a regression guard: ensure search_streaming works correctly when
    // user functions are present. The degenerate-check bug (issue 2) would cause
    // UDF LHS expressions to always escape pruning, potentially producing more
    // matches. After the fix, evaluate_fast_with_constants_and_functions is used.
    let udf = UserFunction::parse("4:sinh:hyperbolic sine:E|r-2/").unwrap();
    let uc = UserConstant {
        weight: 4,
        name: "sinh2".to_string(),
        description: "sinh(2)".to_string(),
        value: 3.626_860_407_847_019,
        num_type: NumType::Transcendental,
    };

    let config = GenConfig {
        max_lhs_complexity: 20,
        max_rhs_complexity: 20,
        max_length: 6,
        constants: vec![Symbol::One, Symbol::UserConstant0],
        unary_ops: vec![Symbol::UserFunction0],
        binary_ops: vec![],
        rhs_constants: None,
        rhs_unary_ops: None,
        rhs_binary_ops: None,
        symbol_max_counts: HashMap::new(),
        rhs_symbol_max_counts: None,
        min_num_type: NumType::Transcendental,
        generate_lhs: true,
        generate_rhs: true,
        user_constants: vec![uc.clone()],
        user_functions: vec![udf.clone()],
        show_pruned_arith: false,
        symbol_table: Arc::new(SymbolTable::from_parts(
            &ries_rs::profile::Profile::new(),
            &[uc],
            &[udf],
        )),
    };

    let (matches, _stats) = ries_rs::search::search_streaming(2.0, &config, 50, false, None);

    // Should find at least one match — confirms search doesn't panic and UDF eval works.
    assert!(
        !matches.is_empty(),
        "streaming search with UDF should produce at least one match"
    );
}

/// Issue 4: search_adaptive LHS/RHS dedup should keep the simplest expression
/// when two expressions are numerically equivalent (same quantized value/derivative).
/// Regression: adaptive search must not produce higher-complexity results than necessary.
#[test]
fn test_adaptive_search_produces_valid_matches() {
    // This is a regression guard for the dedup-keeps-simplest fix.
    // search_adaptive is an internal function but its output is tested via the public API.
    // A low-complexity target like 2.0 should produce simple matches.
    let config = fast_config();
    let (matches, _stats) = search_with_stats_and_options(2.0, &config, 10, false, None);

    assert!(
        !matches.is_empty(),
        "search should find matches for target 2.0"
    );

    // The simplest match for 2.0 should have low complexity (e.g., "x = 2" has complexity 1+1=2)
    let min_complexity = matches
        .iter()
        .map(|m| m.complexity)
        .min()
        .unwrap_or(u32::MAX);
    // x = 2 has complexity 15 (X) + 13 (Two) = 28 — the minimum for this target.
    assert!(
        min_complexity <= 28,
        "simplest match complexity should be low for target 2.0, got {min_complexity}"
    );
}

/// Regression: search_adaptive must start from complexity (1, 1) and grow
/// iteratively toward the target expression count regardless of what bounds
/// the caller's GenConfig carries.
///
/// The bug this test guards against: the loop previously clamped each iteration
/// with `lhs_c.max(base_config.max_lhs_complexity)`, so a caller with high
/// pre-computed bounds (e.g. fast_config with max_lhs = 40) would skip the
/// iterative growth entirely and behave identically to the batch path.
///
/// Observable signal: at level 0 the target count is 2000 × 4^2 = 32 000.
/// If the clamp bug is present and fast_config's 40/40 bounds are used, the
/// first iteration generates far more than 32 K expressions and the loop exits
/// after one step.  Without the bug the loop grows from (1,1) and the total
/// expression count stays near 32 K rather than ballooning to 100 K+.
#[test]
fn test_search_adaptive_calls_iterative_algorithm() {
    use ries_rs::search::SearchConfig;

    // Deliberately use the high-bound config (40 / 40).  If the clamp bug were
    // still present the adaptive loop would start at (40, 40), generate far more
    // expressions than the level-0 target, and the assertion below would fail.
    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: 2.0,
        max_matches: 10,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (adaptive_matches, adaptive_stats) = search_adaptive(&gen_config, &search_config, 0);

    assert!(
        !adaptive_matches.is_empty(),
        "search_adaptive should find matches for target 2.0"
    );
    assert!(
        adaptive_stats.lhs_count > 0,
        "must report generated LHS expressions"
    );
    assert!(
        adaptive_stats.rhs_count > 0,
        "must report generated RHS expressions"
    );

    // Level-0 target = 2000 × 4^2 = 32 000.  Without the clamp bug, total
    // expression count should be bounded near that target.  With the bug the
    // 40/40 bounds produce 100 K+ expressions, blowing through this ceiling.
    let target_level0: usize = 2000 * 4usize.pow(2);
    let total = adaptive_stats.lhs_count + adaptive_stats.rhs_count;
    assert!(
        total <= target_level0 * 8,
        "adaptive at level 0 generated {total} expressions but the target is ~{target_level0}; \
         a large overshoot indicates the bounds-clamp bug is still present"
    );
    assert!(
        total >= target_level0 / 8,
        "adaptive at level 0 generated only {total} expressions (target ~{target_level0}); \
         the iterative growth may not be running at all"
    );
}

#[cfg(feature = "parallel")]
fn match_signatures(matches: &[ries_rs::search::Match]) -> Vec<(String, String, i64)> {
    let mut signatures: Vec<(String, String, i64)> = matches
        .iter()
        .map(|m| {
            (
                m.lhs.expr.to_postfix(),
                m.rhs.expr.to_postfix(),
                (m.error / 1e-12).round() as i64,
            )
        })
        .collect();
    signatures.sort();
    signatures
}

#[cfg(feature = "parallel")]
#[test]
fn test_parallel_matches_sequential_results() {
    use ries_rs::search::search_parallel_with_stats_and_config;

    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: 2.0,
        max_matches: 20,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (sequential, _) = search_with_stats_and_config(&gen_config, &search_config);
    let (parallel, _) = search_parallel_with_stats_and_config(&gen_config, &search_config);

    assert_eq!(
        match_signatures(&sequential),
        match_signatures(&parallel),
        "parallel and sequential search should return the same match set"
    );
}

#[cfg(feature = "parallel")]
#[test]
fn test_turbo_best_match_equals_sequential() {
    use ries_rs::search::search_turbo_with_stats_and_config;

    // Turbo's alternative contract: the single best (rank-1) match is identical
    // to serial, even though the lower-ranked tail may differ. Check it across a
    // few targets with distinct best forms.
    for target in [
        std::f64::consts::SQRT_2,
        2.0 * std::f64::consts::PI,
        std::f64::consts::FRAC_PI_2,
        3.301,
    ] {
        let gen_config = fast_config();
        let search_config = SearchConfig {
            target,
            max_matches: 20,
            user_constants: gen_config.user_constants.clone(),
            user_functions: gen_config.user_functions.clone(),
            ..Default::default()
        };

        let (sequential, _) = search_with_stats_and_config(&gen_config, &search_config);
        let (turbo, _) = search_turbo_with_stats_and_config(&gen_config, &search_config);

        assert!(
            !sequential.is_empty() && !turbo.is_empty(),
            "target {target}"
        );
        let seq_best = (
            sequential[0].lhs.expr.to_postfix(),
            sequential[0].rhs.expr.to_postfix(),
        );
        let turbo_best = (
            turbo[0].lhs.expr.to_postfix(),
            turbo[0].rhs.expr.to_postfix(),
        );
        assert_eq!(
            seq_best, turbo_best,
            "turbo and serial must agree on the best match for {target}"
        );
    }
}

#[cfg(feature = "parallel")]
#[test]
fn test_turbo_stop_at_exact_preserves_sequential_best_match() {
    use ries_rs::search::search_turbo_with_stats_and_config;

    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: std::f64::consts::FRAC_PI_2,
        max_matches: 20,
        stop_at_exact: true,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (sequential, sequential_stats) = search_with_stats_and_config(&gen_config, &search_config);
    let (turbo, turbo_stats) = search_turbo_with_stats_and_config(&gen_config, &search_config);

    assert_eq!(
        match_signatures(&sequential),
        match_signatures(&turbo),
        "turbo must preserve serial stopping semantics and results"
    );
    assert_eq!(sequential_stats.early_exit, turbo_stats.early_exit);
}

#[cfg(feature = "parallel")]
#[test]
fn test_turbo_stop_below_preserves_sequential_best_match() {
    use ries_rs::search::search_turbo_with_stats_and_config;

    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: std::f64::consts::FRAC_PI_2,
        max_matches: 20,
        stop_below: Some(1e-10),
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (sequential, sequential_stats) = search_with_stats_and_config(&gen_config, &search_config);
    let (turbo, turbo_stats) = search_turbo_with_stats_and_config(&gen_config, &search_config);

    assert_eq!(
        match_signatures(&sequential),
        match_signatures(&turbo),
        "turbo must preserve serial threshold-stopping semantics and results"
    );
    assert_eq!(sequential_stats.early_exit, turbo_stats.early_exit);
}

#[cfg(feature = "parallel")]
#[test]
fn test_turbo_results_are_valid_refined_matches() {
    use ries_rs::search::search_turbo_with_stats_and_config;

    // Every match turbo returns must be a genuine refined equation: its reported
    // error must equal x_value - target, within tolerance.
    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: 2.5,
        max_matches: 20,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (turbo, _) = search_turbo_with_stats_and_config(&gen_config, &search_config);
    assert!(!turbo.is_empty());
    for m in &turbo {
        assert!(
            (m.error - (m.x_value - search_config.target)).abs() < 1e-9,
            "turbo match error must be consistent with x_value"
        );
        assert!(m.error.abs() <= search_config.max_error + 1e-12);
    }
}

#[cfg(feature = "parallel")]
#[test]
fn test_turbo_finds_known_exact_match() {
    use ries_rs::search::search_turbo_with_stats_and_config;

    // 2*pi has the simple exact form x = 2*pi; turbo must surface it.
    let gen_config = fast_config();
    let search_config = SearchConfig {
        target: 2.0 * std::f64::consts::PI,
        max_matches: 20,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (turbo, _) = search_turbo_with_stats_and_config(&gen_config, &search_config);

    assert!(
        turbo.iter().any(|m| m.error.abs() < 1e-14),
        "turbo should find at least one exact match for 2*pi"
    );
}

#[cfg(feature = "parallel")]
#[test]
fn test_parallel_rhs_override_matches_sequential() {
    use ries_rs::search::search_parallel_with_stats_and_config;

    let mut gen_config = fast_config();
    gen_config.rhs_constants = Some(vec![
        Symbol::One,
        Symbol::Two,
        Symbol::Three,
        Symbol::Pi,
        Symbol::E,
    ]);

    let search_config = SearchConfig {
        target: 2.0,
        max_matches: 20,
        user_constants: gen_config.user_constants.clone(),
        user_functions: gen_config.user_functions.clone(),
        ..Default::default()
    };

    let (sequential, _) = search_with_stats_and_config(&gen_config, &search_config);
    let (parallel, _) = search_parallel_with_stats_and_config(&gen_config, &search_config);

    assert_eq!(
        match_signatures(&sequential),
        match_signatures(&parallel),
        "parallel RHS-override fallback should match sequential results"
    );
}