clipper2-rust 1.0.3

Pure Rust port of the Clipper2 polygon clipping and offsetting library
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
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
use super::*;

#[test]
fn test_get_segment_intersect_pt_basic() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(10, 10);
    let p3 = Point64::new(0, 10);
    let p4 = Point64::new(10, 0);
    let mut ip = Point64::new(0, 0);

    // Crossing lines should intersect at (5, 5)
    let result = get_segment_intersect_pt(p1, p2, p3, p4, &mut ip);
    assert!(result);
    assert_eq!(ip.x, 5);
    assert_eq!(ip.y, 5);
}

#[test]
fn test_get_segment_intersect_pt_parallel() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(10, 0);
    let p3 = Point64::new(0, 5);
    let p4 = Point64::new(10, 5);
    let mut ip = Point64::new(0, 0);

    // Parallel lines should not intersect
    let result = get_segment_intersect_pt(p1, p2, p3, p4, &mut ip);
    assert!(!result);
}

#[test]
fn test_get_segment_intersect_pt_endpoint() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(10, 0);
    let p3 = Point64::new(0, 0);
    let p4 = Point64::new(0, 10);
    let mut ip = Point64::new(0, 0);

    // Lines that meet at endpoint should return endpoint
    let result = get_segment_intersect_pt(p1, p2, p3, p4, &mut ip);
    assert!(result);
    assert_eq!(ip.x, 0);
    assert_eq!(ip.y, 0);
}

#[test]
fn test_is_collinear_true() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(5, 5);
    let p3 = Point64::new(10, 10);

    // Points on a diagonal line should be collinear
    assert!(is_collinear(p1, p2, p3));

    // Test horizontal line
    let p4 = Point64::new(0, 5);
    let p5 = Point64::new(5, 5);
    let p6 = Point64::new(10, 5);
    assert!(is_collinear(p4, p5, p6));

    // Test vertical line
    let p7 = Point64::new(5, 0);
    let p8 = Point64::new(5, 5);
    let p9 = Point64::new(5, 10);
    assert!(is_collinear(p7, p8, p9));
}

#[test]
fn test_is_collinear_false() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(5, 5);
    let p3 = Point64::new(10, 5);

    // Points forming a right angle should not be collinear
    assert!(!is_collinear(p1, p2, p3));
}

#[test]
fn test_point_in_polygon_inside() {
    // Square polygon
    let square = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
    ];

    // Point inside square
    let inside_pt = Point64::new(5, 5);
    assert_eq!(
        point_in_polygon(inside_pt, &square),
        PointInPolygonResult::IsInside
    );
}

#[test]
fn test_point_in_polygon_outside() {
    // Square polygon
    let square = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
    ];

    // Point outside square
    let outside_pt = Point64::new(15, 15);
    assert_eq!(
        point_in_polygon(outside_pt, &square),
        PointInPolygonResult::IsOutside
    );

    // Point to the left
    let left_pt = Point64::new(-5, 5);
    assert_eq!(
        point_in_polygon(left_pt, &square),
        PointInPolygonResult::IsOutside
    );
}

#[test]
fn test_point_in_polygon_on_edge() {
    // Square polygon
    let square = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
    ];

    // Point on edge
    let edge_pt = Point64::new(5, 0);
    assert_eq!(
        point_in_polygon(edge_pt, &square),
        PointInPolygonResult::IsOn
    );

    // Point at vertex
    let vertex_pt = Point64::new(0, 0);
    assert_eq!(
        point_in_polygon(vertex_pt, &square),
        PointInPolygonResult::IsOn
    );
}

#[test]
fn test_point_in_polygon_triangle() {
    // Triangle polygon
    let triangle = vec![Point64::new(0, 0), Point64::new(10, 0), Point64::new(5, 10)];

    // Point inside triangle
    let inside_pt = Point64::new(5, 3);
    assert_eq!(
        point_in_polygon(inside_pt, &triangle),
        PointInPolygonResult::IsInside
    );

    // Point outside triangle
    let outside_pt = Point64::new(1, 8);
    assert_eq!(
        point_in_polygon(outside_pt, &triangle),
        PointInPolygonResult::IsOutside
    );
}

#[test]
fn test_point_in_polygon_degenerate() {
    // Line (only 2 points) - should always be outside
    let line = vec![Point64::new(0, 0), Point64::new(10, 0)];

    let test_pt = Point64::new(5, 0);
    assert_eq!(
        point_in_polygon(test_pt, &line),
        PointInPolygonResult::IsOutside
    );
}

#[test]
fn test_get_location_basic() {
    let rect = Rect64::new(10, 10, 50, 50);
    let mut loc = Location::Inside;

    // Point inside rectangle
    let inside_pt = Point64::new(25, 25);
    let result = get_location(&rect, &inside_pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Inside);

    // Point to the left
    let left_pt = Point64::new(5, 25);
    let result = get_location(&rect, &left_pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Left);

    // Point to the right
    let right_pt = Point64::new(55, 25);
    let result = get_location(&rect, &right_pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Right);

    // Point above
    let top_pt = Point64::new(25, 5);
    let result = get_location(&rect, &top_pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Top);

    // Point below
    let bottom_pt = Point64::new(25, 55);
    let result = get_location(&rect, &bottom_pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Bottom);
}

#[test]
fn test_get_location_on_edge() {
    let rect = Rect64::new(10, 10, 50, 50);
    let mut loc = Location::Inside;

    // Point on left edge
    let left_edge_pt = Point64::new(10, 25);
    let result = get_location(&rect, &left_edge_pt, &mut loc);
    assert!(!result); // Returns false when on edge
    assert_eq!(loc, Location::Left);

    // Point on right edge
    let right_edge_pt = Point64::new(50, 25);
    let result = get_location(&rect, &right_edge_pt, &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Right);

    // Point on top edge
    let top_edge_pt = Point64::new(25, 10);
    let result = get_location(&rect, &top_edge_pt, &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Top);

    // Point on bottom edge
    let bottom_edge_pt = Point64::new(25, 50);
    let result = get_location(&rect, &bottom_edge_pt, &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Bottom);
}

#[test]
fn test_is_horizontal() {
    let p1 = Point64::new(0, 5);
    let p2 = Point64::new(10, 5);
    assert!(is_horizontal(&p1, &p2));

    let p3 = Point64::new(0, 5);
    let p4 = Point64::new(10, 10);
    assert!(!is_horizontal(&p3, &p4));
}

#[test]
fn test_fill_rule_default() {
    assert_eq!(FillRule::default(), FillRule::EvenOdd);
}

#[test]
fn test_fill_rule_variants() {
    let rules = [
        FillRule::EvenOdd,
        FillRule::NonZero,
        FillRule::Positive,
        FillRule::Negative,
    ];
    assert_eq!(rules.len(), 4);

    // Test each variant is unique
    for i in 0..rules.len() {
        for j in (i + 1)..rules.len() {
            assert_ne!(rules[i], rules[j]);
        }
    }
}

#[test]
fn test_clipper2_exception() {
    let err = Clipper2Exception::new("test error");
    assert_eq!(err.description(), "test error");
    assert_eq!(err.to_string(), "Clipper2Exception: test error");
}

#[test]
fn test_point_creation() {
    let p1 = Point::new(10i32, 20i32);
    assert_eq!(p1.x, 10);
    assert_eq!(p1.y, 20);

    let p2 = Point::<f64>::zero();
    assert_eq!(p2.x, 0.0);
    assert_eq!(p2.y, 0.0);
}

#[test]
fn test_point_operations() {
    let p1 = Point::new(10i32, 20i32);
    let p2 = Point::new(5i32, 15i32);

    let sum = p1 + p2;
    assert_eq!(sum.x, 15);
    assert_eq!(sum.y, 35);

    let diff = p1 - p2;
    assert_eq!(diff.x, 5);
    assert_eq!(diff.y, 5);

    let neg = -p1;
    assert_eq!(neg.x, -10);
    assert_eq!(neg.y, -20);
}

#[test]
fn test_point_scale() {
    let p1 = Point::new(10i32, 20i32);
    let scaled = p1.scale(2.5f64);
    assert_eq!(scaled.x, 25.0);
    assert_eq!(scaled.y, 50.0);
}

#[test]
fn test_rect_creation() {
    let rect = Rect::new(0i32, 0i32, 100i32, 200i32);
    assert_eq!(rect.left, 0);
    assert_eq!(rect.top, 0);
    assert_eq!(rect.right, 100);
    assert_eq!(rect.bottom, 200);
}

#[test]
fn test_rect_properties() {
    let rect = Rect::new(10i32, 20i32, 110i32, 220i32);

    assert!(rect.is_valid());
    assert_eq!(rect.width(), 100);
    assert_eq!(rect.height(), 200);
    assert!(!rect.is_empty());

    let empty_rect = Rect::new(100i32, 200i32, 50i32, 150i32);
    assert!(empty_rect.is_valid()); // Geometrically invalid but not sentinel invalid
    assert!(empty_rect.is_empty());
}

#[test]
fn test_rect_modification() {
    let mut rect = Rect::new(0i32, 0i32, 50i32, 100i32);

    rect.set_width(200);
    assert_eq!(rect.right, 200);
    assert_eq!(rect.width(), 200);

    rect.set_height(300);
    assert_eq!(rect.bottom, 300);
    assert_eq!(rect.height(), 300);
}

#[test]
fn test_rect_scale() {
    let mut rect = RectD::new(10.0, 20.0, 30.0, 40.0);
    rect.scale(2.0);
    assert_eq!(rect.left, 20.0);
    assert_eq!(rect.top, 40.0);
    assert_eq!(rect.right, 60.0);
    assert_eq!(rect.bottom, 80.0);
}

