File: test_3475_virtualarray_unknown_length.py

package info (click to toggle)
python-awkward 2.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,932 kB
  • sloc: python: 178,875; cpp: 33,828; sh: 432; makefile: 21; javascript: 8
file content (2993 lines) | stat: -rw-r--r-- 99,964 bytes parent folder | download
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
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
from __future__ import annotations

import numpy as np
import pytest

from awkward._backends.dispatch import backend_of_obj
from awkward._nplikes.array_like import maybe_materialize
from awkward._nplikes.dispatch import nplike_of_obj
from awkward._nplikes.numpy import Numpy
from awkward._nplikes.shape import unknown_length
from awkward._nplikes.virtual import VirtualNDArray


@pytest.fixture
def numpy_like():
    return Numpy.instance()


@pytest.fixture(
    params=[
        None,  # Don't use shape_generator
        "use_shape_generator",  # Use shape_generator
    ]
)
def shape_generator_param(request):
    return request.param


@pytest.fixture
def simple_array_generator():
    return lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64)


@pytest.fixture
def virtual_array(numpy_like, simple_array_generator, shape_generator_param):
    if shape_generator_param is None:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=simple_array_generator,
        )
    else:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=simple_array_generator,
            shape_generator=lambda: (5,),
        )


@pytest.fixture
def two_dim_array_generator():
    return lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)


@pytest.fixture
def two_dim_virtual_array(numpy_like, two_dim_array_generator, shape_generator_param):
    if shape_generator_param is None:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=two_dim_array_generator,
        )
    else:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=two_dim_array_generator,
            shape_generator=lambda: (2, 3),
        )


@pytest.fixture
def scalar_array_generator():
    return lambda: np.array(42, dtype=np.int64)


@pytest.fixture
def scalar_virtual_array(numpy_like, scalar_array_generator, shape_generator_param):
    if shape_generator_param is None:
        return VirtualNDArray(
            numpy_like,
            shape=(),
            dtype=np.dtype(np.int64),
            generator=scalar_array_generator,
        )
    else:
        return VirtualNDArray(
            numpy_like,
            shape=(),
            dtype=np.dtype(np.int64),
            generator=scalar_array_generator,
            shape_generator=lambda: (),
        )


@pytest.fixture
def float_array_generator():
    return lambda: np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float64)


@pytest.fixture
def float_virtual_array(numpy_like, float_array_generator, shape_generator_param):
    if shape_generator_param is None:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
        )
    else:
        return VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )


# Test initialization
def test_init_valid(numpy_like, simple_array_generator, shape_generator_param):
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=simple_array_generator,
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=simple_array_generator,
            shape_generator=lambda: (5,),
        )

    assert va._shape == (unknown_length,)
    assert va.shape == (5,)

    assert va.dtype == np.dtype(np.int64)
    if shape_generator_param is None:
        assert va.is_materialized
    else:
        assert not va.is_materialized


def test_init_invalid_shape():
    nplike = Numpy.instance()
    with pytest.raises(
        TypeError,
        match=r"Only shapes of integer dimensions or unknown_length are supported",
    ):
        VirtualNDArray(
            nplike,
            shape=("not_an_integer", 5),
            dtype=np.int64,
            generator=lambda: np.array([[1, 2, 3, 4, 5]], dtype=np.int64),
        )


# Test properties
def test_dtype(virtual_array):
    assert virtual_array.dtype == np.dtype(np.int64)


def test_shape(virtual_array, two_dim_virtual_array, shape_generator_param):
    assert virtual_array._shape == (unknown_length,)
    assert two_dim_virtual_array._shape == (unknown_length, 3)
    assert virtual_array.shape == (5,)
    assert two_dim_virtual_array.shape == (2, 3)


def test_ndim(virtual_array, two_dim_virtual_array, scalar_virtual_array):
    assert virtual_array.ndim == 1
    assert two_dim_virtual_array.ndim == 2
    assert scalar_virtual_array.ndim == 0


def test_size(
    virtual_array, two_dim_virtual_array, scalar_virtual_array, shape_generator_param
):
    if shape_generator_param is None:
        # Without shape_generator, we know exact sizes
        assert virtual_array.size == 5
        assert two_dim_virtual_array.size == 6
    else:
        # With shape_generator, materializing to check size
        assert virtual_array.materialize().size == 5
        assert two_dim_virtual_array.materialize().size == 6

    assert scalar_virtual_array.size == 1


def test_nbytes_unmaterialized(virtual_array, shape_generator_param):
    if shape_generator_param is None:
        assert virtual_array.nbytes == unknown_length
    else:
        # Materialize to check bytes
        virtual_array.get_shape()
        assert virtual_array.nbytes == 5 * virtual_array.dtype.itemsize


def test_nbytes_materialized(virtual_array):
    virtual_array.materialize()
    assert virtual_array.nbytes == np.array([1, 2, 3, 4, 5], dtype=np.int64).nbytes


def test_strides(virtual_array, shape_generator_param):
    if shape_generator_param is not None:
        virtual_array.materialize()

    expected_strides = np.array([1, 2, 3, 4, 5], dtype=np.int64).strides
    assert virtual_array.strides == expected_strides


# Test materialization
def test_materialize(virtual_array):
    result = virtual_array.materialize()
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))
    assert virtual_array.is_materialized

    # After materialization, shape should be concrete regardless of shape_generator
    assert virtual_array.shape == (5,)


def test_is_materialized(virtual_array):
    assert not virtual_array.is_materialized
    virtual_array.materialize()
    assert virtual_array.is_materialized


def test_materialize_shape(numpy_like):
    # Generator returns array with different shape than declared
    va = VirtualNDArray(
        numpy_like,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([1, 2, 3], dtype=np.int64),
    )
    va.get_shape()
    assert va.shape == (3,)


def test_materialize_shape_mismatch(numpy_like):
    # Generator returns array with different shape than declared
    va = VirtualNDArray(
        numpy_like,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([1, 2, 3], dtype=np.int64),
        shape_generator=lambda: (5,),
    )
    va.get_shape()
    with pytest.raises(
        ValueError,
        match=r"had shape \(5,\) before materialization while the materialized array has shape \(3,\)",
    ):
        va.materialize()


def test_materialize_dtype_mismatch(numpy_like):
    # Generator returns array with different dtype than declared
    with pytest.raises(
        ValueError,
        match=r"had dtype int64 before materialization while the materialized array has dtype float64",
    ):
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: np.array([1.0, 2.0, 3.0], dtype=np.float64),
        )
        va.materialize()


# Test transpose
def test_T_unmaterialized(two_dim_virtual_array, shape_generator_param):
    transposed = two_dim_virtual_array.T
    assert isinstance(transposed, VirtualNDArray)

    assert transposed._shape == (3, unknown_length)
    assert transposed.shape == (3, 2)
    assert not transposed.is_materialized


def test_T_materialized(two_dim_virtual_array):
    two_dim_virtual_array.materialize()
    transposed = two_dim_virtual_array.T
    assert isinstance(transposed, np.ndarray)
    assert transposed.shape == (3, 2)


# Test view
def test_view_unmaterialized(virtual_array):
    view = virtual_array.view(np.float64)
    assert isinstance(view, VirtualNDArray)
    assert view.dtype == np.dtype(np.float64)
    assert not view.is_materialized


def test_view_materialized(virtual_array):
    virtual_array.materialize()
    view = virtual_array.view(np.float64)
    assert isinstance(view, np.ndarray)
    assert view.dtype == np.dtype(np.float64)


def test_view_invalid_size():
    nplike = Numpy.instance()
    va = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int8,
        generator=lambda: np.array([1, 2, 3], dtype=np.int8),
    )
    with pytest.raises(
        ValueError, match=r"new size of array with larger dtype must be a divisor"
    ):
        va.view(np.int32)


# Test generator property
def test_generator(virtual_array, simple_array_generator):
    assert virtual_array._generator is simple_array_generator


# Test nplike property
def test_nplike(virtual_array, numpy_like):
    assert virtual_array.nplike is numpy_like


