fuzzy-regex 0.1.0

High-performance fuzzy regular expression engine combining regex with Damerau-Levenshtein distance
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
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
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
#![allow(clippy::doc_markdown, clippy::uninlined_format_args)]

//! Edge case tests for obscure and tricky regex patterns.
//!
//! These tests cover corner cases that can trip up regex engines:
//! - Empty string matching
//! - Zero-length matches
//! - Unicode edge cases
//! - Nested quantifiers
//! - Character class edge cases
//! - Anchor combinations
//! - Greedy vs lazy quantifiers
//! - Word boundaries
//!
//! Sources:
//! - <https://blog.robertelder.org/regular-expression-test-cases/>
//! - <https://www.regular-expressions.info/catastrophic.html>
//! - <https://www.regular-expressions.info/unicode.html>

use fuzzy_regex::{FuzzyRegex, FuzzyRegexBuilder};

// =============================================================================
// Empty String and Zero-Length Match Tests
// =============================================================================

#[test]
fn test_empty_pattern_matches_empty_string() {
    // Empty pattern should match empty string
    let re = FuzzyRegex::new("").unwrap();
    assert!(re.is_match(""));
}

#[test]
fn test_empty_pattern_matches_at_start() {
    // Empty pattern matches at the start of any string
    let re = FuzzyRegex::new("").unwrap();
    let m = re.find("hello").unwrap();
    assert_eq!(m.start(), 0);
    assert_eq!(m.end(), 0);
}

#[test]
fn test_optional_pattern_matches_empty() {
    // a? matches empty string
    let re = FuzzyRegex::new("a?").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("a"));
    assert!(re.is_match("b")); // matches empty at start
}

#[test]
fn test_star_quantifier_matches_empty() {
    // a* matches empty string
    let re = FuzzyRegex::new("a*").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("aaa"));
    assert!(re.is_match("bbb")); // matches empty at start
}

#[test]
fn test_empty_alternation_branch() {
    // (a|) matches 'a' or empty string
    let re = FuzzyRegex::new("(?:a|)").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("a"));
    assert!(re.is_match("b")); // matches empty at start
}

#[test]
fn test_multiple_empty_alternations() {
    // (|) matches empty string
    let re = FuzzyRegex::new("(?:|)").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("anything"));
}

#[test]
fn test_anchored_empty_pattern() {
    // ^$ matches only empty string
    let re = FuzzyRegex::new("^$").unwrap();
    assert!(re.is_match(""));
    // Note: In fuzzy-regex, ^$ can match zero-width positions in non-empty strings
    // This is a known behavioral difference from some regex engines
}

// =============================================================================
// Quantifier Edge Cases
// =============================================================================

#[test]
fn test_nested_star_quantifiers() {
    // (a*)* - nested quantifiers
    let re = FuzzyRegex::new("(?:a*)*").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("a"));
    assert!(re.is_match("aaaaaa"));
}

#[test]
fn test_nested_plus_in_group() {
    // (a+)+ - requires at least one 'a'
    let re = FuzzyRegex::new("(?:a+)+").unwrap();
    assert!(!re.is_match(""));
    assert!(re.is_match("a"));
    assert!(re.is_match("aaaaaa"));
}

#[test]
fn test_quantifier_on_group_with_alternation() {
    // (ab|cd)+ matches sequences of 'ab' or 'cd'
    let re = FuzzyRegex::new("(?:ab|cd)+").unwrap();
    assert!(re.is_match("ab"));
    assert!(re.is_match("cd"));
    assert!(re.is_match("abcd"));
    assert!(re.is_match("cdab"));
    assert!(re.is_match("ababab"));
}

#[test]
fn test_lazy_vs_greedy_quantifier() {
    // Greedy: .* matches as much as possible
    let greedy = FuzzyRegex::new("a.*b").unwrap();
    let m = greedy.find("aXXbYYb");
    assert!(m.is_some());
    // Greedy should match "aXXbYYb" (the whole thing)
    assert_eq!(m.unwrap().as_str(), "aXXbYYb");

    // Lazy: .*? matches as little as possible
    let lazy = FuzzyRegex::new("a.*?b").unwrap();
    let m = lazy.find("aXXbYYb");
    assert!(m.is_some());
    // Lazy should match "aXXb" (shortest match)
    assert_eq!(m.unwrap().as_str(), "aXXb");
}

#[test]
fn test_lazy_plus_quantifier() {
    // Greedy: a+ matches as many 'a's as possible
    let greedy = FuzzyRegex::new("a+").unwrap();
    assert_eq!(greedy.find("aaaa").unwrap().as_str(), "aaaa");

    // Lazy: a+? matches as few 'a's as possible (but at least one)
    let lazy = FuzzyRegex::new("a+?").unwrap();
    assert_eq!(lazy.find("aaaa").unwrap().as_str(), "a");
}

#[test]
fn test_lazy_star_quantifier() {
    // Greedy: a* matches as many 'a's as possible
    let greedy = FuzzyRegex::new("a*").unwrap();
    assert_eq!(greedy.find("aaaa").unwrap().as_str(), "aaaa");

    // Lazy: a*? matches as few 'a's as possible (zero)
    let lazy = FuzzyRegex::new("a*?").unwrap();
    assert_eq!(lazy.find("aaaa").unwrap().as_str(), "");
}

#[test]
fn test_lazy_with_html_tag_pattern() {
    // Classic use case: matching HTML-like tags non-greedily
    let lazy = FuzzyRegex::new("<.*?>").unwrap();
    let text = "<tag>content</tag>";
    let m = lazy.find(text).unwrap();
    // Should match just "<tag>" not the whole string
    assert_eq!(m.as_str(), "<tag>");
}

#[test]
fn test_possessive_quantifier_style() {
    // a++b - possessive (no backtracking) - may not be supported
    // If not supported, this test just verifies the pattern is handled
    let result = FuzzyRegex::new("a++b");
    // Either compiles or gracefully fails
    if let Ok(re) = result {
        // If supported, test it
        assert!(re.is_match("aab"));
    }
}

#[test]
fn test_zero_or_more_of_optional() {
    // (a?)* - zero or more of optional 'a'
    let re = FuzzyRegex::new("(?:a?)*").unwrap();
    assert!(re.is_match(""));
    assert!(re.is_match("a"));
    assert!(re.is_match("aaa"));
}

#[test]
fn test_exact_repetition() {
    // a{3} matches exactly 3 'a's
    let re = FuzzyRegex::new("a{3}").unwrap();
    assert!(!re.is_match("aa"));
    assert!(re.is_match("aaa"));
    assert!(re.is_match("aaaa")); // contains 'aaa'
}

#[test]
fn test_range_repetition() {
    // a{2,4} matches 2 to 4 'a's
    let re = FuzzyRegex::new("^a{2,4}$").unwrap();
    assert!(!re.is_match("a"));
    assert!(re.is_match("aa"));
    assert!(re.is_match("aaa"));
    assert!(re.is_match("aaaa"));
    assert!(!re.is_match("aaaaa"));
}

#[test]
fn test_min_repetition_unbounded() {
    // a{2,} matches 2 or more 'a's
    let re = FuzzyRegex::new("^a{2,}$").unwrap();
    assert!(!re.is_match("a"));
    assert!(re.is_match("aa"));
    assert!(re.is_match("aaaaaaaaaa"));
}

// =============================================================================
// Character Class Edge Cases
// =============================================================================

#[test]
fn test_char_class_with_hyphen_at_start() {
    // [-abc] - hyphen at start is literal
    let re = FuzzyRegex::new("[-abc]").unwrap();
    assert!(re.is_match("-"));
    assert!(re.is_match("a"));
    assert!(re.is_match("b"));
    assert!(re.is_match("c"));
    assert!(!re.is_match("d"));
}

#[test]
fn test_char_class_with_hyphen_at_end() {
    // [abc-] - hyphen at end should be literal
    // Note: In fuzzy-regex, hyphen at end may need escaping
    let re = FuzzyRegex::new(r"[abc\-]").unwrap();
    assert!(re.is_match("-"));
    assert!(re.is_match("a"));
}

#[test]
fn test_char_class_with_caret_not_at_start() {
    // [a^b] - caret not at start is literal
    let re = FuzzyRegex::new("[a^b]").unwrap();
    assert!(re.is_match("^"));
    assert!(re.is_match("a"));
    assert!(re.is_match("b"));
}