#[test]
fn test_type_aliases() {
    let p64 = Point64::new(100, 200);
    let pd = PointD::new(10.5, 20.5);
    let r64 = Rect64::new(0, 0, 100, 200);
    let rd = RectD::new(0.0, 0.0, 100.0, 200.0);

    assert_eq!(p64.x, 100);
    assert_eq!(pd.x, 10.5);
    assert_eq!(r64.width(), 100);
    assert_eq!(rd.width(), 100.0);
}

#[test]
fn test_path_types() {
    let mut path64: Path64 = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
    ];

    path64.push(Point64::new(0, 100));
    assert_eq!(path64.len(), 4);

    let paths64: Paths64 = vec![path64];
    assert_eq!(paths64.len(), 1);
    assert_eq!(paths64[0].len(), 4);
}

#[test]
fn test_invalid_points() {
    assert_eq!(INVALID_POINT64.x, i64::MAX);
    assert_eq!(INVALID_POINT64.y, i64::MAX);
    assert_eq!(INVALID_POINTD.x, f64::MAX);
    assert_eq!(INVALID_POINTD.y, f64::MAX);
}

#[test]
fn test_mid_point() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(100, 200);
    let mid = mid_point(p1, p2);
    assert_eq!(mid.x, 50);
    assert_eq!(mid.y, 100);

    let p3 = PointD::new(1.0, 3.0);
    let p4 = PointD::new(5.0, 7.0);
    let mid2 = mid_point(p3, p4);
    assert_eq!(mid2.x, 3.0);
    assert_eq!(mid2.y, 5.0);
}

#[test]
fn test_cross_product_three_points() {
    // Test with points that form a right turn (negative cross product)
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(1, 0);
    let p3 = Point64::new(1, 1);
    let cross = cross_product_three_points(p1, p2, p3);
    assert_eq!(cross, 1.0);

    // Test with points that form a left turn (positive cross product)
    let p4 = Point64::new(0, 0);
    let p5 = Point64::new(1, 0);
    let p6 = Point64::new(1, -1);
    let cross2 = cross_product_three_points(p4, p5, p6);
    assert_eq!(cross2, -1.0);

    // Test collinear points (zero cross product)
    let p7 = Point64::new(0, 0);
    let p8 = Point64::new(1, 1);
    let p9 = Point64::new(2, 2);
    let cross3 = cross_product_three_points(p7, p8, p9);
    assert_eq!(cross3, 0.0);
}

#[test]
fn test_cross_product_two_vectors() {
    let vec1 = Point64::new(1, 0);
    let vec2 = Point64::new(0, 1);
    let cross = cross_product_two_vectors(vec1, vec2);

    // Check actual calculation: vec1.y * vec2.x - vec2.y * vec1.x = 0*0 - 1*1 = -1
    assert_eq!(cross, -1.0);

    let vec3 = Point64::new(2, 3);
    let vec4 = Point64::new(4, 5);
    let cross2 = cross_product_two_vectors(vec3, vec4);
    // vec3.y * vec4.x - vec4.y * vec3.x = 3*4 - 5*2 = 12 - 10 = 2
    assert_eq!(cross2, 2.0);
}

#[test]
fn test_dot_product_three_points() {
    // Test with perpendicular vectors (dot product = 0)
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(1, 0);
    let p3 = Point64::new(1, 1);
    let dot = dot_product_three_points(p1, p2, p3);
    assert_eq!(dot, 0.0); // (1,0) . (0,1) = 0

    // Test with parallel vectors (positive dot product)
    let p4 = Point64::new(0, 0);
    let p5 = Point64::new(1, 0);
    let p6 = Point64::new(2, 0);
    let dot2 = dot_product_three_points(p4, p5, p6);
    assert_eq!(dot2, 1.0); // (1,0) . (1,0) = 1

    // Test with opposite vectors (negative dot product)
    let p7 = Point64::new(0, 0);
    let p8 = Point64::new(1, 0);
    let p9 = Point64::new(0, 0);
    let dot3 = dot_product_three_points(p7, p8, p9);
    assert_eq!(dot3, -1.0); // (1,0) . (-1,0) = -1
}

#[test]
fn test_dot_product_two_vectors() {
    let vec1 = Point64::new(3, 4);
    let vec2 = Point64::new(2, 1);
    let dot = dot_product_two_vectors(vec1, vec2);
    assert_eq!(dot, 10.0); // 3*2 + 4*1 = 6 + 4 = 10

    // Test with perpendicular vectors
    let vec3 = Point64::new(1, 0);
    let vec4 = Point64::new(0, 1);
    let dot2 = dot_product_two_vectors(vec3, vec4);
    assert_eq!(dot2, 0.0);
}

#[test]
fn test_rect_validity() {
    // Test valid rectangle creation
    let valid_rect = Rect64::new_with_validity(true);
    assert!(valid_rect.is_valid());
    assert_eq!(valid_rect.left, 0);
    assert_eq!(valid_rect.right, 0);

    // Test invalid rectangle creation
    let invalid_rect = Rect64::new_with_validity(false);
    assert!(!invalid_rect.is_valid());

    // Test invalid rectangle factory method
    let invalid_rect2 = Rect64::invalid();
    assert!(!invalid_rect2.is_valid());
}

#[test]
fn test_rect_midpoint() {
    let rect = Rect64::new(10, 20, 30, 40);
    let mid = rect.mid_point();
    assert_eq!(mid.x, 20); // (10 + 30) / 2
    assert_eq!(mid.y, 30); // (20 + 40) / 2
}

#[test]
fn test_rect_as_path() {
    let rect = Rect64::new(0, 0, 100, 200);
    let path = rect.as_path();
    assert_eq!(path.len(), 4);

    // Clockwise from top-left
    assert_eq!(path[0], Point64::new(0, 0)); // top-left
    assert_eq!(path[1], Point64::new(100, 0)); // top-right
    assert_eq!(path[2], Point64::new(100, 200)); // bottom-right
    assert_eq!(path[3], Point64::new(0, 200)); // bottom-left
}

#[test]
fn test_rect_contains_point() {
    let rect = Rect64::new(10, 10, 100, 100);

    // Point inside (exclusive bounds)
    assert!(rect.contains_point(&Point64::new(50, 50)));

    // Points on edges should not be contained (exclusive)
    assert!(!rect.contains_point(&Point64::new(10, 50))); // left edge
    assert!(!rect.contains_point(&Point64::new(100, 50))); // right edge
    assert!(!rect.contains_point(&Point64::new(50, 10))); // top edge
    assert!(!rect.contains_point(&Point64::new(50, 100))); // bottom edge

    // Points outside
    assert!(!rect.contains_point(&Point64::new(5, 50)));
    assert!(!rect.contains_point(&Point64::new(150, 50)));
}

#[test]
fn test_rect_contains_rect() {
    let outer = Rect64::new(0, 0, 100, 100);
    let inner = Rect64::new(10, 10, 90, 90);
    let overlapping = Rect64::new(50, 50, 150, 150);
    let outside = Rect64::new(200, 200, 300, 300);

    assert!(outer.contains_rect(&inner));
    assert!(!outer.contains_rect(&overlapping));
    assert!(!outer.contains_rect(&outside));

    // Same rectangle should contain itself
    assert!(outer.contains_rect(&outer));
}

#[test]
fn test_rect_intersects() {
    let rect1 = Rect64::new(0, 0, 100, 100);
    let rect2 = Rect64::new(50, 50, 150, 150); // overlapping
    let rect3 = Rect64::new(200, 200, 300, 300); // separate
    let rect4 = Rect64::new(100, 0, 200, 100); // touching edge

    assert!(rect1.intersects(&rect2));
    assert!(!rect1.intersects(&rect3));
    assert!(rect1.intersects(&rect4)); // touching edges do intersect

    // Rectangle intersects with itself
    assert!(rect1.intersects(&rect1));
}

#[test]
fn test_rect_equality() {
    let rect1 = Rect64::new(10, 20, 30, 40);
    let rect2 = Rect64::new(10, 20, 30, 40);
    let rect3 = Rect64::new(10, 20, 30, 41);

    assert_eq!(rect1, rect2);
    assert_ne!(rect1, rect3);
}

#[test]
fn test_rect_union_operator() {
    let mut rect1 = Rect64::new(0, 0, 50, 50);
    let rect2 = Rect64::new(25, 25, 100, 100);

    rect1 += rect2;

    // Result should be bounding box of both rectangles
    assert_eq!(rect1.left, 0);
    assert_eq!(rect1.top, 0);
    assert_eq!(rect1.right, 100);
    assert_eq!(rect1.bottom, 100);
}

#[test]
fn test_constants() {
    use constants::*;

    assert!((PI - std::f64::consts::PI).abs() < 0.000_000_1);
    assert_eq!(CLIPPER2_MAX_DEC_PRECISION, 8);
    assert_eq!(MIN_COORD, -MAX_COORD);
    assert_eq!(INVALID, i64::MAX);
    // Constants are verified at compile time - these runtime checks are redundant
}

#[test]
fn test_do_error() {
    use errors::*;

    let result = do_error(PRECISION_ERROR_I);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().description(), PRECISION_ERROR);

    let result2 = do_error(SCALE_ERROR_I);
    assert!(result2.is_err());
    assert_eq!(result2.unwrap_err().description(), SCALE_ERROR);

    let result3 = do_error(999); // unknown error
    assert!(result3.is_err());
    assert_eq!(result3.unwrap_err().description(), "Unknown error");
}