# Test shape_generator property
def test_shape_generator(virtual_array, shape_generator_param):
    if shape_generator_param is None:
        assert virtual_array._shape == (unknown_length,)
        assert virtual_array._shape_generator is None
        virtual_array.get_shape()
        assert virtual_array.is_materialized
        assert virtual_array._shape == (5,)
    else:
        assert virtual_array._shape == (unknown_length,)
        assert virtual_array._shape_generator is not None
        # Can't directly compare lambdas, so check if it's callable
        assert callable(virtual_array._shape_generator)
        virtual_array.get_shape()
        assert virtual_array._shape == (5,)
        assert not virtual_array.is_materialized
        with pytest.raises(
            RuntimeError, match="this generator function should never be run"
        ):
            assert virtual_array._shape_generator() == (5,)


# Test copy
def test_copy(virtual_array, shape_generator_param):
    copy = virtual_array.copy()
    assert isinstance(copy, VirtualNDArray)
    assert copy._generator is not virtual_array._generator
    assert copy._shape == virtual_array._shape
    assert copy.shape == virtual_array.shape
    assert copy.dtype == virtual_array.dtype
    if shape_generator_param is None:
        assert copy.is_materialized
    assert id(copy) != id(virtual_array)  # Different objects


# Test tolist
def test_tolist(virtual_array):
    assert virtual_array.tolist() == [1, 2, 3, 4, 5]
    # Should be materialized after this
    assert virtual_array.is_materialized


# Test tobytes
def test_tobytes(virtual_array):
    assert virtual_array.tobytes(order="C") == np.array(
        [1, 2, 3, 4, 5], dtype=np.int64
    ).tobytes(order="C")
    assert virtual_array.tobytes(order="F") == np.array(
        [1, 2, 3, 4, 5], dtype=np.int64
    ).tobytes(order="F")
    assert virtual_array.is_materialized


# Test __repr__ and __str__
def test_repr(virtual_array, shape_generator_param):
    repr_str = repr(virtual_array)
    assert "VirtualNDArray" in repr_str
    assert "shape=(awkward._nplikes.shape.unknown_length,)" in repr_str


def test_str_scalar(scalar_virtual_array):
    assert str(scalar_virtual_array) == "??"


def test_str_array(virtual_array):
    str_val = str(virtual_array)
    assert "VirtualNDArray" in str_val


# Test __getitem__
def test_getitem_index(virtual_array):
    assert virtual_array[0] == 1
    assert virtual_array[4] == 5
    assert virtual_array[-1] == 5
    # Should be materialized after this
    assert virtual_array.is_materialized


def test_getitem_slice(virtual_array, shape_generator_param):
    sliced = virtual_array[1:4]
    assert isinstance(sliced, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced.shape == (3,)
    else:
        # With shape_generator, need to materialize to check shape
        materialized = sliced.materialize()
        assert materialized.shape == (3,)

    np.testing.assert_array_equal(sliced.materialize(), np.array([2, 3, 4]))


def test_getitem_slice_with_step(virtual_array, shape_generator_param):
    sliced = virtual_array[::2]
    assert isinstance(sliced, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced.shape == (3,)
    else:
        # With shape_generator, need to materialize to check shape
        materialized = sliced.materialize()
        assert materialized.shape == (3,)

    np.testing.assert_array_equal(sliced.materialize(), np.array([1, 3, 5]))


def test_getitem_slice_with_unknown_length():
    nplike = Numpy.instance()
    va = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
    )
    with pytest.raises(
        TypeError, match=r"does not support slicing with unknown_length"
    ):
        va[unknown_length:4]


# Test __setitem__
def test_setitem(virtual_array):
    virtual_array[2] = 10
    assert virtual_array[2] == 10
    np.testing.assert_array_equal(
        virtual_array.materialize(), np.array([1, 2, 10, 4, 5])
    )


# Test __bool__
def test_bool_scalar(scalar_virtual_array):
    assert bool(scalar_virtual_array) is True

    # Test with zero value
    nplike = Numpy.instance()
    va_zero = VirtualNDArray(
        nplike, shape=(), dtype=np.int64, generator=lambda: np.array(0, dtype=np.int64)
    )
    assert bool(va_zero) is False


def test_bool_array(virtual_array):
    with pytest.raises(
        ValueError,
        match=r"The truth value of an array with more than one element is ambiguous",
    ):
        bool(virtual_array)


# Test __int__
def test_int_scalar(scalar_virtual_array):
    assert int(scalar_virtual_array) == 42


def test_int_array(virtual_array):
    with pytest.raises(
        TypeError, match=r"Only scalar arrays can be converted to an int"
    ):
        int(virtual_array)


# Test __index__
def test_index_scalar(scalar_virtual_array):
    assert scalar_virtual_array.__index__() == 42


def test_index_array(virtual_array):
    with pytest.raises(TypeError, match=r"Only scalar arrays can be used as an index"):
        virtual_array.__index__()


# Test __len__
def test_len(virtual_array, two_dim_virtual_array, shape_generator_param):
    if shape_generator_param is None:
        assert len(virtual_array) == 5
        assert len(two_dim_virtual_array) == 2
    else:
        # Materialize to get concrete length
        virtual_array.materialize()
        two_dim_virtual_array.materialize()
        assert len(virtual_array) == 5
        assert len(two_dim_virtual_array) == 2


def test_len_scalar():
    # Scalar arrays don't have a length
    nplike = Numpy.instance()
    scalar_va = VirtualNDArray(
        nplike, shape=(), dtype=np.int64, generator=lambda: np.array(42, dtype=np.int64)
    )
    with pytest.raises(TypeError, match=r"len\(\) of unsized object"):
        len(scalar_va)


# Test __iter__
def test_iter(virtual_array):
    assert list(virtual_array) == [1, 2, 3, 4, 5]
    # Should be materialized after this
    assert virtual_array.is_materialized


# Test __dlpack__ and __dlpack_device__
@pytest.mark.skipif(
    tuple(map(int, np.__version__.split(".")[:2])) < (1, 23),
    reason="Test requires NumPy >= 1.23",
)
def test_dlpack_device(virtual_array):
    virtual_array.__dlpack_device__()


@pytest.mark.skipif(
    tuple(map(int, np.__version__.split(".")[:2])) < (1, 23),
    reason="Test requires NumPy >= 1.23",
)
def test_dlpack(virtual_array):
    virtual_array.__dlpack__()


# Test __array_ufunc__
def test_array_ufunc(virtual_array, monkeypatch):
    # Call a ufunc on the virtual array
    result = np.add(virtual_array, np.array([1, 2, 3, 4, 5]))
    assert virtual_array.is_materialized
    np.testing.assert_array_equal(result, np.array([2, 4, 6, 8, 10]))


# Test the helper function maybe_materialize
def test_maybe_materialize():
    nplike = Numpy.instance()
    va1 = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([1, 2, 3], dtype=np.int64),
    )
    va2 = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([4, 5], dtype=np.int64),
    )
    regular_array = np.array([6, 7, 8])

    result = maybe_materialize(va1, regular_array, va2)

    assert len(result) == 3
    assert isinstance(result[0], np.ndarray)
    assert isinstance(result[1], np.ndarray)
    assert isinstance(result[2], np.ndarray)
    np.testing.assert_array_equal(result[0], np.array([1, 2, 3]))
    np.testing.assert_array_equal(result[1], np.array([6, 7, 8]))
    np.testing.assert_array_equal(result[2], np.array([4, 5]))


# Tests for float virtual array
def test_float_array_init(numpy_like, float_array_generator, shape_generator_param):
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=float_array_generator,
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )

    assert va._shape == (unknown_length,)
    assert va.shape == (5,)

    assert va.dtype == np.dtype(np.float64)
    if shape_generator_param is not None:
        assert not va.is_materialized
    else:
        assert va.is_materialized


def test_float_array_materialize(float_virtual_array):
    result = float_virtual_array.materialize()
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1.1, 2.2, 3.3, 4.4, 5.5]))
    assert float_virtual_array.is_materialized
    assert float_virtual_array.shape == (
        5,
    )  # Now concrete regardless of shape_generator