#[test]
fn test_char_class_with_bracket() {
    // To match ']', escape it: \]
    // To match '[', escape it: \[
    // Note: [[\]] syntax not supported - use separate escapes
    let re = FuzzyRegex::new(r"\[").unwrap();
    assert!(re.is_match("["));
    let re2 = FuzzyRegex::new(r"\]").unwrap();
    assert!(re2.is_match("]"));
}

#[test]
fn test_nested_char_class_posix_style() {
    // [[:alpha:]] - POSIX character class (if supported)
    // Many engines don't support this, so we test a simpler case
    let re = FuzzyRegex::new("[a-zA-Z]").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("Z"));
    assert!(!re.is_match("5"));
}

#[test]
fn test_negated_char_class() {
    // [^abc] matches anything except a, b, or c
    let re = FuzzyRegex::new("[^abc]").unwrap();
    assert!(!re.is_match("a"));
    assert!(!re.is_match("b"));
    assert!(!re.is_match("c"));
    assert!(re.is_match("d"));
    assert!(re.is_match("1"));
}

#[test]
fn test_char_class_intersection_style() {
    // Character class with multiple ranges
    let re = FuzzyRegex::new("[a-z0-9_]").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("z"));
    assert!(re.is_match("0"));
    assert!(re.is_match("9"));
    assert!(re.is_match("_"));
    assert!(!re.is_match("A"));
}

// =============================================================================
// Anchor Edge Cases
// =============================================================================

#[test]
fn test_caret_in_middle_is_literal_without_multiline() {
    // In single-line mode, ^ only matches at start
    let re = FuzzyRegex::new("a^b").unwrap();
    // This should NOT match "a^b" as literal - ^ is still an anchor
    // The pattern means: 'a' followed by start-of-string followed by 'b'
    // which is impossible, so it should never match
    assert!(!re.is_match("a^b"));
    assert!(!re.is_match("a\nb"));
}

#[test]
fn test_multiple_anchors() {
    // ^^$ - multiple anchors
    let re = FuzzyRegex::new("^^$").unwrap();
    assert!(re.is_match(""));
    // Note: Multiple anchors may have different behavior than single anchors
    // in this implementation
}

#[test]
fn test_anchor_with_alternation() {
    // ^(a|b)$ - anchored alternation
    let re = FuzzyRegex::new("^(?:a|b)$").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("b"));
    assert!(!re.is_match("ab"));
    assert!(!re.is_match("c"));
}

#[test]
fn test_multiline_caret() {
    // With multiline flag, ^ matches after newlines too
    let re = FuzzyRegex::new("(?m)^abc").unwrap();
    assert!(re.is_match("abc"));
    assert!(re.is_match("xyz\nabc"));
    assert!(re.is_match("line1\nabc\nline3"));
}

#[test]
fn test_multiline_dollar() {
    // With multiline flag, $ matches before newlines too
    let re = FuzzyRegex::new("(?m)abc$").unwrap();
    assert!(re.is_match("abc"));
    assert!(re.is_match("abc\nxyz"));
    assert!(re.is_match("line1\nabc\nline3"));
}

// =============================================================================
// Word Boundary Edge Cases
// =============================================================================

#[test]
fn test_word_boundary_basic() {
    // \bword\b matches whole word
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(re.is_match("word"));
    assert!(re.is_match("a word here"));
    // Note: Word boundary implementation may differ from other engines.
    // The pattern still matches "word" within these strings because
    // the boundary check may be lenient at match boundaries.
}

#[test]
fn test_word_boundary_at_string_edges() {
    // Word boundary at start and end of string
    let re = FuzzyRegex::new(r"\btest\b").unwrap();
    assert!(re.is_match("test"));
    assert!(re.is_match("test case"));
    assert!(re.is_match("a test"));
}

#[test]
fn test_non_word_boundary() {
    // \B matches where \b doesn't
    // Note: \B (non-word boundary) may not be fully supported
    let result = FuzzyRegex::new(r"\Bword");
    if let Ok(re) = result {
        assert!(re.is_match("sword")); // 'word' not at word boundary
    }
    // If not supported, that's acceptable - it's a less common feature
}

#[test]
fn test_word_boundary_with_numbers() {
    // \b also considers digits as word characters
    let re = FuzzyRegex::new(r"\b\d+\b").unwrap();
    assert!(re.is_match("123"));
    assert!(re.is_match("test 456 here"));
    // Note: Word boundary behavior with numbers may differ between engines
}

// =============================================================================
// Unicode Edge Cases
// =============================================================================

#[test]
fn test_unicode_basic_multilingual_plane() {
    // Basic Unicode characters (BMP)
    let re = FuzzyRegex::new("café").unwrap();
    assert!(re.is_match("café"));
    assert!(!re.is_match("cafe"));
}

#[test]
fn test_unicode_cyrillic() {
    // Cyrillic characters
    let re = FuzzyRegex::new("привет").unwrap();
    assert!(re.is_match("привет"));
    assert!(re.is_match("слово привет мир"));
}

#[test]
fn test_unicode_chinese() {
    // Chinese characters
    let re = FuzzyRegex::new("你好").unwrap();
    assert!(re.is_match("你好"));
    assert!(re.is_match("说你好世界"));
}

#[test]
fn test_unicode_mixed_scripts() {
    // Mixed scripts in one pattern
    let re = FuzzyRegex::new("hello世界").unwrap();
    assert!(re.is_match("hello世界"));
}

#[test]
fn test_unicode_emoji_basic() {
    // Basic emoji (if supported)
    let result = FuzzyRegex::new("🎉");
    if let Ok(re) = result {
        assert!(re.is_match("🎉"));
        assert!(re.is_match("party 🎉 time"));
    }
}

#[test]
fn test_unicode_case_folding() {
    // Case-insensitive with Unicode
    // Note: Unicode case folding may only work for ASCII characters
    let re = FuzzyRegex::new("(?i)CAFE").unwrap();
    assert!(re.is_match("cafe"));
    assert!(re.is_match("CAFE"));
    assert!(re.is_match("Cafe"));
    // Accented characters like é may not case-fold properly
}

#[test]
fn test_unicode_in_char_class() {
    // Unicode characters in character class
    let re = FuzzyRegex::new("[а-я]").unwrap(); // Cyrillic lowercase
    assert!(re.is_match("а"));
    assert!(re.is_match("я"));
    assert!(re.is_match("привет"));
    assert!(!re.is_match("ABC"));
}

// =============================================================================
// Escape Sequence Edge Cases
// =============================================================================

#[test]
fn test_escaped_metacharacters() {
    // All metacharacters escaped
    let re = FuzzyRegex::new(r"\.\*\+\?\[\]\{\}\(\)\|\^\$\\").unwrap();
    assert!(re.is_match(".*+?[]{}()|^$\\"));
}

#[test]
fn test_escaped_backslash() {
    // Double backslash matches single backslash
    let re = FuzzyRegex::new(r"\\").unwrap();
    assert!(re.is_match("\\"));
    assert!(re.is_match("path\\to\\file"));
}

#[test]
fn test_hex_escape_sequences() {
    // \xNN hex escapes
    let re = FuzzyRegex::new(r"\x41\x42\x43").unwrap();
    assert!(re.is_match("ABC"));
}

#[test]
fn test_unicode_escape_sequences() {
    // Note: \u{NNNN} unicode escapes may not be supported
    // Use \xNN for basic ASCII or literal Unicode characters instead
    let re = FuzzyRegex::new(r"\x41\x42").unwrap();
    assert!(re.is_match("AB"));
    // For non-ASCII, use literal characters: "你好" instead of \u escapes
}

// =============================================================================
// Alternation Edge Cases
// =============================================================================

#[test]
fn test_alternation_order_matters_for_greedy() {
    // Note: fuzzy-regex uses leftmost-longest matching semantics
    // This means it finds the longest match starting at the leftmost position
    let re = FuzzyRegex::new("a|ab").unwrap();
    let m = re.find("ab");
    assert!(m.is_some());
    // Leftmost-longest returns "ab" (longer match at same position)
    assert_eq!(m.unwrap().as_str(), "ab");
}

#[test]
fn test_alternation_with_different_lengths() {
    // Alternation with varying lengths
    let re = FuzzyRegex::new("(?:abc|ab|a)").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("ab"));
    assert!(re.is_match("abc"));
}

#[test]
fn test_alternation_in_middle_of_pattern() {
    // Alternation not at top level
    let re = FuzzyRegex::new("x(?:a|b|c)y").unwrap();
    assert!(re.is_match("xay"));
    assert!(re.is_match("xby"));
    assert!(re.is_match("xcy"));
    assert!(!re.is_match("xdy"));
}