#[test]
fn test_tri_sign() {
    assert_eq!(tri_sign(10), 1);
    assert_eq!(tri_sign(-10), -1);
    assert_eq!(tri_sign(0), 0);
    assert_eq!(tri_sign(i64::MAX), 1);
    assert_eq!(tri_sign(i64::MIN), -1);
    assert_eq!(tri_sign(1), 1);
    assert_eq!(tri_sign(-1), -1);
}

#[test]
fn test_multiply_u64() {
    // Test simple cases
    let result = multiply_u64(0, 0);
    assert_eq!(result.hi, 0);
    assert_eq!(result.lo, 0);

    let result = multiply_u64(1, 1);
    assert_eq!(result.hi, 0);
    assert_eq!(result.lo, 1);

    let result = multiply_u64(10, 20);
    assert_eq!(result.hi, 0);
    assert_eq!(result.lo, 200);

    // Test case that would overflow 64-bit
    let result = multiply_u64(u64::MAX, 2);
    assert_eq!(result.hi, 1);
    assert_eq!(result.lo, u64::MAX - 1);

    // Test maximum values
    let result = multiply_u64(u64::MAX, u64::MAX);
    assert_eq!(result.hi, u64::MAX - 1);
    assert_eq!(result.lo, 1);
}

#[test]
fn test_products_are_equal() {
    // Test basic equality
    assert!(products_are_equal(2, 3, 6, 1));
    assert!(products_are_equal(2, 3, 1, 6));
    assert!(products_are_equal(4, 5, 10, 2));

    // Test basic inequality
    assert!(!products_are_equal(2, 3, 7, 1));
    assert!(!products_are_equal(4, 5, 10, 3));

    // Test with zero values
    assert!(products_are_equal(0, 5, 0, 10));
    assert!(products_are_equal(5, 0, 10, 0));
    assert!(products_are_equal(0, 5, 1, 0)); // Both products are 0
    assert!(!products_are_equal(0, 5, 1, 1)); // 0 != 1

    // Test with negative values
    assert!(products_are_equal(-2, 3, 2, -3));
    assert!(products_are_equal(-2, -3, 2, 3));
    assert!(!products_are_equal(-2, 3, 2, 3));

    // Test large values that might cause overflow
    let large = 1000000000i64;
    assert!(products_are_equal(large, 2, 2 * large, 1));
    assert!(products_are_equal(large, large, large * large, 1));

    // Test edge cases with max values
    assert!(products_are_equal(i64::MAX, 0, 0, i64::MAX));
    assert!(products_are_equal(i64::MIN, 0, 0, i64::MIN));

    // Test sign differentiation - this is important for the algorithm
    assert!(products_are_equal(1, -1, -1, 1)); // Both products are -1
    assert!(products_are_equal(-1, -1, 1, 1)); // Both positive results
    assert!(!products_are_equal(1, -1, 1, 1)); // -1 != 1
}

#[test]
fn test_strip_duplicates_path() {
    // Test open path with duplicates
    let mut open_path = vec![
        Point64::new(0, 0),
        Point64::new(0, 0), // duplicate
        Point64::new(10, 10),
        Point64::new(10, 10), // duplicate
        Point64::new(20, 20),
    ];
    strip_duplicates_path(&mut open_path, false);
    assert_eq!(open_path.len(), 3);
    assert_eq!(open_path[0], Point64::new(0, 0));
    assert_eq!(open_path[1], Point64::new(10, 10));
    assert_eq!(open_path[2], Point64::new(20, 20));

    // Test closed path with duplicates including wrap-around
    let mut closed_path = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
        Point64::new(0, 0), // should be removed for closed path
    ];
    strip_duplicates_path(&mut closed_path, true);
    assert_eq!(closed_path.len(), 4);
    assert_eq!(closed_path[0], Point64::new(0, 0));
    assert_eq!(closed_path[3], Point64::new(0, 10));

    // Test path with no duplicates
    let mut no_dups = vec![
        Point64::new(0, 0),
        Point64::new(10, 10),
        Point64::new(20, 20),
    ];
    let original = no_dups.clone();
    strip_duplicates_path(&mut no_dups, false);
    assert_eq!(no_dups, original);

    // Test empty path
    let mut empty: Path64 = vec![];
    strip_duplicates_path(&mut empty, true);
    assert!(empty.is_empty());

    // Test single point path
    let mut single = vec![Point64::new(0, 0)];
    strip_duplicates_path(&mut single, true);
    assert_eq!(single.len(), 1);
}

#[test]
fn test_strip_duplicates_paths() {
    let mut paths = vec![
        vec![
            Point64::new(0, 0),
            Point64::new(0, 0), // duplicate
            Point64::new(10, 10),
        ],
        vec![
            Point64::new(20, 20),
            Point64::new(30, 30),
            Point64::new(20, 20), // wrap-around duplicate
        ],
    ];

    strip_duplicates_paths(&mut paths, true);

    // First path should have duplicate removed
    assert_eq!(paths[0].len(), 2);
    assert_eq!(paths[0][0], Point64::new(0, 0));
    assert_eq!(paths[0][1], Point64::new(10, 10));

    // Second path should have wrap-around duplicate removed
    assert_eq!(paths[1].len(), 2);
    assert_eq!(paths[1][0], Point64::new(20, 20));
    assert_eq!(paths[1][1], Point64::new(30, 30));
}

#[test]
fn test_check_precision_range() {
    use constants::CLIPPER2_MAX_DEC_PRECISION;
    use errors::PRECISION_ERROR_I;

    // Test valid precision - should not change
    let mut precision = 5;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, 5);
    assert_eq!(error_code, 0);

    // Test maximum valid precision
    let mut precision = CLIPPER2_MAX_DEC_PRECISION;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, 0);

    // Test minimum valid precision
    let mut precision = -CLIPPER2_MAX_DEC_PRECISION;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, -CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, 0);

    // Test positive overflow - should clamp and set error
    let mut precision = CLIPPER2_MAX_DEC_PRECISION + 1;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, PRECISION_ERROR_I);

    // Test negative overflow - should clamp and set error
    let mut precision = -CLIPPER2_MAX_DEC_PRECISION - 1;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, -CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, PRECISION_ERROR_I);

    // Test extreme positive value
    let mut precision = i32::MAX;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, PRECISION_ERROR_I);

    // Test extreme negative value
    let mut precision = i32::MIN;
    let mut error_code = 0;
    check_precision_range(&mut precision, &mut error_code);
    assert_eq!(precision, -CLIPPER2_MAX_DEC_PRECISION);
    assert_eq!(error_code, PRECISION_ERROR_I);
}

#[test]
fn test_check_precision_range_simple() {
    use constants::CLIPPER2_MAX_DEC_PRECISION;

    // Test convenience function
    let mut precision = CLIPPER2_MAX_DEC_PRECISION + 5;
    check_precision_range_simple(&mut precision);
    assert_eq!(precision, CLIPPER2_MAX_DEC_PRECISION);

    let mut precision = -CLIPPER2_MAX_DEC_PRECISION - 3;
    check_precision_range_simple(&mut precision);
    assert_eq!(precision, -CLIPPER2_MAX_DEC_PRECISION);

    let mut precision = 3;
    check_precision_range_simple(&mut precision);
    assert_eq!(precision, 3); // Should remain unchanged
}

#[test]
fn test_get_bounds_path() {
    // Test basic rectangular path
    let path: Path64 = vec![
        Point64::new(10, 20),
        Point64::new(100, 30),
        Point64::new(50, 80),
        Point64::new(0, 10),
    ];

    let bounds = get_bounds_path(&path);
    assert_eq!(bounds.left, 0);
    assert_eq!(bounds.top, 10);
    assert_eq!(bounds.right, 100);
    assert_eq!(bounds.bottom, 80);

    // Test single point path
    let single_path: Path64 = vec![Point64::new(42, 37)];
    let single_bounds = get_bounds_path(&single_path);
    assert_eq!(single_bounds.left, 42);
    assert_eq!(single_bounds.top, 37);
    assert_eq!(single_bounds.right, 42);
    assert_eq!(single_bounds.bottom, 37);

    // Test empty path - should return invalid bounds
    let empty_path: Path64 = vec![];
    let empty_bounds = get_bounds_path(&empty_path);
    assert_eq!(empty_bounds.left, i64::MAX);
    assert_eq!(empty_bounds.top, i64::MAX);
    assert_eq!(empty_bounds.right, i64::MIN);
    assert_eq!(empty_bounds.bottom, i64::MIN);
}

#[test]
fn test_get_bounds_path_double() {
    // Test with floating-point path
    let path: PathD = vec![
        PointD::new(10.5, 20.7),
        PointD::new(100.3, 30.1),
        PointD::new(50.9, 80.4),
        PointD::new(0.2, 10.8),
    ];

    let bounds = get_bounds_path(&path);
    assert_eq!(bounds.left, 0.2);
    assert_eq!(bounds.top, 10.8);
    assert_eq!(bounds.right, 100.3);
    assert_eq!(bounds.bottom, 80.4);
}