def test_float_array_slicing(numpy_like, float_array_generator, shape_generator_param):
    # Test basic slice
    if shape_generator_param is None:
        float_virtual_array1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
        )
    else:
        float_virtual_array1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )

    sliced = float_virtual_array1[1:4]
    assert isinstance(sliced, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced.shape == (3,)
    else:
        # Materialize to check shape
        result = sliced.materialize()
        assert result.shape == (3,)

    np.testing.assert_array_almost_equal(
        sliced.materialize(), np.array([2.2, 3.3, 4.4])
    )

    # Test step slice
    if shape_generator_param is None:
        float_virtual_array2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
        )
    else:
        float_virtual_array2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )

    sliced_step = float_virtual_array2[::2]
    assert isinstance(sliced_step, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced_step.shape == (3,)
    else:
        # Materialize to check shape
        result = sliced_step.materialize()
        assert result.shape == (3,)

    np.testing.assert_array_almost_equal(
        sliced_step.materialize(), np.array([1.1, 3.3, 5.5])
    )

    # Test negative step
    if shape_generator_param is None:
        float_virtual_array3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
        )
    else:
        float_virtual_array3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )

    sliced_neg = float_virtual_array3[::-1]
    assert isinstance(sliced_neg, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced_neg.shape == (5,)
    else:
        # Materialize to check shape
        result = sliced_neg.materialize()
        assert result.shape == (5,)

    np.testing.assert_array_almost_equal(
        sliced_neg.materialize(), np.array([5.5, 4.4, 3.3, 2.2, 1.1])
    )

    # Test complex slice
    if shape_generator_param is None:
        float_virtual_array4 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
        )
    else:
        float_virtual_array4 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=float_array_generator,
            shape_generator=lambda: (5,),
        )

    sliced_complex = float_virtual_array4[4:1:-2]
    assert isinstance(sliced_complex, VirtualNDArray)

    if shape_generator_param is None:
        assert sliced_complex.shape == (2,)
    else:
        # Materialize to check shape
        result = sliced_complex.materialize()
        assert result.shape == (2,)

    np.testing.assert_array_almost_equal(
        sliced_complex.materialize(), np.array([5.5, 3.3])
    )


def test_float_array_operations(float_virtual_array):
    # Test arithmetic operations
    result = float_virtual_array + 1.0
    np.testing.assert_array_almost_equal(result, np.array([2.1, 3.2, 4.3, 5.4, 6.5]))

    # Test multiplication
    result = float_virtual_array * 2.0
    np.testing.assert_array_almost_equal(result, np.array([2.2, 4.4, 6.6, 8.8, 11.0]))

    # Test division
    result = float_virtual_array / 2.0
    np.testing.assert_array_almost_equal(result, np.array([0.55, 1.1, 1.65, 2.2, 2.75]))

    # Test mixed operation with integer array
    int_array = np.array([1, 2, 3, 4, 5])
    result = float_virtual_array + int_array
    np.testing.assert_array_almost_equal(result, np.array([2.1, 4.2, 6.3, 8.4, 10.5]))


def test_float_array_view(float_virtual_array):
    # Test view as different float type
    view = float_virtual_array.view(np.float32)
    assert isinstance(view, VirtualNDArray)
    assert view.dtype == np.dtype(np.float32)

    # Test materialization of view
    materialized = view.materialize()
    assert materialized.dtype == np.dtype(np.float32)


def test_float_to_int_comparison(float_virtual_array, virtual_array):
    # Compare float and int arrays
    float_data = float_virtual_array.materialize()
    int_data = virtual_array.materialize()

    # Test basic properties
    assert float_virtual_array.shape == virtual_array.shape  # After materialization
    assert float_virtual_array.ndim == virtual_array.ndim
    assert float_virtual_array.size == virtual_array.size

    # Test conversion between types
    int_view = float_virtual_array.view(np.int64)
    assert int_view.dtype == np.dtype(np.int64)

    # Test operations between float and int arrays
    result = float_virtual_array + virtual_array
    expected = float_data + int_data
    np.testing.assert_array_almost_equal(result, expected)


# Test rounding operations specific to float arrays
def test_float_array_rounding(shape_generator_param):
    nplike = Numpy.instance()

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=lambda: np.array([1.1, 2.5, 3.7, 4.2, 5.9], dtype=np.float64),
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=lambda: np.array([1.1, 2.5, 3.7, 4.2, 5.9], dtype=np.float64),
            shape_generator=lambda: (5,),
        )

    # Test floor
    result = np.floor(va)
    np.testing.assert_array_almost_equal(result, np.array([1.0, 2.0, 3.0, 4.0, 5.0]))

    # Test ceil
    result = np.ceil(va)
    np.testing.assert_array_almost_equal(result, np.array([2.0, 3.0, 4.0, 5.0, 6.0]))

    # Test round
    result = np.round(va)
    np.testing.assert_array_almost_equal(result, np.array([1.0, 2.0, 4.0, 4.0, 6.0]))


# Test NaN handling
def test_float_array_nan(shape_generator_param):
    nplike = Numpy.instance()

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=lambda: np.array(
                [1.1, np.nan, 3.3, np.nan, 5.5], dtype=np.float64
            ),
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.float64,
            generator=lambda: np.array(
                [1.1, np.nan, 3.3, np.nan, 5.5], dtype=np.float64
            ),
            shape_generator=lambda: (5,),
        )

    # Test isnan
    result = np.isnan(va)
    np.testing.assert_array_equal(result, np.array([False, True, False, True, False]))

    # Test nanmean
    result = np.nanmean(va)
    np.testing.assert_almost_equal(result, (1.1 + 3.3 + 5.5) / 3)


# Multidimensional slicing tests
def test_multidim_slicing(two_dim_virtual_array):
    # Test slicing on first dimension
    sliced = two_dim_virtual_array[0]
    assert isinstance(sliced, np.ndarray)  # Should be materialized
    np.testing.assert_array_equal(sliced, np.array([1, 2, 3]))

    # Fresh array for next test to avoid materialization effects
    nplike = Numpy.instance()
    va = VirtualNDArray(
        nplike,
        shape=(unknown_length, 3),
        dtype=np.int64,
        generator=lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64),
    )

    # Test advanced indexing
    sliced = va[:, 1]
    # This should materialize because it's not a simple first-dimension slice
    assert isinstance(sliced, np.ndarray)
    np.testing.assert_array_equal(sliced, np.array([2, 5]))


# Test empty array handling
def test_empty_array(shape_generator_param):
    nplike = Numpy.instance()

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: np.array([], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: np.array([], dtype=np.int64),
            shape_generator=lambda: (0,),
        )

    materialized = va.materialize()
    assert len(materialized) == 0
    assert va.size == 0

    if shape_generator_param is None:
        assert va.shape == (0,)
    else:
        # After materialization, shape should be concrete
        assert va.shape == (0,)


# Test with structured dtypes
def test_structured_dtype(shape_generator_param):
    dtype = np.dtype([("name", "U10"), ("age", "i4"), ("weight", "f8")])
    data = np.array(
        [("Alice", 25, 55.0), ("Bob", 30, 70.5), ("Charlie", 35, 65.2)], dtype=dtype
    )

    nplike = Numpy.instance()

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=dtype,
            generator=lambda: data,
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=dtype,
            generator=lambda: data,
            shape_generator=lambda: (3,),
        )

    assert va.dtype == dtype
    materialized = va.materialize()
    assert materialized["name"][1] == "Bob"
    assert materialized["age"][2] == 35
    assert materialized["weight"][0] == 55.0


# Test with large arrays to check memory efficiency
def test_large_array_memory(shape_generator_param):
    # Create a large array that would consume significant memory
    nplike = Numpy.instance()

    # Define a generator that would create a large array
    def large_array_generator():
        return np.ones((1000, 1000), dtype=np.float64)

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length, 1000),
            dtype=np.float64,
            generator=large_array_generator,
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length, 1000),
            dtype=np.float64,
            generator=large_array_generator,
            shape_generator=lambda: (1000, 1000),
        )

    assert va.nbytes == unknown_length
    va.get_shape()
    assert va.nbytes == 1000 * 1000 * 8
    if shape_generator_param is not None:
        assert not va.is_materialized

    # Access just one element to check if full materialization happens
    element = va[0, 0]
    assert element == 1.0

    # Now the array should be materialized
    assert va.nbytes == 1000 * 1000 * 8  # Should still be the same
    assert va.is_materialized


# Test error propagation from generator
def test_generator_error():
    nplike = Numpy.instance()

    def failing_generator():
        raise ValueError("Generator failure test")

    va = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=failing_generator,
    )

    with pytest.raises(ValueError, match="Generator failure test"):
        va.get_shape()