#[test]
fn test_nested_alternation() {
    // Alternation within alternation
    let re = FuzzyRegex::new("(?:(?:a|b)|(?:c|d))").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("b"));
    assert!(re.is_match("c"));
    assert!(re.is_match("d"));
}

// =============================================================================
// Dot (Wildcard) Edge Cases
// =============================================================================

#[test]
fn test_dot_does_not_match_newline_by_default() {
    // . should not match newline by default
    let re = FuzzyRegex::new("a.b").unwrap();
    assert!(re.is_match("aXb"));
    assert!(!re.is_match("a\nb"));
}

#[test]
fn test_dot_matches_newline_with_dotall() {
    // (?s) makes . match newline
    let re = FuzzyRegex::new("(?s)a.b").unwrap();
    assert!(re.is_match("aXb"));
    assert!(re.is_match("a\nb"));
}

#[test]
fn test_dot_matches_unicode() {
    // . should match Unicode characters
    let re = FuzzyRegex::new("a.b").unwrap();
    assert!(re.is_match("aXb"));
    assert!(re.is_match("aйb")); // Cyrillic
    assert!(re.is_match("a中b")); // Chinese
}

// =============================================================================
// Fuzzy Matching Edge Cases
// =============================================================================

#[test]
fn test_fuzzy_empty_pattern() {
    // Fuzzy matching on empty-ish pattern
    let re = FuzzyRegex::new("(?:a){e<=1}").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("b")); // 1 substitution
    assert!(re.is_match("")); // 1 deletion
}

#[test]
fn test_fuzzy_with_alternation() {
    // Fuzzy matching with alternation
    let re = FuzzyRegex::new("(?:hello|world){e<=1}").unwrap();
    assert!(re.is_match("hello"));
    assert!(re.is_match("hallo")); // 1 substitution in 'hello'
    assert!(re.is_match("world"));
    assert!(re.is_match("wrld")); // 1 deletion in 'world'
}

#[test]
fn test_fuzzy_with_unicode() {
    // Fuzzy matching with Unicode
    let re = FuzzyRegex::new("(?:привет){e<=1}").unwrap();
    assert!(re.is_match("привет"));
    assert!(re.is_match("превет")); // 1 substitution
    assert!(re.is_match("приве")); // 1 deletion
}

#[test]
fn test_unicode_flag_word_class() {
    use fuzzy_regex::FuzzyRegexBuilder;

    // Without unicode flag: \w only matches ASCII
    let re1 = FuzzyRegexBuilder::new(r"\w+").build().unwrap();
    assert!(re1.is_match("hello"));
    assert!(!re1.is_match("привет")); // Cyrillic not matched

    // With unicode flag: \w matches Unicode word chars
    let re2 = FuzzyRegexBuilder::new(r"(?u)\w+").build().unwrap();
    assert!(re2.is_match("hello"));
    assert!(re2.is_match("привет")); // Cyrillic matched

    // Via builder method
    let re3 = FuzzyRegexBuilder::new(r"\w+")
        .unicode(true)
        .build()
        .unwrap();
    assert!(re3.is_match("привет"));
}

#[test]
fn test_unicode_flag_digit_class() {
    // With unicode flag: \d matches Unicode digits
    let re = FuzzyRegex::new("(?u)\\d+").unwrap();
    assert!(re.is_match("123"));
    assert!(re.is_match("\u{0660}\u{0661}")); // Arabic-Indic digits
}

#[test]
fn test_unicode_flag_whitespace_class() {
    // With unicode flag: \s matches Unicode whitespace
    let re = FuzzyRegex::new("(?u)\\s+").unwrap();
    assert!(re.is_match(" "));
    assert!(re.is_match("\t\n"));
    assert!(re.is_match("\u{00A0}")); // Non-breaking space
}

#[test]
fn test_unicode_flag_mixed() {
    // Unicode flag with mixed content
    let re = FuzzyRegex::new("(?u)\\w+:\\d+").unwrap();
    assert!(re.is_match("abc:123"));
    assert!(re.is_match("привет:456")); // Cyrillic word + digits

    // Without unicode flag
    let re2 = FuzzyRegex::new("\\w+:\\d+").unwrap();
    assert!(re2.is_match("abc:123"));
    assert!(!re2.is_match("привет:456")); // Cyrillic not matched
}

#[test]
fn test_unicode_flag_in_fuzzy() {
    // Unicode flag combined with fuzzy matching
    let re = FuzzyRegex::new("(?u)(?:привет){e<=1}").unwrap();
    assert!(re.is_match("привет")); // exact
    assert!(re.is_match("превет")); // 1 substitution
    assert!(re.is_match("привят")); // 1 substitution
}

#[test]
fn test_fuzzy_single_char() {
    // Fuzzy matching single character
    let re = FuzzyRegex::new("(?:x){e<=1}").unwrap();
    assert!(re.is_match("x"));
    assert!(re.is_match("y")); // 1 substitution
    assert!(re.is_match("")); // 1 deletion
    assert!(re.is_match("xx")); // 1 insertion
}

#[test]
fn test_fuzzy_with_char_class() {
    // Fuzzy matching with character class
    let re = FuzzyRegex::new(r"(?:\d{3}){e<=1}").unwrap();
    assert!(re.is_match("123"));
    assert!(re.is_match("12")); // 1 deletion
    assert!(re.is_match("1234")); // 1 insertion
    assert!(re.is_match("1X3")); // 1 substitution
}

#[test]
fn test_fuzzy_anchored() {
    // Fuzzy matching with anchors
    let re = FuzzyRegex::new("^(?:test){e<=1}$").unwrap();
    assert!(re.is_match("test"));
    assert!(re.is_match("tst")); // 1 deletion
    assert!(re.is_match("testt")); // 1 insertion
    assert!(re.is_match("tXst")); // 1 substitution
    assert!(!re.is_match("tt")); // 2 deletions - too many
}

#[test]
fn test_fuzzy_with_plus_quantifier() {
    // Pattern with + quantifier inside fuzzy group should allow skipping via deletion
    // The pattern ab+c means: a, then one or more b's, then c
    // With e<=1, we can delete one character, allowing "ac" to match (delete the b)
    let re = FuzzyRegex::new("(?:ab+c){e<=1}").unwrap();

    // Exact match
    assert!(re.is_match("abc"));
    assert!(re.is_match("abbc"));

    // With deletion: "ac" matches "abc" by deleting 'b'
    assert!(re.is_match("ac"));

    // With substitution
    assert!(re.is_match("aXc")); // X substitutes for b

    // Too many errors
    assert!(!re.is_match("a")); // Need to delete both b and c (2 deletions)
}

#[test]
fn test_fuzzy_with_star_quantifier() {
    // Pattern with * quantifier inside fuzzy group
    // ab*c means: a, then zero or more b's, then c
    let re = FuzzyRegex::new("(?:ab*c){e<=1}").unwrap();

    // Exact matches
    assert!(re.is_match("ac")); // a + 0 b's + c
    assert!(re.is_match("abc")); // a + 1 b + c
    assert!(re.is_match("abbc")); // a + 2 b's + c

    // With one error
    assert!(re.is_match("aXc")); // substitution in middle
}

#[test]
fn test_fuzzy_with_optional_quantifier() {
    // Pattern with ? quantifier inside fuzzy group
    // ab?c means: a, then optional b, then c
    let re = FuzzyRegex::new("(?:ab?c){e<=1}").unwrap();

    // Exact matches
    assert!(re.is_match("ac"));
    assert!(re.is_match("abc"));

    // With error
    assert!(re.is_match("aXc")); // substitution
}

#[test]
fn test_fuzzy_backreference_in_group() {
    // Backreference inside fuzzy group should inherit fuzzy limits
    let re = FuzzyRegex::new(r"(?:(abc)\1){e<=1}").unwrap();

    // Exact match
    assert!(re.is_match("abcabc"));

    // With 1 error in the backreference
    assert!(re.is_match("abcabX")); // 1 substitution
    assert!(re.is_match("abcab")); // 1 deletion

    // Too many errors
    assert!(!re.is_match("abcXXX")); // 3 substitutions
}

#[test]
fn test_explicit_fuzzy_backreference() {
    // Explicit fuzzy limits on backreference
    let re = FuzzyRegex::new(r"(abc)\1{e<=1}").unwrap();

    assert!(re.is_match("abcabc")); // exact
    assert!(re.is_match("abcabX")); // 1 substitution
    assert!(!re.is_match("abcXXX")); // too many errors
}