#[test]
fn test_get_bounds_paths() {
    // Test multiple paths
    let paths: Paths64 = vec![
        vec![Point64::new(0, 0), Point64::new(50, 25)],
        vec![Point64::new(25, 50), Point64::new(100, 75)],
        vec![Point64::new(-10, -5), Point64::new(30, 40)],
    ];

    let bounds = get_bounds_paths(&paths);
    assert_eq!(bounds.left, -10);
    assert_eq!(bounds.top, -5);
    assert_eq!(bounds.right, 100);
    assert_eq!(bounds.bottom, 75);

    // Test empty paths
    let empty_paths: Paths64 = vec![];
    let empty_bounds = get_bounds_paths(&empty_paths);
    assert_eq!(empty_bounds.left, i64::MAX);
    assert_eq!(empty_bounds.right, i64::MIN);

    // Test paths with empty paths inside
    let mixed_paths: Paths64 = vec![
        vec![Point64::new(10, 20)],
        vec![], // empty path
        vec![Point64::new(30, 40)],
    ];
    let mixed_bounds = get_bounds_paths(&mixed_paths);
    assert_eq!(mixed_bounds.left, 10);
    assert_eq!(mixed_bounds.top, 20);
    assert_eq!(mixed_bounds.right, 30);
    assert_eq!(mixed_bounds.bottom, 40);
}

#[test]
fn test_get_bounds_path_convert() {
    // Test converting from i32 path to i64 bounds
    let path32: Path<i32> = vec![
        Point::new(10i32, 20i32),
        Point::new(100i32, 30i32),
        Point::new(50i32, 80i32),
    ];

    let bounds64: Rect64 = get_bounds_path_convert(&path32);
    assert_eq!(bounds64.left, 10i64);
    assert_eq!(bounds64.top, 20i64);
    assert_eq!(bounds64.right, 100i64);
    assert_eq!(bounds64.bottom, 80i64);

    // Test converting from f32 path to f64 bounds
    let pathf32: Path<f32> = vec![Point::new(10.5f32, 20.7f32), Point::new(100.3f32, 30.1f32)];

    let boundsf64: RectD = get_bounds_path_convert(&pathf32);
    // Use a more generous epsilon for f32 to f64 conversion
    const TOLERANCE: f64 = 1e-6;
    assert!((boundsf64.left - 10.5).abs() < TOLERANCE);
    assert!((boundsf64.top - 20.700000762939453).abs() < TOLERANCE); // f32 precision loss
    assert!((boundsf64.right - 100.30000305175781).abs() < TOLERANCE);
    assert!((boundsf64.bottom - 30.100000381469727).abs() < TOLERANCE);
}

#[test]
fn test_get_bounds_paths_convert() {
    // Test converting multiple paths
    let paths32: Paths<i32> = vec![
        vec![Point::new(10i32, 20i32), Point::new(50i32, 25i32)],
        vec![Point::new(25i32, 50i32), Point::new(100i32, 75i32)],
    ];

    let bounds64: Rect64 = get_bounds_paths_convert(&paths32);
    assert_eq!(bounds64.left, 10i64);
    assert_eq!(bounds64.top, 20i64);
    assert_eq!(bounds64.right, 100i64);
    assert_eq!(bounds64.bottom, 75i64);
}

#[test]
fn test_get_bounds_extreme_values() {
    // Test with extreme coordinate values
    let extreme_path: Path64 = vec![
        Point64::new(i64::MIN + 100, i64::MIN + 200),
        Point64::new(i64::MAX - 100, i64::MAX - 200),
        Point64::new(0, 0),
    ];

    let bounds = get_bounds_path(&extreme_path);
    assert_eq!(bounds.left, i64::MIN + 100);
    assert_eq!(bounds.top, i64::MIN + 200);
    assert_eq!(bounds.right, i64::MAX - 100);
    assert_eq!(bounds.bottom, i64::MAX - 200);
}

#[test]
fn test_get_bounds_negative_coordinates() {
    // Test with all negative coordinates
    let negative_path: Path64 = vec![
        Point64::new(-100, -200),
        Point64::new(-50, -150),
        Point64::new(-75, -175),
    ];

    let bounds = get_bounds_path(&negative_path);
    assert_eq!(bounds.left, -100);
    assert_eq!(bounds.top, -200);
    assert_eq!(bounds.right, -50);
    assert_eq!(bounds.bottom, -150);
}

#[test]
fn test_get_bounds_identical_points() {
    // Test with identical points (degenerate case)
    let identical_path: Path64 = vec![
        Point64::new(42, 37),
        Point64::new(42, 37),
        Point64::new(42, 37),
    ];

    let bounds = get_bounds_path(&identical_path);
    assert_eq!(bounds.left, 42);
    assert_eq!(bounds.top, 37);
    assert_eq!(bounds.right, 42);
    assert_eq!(bounds.bottom, 37);

    // Verify it results in a valid zero-area rectangle
    assert_eq!(bounds.width(), 0);
    assert_eq!(bounds.height(), 0);
    assert!(bounds.is_empty()); // Zero-size rectangles are empty when left==right or top==bottom
}

#[test]
fn test_sqr() {
    // Test basic integer squaring
    assert_eq!(sqr(5i64), 25.0);
    assert_eq!(sqr(-3i32), 9.0);
    assert_eq!(sqr(0i64), 0.0);

    // Test floating point squaring
    assert_eq!(sqr(2.5f64), 6.25);
    assert_eq!(sqr(-1.5f32), 2.25);

    // Test large values
    assert_eq!(sqr(1000i64), 1000000.0);
}

#[test]
fn test_distance_sqr() {
    // Test basic distance
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(3, 4);
    assert_eq!(distance_sqr(p1, p2), 25.0); // 3^2 + 4^2 = 9 + 16 = 25

    // Test zero distance
    let p3 = Point64::new(10, 20);
    let p4 = Point64::new(10, 20);
    assert_eq!(distance_sqr(p3, p4), 0.0);

    // Test negative coordinates
    let p5 = Point64::new(-5, -5);
    let p6 = Point64::new(-8, -1);
    assert_eq!(distance_sqr(p5, p6), 25.0); // (-3)^2 + 4^2 = 9 + 16 = 25

    // Test with floating point
    let pf1 = PointD::new(0.0, 0.0);
    let pf2 = PointD::new(3.0, 4.0);
    assert_eq!(distance_sqr(pf1, pf2), 25.0);
}

#[test]
fn test_perpendicular_distance_from_line_sqr() {
    // Test perpendicular distance to horizontal line
    let pt = Point64::new(5, 10);
    let line1 = Point64::new(0, 5);
    let line2 = Point64::new(10, 5);

    let dist_sqr = perpendicular_distance_from_line_sqr(pt, line1, line2);
    assert_eq!(dist_sqr, 25.0); // Distance of 5 squared = 25

    // Test point on line (should be 0 distance)
    let pt_on_line = Point64::new(5, 5);
    let dist_on_line = perpendicular_distance_from_line_sqr(pt_on_line, line1, line2);
    assert_eq!(dist_on_line, 0.0);

    // Test degenerate line (both points same)
    let degenerate_dist = perpendicular_distance_from_line_sqr(pt, line1, line1);
    assert_eq!(degenerate_dist, 0.0);
}

#[test]
fn test_area() {
    // Test triangle area
    let triangle: Path64 = vec![Point64::new(0, 0), Point64::new(10, 0), Point64::new(5, 10)];
    assert_eq!(area(&triangle), 50.0); // Area = 0.5 * base * height = 0.5 * 10 * 10 = 50

    // Test rectangle area (clockwise, should be negative)
    let rectangle: Path64 = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
    ];
    assert_eq!(area(&rectangle), 100.0); // This gives positive because of the shoelace formula

    // Test rectangle area (counterclockwise, should be positive)
    let rectangle_ccw: Path64 = vec![
        Point64::new(0, 0),
        Point64::new(0, 10),
        Point64::new(10, 10),
        Point64::new(10, 0),
    ];
    assert_eq!(area(&rectangle_ccw), -100.0); // This gives negative because of the shoelace formula

    // Test empty path
    let empty: Path64 = vec![];
    assert_eq!(area(&empty), 0.0);

    // Test path with less than 3 points
    let line: Path64 = vec![Point64::new(0, 0), Point64::new(10, 10)];
    assert_eq!(area(&line), 0.0);
}

#[test]
fn test_area_paths() {
    let triangle1: Path64 = vec![Point64::new(0, 0), Point64::new(10, 0), Point64::new(5, 10)];

    let triangle2: Path64 = vec![Point64::new(0, 0), Point64::new(0, 10), Point64::new(-5, 5)];

    // Calculate areas before moving into paths
    let area1 = area(&triangle1);
    let area2 = area(&triangle2);
    let expected_total = area1 + area2;

    let paths = vec![triangle1, triangle2];
    assert_eq!(area_paths(&paths), expected_total);

    // Test empty paths
    let empty_paths: Paths64 = vec![];
    assert_eq!(area_paths(&empty_paths), 0.0);
}

#[test]
fn test_is_positive() {
    // Test rectangle with positive area (this path gives positive area)
    let positive_rect: Path64 = vec![
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
    ];
    assert!(is_positive(&positive_rect));

    // Test rectangle with negative area (this path gives negative area)
    let negative_rect: Path64 = vec![
        Point64::new(0, 0),
        Point64::new(0, 10),
        Point64::new(10, 10),
        Point64::new(10, 0),
    ];
    assert!(!is_positive(&negative_rect));

    // Test degenerate case (zero area should be considered positive)
    let line: Path64 = vec![Point64::new(0, 0), Point64::new(10, 10)];
    assert!(is_positive(&line)); // Zero area returns true (>= 0.0)

    // Test empty path
    let empty: Path64 = vec![];
    assert!(is_positive(&empty)); // Zero area returns true
}