# Test nested VirtualNDArrays (generator returns another VirtualNDArray)
def test_nested_virtual_arrays(shape_generator_param):
    nplike = Numpy.instance()

    # Inner virtual array
    if shape_generator_param is None:
        inner_va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: np.array([10, 20, 30], np.int64),
        )
    else:
        inner_va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: np.array([10, 20, 30], np.int64),
            shape_generator=lambda: (3,),
        )

    # Outer virtual array, generator returns inner virtual array
    if shape_generator_param is None:
        outer_va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: inner_va,
        )
    else:
        outer_va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=lambda: inner_va,
            shape_generator=lambda: (3,),
        )

    # Should materialize both
    result = outer_va.materialize()
    np.testing.assert_array_equal(result, np.array([10, 20, 30]))
    assert inner_va.is_materialized
    assert outer_va.is_materialized


# Test with complex numbers
def test_complex_numbers(shape_generator_param):
    nplike = Numpy.instance()

    if shape_generator_param is None:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.complex128,
            generator=lambda: np.array([1 + 2j, 3 + 4j, 5 + 6j], dtype=np.complex128),
        )
    else:
        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.complex128,
            generator=lambda: np.array([1 + 2j, 3 + 4j, 5 + 6j], dtype=np.complex128),
            shape_generator=lambda: (3,),
        )

    assert va.dtype == np.dtype(np.complex128)
    materialized = va.materialize()
    np.testing.assert_array_equal(materialized, np.array([1 + 2j, 3 + 4j, 5 + 6j]))

    # Test complex operations
    result = va * (2 + 1j)
    expected = np.array([1 + 2j, 3 + 4j, 5 + 6j]) * (2 + 1j)
    np.testing.assert_array_almost_equal(result, expected)


# Test slice with 0 step raises error
def test_slice_zero_step():
    nplike = Numpy.instance()
    va = VirtualNDArray(
        nplike,
        shape=(unknown_length,),
        dtype=np.int64,
        generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
    )

    with pytest.raises(ValueError):
        va[::0]  # Step can't be zero


def test_slice_length_calculation():
    nplike = Numpy.instance()
    test_cases = [
        # (slice, expected_length, array_length)
        (slice(None), 5, 5),  # [:] -> 5
        (slice(1, 4), 3, 5),  # [1:4] -> 3
        (slice(None, None, 2), 3, 5),  # [::2] -> 3
        (slice(None, None, -1), 5, 5),  # [::-1] -> 5
        (slice(4, 1, -1), 3, 5),  # [4:1:-1] -> 3
        (slice(1, 10), 4, 5),  # [1:10] -> 4 (out of bounds)
        (slice(-10, 10), 5, 5),  # [-10:10] -> 5 (both out of bounds)
        (slice(10, 20), 0, 5),  # [10:20] -> 0 (start beyond length)
        (slice(None, None, 3), 2, 5),  # [::3] -> 2
    ]

    for slice_obj, expected_length, array_length in test_cases:
        # Create a closure that captures the current value of array_length
        def create_generator(length):
            return lambda: np.ones(length, dtype=np.int64)

        va = VirtualNDArray(
            nplike,
            shape=(unknown_length,),
            dtype=np.int64,
            generator=create_generator(array_length),
        )

        sliced = va[slice_obj]

        if shape_generator_param is None:
            # Without shape_generator, we can check the shape directly
            assert isinstance(sliced, VirtualNDArray)
            assert sliced.shape[0] == expected_length, f"Failed for slice {slice_obj}"
        else:
            # With shape_generator, need to materialize to check shape
            result = sliced.materialize()
            assert len(result) == expected_length, f"Failed for slice {slice_obj}"


# Test nplike of obj
def test_nplike_of_obj(
    virtual_array, float_virtual_array, numpy_like, shape_generator_param
):
    assert nplike_of_obj(virtual_array) is numpy_like
    assert nplike_of_obj(float_virtual_array) is numpy_like


# Test backend of obj
def test_backend_of_obj(virtual_array, float_virtual_array, shape_generator_param):
    assert backend_of_obj(virtual_array).name == "cpu"
    assert backend_of_obj(float_virtual_array).name == "cpu"


# Test array creation methods with VirtualNDArray
def test_asarray_virtual_array_unmaterialized(
    numpy_like, virtual_array, shape_generator_param
):
    # Test with unmaterialized VirtualNDArray
    result = numpy_like.asarray(virtual_array)
    assert result is virtual_array  # Should return the same object
    assert result.dtype == virtual_array.dtype  # Should have same dtype
    assert not virtual_array.is_materialized
    assert not result.is_materialized
    # Check materialized values are correct
    np.testing.assert_array_equal(result.materialize(), np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_materialized(
    numpy_like, virtual_array, shape_generator_param
):
    # Test with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.asarray(virtual_array)
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_with_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test with dtype parameter
    result = numpy_like.asarray(virtual_array, dtype=np.float64)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == np.dtype(np.float64)
    assert not result.is_materialized
    # Check materialized values have correct dtype and values
    materialized = result.materialize()
    assert materialized.dtype == np.dtype(np.float64)
    np.testing.assert_array_equal(
        materialized, np.array([1, 2, 3, 4, 5], dtype=np.float64)
    )


def test_asarray_virtual_array_materialized_copy_false_dtype_error(
    numpy_like, virtual_array, shape_generator_param
):
    # Test materialized VirtualNDArray with copy=False and different dtype raises error
    virtual_array.materialize()
    with pytest.raises(
        ValueError,
        match="asarray was called with copy=False for an array of a different dtype",
    ):
        # Should raise because we're trying to change the dtype without copying
        numpy_like.asarray(virtual_array, dtype=np.float64, copy=False)


def test_asarray_virtual_array_copy_true_same_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test copy=True with same dtype returns new VirtualNDArray
    result = numpy_like.asarray(virtual_array, copy=True)
    assert isinstance(result, VirtualNDArray)
    assert result is not virtual_array
    assert not result.is_materialized
    assert result.dtype == virtual_array.dtype
    # Check materialized values are correct
    np.testing.assert_array_equal(result.materialize(), np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_copy_true_different_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test copy=True with different dtype
    result = numpy_like.asarray(virtual_array, dtype=np.float64, copy=True)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == np.dtype(np.float64)
    assert not result.is_materialized
    # Check materialized values have correct dtype and values
    materialized = result.materialize()
    assert materialized.dtype == np.dtype(np.float64)
    np.testing.assert_array_equal(
        materialized, np.array([1, 2, 3, 4, 5], dtype=np.float64)
    )


def test_asarray_virtual_array_copy_none_same_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test copy=None preserves lazy evaluation with same dtype
    result = numpy_like.asarray(virtual_array, copy=None)
    assert isinstance(result, VirtualNDArray)
    assert not result.is_materialized
    assert result.dtype == virtual_array.dtype
    # Check materialized values are correct
    np.testing.assert_array_equal(result.materialize(), np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_copy_none_different_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test copy=None with different dtype
    result = numpy_like.asarray(virtual_array, dtype=np.float64, copy=None)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == np.dtype(np.float64)
    assert not result.is_materialized
    # Check materialized values have correct dtype and values
    materialized = result.materialize()
    assert materialized.dtype == np.dtype(np.float64)
    np.testing.assert_array_equal(
        materialized, np.array([1, 2, 3, 4, 5], dtype=np.float64)
    )


def test_asarray_virtual_array_copy_false_same_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test VirtualNDArray with copy=False and same dtype
    result = numpy_like.asarray(virtual_array, copy=False)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == virtual_array.dtype
    assert not result.is_materialized
    # Check materialized values are correct
    np.testing.assert_array_equal(result.materialize(), np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_copy_false_different_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test VirtualNDArray copy=False with dtype change - should create VirtualNDArray but error on materialization
    result = numpy_like.asarray(virtual_array, dtype=np.float64, copy=False)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == np.dtype(np.float64)
    assert not result.is_materialized

    # Should error when trying to materialize due to copy=False constraint
    with pytest.raises(
        ValueError,
        match="asarray was called with copy=False for an array of a different dtype",
    ):
        result.materialize()


def test_asarray_virtual_array_dtype_none_behavior(
    numpy_like, virtual_array, shape_generator_param
):
    # Test VirtualNDArray with dtype=None preserves original dtype
    result = numpy_like.asarray(virtual_array, dtype=None)
    assert isinstance(result, VirtualNDArray)
    assert result.dtype == virtual_array.dtype
    assert not result.is_materialized
    # Check materialized values are correct
    np.testing.assert_array_equal(result.materialize(), np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_materialized_copy_true(
    numpy_like, virtual_array, shape_generator_param
):
    # Test materialized VirtualNDArray with copy=True
    virtual_array.materialize()
    result = numpy_like.asarray(virtual_array, copy=True)
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))
    # Should be a copy - modifying result shouldn't affect original
    result[0] = 999
    assert virtual_array.materialize()[0] == 1


def test_asarray_virtual_array_materialized_copy_false_same_dtype(
    numpy_like, virtual_array, shape_generator_param
):
    # Test materialized VirtualNDArray with copy=False and same dtype
    virtual_array.materialize()
    result = numpy_like.asarray(virtual_array, copy=False)
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))


def test_asarray_virtual_array_materialized_dtype_conversion(
    numpy_like, virtual_array, shape_generator_param
):
    # Test materialized VirtualNDArray with dtype conversion
    virtual_array.materialize()
    result = numpy_like.asarray(virtual_array, dtype=np.float64)
    assert isinstance(result, np.ndarray)
    assert result.dtype == np.dtype(np.float64)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5], dtype=np.float64))