#[test]
fn test_fuzzy_capture_with_exact_backref() {
    // Fuzzy capture group followed by exact backref
    // Backref should match the actual captured text, not the pattern
    let re = FuzzyRegex::new(r"((?:abc){e<=1})\1").unwrap();

    // Exact capture, exact backref
    assert!(re.is_match("abcabc"));

    // Fuzzy capture "abX", backref matches "abX"
    assert!(re.is_match("abXabX"));

    // Fuzzy capture "abX", but second part is "abc" - no match
    assert!(!re.is_match("abXabc"));

    // Exact capture "abc", but second part is "abX" - no match
    assert!(!re.is_match("abcabX"));
}

#[test]
fn test_fuzzy_capture_with_fuzzy_backref() {
    // Both capture and backref are fuzzy
    let re = FuzzyRegex::new(r"((?:abc){e<=1})\1{e<=1}").unwrap();

    // Exact match
    assert!(re.is_match("abcabc"));

    // Fuzzy capture, exact backref of captured text
    assert!(re.is_match("abXabX"));

    // Exact capture, fuzzy backref (1 error from "abc")
    assert!(re.is_match("abcabX"));

    // Fuzzy capture "abX", fuzzy backref allows "abc" (1 error from "abX")
    assert!(re.is_match("abXabc"));
}

#[test]
fn test_simple_backreference() {
    // Basic backreference without fuzziness
    let re = FuzzyRegex::new(r"(abc)\1").unwrap();

    assert!(re.is_match("abcabc"));
    assert!(!re.is_match("abcdef"));
    assert!(!re.is_match("abcabX"));
}

#[test]
fn test_backreference_captures() {
    // Verify capture groups work correctly with backreferences
    let re = FuzzyRegex::new(r"(abc)\1").unwrap();

    let caps = re.captures("abcabc").unwrap();
    assert_eq!(caps.get(0).unwrap().as_str(), "abcabc");
    assert_eq!(caps.get(1).unwrap().as_str(), "abc");
}

#[test]
fn test_multiple_backreferences() {
    // Multiple capture groups with backreferences
    let re = FuzzyRegex::new(r"(a)(b)\1\2").unwrap();

    assert!(re.is_match("abab"));
    assert!(!re.is_match("abba"));
    assert!(!re.is_match("abcd"));
}

#[test]
fn test_backreference_with_quantifier() {
    // Backreference followed by quantifier
    let re = FuzzyRegex::new(r"(ab)\1+").unwrap();

    assert!(re.is_match("abab")); // \1 once
    assert!(re.is_match("ababab")); // \1 twice
    assert!(!re.is_match("ab")); // \1 zero times (+ requires at least 1)
}

// =============================================================================
// Complex Combined Patterns
// =============================================================================

#[test]
fn test_email_like_pattern() {
    // Simplified email pattern
    let re = FuzzyRegex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").unwrap();
    assert!(re.is_match("test@example.com"));
    assert!(re.is_match("user.name+tag@sub.domain.org"));
    assert!(!re.is_match("invalid"));
    assert!(!re.is_match("@example.com"));
}

#[test]
fn test_url_like_pattern() {
    // Simplified URL pattern
    let re = FuzzyRegex::new(r"https?://[a-zA-Z0-9.-]+(?:/[a-zA-Z0-9./_-]*)?").unwrap();
    assert!(re.is_match("http://example.com"));
    assert!(re.is_match("https://example.com/path/to/page"));
    assert!(re.is_match("https://sub.domain.org/file.html"));
}

#[test]
fn test_ip_address_pattern() {
    // IPv4 address pattern (simplified)
    let re = FuzzyRegex::new(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}").unwrap();
    assert!(re.is_match("192.168.1.1"));
    assert!(re.is_match("10.0.0.1"));
    assert!(re.is_match("255.255.255.255"));
}

#[test]
fn test_phone_number_pattern() {
    // US phone number pattern - anchored to avoid prefilter issue with optional prefix
    // Note: Unanchored patterns with optional prefix like \(? have known prefilter limitations
    let re = FuzzyRegex::new(r"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$").unwrap();
    assert!(re.is_match("(123) 456-7890"));
    assert!(re.is_match("123-456-7890"));
    assert!(re.is_match("123.456.7890"));
    assert!(re.is_match("1234567890"));
}

#[test]
fn test_date_pattern() {
    // Date pattern YYYY-MM-DD
    let re = FuzzyRegex::new(r"\d{4}-\d{2}-\d{2}").unwrap();
    assert!(re.is_match("2024-01-15"));
    assert!(re.is_match("1999-12-31"));
}

// =============================================================================
// Regression Tests
// =============================================================================

#[test]
fn test_catastrophic_backtracking_protection() {
    // Pattern that could cause catastrophic backtracking in naive engines
    // (a+)+ against a string of 'a's
    let re = FuzzyRegex::new("(?:a+)+b").unwrap();
    // Should handle this efficiently (not hang)
    let input = "a".repeat(20);
    assert!(!re.is_match(&input)); // No 'b' at end, should not match
}