#[test]
fn test_location_variants() {
    let locations = [
        Location::Left,
        Location::Top,
        Location::Right,
        Location::Bottom,
        Location::Inside,
    ];
    assert_eq!(locations.len(), 5);

    // Test each variant is unique
    for i in 0..locations.len() {
        for j in (i + 1)..locations.len() {
            assert_ne!(locations[i], locations[j]);
        }
    }
}

#[test]
fn test_location_copy_clone() {
    let loc = Location::Inside;
    let copied = loc;
    let cloned = loc;

    assert_eq!(loc, copied);
    assert_eq!(loc, cloned);
    assert_eq!(copied, cloned);
}

#[test]
fn test_location_debug() {
    assert_eq!(format!("{:?}", Location::Left), "Left");
    assert_eq!(format!("{:?}", Location::Top), "Top");
    assert_eq!(format!("{:?}", Location::Right), "Right");
    assert_eq!(format!("{:?}", Location::Bottom), "Bottom");
    assert_eq!(format!("{:?}", Location::Inside), "Inside");
}

#[test]
fn test_location_hash() {
    use std::collections::HashMap;

    let mut map = HashMap::new();
    map.insert(Location::Left, "left");
    map.insert(Location::Top, "top");
    map.insert(Location::Right, "right");
    map.insert(Location::Bottom, "bottom");
    map.insert(Location::Inside, "inside");

    assert_eq!(map[&Location::Left], "left");
    assert_eq!(map[&Location::Top], "top");
    assert_eq!(map[&Location::Right], "right");
    assert_eq!(map[&Location::Bottom], "bottom");
    assert_eq!(map[&Location::Inside], "inside");
    assert_eq!(map.len(), 5);
}

#[test]
fn test_location_ordering_properties() {
    let locations = [
        Location::Left,
        Location::Top,
        Location::Right,
        Location::Bottom,
        Location::Inside,
    ];

    // Test that all locations can be compared for equality
    for loc1 in &locations {
        for loc2 in &locations {
            let eq = loc1 == loc2;
            let ne = loc1 != loc2;
            assert_eq!(eq, !ne); // == and != should be opposites
            if std::ptr::eq(loc1, loc2) {
                assert!(eq);
            }
        }
    }
}

#[test]
fn test_get_location_inside() {
    let rect = Rect64::new(10, 10, 100, 100);
    let pt = Point64::new(50, 50);
    let mut loc = Location::Left;

    let result = get_location(&rect, &pt, &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Inside);
}

#[test]
fn test_get_location_left_edge() {
    let rect = Rect64::new(10, 10, 100, 100);
    let pt = Point64::new(10, 50);
    let mut loc = Location::Inside;

    let result = get_location(&rect, &pt, &mut loc);
    assert!(!result); // On edge should return false
    assert_eq!(loc, Location::Left);
}

#[test]
fn test_get_location_right_edge() {
    let rect = Rect64::new(10, 10, 100, 100);
    let pt = Point64::new(100, 50);
    let mut loc = Location::Inside;

    let result = get_location(&rect, &pt, &mut loc);
    assert!(!result); // On edge should return false
    assert_eq!(loc, Location::Right);
}

#[test]
fn test_get_location_top_edge() {
    let rect = Rect64::new(10, 10, 100, 100);
    let pt = Point64::new(50, 10);
    let mut loc = Location::Inside;

    let result = get_location(&rect, &pt, &mut loc);
    assert!(!result); // On edge should return false
    assert_eq!(loc, Location::Top);
}

#[test]
fn test_get_location_bottom_edge() {
    let rect = Rect64::new(10, 10, 100, 100);
    let pt = Point64::new(50, 100);
    let mut loc = Location::Inside;

    let result = get_location(&rect, &pt, &mut loc);
    assert!(!result); // On edge should return false
    assert_eq!(loc, Location::Bottom);
}

#[test]
fn test_get_location_corners_on_edge() {
    let rect = Rect64::new(10, 10, 100, 100);
    let mut loc = Location::Inside;

    // Top-left corner (on left edge)
    let result = get_location(&rect, &Point64::new(10, 10), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Left);

    // Top-right corner (on right edge - C++ implementation prioritizes x over y)
    let result = get_location(&rect, &Point64::new(100, 10), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Right);

    // Bottom-left corner (on left edge)
    let result = get_location(&rect, &Point64::new(10, 100), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Left);

    // Bottom-right corner (on right edge - C++ implementation prioritizes x over y)
    let result = get_location(&rect, &Point64::new(100, 100), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Right);
}

#[test]
fn test_get_location_outside_regions() {
    let rect = Rect64::new(10, 10, 100, 100);
    let mut loc = Location::Inside;

    // Left region
    let result = get_location(&rect, &Point64::new(5, 50), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Left);

    // Right region
    let result = get_location(&rect, &Point64::new(150, 50), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Right);

    // Top region
    let result = get_location(&rect, &Point64::new(50, 5), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Top);

    // Bottom region
    let result = get_location(&rect, &Point64::new(50, 150), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Bottom);
}

#[test]
fn test_get_location_diagonal_outside() {
    let rect = Rect64::new(10, 10, 100, 100);
    let mut loc = Location::Inside;

    // Top-left diagonal (should be Left since x < left)
    let result = get_location(&rect, &Point64::new(5, 5), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Left);

    // Top-right diagonal (should be Right since x > right)
    let result = get_location(&rect, &Point64::new(150, 5), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Right);

    // Bottom-left diagonal (should be Left since x < left)
    let result = get_location(&rect, &Point64::new(5, 150), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Left);

    // Bottom-right diagonal (should be Right since x > right)
    let result = get_location(&rect, &Point64::new(150, 150), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Right);
}

#[test]
fn test_get_location_edge_cases() {
    let rect = Rect64::new(0, 0, 10, 10);
    let mut loc = Location::Inside;

    // Point at origin (on left edge)
    let result = get_location(&rect, &Point64::new(0, 0), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Left);

    // Single point rectangle
    let single_rect = Rect64::new(5, 5, 5, 5);
    let result = get_location(&single_rect, &Point64::new(5, 5), &mut loc);
    assert!(!result);
    assert_eq!(loc, Location::Left);

    // Point just inside
    let result = get_location(&rect, &Point64::new(1, 1), &mut loc);
    assert!(result);
    assert_eq!(loc, Location::Inside);
}

#[test]
fn test_get_location_comprehensive_coverage() {
    let rect = Rect64::new(10, 10, 100, 100);
    let mut loc = Location::Inside;

    // Test a grid of points to ensure comprehensive coverage
    let test_points = [
        // Inside
        (Point64::new(50, 50), true, Location::Inside),
        (Point64::new(11, 11), true, Location::Inside),
        (Point64::new(99, 99), true, Location::Inside),
        // On edges (should return false)
        (Point64::new(10, 50), false, Location::Left),
        (Point64::new(100, 50), false, Location::Right),
        (Point64::new(50, 10), false, Location::Top),
        (Point64::new(50, 100), false, Location::Bottom),
        // Outside
        (Point64::new(5, 50), true, Location::Left),
        (Point64::new(150, 50), true, Location::Right),
        (Point64::new(50, 5), true, Location::Top),
        (Point64::new(50, 150), true, Location::Bottom),
    ];

    for (point, expected_result, expected_location) in test_points.iter() {
        let result = get_location(&rect, point, &mut loc);
        assert_eq!(
            result, *expected_result,
            "Point {:?} should return {}",
            point, expected_result
        );
        assert_eq!(
            loc, *expected_location,
            "Point {:?} should be at location {:?}",
            point, expected_location
        );
    }
}

#[test]
fn test_is_horizontal_true() {
    let pt1 = Point64::new(10, 50);
    let pt2 = Point64::new(100, 50);
    assert!(is_horizontal(&pt1, &pt2));

    // Test with same point
    assert!(is_horizontal(&pt1, &pt1));

    // Test with zero y-coordinates
    let pt3 = Point64::new(-10, 0);
    let pt4 = Point64::new(20, 0);
    assert!(is_horizontal(&pt3, &pt4));

    // Test with negative y-coordinates
    let pt5 = Point64::new(0, -25);
    let pt6 = Point64::new(100, -25);
    assert!(is_horizontal(&pt5, &pt6));
}

#[test]
fn test_is_horizontal_false() {
    let pt1 = Point64::new(10, 50);
    let pt2 = Point64::new(10, 60);
    assert!(!is_horizontal(&pt1, &pt2));

    // Test diagonal
    let pt3 = Point64::new(0, 0);
    let pt4 = Point64::new(10, 10);
    assert!(!is_horizontal(&pt3, &pt4));

    // Test with different y values
    let pt5 = Point64::new(100, 25);
    let pt6 = Point64::new(100, 26);
    assert!(!is_horizontal(&pt5, &pt6));
}

#[test]
fn test_is_horizontal_edge_cases() {
    // Test with extreme values
    let pt1 = Point64::new(i64::MIN, 0);
    let pt2 = Point64::new(i64::MAX, 0);
    assert!(is_horizontal(&pt1, &pt2));

    let pt3 = Point64::new(0, i64::MIN);
    let pt4 = Point64::new(0, i64::MAX);
    assert!(!is_horizontal(&pt3, &pt4));

    // Test with same x, different y
    let pt5 = Point64::new(42, -100);
    let pt6 = Point64::new(42, 100);
    assert!(!is_horizontal(&pt5, &pt6));

    // Test with different x, same y
    let pt7 = Point64::new(-100, 42);
    let pt8 = Point64::new(100, 42);
    assert!(is_horizontal(&pt7, &pt8));
}