def test_asarray_virtual_array_complex_dtype_chain(
    numpy_like, virtual_array, shape_generator_param
):
    # Test chaining dtype conversions with VirtualNDArray
    float_result = numpy_like.asarray(virtual_array, dtype=np.float32)
    assert isinstance(float_result, VirtualNDArray)
    assert float_result.dtype == np.dtype(np.float32)

    # Further conversion
    double_result = numpy_like.asarray(float_result, dtype=np.float64)
    assert isinstance(double_result, VirtualNDArray)
    assert double_result.dtype == np.dtype(np.float64)

    # Check final materialized values
    materialized = double_result.materialize()
    assert materialized.dtype == np.dtype(np.float64)
    np.testing.assert_array_equal(
        materialized, np.array([1, 2, 3, 4, 5], dtype=np.float64)
    )


def test_ascontiguousarray_unmaterialized(
    numpy_like, virtual_array, shape_generator_param
):
    # Test with unmaterialized VirtualNDArray
    result = numpy_like.ascontiguousarray(virtual_array)
    assert isinstance(result, VirtualNDArray)
    assert not result.is_materialized
    assert result.shape == virtual_array.shape
    assert result.dtype == virtual_array.dtype


def test_ascontiguousarray_materialized(
    numpy_like, virtual_array, shape_generator_param
):
    # Test with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.ascontiguousarray(virtual_array)
    assert isinstance(result, np.ndarray)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))


def test_frombuffer_with_virtual_array(
    numpy_like, virtual_array, shape_generator_param
):
    # Test frombuffer with VirtualNDArray (should raise TypeError)
    with pytest.raises(
        TypeError, match="virtual arrays are not supported in `frombuffer`"
    ):
        numpy_like.frombuffer(virtual_array)


# Test array creation methods using materialization info
def test_zeros_like_unmaterialized(numpy_like, virtual_array, shape_generator_param):
    # Test zeros_like with unmaterialized VirtualNDArray
    result = numpy_like.zeros_like(virtual_array)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.zeros(5, dtype=np.int64))
    if shape_generator_param is None:
        assert virtual_array.is_materialized
    else:
        assert not virtual_array.is_materialized


def test_zeros_like_materialized(numpy_like, virtual_array, shape_generator_param):
    # Test zeros_like with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.zeros_like(virtual_array)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.zeros(5, dtype=np.int64))


def test_ones_like_unmaterialized(numpy_like, virtual_array, shape_generator_param):
    # Test ones_like with unmaterialized VirtualNDArray
    result = numpy_like.ones_like(virtual_array)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.ones(5, dtype=np.int64))
    if shape_generator_param is None:
        assert virtual_array.is_materialized
    else:
        assert not virtual_array.is_materialized


def test_ones_like_materialized(numpy_like, virtual_array, shape_generator_param):
    # Test ones_like with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.ones_like(virtual_array)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.ones(5, dtype=np.int64))


def test_full_like_unmaterialized(numpy_like, virtual_array, shape_generator_param):
    # Test full_like with unmaterialized VirtualNDArray
    result = numpy_like.full_like(virtual_array, 7)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.full(5, 7, dtype=np.int64))
    if shape_generator_param is None:
        assert virtual_array.is_materialized
    else:
        assert not virtual_array.is_materialized


def test_full_like_materialized(numpy_like, virtual_array):
    # Test full_like with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.full_like(virtual_array, 7)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5,)
    assert result.dtype == np.dtype(np.int64)
    np.testing.assert_array_equal(result, np.full(5, 7, dtype=np.int64))


# Test arange and meshgrid with VirtualNDArray parameters
def test_arange_with_virtual_array_start(numpy_like, scalar_virtual_array):
    # Test arange with VirtualNDArray parameter
    arange = numpy_like.arange(scalar_virtual_array, 10)
    assert scalar_virtual_array.is_materialized
    np.testing.assert_array_equal(arange, np.arange(42, 10))


def test_meshgrid_with_virtual_array(numpy_like, virtual_array):
    # Test meshgrid with VirtualNDArray parameter
    virtual_array.materialize()
    result = numpy_like.meshgrid(virtual_array)
    assert len(result) == 1
    np.testing.assert_array_equal(result[0], np.array([1, 2, 3, 4, 5]))


# Test testing functions with VirtualNDArray
def test_array_equal_with_virtual_arrays(
    numpy_like, virtual_array, shape_generator_param
):
    # Create two identical VirtualNDArrays
    va1 = virtual_array
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    # Test array_equal
    result = numpy_like.array_equal(va1, va2)
    assert result is True

    # Test with a different VirtualNDArray
    if shape_generator_param is None:
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array(
                [1, 2, 3, 4, 6], dtype=np.int64
            ),  # Different last value
        )
    else:
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array(
                [1, 2, 3, 4, 6], dtype=np.int64
            ),  # Different last value
            shape_generator=lambda: (5,),
        )

    result = numpy_like.array_equal(va1, va3)
    assert result is False


def test_array_equal_with_equal_nan(numpy_like, shape_generator_param):
    # Test array_equal with equal_nan=True
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0], dtype=np.float64),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0], dtype=np.float64),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )

    # Should be False by default (NaN != NaN)
    result = numpy_like.array_equal(va1, va2)
    assert bool(result) is False

    # Should be True with equal_nan=True
    result = numpy_like.array_equal(va1, va2, equal_nan=True)
    assert bool(result) is True


def test_searchsorted_with_virtual_arrays(
    numpy_like, virtual_array, shape_generator_param
):
    # Test searchsorted with VirtualNDArray
    if shape_generator_param is None:
        values = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([0, 3, 6], dtype=np.int64),
        )
    else:
        values = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([0, 3, 6], dtype=np.int64),
            shape_generator=lambda: (3,),
        )

    result = numpy_like.searchsorted(virtual_array, values)
    np.testing.assert_array_equal(
        result, np.array([0, 2, 5])
    )  # Indices where values would be inserted


# Test ufunc application with VirtualNDArray
def test_apply_ufunc_with_virtual_arrays(
    numpy_like, virtual_array, shape_generator_param
):
    # Test apply_ufunc with add operation
    result = numpy_like.apply_ufunc(np.add, "__call__", [virtual_array, 10])
    np.testing.assert_array_equal(result, np.array([11, 12, 13, 14, 15]))

    # Test apply_ufunc with multiple VirtualNDArrays
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    result = numpy_like.apply_ufunc(np.multiply, "__call__", [virtual_array, va2])
    np.testing.assert_array_equal(result, np.array([10, 40, 90, 160, 250]))


# Test manipulation functions with VirtualNDArray
def test_broadcast_arrays_with_virtual_arrays(
    numpy_like, virtual_array, shape_generator_param
):
    # Test broadcast_arrays with VirtualNDArrays
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10], dtype=np.int64),
            shape_generator=lambda: (1,),
        )

    result = numpy_like.broadcast_arrays(virtual_array, va2)
    assert len(result) == 2
    np.testing.assert_array_equal(result[0], np.array([1, 2, 3, 4, 5]))
    np.testing.assert_array_equal(result[1], np.array([10, 10, 10, 10, 10]))