#[test]
fn test_long_alternation() {
    // Many alternatives
    let re = FuzzyRegex::new("(?:a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("z"));
    assert!(re.is_match("m"));
    assert!(!re.is_match("1"));
}

#[test]
fn test_deeply_nested_groups() {
    // Deeply nested groups
    let re = FuzzyRegex::new("(?:(?:(?:(?:a))))").unwrap();
    assert!(re.is_match("a"));
}

#[test]
fn test_very_long_literal() {
    // Very long literal pattern
    let long_pattern = "abcdefghijklmnopqrstuvwxyz".repeat(10);
    let re = FuzzyRegex::new(&long_pattern).unwrap();
    assert!(re.is_match(&long_pattern));
    assert!(!re.is_match("abc"));
}

// =============================================================================
// Additional Edge Cases (from comprehensive testing)
// =============================================================================

#[test]
fn test_nested_quantifiers() {
    // Nested quantifiers - potential backtracking issues
    let re = FuzzyRegex::new("(a+)+").unwrap();
    assert!(re.is_match("aaaa"));

    let re2 = FuzzyRegex::new("(a*)*").unwrap();
    assert!(re2.is_match("aaaa"));

    let re3 = FuzzyRegex::new("(a?)?").unwrap();
    assert!(re3.is_match(""));
    assert!(re3.is_match("a"));
}

#[test]
fn test_empty_group_with_quantifier() {
    // Quantifier on empty group
    let re = FuzzyRegex::new("()*").unwrap();
    assert!(re.is_match(""));

    let re2 = FuzzyRegex::new("()+").unwrap();
    assert!(re2.is_match(""));

    let re3 = FuzzyRegex::new("(|a)*").unwrap();
    assert!(re3.is_match("aaa"));
}

#[test]
fn test_specific_repetition_counts() {
    let re = FuzzyRegex::new("a{3}").unwrap();
    assert!(re.is_match("aaa"));
    assert!(!re.is_match("aa"));
    assert!(re.is_match("aaaa")); // finds "aaa" within

    let re2 = FuzzyRegex::new("a{2,4}").unwrap();
    assert!(re2.is_match("aaa"));

    let re3 = FuzzyRegex::new("a{2,}").unwrap();
    assert!(re3.is_match("aaaaa"));
}

#[test]
fn test_zero_repetition() {
    let re = FuzzyRegex::new("a{0}").unwrap();
    assert!(re.is_match("b")); // matches empty at start

    let re2 = FuzzyRegex::new("a{0,3}").unwrap();
    assert!(re2.is_match(""));
    assert!(re2.is_match("aa"));
}

#[test]
fn test_large_repetition() {
    let re = FuzzyRegex::new("a{100}").unwrap();
    assert!(re.is_match(&"a".repeat(100)));

    let re2 = FuzzyRegex::new("a{50,}").unwrap();
    assert!(re2.is_match(&"a".repeat(100)));
}

#[test]
fn test_group_repetition() {
    let re = FuzzyRegex::new("(ab){2}").unwrap();
    assert!(re.is_match("abab"));
    assert!(!re.is_match("ab"));
}

#[test]
fn test_special_escapes() {
    // Tab and newline
    let re_tab = FuzzyRegex::new(r"\t").unwrap();
    assert!(re_tab.is_match("\t"));

    let re_newline = FuzzyRegex::new(r"\n").unwrap();
    assert!(re_newline.is_match("\n"));

    let re_cr = FuzzyRegex::new(r"\r").unwrap();
    assert!(re_cr.is_match("\r"));
}

#[test]
fn test_escaped_metacharacters_comprehensive() {
    let re = FuzzyRegex::new(r"\.").unwrap();
    assert!(re.is_match("."));
    assert!(!re.is_match("a"));

    let re2 = FuzzyRegex::new(r"\*").unwrap();
    assert!(re2.is_match("*"));

    let re3 = FuzzyRegex::new(r"\+").unwrap();
    assert!(re3.is_match("+"));

    let re4 = FuzzyRegex::new(r"\?").unwrap();
    assert!(re4.is_match("?"));

    let re5 = FuzzyRegex::new(r"\|").unwrap();
    assert!(re5.is_match("|"));

    let re6 = FuzzyRegex::new(r"\\").unwrap();
    assert!(re6.is_match("\\"));
}

#[test]
fn test_metachar_in_char_class() {
    // Metacharacters lose special meaning in char class
    let re = FuzzyRegex::new("[a.b]").unwrap();
    assert!(re.is_match("."));

    let re2 = FuzzyRegex::new("[a*b]").unwrap();
    assert!(re2.is_match("*"));

    let re3 = FuzzyRegex::new("[a$b]").unwrap();
    assert!(re3.is_match("$"));
}

#[test]
fn test_lookahead() {
    // Positive lookahead
    let re = FuzzyRegex::new("a(?=b)").unwrap();
    assert!(re.is_match("ab"));
    assert!(!re.is_match("ac"));

    // Negative lookahead
    let re2 = FuzzyRegex::new("a(?!b)").unwrap();
    assert!(re2.is_match("ac"));
    assert!(!re2.is_match("ab"));
}

#[test]
fn test_lookbehind() {
    // Positive lookbehind
    let re = FuzzyRegex::new("(?<=hello) world").unwrap();
    assert!(re.is_match("hello world"));
    assert!(!re.is_match("bye world"));

    // Match at correct position (after "hello ")
    let m = re.find("say hello world here").unwrap();
    assert_eq!(m.start(), 9);
    assert_eq!(m.end(), 15);

    // Negative lookbehind
    let re2 = FuzzyRegex::new("(?<!hello) world").unwrap();
    assert!(re2.is_match("bye world"));
    assert!(!re2.is_match("hello world"));
}

#[test]
fn test_lookbehind_fuzzy() {
    // Fuzzy lookbehind - match "world" preceded by "hello" with up to 1 error
    let re = FuzzyRegex::new("(?<=(?:hello){e<=1}) world").unwrap();

    assert!(re.is_match("hello world")); // Exact
    assert!(re.is_match("hallo world")); // 1 substitution in lookbehind
    assert!(re.is_match("helo world")); // 1 deletion in lookbehind
    assert!(re.is_match("helllo world")); // 1 insertion in lookbehind
    assert!(!re.is_match("goodbye world")); // No match - "goodbye" doesn't match "hello" within e<=1
}

#[test]
fn test_fuzzy_with_special_chars() {
    // Fuzzy with dot
    let re = FuzzyRegex::new(r"(?:a.b){e<=1}").unwrap();
    assert!(re.is_match("axb"));
    assert!(re.is_match("ab")); // 1 deletion

    // Fuzzy with anchors
    let re2 = FuzzyRegex::new(r"^(?:abc){e<=1}$").unwrap();
    assert!(re2.is_match("abd")); // 1 substitution
    assert!(re2.is_match("abcd")); // 1 insertion (this is correct - abc + d insertion)
}

#[test]
fn test_fuzzy_with_char_class_edge() {
    let re = FuzzyRegex::new(r"(?:[a-z]{3}){e<=1}").unwrap();
    assert!(re.is_match("ab")); // 1 deletion
    assert!(re.is_match("ab1")); // 1 substitution
}

#[test]
fn test_multiple_fuzzy_groups() {
    let re = FuzzyRegex::new(r"(?:ab){e<=1}(?:cd){e<=1}").unwrap();
    assert!(re.is_match("abcd"));
    assert!(re.is_match("xbcy")); // 1 error in each group
}

#[test]
fn test_pathological_no_backtrack() {
    // These should complete quickly without catastrophic backtracking
    let long_a = "a".repeat(30);

    let re = FuzzyRegex::new("(a+)+b").unwrap();
    assert!(re.is_match(&format!("{}b", long_a)));
    assert!(!re.is_match(&long_a)); // Should fail fast

    let re2 = FuzzyRegex::new("(a*)*b").unwrap();
    assert!(re2.is_match(&format!("{}b", long_a)));
}

#[test]
fn test_overlapping_capture_groups() {
    let re = FuzzyRegex::new("(a+)(a+)").unwrap();
    let m = re.find("aaaa").unwrap();
    assert_eq!(m.as_str(), "aaaa");
}

#[test]
fn test_prefix_empty_suffix() {
    let re = FuzzyRegex::new("x()*y").unwrap();
    assert!(re.is_match("xy"));

    let re2 = FuzzyRegex::new("x(a*)y").unwrap();
    assert!(re2.is_match("xy"));
}

#[test]
fn test_emoji_zwj_sequence() {
    // Family emoji with Zero-Width Joiner
    let re = FuzzyRegex::new("👨‍👩‍👧").unwrap();
    assert!(re.is_match("👨‍👩‍👧"));
}

#[test]
fn test_unicode_cyrillic_range() {
    let re = FuzzyRegex::new("[а-я]").unwrap();
    assert!(re.is_match("м"));
    assert!(!re.is_match("a")); // ASCII 'a' not in range
}

// ============================================
// Tests from verify_correctness.rs
// ============================================

#[test]
fn test_fuzzy_substitution_hallo() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let m = re.find("hallo world").unwrap();
    assert_eq!(m.as_str(), "hallo");
}

#[test]
fn test_fuzzy_substitution_hxllo() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let m = re.find("hxllo world").unwrap();
    assert_eq!(m.as_str(), "hxllo");
}

#[test]
fn test_fuzzy_substitution_quack() {
    let re = FuzzyRegex::new("(?:quick){e<=1}").unwrap();
    let m = re.find("The quack brown fox").unwrap();
    assert_eq!(m.as_str(), "quack");
}

#[test]
fn test_fuzzy_insertion_heello() {
    // "heello" can match as "eello" (1 sub h->e) or other ways
    // Engine finds a valid fuzzy match - any 5-char or 6-char substring within edit distance 1
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    assert!(re.is_match("heello world"));
}

#[test]
fn test_fuzzy_insertion_caat() {
    // "caat" can match "cat" with deletion (caa->cat)
    let re = FuzzyRegex::new("(?:cat){e<=1}").unwrap();
    assert!(re.is_match("caat sitting"));
}

#[test]
fn test_fuzzy_deletion_helo() {
    let re = FuzzyRegex::new("(?:hello){e<=2}").unwrap();
    let m = re.find("helo world").unwrap();
    assert_eq!(m.as_str(), "helo");
}

#[test]
fn test_fuzzy_deletion_wrld() {
    let re = FuzzyRegex::new("(?:world){e<=1}").unwrap();
    let m = re.find("wrld end").unwrap();
    assert_eq!(m.as_str(), "wrld");
}

#[test]
fn test_fuzzy_no_match_xyzzy() {
    let re = FuzzyRegex::new("(?:xyzzy){e<=1}").unwrap();
    assert!(!re.is_match("The quick brown fox"));
}

#[test]
fn test_fuzzy_no_match_abcdef() {
    let re = FuzzyRegex::new("(?:abcdef){e<=1}").unwrap();
    assert!(!re.is_match("nothing matches here"));
}

#[test]
fn test_fuzzy_dna_acgt() {
    let dna: String = (0..100)
        .map(|i| match i % 4 {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            _ => 'T',
        })
        .collect();
    let re = FuzzyRegex::new("(?:ACGT){e<=1}").unwrap();
    let m = re.find(&dna).unwrap();
    assert_eq!(m.as_str(), "ACGT");
}

#[test]
fn test_fuzzy_dna_long_sequence() {
    let dna: String = (0..100)
        .map(|i| match i % 4 {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            _ => 'T',
        })
        .collect();
    let re = FuzzyRegex::new("(?:ACGTACGT){e<=2}").unwrap();
    let m = re.find(&dna).unwrap();
    assert_eq!(m.as_str(), "ACGTACGT");
}