#[test]
fn test_is_horizontal_symmetry() {
    let pt1 = Point64::new(10, 20);
    let pt2 = Point64::new(30, 20);
    let pt3 = Point64::new(10, 25);

    // Function should be symmetric
    assert_eq!(is_horizontal(&pt1, &pt2), is_horizontal(&pt2, &pt1));
    assert_eq!(is_horizontal(&pt1, &pt3), is_horizontal(&pt3, &pt1));

    // Test multiple symmetric cases
    let test_pairs = [
        (Point64::new(0, 10), Point64::new(100, 10)),
        (Point64::new(-50, -20), Point64::new(75, -20)),
        (Point64::new(5, 5), Point64::new(5, 15)),
        (Point64::new(-10, 0), Point64::new(-5, 0)),
    ];

    for (p1, p2) in test_pairs.iter() {
        assert_eq!(
            is_horizontal(p1, p2),
            is_horizontal(p2, p1),
            "Symmetry failed for points {:?} and {:?}",
            p1,
            p2
        );
    }
}

#[test]
fn test_is_horizontal_comprehensive() {
    // Create a comprehensive test with various combinations
    let horizontal_pairs = [
        (Point64::new(0, 0), Point64::new(1, 0)),
        (Point64::new(-10, 5), Point64::new(20, 5)),
        (Point64::new(100, -50), Point64::new(-100, -50)),
        (Point64::new(42, 42), Point64::new(42, 42)), // same point
    ];

    let non_horizontal_pairs = [
        (Point64::new(0, 0), Point64::new(0, 1)),
        (Point64::new(10, 10), Point64::new(15, 15)),
        (Point64::new(-5, 20), Point64::new(-5, 19)),
        (Point64::new(100, 0), Point64::new(100, -1)),
    ];

    for (p1, p2) in horizontal_pairs.iter() {
        assert!(
            is_horizontal(p1, p2),
            "Expected horizontal line for points {:?} and {:?}",
            p1,
            p2
        );
    }

    for (p1, p2) in non_horizontal_pairs.iter() {
        assert!(
            !is_horizontal(p1, p2),
            "Expected non-horizontal line for points {:?} and {:?}",
            p1,
            p2
        );
    }
}

// ============================================================================
// Tests for Phase 1: Missing core.rs functions
// ============================================================================

// --- ScaleRect tests ---

#[test]
fn test_scale_rect_i64_to_f64() {
    let rect = Rect64::new(10, 20, 30, 40);
    let scaled: RectD = scale_rect(&rect, 2.0);
    assert_eq!(scaled.left, 20.0);
    assert_eq!(scaled.top, 40.0);
    assert_eq!(scaled.right, 60.0);
    assert_eq!(scaled.bottom, 80.0);
}

#[test]
fn test_scale_rect_f64_to_i64() {
    let rect = RectD::new(10.5, 20.5, 30.5, 40.5);
    let scaled: Rect64 = scale_rect(&rect, 2.0);
    // Should round: 10.5*2=21.0, 20.5*2=41.0, etc.
    assert_eq!(scaled.left, 21);
    assert_eq!(scaled.top, 41);
    assert_eq!(scaled.right, 61);
    assert_eq!(scaled.bottom, 81);
}

#[test]
fn test_scale_rect_fractional() {
    let rect = RectD::new(1.0, 2.0, 3.0, 4.0);
    let scaled: RectD = scale_rect(&rect, 0.5);
    assert_eq!(scaled.left, 0.5);
    assert_eq!(scaled.top, 1.0);
    assert_eq!(scaled.right, 1.5);
    assert_eq!(scaled.bottom, 2.0);
}

#[test]
fn test_scale_rect_identity() {
    let rect = Rect64::new(10, 20, 30, 40);
    let scaled: Rect64 = scale_rect(&rect, 1.0);
    assert_eq!(scaled, rect);
}

// --- ScalePath tests ---

#[test]
fn test_scale_path_d_to_i64() {
    let path: PathD = vec![
        PointD::new(1.5, 2.5),
        PointD::new(3.5, 4.5),
        PointD::new(5.5, 6.5),
    ];
    let mut error_code = 0;
    let result: Path64 = scale_path(&path, 2.0, 2.0, &mut error_code);
    assert_eq!(error_code, 0);
    assert_eq!(result.len(), 3);
    assert_eq!(result[0], Point64::new(3, 5)); // 1.5*2=3.0, 2.5*2=5.0
    assert_eq!(result[1], Point64::new(7, 9)); // 3.5*2=7.0, 4.5*2=9.0
    assert_eq!(result[2], Point64::new(11, 13)); // 5.5*2=11.0, 6.5*2=13.0
}

#[test]
fn test_scale_path_i64_to_d() {
    let path: Path64 = vec![Point64::new(10, 20), Point64::new(30, 40)];
    let mut error_code = 0;
    let result: PathD = scale_path(&path, 0.1, 0.1, &mut error_code);
    assert_eq!(error_code, 0);
    assert_eq!(result.len(), 2);
    assert!((result[0].x - 1.0).abs() < 1e-10);
    assert!((result[0].y - 2.0).abs() < 1e-10);
    assert!((result[1].x - 3.0).abs() < 1e-10);
    assert!((result[1].y - 4.0).abs() < 1e-10);
}

#[test]
fn test_scale_path_different_xy_scales() {
    let path: PathD = vec![PointD::new(10.0, 20.0)];
    let mut error_code = 0;
    let result: Path64 = scale_path(&path, 2.0, 3.0, &mut error_code);
    assert_eq!(error_code, 0);
    assert_eq!(result[0], Point64::new(20, 60));
}

#[test]
fn test_scale_path_zero_scale_error() {
    let path: PathD = vec![PointD::new(10.0, 20.0)];
    let mut error_code = 0;
    let result: Path64 = scale_path(&path, 0.0, 2.0, &mut error_code);
    // Should set error code but still produce result (non-fatal)
    assert_ne!(error_code & errors::SCALE_ERROR_I, 0);
    // With zero scale treated as 1.0, x should be 10, y should be 40
    assert_eq!(result[0], Point64::new(10, 40));
}

#[test]
fn test_scale_path_empty() {
    let path: PathD = vec![];
    let mut error_code = 0;
    let result: Path64 = scale_path(&path, 2.0, 2.0, &mut error_code);
    assert_eq!(error_code, 0);
    assert!(result.is_empty());
}

#[test]
fn test_scale_path_uniform() {
    let path: Path64 = vec![Point64::new(10, 20), Point64::new(30, 40)];
    let mut error_code = 0;
    let result: PathD = scale_path_uniform(&path, 0.5, &mut error_code);
    assert_eq!(error_code, 0);
    assert!((result[0].x - 5.0).abs() < 1e-10);
    assert!((result[0].y - 10.0).abs() < 1e-10);
}

// --- ScalePaths tests ---

#[test]
fn test_scale_paths_basic() {
    let paths: PathsD = vec![
        vec![PointD::new(1.0, 2.0), PointD::new(3.0, 4.0)],
        vec![PointD::new(5.0, 6.0)],
    ];
    let mut error_code = 0;
    let result: Paths64 = scale_paths(&paths, 10.0, 10.0, &mut error_code);
    assert_eq!(error_code, 0);
    assert_eq!(result.len(), 2);
    assert_eq!(result[0][0], Point64::new(10, 20));
    assert_eq!(result[0][1], Point64::new(30, 40));
    assert_eq!(result[1][0], Point64::new(50, 60));
}

#[test]
fn test_scale_paths_range_error() {
    // Create paths that when scaled would exceed MAX_COORD
    let large_val = (constants::MAX_COORD / 2) as f64;
    let paths: PathsD = vec![vec![PointD::new(large_val, large_val)]];
    let mut error_code = 0;
    let result: Paths64 = scale_paths(&paths, 10.0, 10.0, &mut error_code);
    assert_ne!(error_code & errors::RANGE_ERROR_I, 0);
    assert!(result.is_empty());
}

#[test]
fn test_scale_paths_uniform() {
    let paths: Paths64 = vec![vec![Point64::new(100, 200)]];
    let mut error_code = 0;
    let result: PathsD = scale_paths_uniform(&paths, 0.01, &mut error_code);
    assert_eq!(error_code, 0);
    assert!((result[0][0].x - 1.0).abs() < 1e-10);
    assert!((result[0][0].y - 2.0).abs() < 1e-10);
}

// --- TransformPath tests ---

#[test]
fn test_transform_path_d_to_i64() {
    let path: PathD = vec![PointD::new(1.4, 2.6), PointD::new(3.5, 4.5)];
    let result: Path64 = transform_path(&path);
    assert_eq!(result[0], Point64::new(1, 3)); // rounds
    assert_eq!(result[1], Point64::new(4, 5)); // rounds 3.5->4, 4.5->5 (banker's rounding: 4)
}

#[test]
fn test_transform_path_i64_to_d() {
    let path: Path64 = vec![Point64::new(10, 20)];
    let result: PathD = transform_path(&path);
    assert_eq!(result[0].x, 10.0);
    assert_eq!(result[0].y, 20.0);
}

#[test]
fn test_transform_paths_basic() {
    let paths: Paths64 = vec![
        vec![Point64::new(1, 2), Point64::new(3, 4)],
        vec![Point64::new(5, 6)],
    ];
    let result: PathsD = transform_paths(&paths);
    assert_eq!(result.len(), 2);
    assert_eq!(result[0][0].x, 1.0);
    assert_eq!(result[0][1].y, 4.0);
    assert_eq!(result[1][0].x, 5.0);
}