def test_reshape_unmaterialized(numpy_like, virtual_array, shape_generator_param):
    # Test reshape with unmaterialized VirtualNDArray
    result = numpy_like.reshape(virtual_array, (5, 1))
    assert isinstance(result, VirtualNDArray)
    assert not result.is_materialized
    assert result.shape == (5, 1)

    # Test reshape with -1 dimension
    result = numpy_like.reshape(virtual_array, (-1, 1))
    if shape_generator_param is None:
        assert isinstance(result, np.ndarray)
        assert virtual_array.is_materialized
    else:
        assert isinstance(result, VirtualNDArray)
        assert not virtual_array.is_materialized
    assert result.shape == (5, 1)


def test_reshape_materialized(numpy_like, virtual_array):
    # Test reshape with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.reshape(virtual_array, (5, 1))
    assert isinstance(result, np.ndarray)
    assert result.shape == (5, 1)

    # Test reshape with copy=True
    result = numpy_like.reshape(virtual_array, (5, 1), copy=True)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5, 1)

    # Test reshape with copy=False
    result = numpy_like.reshape(virtual_array, (5, 1), copy=False)
    assert isinstance(result, np.ndarray)
    assert result.shape == (5, 1)


def test_derive_slice_for_length(numpy_like):
    # Test derive_slice_for_length method
    slice_obj = slice(1, 4, 1)
    start, stop, step, slice_length = numpy_like.derive_slice_for_length(slice_obj, 5)
    assert start == 1
    assert stop == 4
    assert step == 1
    assert slice_length == 3

    # Test with negative step
    slice_obj = slice(4, 1, -1)
    start, stop, step, slice_length = numpy_like.derive_slice_for_length(slice_obj, 5)
    assert start == 4
    assert stop == 1
    assert step == -1
    assert slice_length == 3

    # Test with None values
    slice_obj = slice(None, None, 2)
    start, stop, step, slice_length = numpy_like.derive_slice_for_length(slice_obj, 5)
    assert start == 0
    assert stop == 5
    assert step == 2
    assert slice_length == 3


def test_nonzero_with_virtual_array(numpy_like, virtual_array):
    # Test nonzero with VirtualNDArray
    result = numpy_like.nonzero(virtual_array)
    assert len(result) == 1
    np.testing.assert_array_equal(
        result[0], np.array([0, 1, 2, 3, 4])
    )  # All values are non-zero


def test_where_with_virtual_arrays(numpy_like, virtual_array, shape_generator_param):
    # Test where with VirtualNDArrays
    if shape_generator_param is None:
        condition = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [True, False, True, False, True], dtype=np.bool_
            ),
        )
        x1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
        )
    else:
        condition = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [True, False, True, False, True], dtype=np.bool_
            ),
            shape_generator=lambda: (5,),
        )
        x1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    result = numpy_like.where(condition, virtual_array, x1)
    np.testing.assert_array_equal(result, np.array([1, 20, 3, 40, 5]))


def test_unique_values_with_virtual_array(numpy_like, shape_generator_param):
    # Test unique_values with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 2, 3, 3, 3, 1], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 2, 3, 3, 3, 1], dtype=np.int64),
            shape_generator=lambda: (7,),
        )

    result = numpy_like.unique_values(va)
    np.testing.assert_array_equal(result, np.array([1, 2, 3]))


def test_unique_all_with_virtual_array(numpy_like, shape_generator_param):
    # Test unique_all with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 2, 3, 3, 3, 1], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 2, 3, 3, 3, 1], dtype=np.int64),
            shape_generator=lambda: (7,),
        )

    result = numpy_like.unique_all(va)
    np.testing.assert_array_equal(result.values, np.array([1, 2, 3]))
    np.testing.assert_array_equal(result.counts, np.array([2, 2, 3]))
    # Check inverse indices have original shape
    assert result.inverse_indices.shape == (7,)


def test_sort_with_virtual_array(numpy_like, shape_generator_param):
    # Test sort with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    # Sort ascending
    result = numpy_like.sort(va)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))

    # Sort descending
    result = numpy_like.sort(va, descending=True)
    np.testing.assert_array_equal(result, np.array([5, 4, 3, 2, 1]))

    # Test with 2D array and axis
    if shape_generator_param is None:
        va2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
        )
    else:
        va2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    result = numpy_like.sort(va2d, axis=1)
    np.testing.assert_array_equal(result, np.array([[1, 2, 3], [4, 5, 6]]))


def test_concat_with_virtual_arrays(numpy_like, virtual_array, shape_generator_param):
    # Test concat with VirtualNDArrays
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([6, 7, 8], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([6, 7, 8], dtype=np.int64),
            shape_generator=lambda: (3,),
        )

    result = numpy_like.concat([virtual_array, va2])
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5, 6, 7, 8]))

    # Test with axis parameter
    if shape_generator_param is None:
        va2d1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 2),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2], [3, 4]], dtype=np.int64),
        )
        va2d2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 2),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[5, 6], [7, 8]], dtype=np.int64),
        )
    else:
        va2d1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 2),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2], [3, 4]], dtype=np.int64),
            shape_generator=lambda: (2, 2),
        )
        va2d2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 2),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[5, 6], [7, 8]], dtype=np.int64),
            shape_generator=lambda: (2, 2),
        )

    result = numpy_like.concat([va2d1, va2d2], axis=1)
    np.testing.assert_array_equal(result, np.array([[1, 2, 5, 6], [3, 4, 7, 8]]))


def test_repeat_with_virtual_array(numpy_like, virtual_array, shape_generator_param):
    # Test repeat with VirtualNDArray
    result = numpy_like.repeat(virtual_array, 2)
    np.testing.assert_array_equal(result, np.array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))

    # Test with axis parameter
    if shape_generator_param is None:
        va2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64),
        )
    else:
        va2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    result = numpy_like.repeat(va2d, 2, axis=0)
    np.testing.assert_array_equal(
        result, np.array([[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]])
    )


def test_stack_with_virtual_arrays(numpy_like, virtual_array, shape_generator_param):
    # Test stack with VirtualNDArrays
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([6, 7, 8, 9, 10], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([6, 7, 8, 9, 10], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    result = numpy_like.stack([virtual_array, va2])
    np.testing.assert_array_equal(result, np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]))

    # Test with axis parameter
    result = numpy_like.stack([virtual_array, va2], axis=1)
    np.testing.assert_array_equal(
        result, np.array([[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]])
    )


def test_packbits_with_virtual_array(numpy_like, shape_generator_param):
    # Test packbits with VirtualNDArray of booleans
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([1, 0, 1, 0, 1, 0, 1, 0], dtype=bool),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([1, 0, 1, 0, 1, 0, 1, 0], dtype=bool),
            shape_generator=lambda: (8,),
        )

    result = numpy_like.packbits(va)
    np.testing.assert_array_equal(
        result, np.array([170], dtype=np.uint8)
    )  # 10101010 in binary = 170


def test_unpackbits_with_virtual_array(numpy_like, shape_generator_param):
    # Test unpackbits with VirtualNDArray of uint8
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.uint8),
            generator=lambda: np.array([170], dtype=np.uint8),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.uint8),
            generator=lambda: np.array([170], dtype=np.uint8),
            shape_generator=lambda: (1,),
        )

    result = numpy_like.unpackbits(va)
    np.testing.assert_array_equal(
        result, np.array([1, 0, 1, 0, 1, 0, 1, 0], dtype=np.uint8)
    )


def test_broadcast_to_with_virtual_array(numpy_like, virtual_array):
    # Test broadcast_to with VirtualNDArray
    result = numpy_like.broadcast_to(virtual_array, (3, 5))
    assert result.shape == (3, 5)

    # Check all rows are the same
    np.testing.assert_array_equal(result[0], np.array([1, 2, 3, 4, 5]))
    np.testing.assert_array_equal(result[1], np.array([1, 2, 3, 4, 5]))
    np.testing.assert_array_equal(result[2], np.array([1, 2, 3, 4, 5]))


def test_strides_with_virtual_array(numpy_like, virtual_array):
    # Test strides with VirtualNDArray
    # First test without materializing
    assert numpy_like.strides(virtual_array) == (8,)  # 8 bytes per int64

    # Then test after materializing
    virtual_array.materialize()
    assert numpy_like.strides(virtual_array) == (8,)


# Test addition and logical operations
def test_add_with_virtual_arrays(numpy_like, virtual_array, shape_generator_param):
    # Test add with VirtualNDArrays
    if shape_generator_param is None:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
        )
    else:
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 20, 30, 40, 50], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    result = numpy_like.add(virtual_array, va2)
    np.testing.assert_array_equal(result, np.array([11, 22, 33, 44, 55]))