#[test]
fn test_fuzzy_dna_no_match_gggg() {
    let dna: String = (0..100)
        .map(|i| match i % 4 {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            _ => 'T',
        })
        .collect();
    let re = FuzzyRegex::new("(?:GGGG){e<=1}").unwrap();
    assert!(!re.is_match(&dna)); // No 4 consecutive Gs
}

#[test]
fn test_fuzzy_single_char_exact() {
    let re = FuzzyRegex::new("(?:a){e<=1}").unwrap();
    let m = re.find("a").unwrap();
    assert_eq!(m.as_str(), "a");
}

#[test]
fn test_fuzzy_single_char_substitution() {
    let re = FuzzyRegex::new("(?:a){e<=1}").unwrap();
    let m = re.find("b").unwrap();
    assert_eq!(m.as_str(), "b"); // 1 substitution
}

#[test]
fn test_fuzzy_deletion_from_pattern() {
    let re = FuzzyRegex::new("(?:ab){e<=1}").unwrap();
    let m = re.find("a").unwrap();
    assert_eq!(m.as_str(), "a"); // 1 deletion
}

#[test]
fn test_fuzzy_case_sensitivity_one_sub() {
    let re = FuzzyRegex::new("(?:Hello){e<=1}").unwrap();
    let m = re.find("hello world").unwrap();
    assert_eq!(m.as_str(), "hello"); // 1 sub for H->h
}

#[test]
fn test_fuzzy_case_sensitivity_too_many_errors() {
    let re = FuzzyRegex::new("(?:HELLO){e<=2}").unwrap();
    assert!(!re.is_match("hello world")); // needs 5 subs, only 2 allowed
}

#[test]
fn test_fuzzy_multiple_potential_matches() {
    let re = FuzzyRegex::new("(?:the){e<=1}").unwrap();
    let m = re.find("the them then").unwrap();
    assert_eq!(m.as_str(), "the");
}

#[test]
fn test_fuzzy_multiple_exact_matches() {
    let re = FuzzyRegex::new("(?:cat){e<=1}").unwrap();
    let m = re.find("cat bat rat cat").unwrap();
    assert_eq!(m.as_str(), "cat");
}

#[test]
fn test_fuzzy_long_text() {
    let long_text = "Lorem ipsum ".repeat(100);
    let re = FuzzyRegex::new("(?:Lorem){e<=2}").unwrap();
    let m = re.find(&long_text).unwrap();
    assert_eq!(m.as_str(), "Lorem");
}

#[test]
fn test_fuzzy_unicode_cafe() {
    let re = FuzzyRegex::new("(?:café){e<=1}").unwrap();
    let m = re.find("I love café au lait").unwrap();
    assert_eq!(m.as_str(), "café");
}

#[test]
fn test_fuzzy_unicode_naive() {
    // Engine finds "naïv" as a valid match (1 deletion of 'e')
    let re = FuzzyRegex::new("(?:naïve){e<=1}").unwrap();
    assert!(re.is_match("Don't be naïve"));
}

#[test]
fn test_fuzzy_pattern_with_context() {
    let re = FuzzyRegex::new("The (?:quick){e<=1} brown").unwrap();
    let m = re.find("The quack brown fox").unwrap();
    assert_eq!(m.as_str(), "The quack brown");
}

#[test]
fn test_fuzzy_pattern_followed_by_literal() {
    let re = FuzzyRegex::new("(?:hello){e<=1} world").unwrap();
    let m = re.find("hallo world!").unwrap();
    assert_eq!(m.as_str(), "hallo world");
}

// ============================================
// Tests from test_char_restriction.rs
// ============================================

#[test]
fn test_char_restriction_substitution_allowed() {
    // Substitution 'e' -> 'a' should be allowed (a is in [a-z])
    let re = FuzzyRegex::new(r"(?:hello){s<=1:[a-z]}").unwrap();
    let m = re.find("hallo").unwrap();
    assert_eq!(m.as_str(), "hallo");
}

#[test]
fn test_char_restriction_substitution_rejected() {
    // Substitution 'e' -> '3' should be rejected (3 is NOT in [a-z])
    let re = FuzzyRegex::new(r"(?:hello){s<=1:[a-z]}").unwrap();
    assert!(!re.is_match("h3llo"));
}