#[test]
fn test_transform_path_empty() {
    let path: PathD = vec![];
    let result: Path64 = transform_path(&path);
    assert!(result.is_empty());
}

#[test]
fn test_transform_paths_empty() {
    let paths: PathsD = vec![];
    let result: Paths64 = transform_paths(&paths);
    assert!(result.is_empty());
}

// --- NearEqual tests ---

#[test]
fn test_near_equal_same_point() {
    let p = Point64::new(10, 20);
    assert!(near_equal(&p, &p, 1.0));
}

#[test]
fn test_near_equal_close_points() {
    let p1 = PointD::new(1.0, 1.0);
    let p2 = PointD::new(1.0, 1.5);
    assert!(near_equal(&p1, &p2, 1.0)); // dist_sqr = 0.25 < 1.0
    assert!(!near_equal(&p1, &p2, 0.1)); // dist_sqr = 0.25 >= 0.1
}

#[test]
fn test_near_equal_far_points() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(100, 100);
    assert!(!near_equal(&p1, &p2, 100.0));
}

#[test]
fn test_near_equal_threshold_boundary() {
    let p1 = PointD::new(0.0, 0.0);
    let p2 = PointD::new(1.0, 0.0);
    // dist_sqr = 1.0, threshold = 1.0 -> false (strictly less than)
    assert!(!near_equal(&p1, &p2, 1.0));
    // dist_sqr = 1.0, threshold = 1.01 -> true
    assert!(near_equal(&p1, &p2, 1.01));
}

// --- StripNearEqual tests ---

#[test]
fn test_strip_near_equal_basic() {
    let path: PathD = vec![
        PointD::new(0.0, 0.0),
        PointD::new(0.1, 0.1),
        PointD::new(10.0, 10.0),
        PointD::new(10.05, 10.05),
        PointD::new(20.0, 20.0),
    ];
    // With threshold of 1.0 (max_dist_sqrd), 0.1^2 + 0.1^2 = 0.02 < 1.0
    let result = strip_near_equal(&path, 1.0, false);
    assert_eq!(result.len(), 3); // should keep (0,0), (10,10), (20,20)
    assert_eq!(result[0], PointD::new(0.0, 0.0));
    assert_eq!(result[1], PointD::new(10.0, 10.0));
    assert_eq!(result[2], PointD::new(20.0, 20.0));
}

#[test]
fn test_strip_near_equal_closed_path() {
    let path: PathD = vec![
        PointD::new(0.0, 0.0),
        PointD::new(10.0, 0.0),
        PointD::new(10.0, 10.0),
        PointD::new(0.05, 0.05), // near first point
    ];
    let result = strip_near_equal(&path, 1.0, true);
    assert_eq!(result.len(), 3); // last point removed (near first)
}

#[test]
fn test_strip_near_equal_empty() {
    let path: PathD = vec![];
    let result = strip_near_equal(&path, 1.0, false);
    assert!(result.is_empty());
}

#[test]
fn test_strip_near_equal_single_point() {
    let path: PathD = vec![PointD::new(1.0, 2.0)];
    let result = strip_near_equal(&path, 1.0, false);
    assert_eq!(result.len(), 1);
}

#[test]
fn test_strip_near_equal_paths_basic() {
    let paths: PathsD = vec![
        vec![
            PointD::new(0.0, 0.0),
            PointD::new(0.01, 0.01),
            PointD::new(10.0, 10.0),
        ],
        vec![
            PointD::new(5.0, 5.0),
            PointD::new(5.0, 5.0),
            PointD::new(15.0, 15.0),
        ],
    ];
    let result = strip_near_equal_paths(&paths, 1.0, false);
    assert_eq!(result.len(), 2);
    assert_eq!(result[0].len(), 2); // stripped near-equal
    assert_eq!(result[1].len(), 2); // stripped duplicate
}

// --- TranslatePoint tests ---

#[test]
fn test_translate_point_i64() {
    let pt = Point64::new(10, 20);
    let result = translate_point(&pt, 5.0, -3.0);
    assert_eq!(result, Point64::new(15, 17));
}

#[test]
fn test_translate_point_f64() {
    let pt = PointD::new(1.0, 2.0);
    let result = translate_point(&pt, 0.5, 0.5);
    assert!((result.x - 1.5).abs() < 1e-10);
    assert!((result.y - 2.5).abs() < 1e-10);
}

#[test]
fn test_translate_point_zero() {
    let pt = Point64::new(42, 99);
    let result = translate_point(&pt, 0.0, 0.0);
    assert_eq!(result, pt);
}

#[test]
fn test_translate_point_negative() {
    let pt = Point64::new(100, 200);
    let result = translate_point(&pt, -50.0, -100.0);
    assert_eq!(result, Point64::new(50, 100));
}

// --- ReflectPoint tests ---

#[test]
fn test_reflect_point_basic() {
    let pt = Point64::new(10, 20);
    let pivot = Point64::new(15, 25);
    let result = reflect_point(&pt, &pivot);
    assert_eq!(result, Point64::new(20, 30));
}

#[test]
fn test_reflect_point_origin() {
    let pt = Point64::new(5, 5);
    let pivot = Point64::new(0, 0);
    let result = reflect_point(&pt, &pivot);
    assert_eq!(result, Point64::new(-5, -5));
}

#[test]
fn test_reflect_point_same() {
    let pt = Point64::new(10, 20);
    let result = reflect_point(&pt, &pt);
    assert_eq!(result, pt);
}

#[test]
fn test_reflect_point_f64() {
    let pt = PointD::new(1.0, 2.0);
    let pivot = PointD::new(3.0, 4.0);
    let result = reflect_point(&pt, &pivot);
    assert!((result.x - 5.0).abs() < 1e-10);
    assert!((result.y - 6.0).abs() < 1e-10);
}

// --- GetSign tests ---

#[test]
fn test_get_sign_positive() {
    assert_eq!(get_sign(&5i64), 1);
    assert_eq!(get_sign(&1i64), 1);
    assert_eq!(get_sign(&100.0f64), 1);
    assert_eq!(get_sign(&0.001f64), 1);
}

#[test]
fn test_get_sign_negative() {
    assert_eq!(get_sign(&-5i64), -1);
    assert_eq!(get_sign(&-1i64), -1);
    assert_eq!(get_sign(&-100.0f64), -1);
    assert_eq!(get_sign(&-0.001f64), -1);
}

#[test]
fn test_get_sign_zero() {
    assert_eq!(get_sign(&0i64), 0);
    assert_eq!(get_sign(&0.0f64), 0);
}

// --- CrossProductSign tests ---

#[test]
fn test_cross_product_sign_positive() {
    // Counter-clockwise turn -> positive cross product
    let pt1 = Point64::new(0, 0);
    let pt2 = Point64::new(10, 0);
    let pt3 = Point64::new(10, 10);
    assert_eq!(cross_product_sign(pt1, pt2, pt3), 1);
}

#[test]
fn test_cross_product_sign_negative() {
    // Clockwise turn -> negative cross product
    let pt1 = Point64::new(0, 0);
    let pt2 = Point64::new(10, 0);
    let pt3 = Point64::new(10, -10);
    assert_eq!(cross_product_sign(pt1, pt2, pt3), -1);
}

#[test]
fn test_cross_product_sign_collinear() {
    let pt1 = Point64::new(0, 0);
    let pt2 = Point64::new(5, 5);
    let pt3 = Point64::new(10, 10);
    assert_eq!(cross_product_sign(pt1, pt2, pt3), 0);
}

#[test]
fn test_cross_product_sign_large_values() {
    // Test with values that would overflow 64-bit multiplication
    let large = i64::MAX / 4;
    let pt1 = Point64::new(0, 0);
    let pt2 = Point64::new(large, 0);
    let pt3 = Point64::new(large, large);
    assert_eq!(cross_product_sign(pt1, pt2, pt3), 1);
}

#[test]
fn test_cross_product_sign_consistency_with_cross_product() {
    // Verify sign matches the floating-point cross product
    let test_cases = [
        (Point64::new(0, 0), Point64::new(10, 0), Point64::new(5, 5)),
        (Point64::new(0, 0), Point64::new(10, 0), Point64::new(5, -5)),
        (
            Point64::new(0, 0),
            Point64::new(10, 10),
            Point64::new(20, 20),
        ),
        (
            Point64::new(-100, -100),
            Point64::new(100, 50),
            Point64::new(50, 200),
        ),
    ];

    for (pt1, pt2, pt3) in test_cases {
        let cp = cross_product_three_points(pt1, pt2, pt3);
        let cps = cross_product_sign(pt1, pt2, pt3);
        if cp > 0.0 {
            assert_eq!(cps, 1, "Mismatch for {:?}, {:?}, {:?}", pt1, pt2, pt3);
        } else if cp < 0.0 {
            assert_eq!(cps, -1, "Mismatch for {:?}, {:?}, {:?}", pt1, pt2, pt3);
        } else {
            assert_eq!(cps, 0, "Mismatch for {:?}, {:?}, {:?}", pt1, pt2, pt3);
        }
    }
}

// --- SegmentsIntersect tests ---

#[test]
fn test_segments_intersect_crossing() {
    let a = Point64::new(0, 0);
    let b = Point64::new(10, 10);
    let c = Point64::new(0, 10);
    let d = Point64::new(10, 0);
    assert!(segments_intersect(a, b, c, d, false));
    assert!(segments_intersect(a, b, c, d, true));
}