def test_logical_or_with_virtual_arrays(numpy_like, shape_generator_param):
    # Test logical_or with VirtualNDArrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, False, False], dtype=np.bool_),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, False, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.logical_or(va1, va2)
    np.testing.assert_array_equal(result, np.array([True, True, True, False]))


def test_logical_and_with_virtual_arrays(numpy_like, shape_generator_param):
    # Test logical_and with VirtualNDArrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, False, False], dtype=np.bool_),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, False, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.logical_and(va1, va2)
    np.testing.assert_array_equal(result, np.array([True, False, False, False]))


def test_logical_not_with_virtual_array(numpy_like, shape_generator_param):
    # Test logical_not with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.logical_not(va)
    np.testing.assert_array_equal(result, np.array([False, True, False, True]))


# Test mathematical operations
def test_sqrt_with_virtual_array(numpy_like, shape_generator_param):
    # Test sqrt with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([4.0, 9.0, 16.0, 25.0], dtype=np.float64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([4.0, 9.0, 16.0, 25.0], dtype=np.float64),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.sqrt(va)
    np.testing.assert_array_almost_equal(result, np.array([2.0, 3.0, 4.0, 5.0]))


def test_exp_with_virtual_array(numpy_like, shape_generator_param):
    # Test exp with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([0.0, 1.0, 2.0], dtype=np.float64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([0.0, 1.0, 2.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )

    result = numpy_like.exp(va)
    np.testing.assert_array_almost_equal(result, np.array([1.0, np.e, np.e**2]))


def test_divide_with_virtual_arrays(numpy_like, shape_generator_param):
    # Test divide with VirtualNDArrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([10.0, 20.0, 30.0, 40.0], dtype=np.float64),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([2.0, 4.0, 5.0, 8.0], dtype=np.float64),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([10.0, 20.0, 30.0, 40.0], dtype=np.float64),
            shape_generator=lambda: (4,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([2.0, 4.0, 5.0, 8.0], dtype=np.float64),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.divide(va1, va2)
    np.testing.assert_array_almost_equal(result, np.array([5.0, 5.0, 6.0, 5.0]))


# Test special operations
def test_nan_to_num_with_virtual_array(numpy_like, shape_generator_param):
    # Test nan_to_num with VirtualNDArray containing NaN and infinity
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array(
                [1.0, np.nan, np.inf, -np.inf], dtype=np.float64
            ),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array(
                [1.0, np.nan, np.inf, -np.inf], dtype=np.float64
            ),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.nan_to_num(va)
    # NaN becomes 0.0, inf becomes large finite number, -inf becomes large negative number
    assert result[0] == 1.0
    assert result[1] == 0.0
    assert result[2] > 1e300  # Large positive number
    assert result[3] < -1e300  # Large negative number


def test_isclose_with_virtual_arrays(numpy_like, shape_generator_param):
    # Test isclose with VirtualNDArrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, 2.0, 3.0, np.nan], dtype=np.float64),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0001, 2.0, 3.1, np.nan], dtype=np.float64),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, 2.0, 3.0, np.nan], dtype=np.float64),
            shape_generator=lambda: (4,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0001, 2.0, 3.1, np.nan], dtype=np.float64),
            shape_generator=lambda: (4,),
        )

    # Default tolerance
    result = numpy_like.isclose(va1, va2)
    np.testing.assert_array_equal(result, np.array([False, True, False, False]))

    # Custom tolerance
    result = numpy_like.isclose(va1, va2, rtol=0.05)
    np.testing.assert_array_equal(result, np.array([True, True, True, False]))

    # Equal_nan parameter
    result = numpy_like.isclose(va1, va2, equal_nan=True)
    np.testing.assert_array_equal(result, np.array([False, True, False, True]))


def test_isnan_with_virtual_array(numpy_like, shape_generator_param):
    # Test isnan with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0, np.nan], dtype=np.float64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, np.nan, 3.0, np.nan], dtype=np.float64),
            shape_generator=lambda: (4,),
        )

    result = numpy_like.isnan(va)
    np.testing.assert_array_equal(result, np.array([False, True, False, True]))


# Test reduction operations
def test_all_with_virtual_array(numpy_like, shape_generator_param):
    # Test all with VirtualNDArray
    if shape_generator_param is None:
        va_all_true = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, True, True], dtype=np.bool_),
        )
        va_mixed = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, True], dtype=np.bool_),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [[True, True, False], [True, True, True]], dtype=np.bool_
            ),
        )
    else:
        va_all_true = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, True, True, True], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va_mixed = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([True, False, True, True], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [[True, True, False], [True, True, True]], dtype=np.bool_
            ),
            shape_generator=lambda: (2, 3),
        )

    # Test all(all True)
    result = numpy_like.all(va_all_true)
    assert result

    # Test all(mixed)
    result = numpy_like.all(va_mixed)
    assert not result

    # Test with axis parameter
    result = numpy_like.all(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([False, True]))

    # Test with keepdims
    result = numpy_like.all(va_2d, axis=1, keepdims=True)
    np.testing.assert_array_equal(result, np.array([[False], [True]]))


def test_any_with_virtual_array(numpy_like, shape_generator_param):
    # Test any with VirtualNDArray
    if shape_generator_param is None:
        va_all_false = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([False, False, False, False], dtype=np.bool_),
        )
        va_mixed = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([False, False, True, False], dtype=np.bool_),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [[False, False, False], [False, True, False]], dtype=np.bool_
            ),
        )
    else:
        va_all_false = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([False, False, False, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va_mixed = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array([False, False, True, False], dtype=np.bool_),
            shape_generator=lambda: (4,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.bool_),
            generator=lambda: np.array(
                [[False, False, False], [False, True, False]], dtype=np.bool_
            ),
            shape_generator=lambda: (2, 3),
        )

    # Test any(all False)
    result = numpy_like.any(va_all_false)
    assert not result

    # Test any(mixed)
    result = numpy_like.any(va_mixed)
    assert result

    # Test with axis parameter
    result = numpy_like.any(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([False, True]))


def test_min_with_virtual_array(numpy_like, shape_generator_param):
    # Test min with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
            shape_generator=lambda: (5,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    # Test overall min
    result = numpy_like.min(va)
    assert result == 1

    # Test with 2D array and axis
    result = numpy_like.min(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([1, 4]))


def test_max_with_virtual_array(numpy_like, shape_generator_param):
    # Test max with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([5, 3, 1, 4, 2], dtype=np.int64),
            shape_generator=lambda: (5,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[3, 1, 2], [6, 4, 5]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    # Test overall max
    result = numpy_like.max(va)
    assert result == 5

    # Test with 2D array and axis
    result = numpy_like.max(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([3, 6]))


def test_count_nonzero_with_virtual_array(numpy_like, shape_generator_param):
    # Test count_nonzero with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 0, 3, 0, 5, 0], dtype=np.int64),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 0, 2], [0, 4, 0]], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 0, 3, 0, 5, 0], dtype=np.int64),
            shape_generator=lambda: (6,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 0, 2], [0, 4, 0]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    # Test overall count
    result = numpy_like.count_nonzero(va)
    assert result == 3

    # Test with 2D array and axis
    result = numpy_like.count_nonzero(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([2, 1]))


def test_cumsum_with_virtual_array(numpy_like, shape_generator_param):
    # Test cumsum with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
            shape_generator=lambda: (5,),
        )
        va_2d = VirtualNDArray(
            numpy_like,
            shape=(unknown_length, 3),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64),
            shape_generator=lambda: (2, 3),
        )

    # Test cumsum
    result = numpy_like.cumsum(va)
    np.testing.assert_array_equal(result, np.array([1, 3, 6, 10, 15]))

    # Test with 2D array and axis
    result = numpy_like.cumsum(va_2d, axis=1)
    np.testing.assert_array_equal(result, np.array([[1, 3, 6], [4, 9, 15]]))


def test_real_imag_with_complex_virtual_array(numpy_like, shape_generator_param):
    # Test real and imag with complex VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.complex128),
            generator=lambda: np.array([1 + 2j, 3 + 4j, 5 + 6j], dtype=np.complex128),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.complex128),
            generator=lambda: np.array([1 + 2j, 3 + 4j, 5 + 6j], dtype=np.complex128),
            shape_generator=lambda: (3,),
        )

    # Test real
    result = numpy_like.real(va)
    np.testing.assert_array_equal(result, np.array([1.0, 3.0, 5.0]))

    # Test imag
    result = numpy_like.imag(va)
    np.testing.assert_array_equal(result, np.array([2.0, 4.0, 6.0]))