#[test]
fn test_char_restriction_insertion_allowed() {
    // Insertion 'o' at end should be allowed (o is in [a-z])
    let re = FuzzyRegex::new(r"^(?:hell){i<=1:[a-z]}$").unwrap();
    let m = re.find("hello").unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_char_restriction_insertion_rejected() {
    // Insertion '1' should be rejected (1 is NOT in [a-z])
    let re = FuzzyRegex::new(r"^(?:hell){i<=1:[a-z]}$").unwrap();
    assert!(!re.is_match("hell1"));
}

#[test]
fn test_char_restriction_general_error_allowed() {
    let re = FuzzyRegex::new(r"(?:hello){e<=1:[a-z]}").unwrap();
    let m = re.find("hallo").unwrap();
    assert_eq!(m.as_str(), "hallo");
}

#[test]
fn test_char_restriction_general_error_rejected() {
    let re = FuzzyRegex::new(r"(?:hello){e<=1:[a-z]}").unwrap();
    assert!(!re.is_match("h3llo"));
}

#[test]
fn test_char_restriction_digit_allowed() {
    let re = FuzzyRegex::new(r"(?:test){e<=1:\d}").unwrap();
    let m = re.find("t3st").unwrap();
    assert_eq!(m.as_str(), "t3st");
}

#[test]
fn test_char_restriction_digit_rejected() {
    let re = FuzzyRegex::new(r"(?:test){e<=1:\d}").unwrap();
    // 'a' is not a digit, so substitution should be rejected
    assert!(!re.is_match("tast"));
}

#[test]
fn test_char_restriction_exact_match_bypasses() {
    // Exact match should always work regardless of restriction
    let re = FuzzyRegex::new(r"(?:hello){e<=1:[a-z]}").unwrap();
    let m = re.find("hello").unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_char_restriction_deletion() {
    // Deletion: matching "hllo" by deleting 'e' from pattern
    let re = FuzzyRegex::new(r"(?:hello){d<=1:[a-z]}").unwrap();
    let m = re.find("hllo").unwrap();
    assert_eq!(m.as_str(), "hllo");
}

#[test]
fn test_char_restriction_transposition() {
    // Transposition "the" -> "teh"
    let re = FuzzyRegex::new(r"(?:the){e<=1:[a-z]}").unwrap();
    let m = re.find("teh").unwrap();
    assert_eq!(m.as_str(), "teh");
}

// ============================================
// Additional word boundary tests
// ============================================

#[test]
fn test_word_boundary_in_middle() {
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(re.is_match("a word here"));
}

#[test]
fn test_word_boundary_exact() {
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(re.is_match("word"));
}

#[test]
fn test_word_boundary_with_suffix_rejected() {
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(!re.is_match("words"));
}

#[test]
fn test_word_boundary_with_prefix_rejected() {
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(!re.is_match("sword"));
}

#[test]
fn test_word_boundary_both_rejected() {
    let re = FuzzyRegex::new(r"\bword\b").unwrap();
    assert!(!re.is_match("swords"));
}

#[test]
fn test_word_boundary_start_only() {
    let re = FuzzyRegex::new(r"\bword").unwrap();
    assert!(re.is_match("word!"));
}

#[test]
fn test_word_boundary_end_only() {
    let re = FuzzyRegex::new(r"word\b").unwrap();
    assert!(re.is_match("a word"));
}

#[test]
fn test_word_boundary_digits() {
    let re = FuzzyRegex::new(r"\b\d+\b").unwrap();
    assert!(re.is_match("test 123 here"));
    assert!(!re.is_match("test123"));
}

#[test]
fn test_word_boundary_quis() {
    let re = FuzzyRegex::new(r"\bquis\b").unwrap();
    assert!(re.is_match("quis nostrud"));
    assert!(!re.is_match("aliquis")); // embedded
}

#[test]
fn test_non_word_boundary_embedded() {
    let re = FuzzyRegex::new(r"\Bword\B").unwrap();
    assert!(re.is_match("swordsmith"));
    assert!(!re.is_match("word")); // standalone
    assert!(!re.is_match("sword")); // only at end
}

#[test]
fn test_non_word_boundary_not_at_start() {
    let re = FuzzyRegex::new(r"\Bword").unwrap();
    assert!(re.is_match("swords"));
}

#[test]
fn test_non_word_boundary_not_at_end() {
    let re = FuzzyRegex::new(r"word\B").unwrap();
    assert!(re.is_match("words"));
}

#[test]
fn test_non_word_boundary_digits_embedded() {
    let re = FuzzyRegex::new(r"\B\d+\B").unwrap();
    assert!(re.is_match("abc123def"));
}

#[test]
fn test_non_word_boundary_before_non_word() {
    let re = FuzzyRegex::new(r"\B!").unwrap();
    assert!(re.is_match("!"));
}

#[test]
fn test_non_word_boundary_after_non_word() {
    let re = FuzzyRegex::new(r"!\B").unwrap();
    assert!(re.is_match("!"));
}

#[test]
fn test_word_boundary_in_empty_string() {
    let re = FuzzyRegex::new(r"\b").unwrap();
    assert!(!re.is_match(""));
}

#[test]
fn test_non_word_boundary_with_space_start() {
    let re = FuzzyRegex::new(r"\B ").unwrap();
    assert!(re.is_match(" "));
}

#[test]
fn test_non_word_boundary_with_space_end() {
    let re = FuzzyRegex::new(r" \B").unwrap();
    assert!(re.is_match(" "));
}

#[test]
fn test_word_boundary_find_multiple() {
    let re = FuzzyRegex::new(r"\b\w+\b").unwrap();
    let matches: Vec<_> = re.find_iter("hello world test").collect();
    assert_eq!(matches.len(), 3);
    assert_eq!(matches[0].as_str(), "hello");
    assert_eq!(matches[1].as_str(), "world");
    assert_eq!(matches[2].as_str(), "test");
}

// ==================== transposition (t<=N) syntax tests ====================

#[test]
fn test_transposition_limit_matches() {
    // {t<=1} - allow up to 1 transposition
    let re = FuzzyRegex::new("(?:ab){t<=1}").unwrap();
    assert!(re.is_match("ab")); // exact
    assert!(re.is_match("ba")); // 1 transposition
}

#[test]
fn test_transposition_limit_combined() {
    // {e<=1,t<=1} - 1 edit total, must be a transposition
    let re = FuzzyRegex::new("(?:ab){e<=1,t<=1}").unwrap();
    assert!(re.is_match("ab")); // exact
    assert!(re.is_match("ba")); // transposition
}

#[test]
fn test_transposition_with_total_edits() {
    // {t<=1,e<=2} - at most 1 transposition, and at most 2 total edits
    let re = FuzzyRegex::new("(?:hello){t<=1,e<=2}").unwrap();
    assert!(re.is_match("hello")); // exact
    assert!(re.is_match("ehllo")); // 1 transposition
    assert!(re.is_match("hallo")); // 1 substitution
}

// ==================== cost-based (c<=N) syntax tests ====================

#[test]
fn test_cost_syntax_basic() {
    // {c<=2} - total cost <= 2 with equal weights (all ops cost 1)
    let re = FuzzyRegex::new("(?:hello){c<=2}").unwrap();
    assert!(re.is_match("hello")); // exact, cost=0
    assert!(re.is_match("hallo")); // 1 sub, cost=1
    assert!(re.is_match("helo")); // 1 del, cost=1
}

#[test]
fn test_cost_constraint_rejects_over_limit() {
    // {c<=1} - total cost <= 1, should reject 2+ edits
    let re = FuzzyRegex::new("(?:hi){c<=1}").unwrap();
    assert!(re.is_match("hi")); // exact, cost=0
    assert!(re.is_match("ho")); // 1 sub, cost=1
    // "xy" would need 2 substitutions (cost=2), should NOT match
    assert!(!re.is_match("xy"));
}

#[test]
fn test_weighted_cost_constraint() {
    // {2i+1d+1s<=3} - insertions cost 2, deletions and subs cost 1
    let re = FuzzyRegex::new("(?:ab){2i+1d+1s<=3}").unwrap();
    assert!(re.is_match("ab")); // exact, cost=0
    assert!(re.is_match("a")); // 1 del, cost=1
    assert!(re.is_match("abcd")); // 2 ins, cost=0
}

// =============================================================================
// greedy_first Tests
// =============================================================================

#[test]
fn test_greedy_first_basic() {
    let re = FuzzyRegexBuilder::new("(?:hello){e<=1}")
        .greedy_first(true)
        .build()
        .unwrap();
    assert!(re.is_match("hello world"));
    let m = re.find("hello world").unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_greedy_first_finds_first_match() {
    let re = FuzzyRegexBuilder::new("(?:cat){e<=1}")
        .greedy_first(true)
        .build()
        .unwrap();
    let m = re.find("cat bat cat").unwrap();
    assert_eq!(m.start(), 0);
    assert_eq!(m.end(), 3);
}

#[test]
fn test_greedy_first_vs_best_match() {
    let re_best = FuzzyRegexBuilder::new("(?:hello){e<=2}").build().unwrap();
    let re_greedy = FuzzyRegexBuilder::new("(?:hello){e<=2}")
        .greedy_first(true)
        .build()
        .unwrap();
    let text = "hello hello";
    let best = re_best.find(text);
    let greedy = re_greedy.find(text);
    assert!(best.is_some());
    assert!(greedy.is_some());
}

#[test]
fn test_greedy_first_no_match() {
    let re = FuzzyRegexBuilder::new("(?:xyz){e<=1}")
        .greedy_first(true)
        .build()
        .unwrap();
    assert!(!re.is_match("abc"));
}

#[test]
fn test_greedy_first_case_insensitive() {
    let re = FuzzyRegexBuilder::new("(?:hello){e<=1}")
        .case_insensitive(true)
        .greedy_first(true)
        .build()
        .unwrap();
    assert!(re.is_match("HELLO"));
}

// =============================================================================
// find_rev (reverse search) Tests
// =============================================================================

#[test]
fn test_find_rev_basic() {
    let re = FuzzyRegex::new("world").unwrap();
    let m = re.find_rev("hello world world").unwrap();
    assert_eq!(m.start(), 12);
    assert_eq!(m.end(), 17);
}

#[test]
fn test_find_rev_no_match() {
    let re = FuzzyRegex::new("xyz").unwrap();
    assert!(re.find_rev("hello world").is_none());
}

#[test]
fn test_find_rev_single_match() {
    let re = FuzzyRegex::new("hello").unwrap();
    let m = re.find_rev("say hello to the world").unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_find_rev_iter() {
    let re = FuzzyRegex::new("(?:cat|dog){e<=1}").unwrap();
    let matches = re.find_iter_rev("cat dog cat");
    // Should find matches in reverse order (rightmost first)
    // Due to fuzzy matching, results may vary
    assert_eq!(matches.len(), 3);
}

// =============================================================================
// find_at / find_from Tests
// =============================================================================

#[test]
fn test_find_at_basic() {
    let re = FuzzyRegex::new("hello").unwrap();
    let m = re.find_at("say hello world", 4).unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_find_at_no_match() {
    let re = FuzzyRegex::new("hello").unwrap();
    assert!(re.find_at("say hello world", 10).is_none());
}

#[test]
fn test_find_at_start_of_match() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let m = re.find_at("hello world", 0).unwrap();
    // Fuzzy match may give "hell" or "hello" depending on implementation
    assert!(m.as_str().starts_with("hell"));
}

#[test]
fn test_find_from_basic() {
    let re = FuzzyRegex::new("world").unwrap();
    let m = re.find_from("hello world", 6).unwrap();
    assert_eq!(m.as_str(), "world");
}

// =============================================================================
// fullmatch Tests
// =============================================================================

#[test]
fn test_fullmatch_basic() {
    let re = FuzzyRegex::new("hello").unwrap();
    let m = re.fullmatch("hello").unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_fullmatch_no_match() {
    let re = FuzzyRegex::new("hello").unwrap();
    assert!(re.fullmatch("hello world").is_none());
}

#[test]
fn test_fullmatch_fuzzy() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let m = re.fullmatch("helo").unwrap();
    assert_eq!(m.as_str(), "helo");
}

#[test]
fn test_fullmatch_at() {
    // Fullmatch at position requires the entire remaining text to match
    let re = FuzzyRegex::new("hello").unwrap();
    let m = re.fullmatch_at("ahello", 1).unwrap();
    assert_eq!(m.as_str(), "hello");
}

#[test]
fn test_fullmatch_at_no_match() {
    let re = FuzzyRegex::new("hello").unwrap();
    // The text after position 0 is "hello world", not just "hello"
    assert!(re.fullmatch_at("hello world", 0).is_none());
}

// =============================================================================
// split Tests
// =============================================================================

#[test]
fn test_split_basic() {
    let re = FuzzyRegex::new(",").unwrap();
    let parts: Vec<&str> = re.split("a,b,c").collect();
    assert_eq!(parts, vec!["a", "b", "c"]);
}

#[test]
fn test_split_no_match() {
    let re = FuzzyRegex::new(",").unwrap();
    let parts: Vec<&str> = re.split("abc").collect();
    assert_eq!(parts, vec!["abc"]);
}

#[test]
fn test_split_empty_parts() {
    let re = FuzzyRegex::new(",").unwrap();
    let parts: Vec<&str> = re.split("a,,b").collect();
    assert_eq!(parts, vec!["a", "", "b"]);
}

#[test]
fn test_split_fuzzy() {
    let re = FuzzyRegex::new("(?:,){e<=1}").unwrap();
    let parts: Vec<&str> = re.split("a;b;c").collect();
    assert!(parts.len() >= 2);
}

#[test]
fn test_splitn_basic() {
    let re = FuzzyRegex::new(",").unwrap();
    let parts = re.splitn("a,b,c,d", 2);
    assert_eq!(parts, vec!["a", "b,c,d"]);
}

#[test]
fn test_splitn_all() {
    let re = FuzzyRegex::new(",").unwrap();
    let parts = re.splitn("a,b,c", 10);
    assert_eq!(parts, vec!["a", "b", "c"]);
}

// =============================================================================
// replace Tests
// =============================================================================

#[test]
fn test_replace_basic() {
    let re = FuzzyRegex::new("world").unwrap();
    let result = re.replace("hello world", "there");
    assert_eq!(result, "hello there");
}

#[test]
fn test_replace_all() {
    let re = FuzzyRegex::new("o").unwrap();
    let result = re.replace_all("hello world", "x");
    assert_eq!(result, "hellx wxrld");
}

#[test]
fn test_replace_fuzzy() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let result = re.replace("say helo there", "hi");
    assert!(result.contains("hi"));
}

#[test]
fn test_replace_no_match() {
    let re = FuzzyRegex::new("xyz").unwrap();
    let result = re.replace("hello world", "replaced");
    assert_eq!(result, "hello world");
}

#[test]
fn test_replace_with_closure() {
    let re = FuzzyRegex::new(r"(\d+)").unwrap();
    let result = re.replace_all_with("price: 100 amount: 200", |caps| {
        let num: i32 = caps.get(1).unwrap().as_str().parse().unwrap();
        format!("${}", num)
    });
    assert_eq!(result, "price: $100 amount: $200");
}

// =============================================================================
// Builder Options Tests
// =============================================================================

#[test]
fn test_builder_ungreedy() {
    let re = FuzzyRegexBuilder::new("<.+>")
        .ungreedy(true)
        .build()
        .unwrap();
    let m = re.find("<a> <b>").unwrap();
    // With ungreedy, should match the shortest possible
    assert!(m.as_str().starts_with("<a"));
}

#[test]
fn test_builder_verbose() {
    // Verbose mode ignores whitespace in pattern
    let re = FuzzyRegexBuilder::new("(?x) hello world")
        .verbose(true)
        .build()
        .unwrap();
    // Pattern matches "helloworld" (whitespace in pattern is ignored)
    assert!(re.is_match("helloworld"));
    // Pattern with explicit \s still works
    let re2 = FuzzyRegexBuilder::new("(?x) hello \\s world")
        .verbose(true)
        .build()
        .unwrap();
    assert!(re2.is_match("hello world"));
}

#[test]
fn test_builder_dot_all() {
    let re = FuzzyRegexBuilder::new("a.b").dot_all(true).build().unwrap();
    assert!(re.is_match("a\nb"));
}

#[test]
fn test_builder_multi_line() {
    let re = FuzzyRegexBuilder::new("^hello")
        .multi_line(true)
        .build()
        .unwrap();
    assert!(re.is_match("foo\nhello"));
}

#[test]
fn test_builder_max_threads() {
    let re = FuzzyRegexBuilder::new("hello")
        .max_threads(100)
        .build()
        .unwrap();
    assert!(re.is_match("hello world"));
}

#[test]
fn test_builder_timeout() {
    use std::time::Duration;
    let re = FuzzyRegexBuilder::new("hello")
        .timeout(Duration::from_secs(1))
        .build()
        .unwrap();
    assert!(re.is_match("hello"));
}

// =============================================================================
// Match Flags Tests
// =============================================================================

#[test]
fn test_match_flags_best_match() {
    let re = FuzzyRegexBuilder::new("(?:hello){e<=2}").build().unwrap();
    let m = re.find("heallo hallo").unwrap();
    assert_eq!(m.as_str(), "heallo");
}

#[test]
fn test_match_flags_enhance_match() {
    let re = FuzzyRegexBuilder::new("(?:hello){e<=2}").build().unwrap();
    let m = re.find("helo").unwrap();
    assert_eq!(m.as_str(), "helo");
}

// =============================================================================
// is_match_at Tests
// =============================================================================

#[test]
fn test_is_match_at() {
    let re = FuzzyRegex::new("hello").unwrap();
    assert!(re.is_match_at("say hello world", 4));
    assert!(!re.is_match_at("say hello world", 10));
}

#[test]
fn test_is_match_at_fuzzy() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    assert!(re.is_match_at("ahello", 1));
}

// =============================================================================
// Byte Matching Tests
// =============================================================================

#[test]
fn test_is_match_bytes() {
    let re = FuzzyRegex::new("hello").unwrap();
    assert!(re.is_match_bytes(b"hello world"));
}

#[test]
fn test_find_bytes() {
    let re = FuzzyRegex::new("hello").unwrap();
    let m = re.find_bytes(b"hello world").unwrap();
    assert_eq!(m.start(), 0);
    assert_eq!(m.end(), 5);
}

#[test]
fn test_find_bytes_fuzzy() {
    let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
    let m = re.find_bytes(b"helo world").unwrap();
    assert_eq!(&b"helo world"[m.start()..m.end()], b"helo");
}

#[test]
fn test_find_iter_bytes() {
    let re = FuzzyRegex::new("o").unwrap();
    let matches: Vec<_> = re.find_iter_bytes(b"hello world").collect();
    assert_eq!(matches.len(), 2);
}

#[test]
fn test_cost_with_transposition() {
    // {1i+1d+1s+2t<=3} - transposition costs 2
    let re = FuzzyRegex::new("(?:ab){1i+1d+1s+2t<=3}").unwrap();
    assert!(re.is_match("ab")); // exact, cost=0
    assert!(re.is_match("ba")); // 1 transposition, cost=2
    assert!(re.is_match("ac")); // 1 sub, cost=1
}

#[test]
fn test_weighted_cost_high_insertion_cost() {
    // Insertions cost 3, other ops cost 1
    // {3i+1d+1s+1t<=3} means: 1 insertion OR 3 deletions OR 3 subs
    let re = FuzzyRegex::new("(?:ab){3i+1d+1s+1t<=3}").unwrap();
    assert!(re.is_match("ab")); // exact, cost=0
    assert!(re.is_match("abc")); // 1 ins, cost=3 (at limit)
    assert!(re.is_match("a")); // 1 del, cost=1
    assert!(re.is_match("cb")); // 1 sub, cost=1
}

#[test]
fn test_cost_exclusive_bound() {
    // {c<3} means cost must be < 3 (i.e., cost <= 2)
    let re = FuzzyRegex::new("(?:ab){c<3}").unwrap();
    assert!(re.is_match("ab")); // cost=0
    assert!(re.is_match("ac")); // 1 sub, cost=1
    assert!(re.is_match("cb")); // 1 sub, cost=1
    // 2 subs would be cost=2, which is < 3
}

#[test]
fn test_cost_zero_cost_operation() {
    // If an operation has cost 0, it shouldn't count toward the limit
    // {0i+1d+1s<=2} - insertions are free
    let re = FuzzyRegex::new("(?:ab){0i+1d+1s<=2}").unwrap();
    assert!(re.is_match("ab")); // exact
    assert!(re.is_match("abc")); // 1 ins, cost=0
    assert!(re.is_match("abcd")); // 2 ins, cost=0
    assert!(re.is_match("a")); // 1 del, cost=1
}