#[test]
fn test_segments_intersect_parallel() {
    let a = Point64::new(0, 0);
    let b = Point64::new(10, 0);
    let c = Point64::new(0, 5);
    let d = Point64::new(10, 5);
    assert!(!segments_intersect(a, b, c, d, false));
    assert!(!segments_intersect(a, b, c, d, true));
}

#[test]
fn test_segments_intersect_non_crossing() {
    let a = Point64::new(0, 0);
    let b = Point64::new(5, 5);
    let c = Point64::new(6, 0);
    let d = Point64::new(10, 0);
    assert!(!segments_intersect(a, b, c, d, false));
}

#[test]
fn test_segments_intersect_t_shape_inclusive() {
    // Segment endpoint touches the other segment
    let a = Point64::new(0, 0);
    let b = Point64::new(10, 0);
    let c = Point64::new(5, 5);
    let d = Point64::new(5, 0);
    // With inclusive=true, touching at endpoint should count
    assert!(segments_intersect(a, b, c, d, true));
    // With inclusive=false, should not count
    assert!(!segments_intersect(a, b, c, d, false));
}

#[test]
fn test_segments_intersect_collinear() {
    // Collinear segments (overlapping)
    let a = Point64::new(0, 0);
    let b = Point64::new(10, 0);
    let c = Point64::new(5, 0);
    let d = Point64::new(15, 0);
    // Collinear segments should return false even with inclusive
    assert!(!segments_intersect(a, b, c, d, true));
}

// --- GetClosestPointOnSegment tests ---

#[test]
fn test_closest_point_on_segment_midpoint() {
    let off = Point64::new(5, 10);
    let seg1 = Point64::new(0, 0);
    let seg2 = Point64::new(10, 0);
    let result = get_closest_point_on_segment(off, seg1, seg2);
    assert_eq!(result, Point64::new(5, 0));
}

#[test]
fn test_closest_point_on_segment_start() {
    let off = Point64::new(-5, 5);
    let seg1 = Point64::new(0, 0);
    let seg2 = Point64::new(10, 0);
    let result = get_closest_point_on_segment(off, seg1, seg2);
    assert_eq!(result, Point64::new(0, 0));
}

#[test]
fn test_closest_point_on_segment_end() {
    let off = Point64::new(15, 5);
    let seg1 = Point64::new(0, 0);
    let seg2 = Point64::new(10, 0);
    let result = get_closest_point_on_segment(off, seg1, seg2);
    assert_eq!(result, Point64::new(10, 0));
}

#[test]
fn test_closest_point_on_segment_degenerate() {
    let off = Point64::new(5, 5);
    let seg1 = Point64::new(3, 3);
    let seg2 = Point64::new(3, 3); // degenerate segment (point)
    let result = get_closest_point_on_segment(off, seg1, seg2);
    assert_eq!(result, Point64::new(3, 3));
}

#[test]
fn test_closest_point_on_segment_f64() {
    let off = PointD::new(5.0, 10.0);
    let seg1 = PointD::new(0.0, 0.0);
    let seg2 = PointD::new(10.0, 0.0);
    let result = get_closest_point_on_segment(off, seg1, seg2);
    assert!((result.x - 5.0).abs() < 1e-10);
    assert!((result.y - 0.0).abs() < 1e-10);
}

#[test]
fn test_closest_point_on_segment_diagonal() {
    let off = PointD::new(0.0, 10.0);
    let seg1 = PointD::new(0.0, 0.0);
    let seg2 = PointD::new(10.0, 10.0);
    let result = get_closest_point_on_segment(off, seg1, seg2);
    // Projection of (0,10) onto line from (0,0) to (10,10)
    // q = ((0-0)*10 + (10-0)*10) / (100+100) = 100/200 = 0.5
    // result = (0 + 0.5*10, 0 + 0.5*10) = (5, 5)
    assert!((result.x - 5.0).abs() < 1e-10);
    assert!((result.y - 5.0).abs() < 1e-10);
}

// --- FromF64 trait tests ---

#[test]
fn test_from_f64_i64_rounding() {
    assert_eq!(i64::from_f64(1.4), 1);
    assert_eq!(i64::from_f64(1.5), 2);
    assert_eq!(i64::from_f64(1.6), 2);
    assert_eq!(i64::from_f64(-1.4), -1);
    assert_eq!(i64::from_f64(-1.5), -2);
    assert_eq!(i64::from_f64(-1.6), -2);
}

#[test]
fn test_from_f64_f64_exact() {
    assert_eq!(f64::from_f64(1.4), 1.4);
    assert_eq!(f64::from_f64(-1.5), -1.5);
    assert_eq!(f64::from_f64(0.0), 0.0);
}

#[test]
fn test_is_integral() {
    assert!(i64::is_integral());
    assert!(!f64::is_integral());
}

// Fix 1: point_in_polygon must use i128 cross product to avoid f64 precision loss
#[test]
fn test_point_in_polygon_large_coords_i128_precision() {
    // Polygon: triangle with large coordinates
    // (0,0) -> (100000000, 100000001) -> (200000000, 0)
    // Point (1,1) is inside this triangle.
    // With f64 cross product: edge (0,0)->(100000000,100000001) gives 0.0 for pt=(1,1)
    //   because f64 can't distinguish the cross product of -1 from 0 at this scale.
    // With i128 cross product: gives -1, correctly identifying point as not on the edge.
    let polygon = vec![
        Point64::new(0, 0),
        Point64::new(100_000_000, 100_000_001),
        Point64::new(200_000_000, 0),
    ];
    let pt = Point64::new(1, 1);
    let result = point_in_polygon(pt, &polygon);
    assert_eq!(
        result,
        PointInPolygonResult::IsInside,
        "Point (1,1) should be inside the triangle, not {:?}",
        result
    );
}

// Fix 2: nearbyint_f64 (banker's rounding)
#[test]
fn test_nearbyint_f64_basic() {
    // Half-to-even (banker's rounding)
    assert_eq!(nearbyint_f64(0.5), 0.0, "0.5 should round to 0 (even)");
    assert_eq!(nearbyint_f64(1.5), 2.0, "1.5 should round to 2 (even)");
    assert_eq!(nearbyint_f64(2.5), 2.0, "2.5 should round to 2 (even)");
    assert_eq!(nearbyint_f64(3.5), 4.0, "3.5 should round to 4 (even)");
    assert_eq!(nearbyint_f64(-0.5), 0.0, "-0.5 should round to 0 (even)");
    assert_eq!(nearbyint_f64(-1.5), -2.0, "-1.5 should round to -2 (even)");

    // Non-half values should round normally
    assert_eq!(nearbyint_f64(0.3), 0.0);
    assert_eq!(nearbyint_f64(0.7), 1.0);
    assert_eq!(nearbyint_f64(-0.3), 0.0);
    assert_eq!(nearbyint_f64(-0.7), -1.0);
}

// Fix 3: get_closest_point_on_segment should use nearbyint before adding offset
#[test]
fn test_get_closest_point_on_segment_nearbyint() {
    // seg1=(0,0), seg2=(1,1), off_pt=(0,1)
    // The closest point on the segment is at parameter q=0.5, so (0.5, 0.5)
    // C++ (nearbyint): nearbyint(0.5) = 0 -> result (0+0, 0+0) = (0, 0)
    // Old Rust (.round() of sum): round(0+0.5) = round(0.5) = 1 -> result (1, 1) (wrong!)
    let seg1 = Point64::new(0, 0);
    let seg2 = Point64::new(1, 1);
    let off_pt = Point64::new(0, 1);
    let result = get_closest_point_on_segment(off_pt, seg1, seg2);
    assert_eq!(
        result,
        Point64::new(0, 0),
        "Expected (0,0) with banker's rounding, got ({}, {})",
        result.x,
        result.y
    );
}

// Fix 4: segments_intersect must use parametric algorithm matching C++
#[test]
fn test_segments_intersect_parametric_crossing() {
    // Two crossing segments
    let seg1a = Point64::new(0, 0);
    let seg1b = Point64::new(10, 10);
    let seg2a = Point64::new(0, 10);
    let seg2b = Point64::new(10, 0);
    assert!(segments_intersect(seg1a, seg1b, seg2a, seg2b, false));
    assert!(segments_intersect(seg1a, seg1b, seg2a, seg2b, true));
}

#[test]
fn test_segments_intersect_parametric_parallel() {
    // Parallel segments should return false (cp == 0)
    let seg1a = Point64::new(0, 0);
    let seg1b = Point64::new(10, 0);
    let seg2a = Point64::new(0, 5);
    let seg2b = Point64::new(10, 5);
    assert!(!segments_intersect(seg1a, seg1b, seg2a, seg2b, false));
    assert!(!segments_intersect(seg1a, seg1b, seg2a, seg2b, true));
}

#[test]
fn test_segments_intersect_parametric_endpoint_touching() {
    // Segments that share an endpoint: T-junction
    // seg1: (0,0)-(10,0), seg2: (5,0)-(5,10)
    // seg2 starts ON seg1
    let seg1a = Point64::new(0, 0);
    let seg1b = Point64::new(10, 0);
    let seg2a = Point64::new(5, 0);
    let seg2b = Point64::new(5, 10);
    // Non-inclusive: endpoint touching should return false
    assert!(
        !segments_intersect(seg1a, seg1b, seg2a, seg2b, false),
        "Non-inclusive should return false for endpoint touching"
    );
    // Inclusive: endpoint touching should return true
    assert!(
        segments_intersect(seg1a, seg1b, seg2a, seg2b, true),
        "Inclusive should return true for endpoint touching"
    );
}