def test_angle_with_complex_virtual_array(numpy_like, shape_generator_param):
    # Test angle with complex VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.complex128),
            generator=lambda: np.array(
                [1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j], dtype=np.complex128
            ),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.complex128),
            generator=lambda: np.array(
                [1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j], dtype=np.complex128
            ),
            shape_generator=lambda: (4,),
        )

    # Test angle in radians
    result = numpy_like.angle(va)
    np.testing.assert_array_almost_equal(
        result, np.array([0, np.pi / 2, np.pi, -np.pi / 2])
    )

    # Test angle in degrees
    result = numpy_like.angle(va, deg=True)
    np.testing.assert_array_almost_equal(result, np.array([0, 90, 180, -90]))


def test_round_with_virtual_array(numpy_like, shape_generator_param):
    # Test round with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.234, 2.567, 3.499, 4.501], dtype=np.float64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.234, 2.567, 3.499, 4.501], dtype=np.float64),
            shape_generator=lambda: (4,),
        )

    # Test round to nearest integer
    result = numpy_like.round(va)
    np.testing.assert_array_equal(result, np.array([1.0, 3.0, 3.0, 5.0]))

    # Test round to 1 decimal place
    result = numpy_like.round(va, decimals=1)
    np.testing.assert_array_equal(result, np.array([1.2, 2.6, 3.5, 4.5]))

    # Test round to 2 decimal places
    result = numpy_like.round(va, decimals=2)
    np.testing.assert_array_equal(result, np.array([1.23, 2.57, 3.50, 4.50]))


def test_array_str_with_virtual_array_unmaterialized(numpy_like, virtual_array):
    # Test array_str with unmaterialized VirtualNDArray
    result = numpy_like.array_str(virtual_array)
    assert result == "[## ... ##]"


def test_array_str_with_virtual_array_materialized(numpy_like, virtual_array):
    # Test array_str with materialized VirtualNDArray
    virtual_array.materialize()
    result = numpy_like.array_str(virtual_array)
    assert "[1 2 3 4 5]" in result


def test_astype_with_virtual_array(numpy_like, virtual_array):
    # Test astype with VirtualNDArray
    result = numpy_like.astype(virtual_array, np.float64)
    np.testing.assert_array_equal(result, np.array([1.0, 2.0, 3.0, 4.0, 5.0]))
    assert result.dtype == np.dtype(np.float64)

    # Test with copy=False
    result = numpy_like.astype(virtual_array, np.int64, copy=False)
    np.testing.assert_array_equal(result, np.array([1, 2, 3, 4, 5]))
    assert result.dtype == np.dtype(np.int64)


def test_can_cast_with_virtual_array_dtype(numpy_like, virtual_array):
    # Test can_cast with VirtualNDArray's dtype
    # int64 can be cast to float64 with same_kind casting
    assert numpy_like.can_cast(virtual_array.dtype, np.float64) is True

    # int64 can be cast to complex64 with same_kind casting
    assert numpy_like.can_cast(virtual_array.dtype, np.complex64) is True


# Test various combinations and edge cases
def test_maybe_materialize_function(numpy_like, shape_generator_param):
    # Test the maybe_materialize utility function directly

    # Create a mix of VirtualNDArrays and regular arrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3], dtype=np.int64),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([4, 5, 6], dtype=np.int64),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3], dtype=np.int64),
            shape_generator=lambda: (3,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([4, 5, 6], dtype=np.int64),
            shape_generator=lambda: (3,),
        )

    regular_array = np.array([7, 8, 9])

    # Materialize none of them
    results = maybe_materialize(va1, va2, regular_array)
    assert len(results) == 3
    np.testing.assert_array_equal(results[0], np.array([1, 2, 3]))
    np.testing.assert_array_equal(results[1], np.array([4, 5, 6]))
    np.testing.assert_array_equal(results[2], np.array([7, 8, 9]))

    # Check that the VirtualNDArrays were materialized
    assert va1.is_materialized
    assert va2.is_materialized

    # Test with already materialized VirtualNDArrays
    if shape_generator_param is None:
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 11], dtype=np.int64),
        )
    else:
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([10, 11], dtype=np.int64),
            shape_generator=lambda: (2,),
        )

    va3.materialize()  # Pre-materialize

    results = maybe_materialize(va3, regular_array)
    assert len(results) == 2
    np.testing.assert_array_equal(results[0], np.array([10, 11]))
    np.testing.assert_array_equal(results[1], np.array([7, 8, 9]))


def test_operations_with_multiple_virtual_arrays(numpy_like, shape_generator_param):
    # Test a complex operation involving multiple VirtualNDArrays
    if shape_generator_param is None:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, 2.0, 3.0], dtype=np.float64),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([4.0, 5.0, 6.0], dtype=np.float64),
        )
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([7.0, 8.0, 9.0], dtype=np.float64),
        )
    else:
        va1 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([1.0, 2.0, 3.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )
        va2 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([4.0, 5.0, 6.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )
        va3 = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.float64),
            generator=lambda: np.array([7.0, 8.0, 9.0], dtype=np.float64),
            shape_generator=lambda: (3,),
        )

    # Expression: (va1 + va2) * va3
    # Should materialize all VirtualNDArrays
    result = numpy_like.add(va1, va2) * va3
    np.testing.assert_array_equal(
        result,
        np.array([35.0, 56.0, 81.0]),  # (1+4)*7, (2+5)*8, (3+6)*9
    )

    # Check that all VirtualNDArrays were materialized
    assert va1.is_materialized
    assert va2.is_materialized
    assert va3.is_materialized


def test_is_own_array_with_virtual_array(numpy_like, shape_generator_param):
    # Test is_own_array method with VirtualNDArray
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3], dtype=np.int64),
            shape_generator=lambda: (3,),
        )

    # Before materialization
    result = numpy_like.is_own_array(va)
    assert (
        result
    )  # Should be True because VirtualNDArray.nplike.ndarray is numpy.ndarray

    # After materialization
    va.materialize()
    result = numpy_like.is_own_array(va)
    assert result


def test_virtual_array_with_structured_dtype(numpy_like, shape_generator_param):
    # Test VirtualNDArray with structured dtype
    dtype = np.dtype([("name", "U10"), ("age", "i4"), ("weight", "f8")])

    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=dtype,
            generator=lambda: np.array(
                [("Alice", 25, 55.0), ("Bob", 30, 70.5)], dtype=dtype
            ),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=dtype,
            generator=lambda: np.array(
                [("Alice", 25, 55.0), ("Bob", 30, 70.5)], dtype=dtype
            ),
            shape_generator=lambda: (2,),
        )

    # Test properties
    assert va.dtype == dtype
    assert va._shape[0] == unknown_length
    assert va.shape[0] == 2

    # Test materialization
    materialized = va.materialize()
    assert materialized["name"][0] == "Alice"
    assert materialized["age"][1] == 30
    assert materialized["weight"][1] == 70.5


def test_virtual_array_with_empty_array(numpy_like, shape_generator_param):
    # Test VirtualNDArray with empty array
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([], dtype=np.int64),
            shape_generator=lambda: (0,),
        )

    # Test materialization
    materialized = va.materialize()
    assert len(materialized) == 0
    assert materialized.size == 0


def test_chained_operations_materialization(numpy_like, shape_generator_param):
    # Test that chained operations correctly materialize VirtualNDArrays
    if shape_generator_param is None:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
        )
    else:
        va = VirtualNDArray(
            numpy_like,
            shape=(unknown_length,),
            dtype=np.dtype(np.int64),
            generator=lambda: np.array([1, 2, 3, 4, 5], dtype=np.int64),
            shape_generator=lambda: (5,),
        )

    # Chain of operations
    # 1. Add 10
    # 2. Multiply by 2
    # 3. Check if > 25
    # Each step should materialize the VirtualNDArray

    result1 = numpy_like.add(va, 10)  # [11, 12, 13, 14, 15]
    assert va.is_materialized

    result2 = result1 * 2  # [22, 24, 26, 28, 30]

    result3 = result2 > 25  # [False, False, True, True, True]
    np.testing.assert_array_equal(result3, np.array([False, False, True, True, True]))