File: test_stl.py

package info (click to toggle)
nanobind 2.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,300 kB
  • sloc: cpp: 12,232; python: 6,315; ansic: 4,813; makefile: 22; sh: 15
file content (881 lines) | stat: -rw-r--r-- 28,366 bytes parent folder | download | duplicates (2)
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
import test_stl_ext as t
import typing
import pytest
import sys
from common import collect, skip_on_pypy



def optional(arg: str, /) -> str:
    if sys.version_info < (3, 10):
        return "typing.Optional[" + arg + "]"
    else:
        return arg + " | " + "None"


def union(*args: str) -> str:
    if sys.version_info < (3, 10):
        return "typing.Union[" + ", ".join(args) + "]"
    else:
        return " | ".join(args)


@pytest.fixture
def clean():
    collect()
    t.reset()


def assert_stats(**kwargs):
    collect()
    for k, v in t.stats().items():
        fail = False
        if k in kwargs:
            if v != kwargs[k]:
                fail = True
        elif v != 0:
            fail = True
        if fail:
            raise Exception(f"Mismatch for key {k}: {t.stats()}")


# ------------------------------------------------------------------
# The following aren't strictly STL tests, but they are helpful in
# ensuring that move constructors/copy constructors of bound C++ types
# are properly triggered, which the STL type casters depend on.
# ------------------------------------------------------------------


def test01_movable_return(clean):
    assert t.return_movable().value == 5
    assert_stats(default_constructed=1, move_constructed=1, destructed=2)


def test02_movable_return_ptr(clean):
    assert t.return_movable_ptr().value == 5
    assert_stats(default_constructed=1, destructed=1)


def test03_movable_in_value(clean):
    s = t.Movable()
    t.movable_in_value(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test04_movable_in_lvalue_ref(clean):
    s = t.Movable()
    t.movable_in_lvalue_ref(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test05_movable_in_ptr(clean):
    s = t.Movable()
    t.movable_in_ptr(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test06_movable_in_rvalue_ref(clean):
    s = t.Movable()
    t.movable_in_rvalue_ref(s)
    assert s.value == 0
    del s
    assert_stats(default_constructed=1, move_constructed=1, destructed=2)


def test07_copyable_return(clean):
    assert t.return_copyable().value == 5
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test08_copyable_return_ptr(clean):
    assert t.return_copyable_ptr().value == 5
    assert_stats(default_constructed=1, destructed=1)


def test09_copyable_in_value(clean):
    s = t.Copyable()
    t.copyable_in_value(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test10_copyable_in_lvalue_ref(clean):
    s = t.Copyable()
    t.copyable_in_lvalue_ref(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test11_copyable_in_ptr(clean):
    s = t.Copyable()
    t.copyable_in_ptr(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test12_copyable_in_rvalue_ref(clean):
    s = t.Copyable()
    t.copyable_in_rvalue_ref(s)
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


# ------------------------------------------------------------------


def test13_tuple_movable_return(clean):
    assert t.tuple_return_movable()[0].value == 5
    assert_stats(default_constructed=1, move_constructed=2, destructed=3)


def test14_tuple_movable_return_ptr(clean):
    assert t.return_movable_ptr().value == 5
    assert_stats(default_constructed=1, destructed=1)


def test15_tuple_movable_in_value(clean):
    s = t.Movable()
    t.tuple_movable_in_value((s,))
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test16_tuple_movable_in_lvalue_ref(clean):
    s = t.Movable()
    t.tuple_movable_in_lvalue_ref((s,))
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test17_tuple_movable_in_lvalue_ref_2(clean):
    s = t.Movable()
    t.tuple_movable_in_lvalue_ref_2((s,))
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test18_tuple_movable_in_ptr(clean):
    s = t.Movable()
    t.tuple_movable_in_ptr((s,))
    assert s.value == 5
    del s
    assert_stats(default_constructed=1, destructed=1)


def test19_tuple_movable_in_rvalue_ref(clean):
    s = t.Movable()
    t.tuple_movable_in_rvalue_ref((s,))
    assert s.value == 0
    del s
    assert_stats(default_constructed=1, move_constructed=1, destructed=2)


def test20_tuple_movable_in_rvalue_ref_2(clean):
    s = t.Movable()
    t.tuple_movable_in_rvalue_ref_2((s,))
    assert s.value == 5
    del s
    assert_stats(
        default_constructed=1, copy_constructed=1, move_constructed=1, destructed=3
    )


# ------------------------------------------------------------------


def test21_tuple_pair_basic():
    assert t.empty_tuple(()) == ()
    assert t.swap_tuple((1, 2.5)) == (2.5, 1)
    assert t.swap_pair((1, 2.5)) == (2.5, 1)


# ------------------------------------------------------------------


def test22_vec_return_movable(clean):
    for i, x in enumerate(t.vec_return_movable()):
        assert x.value == i
    del x
    assert_stats(value_constructed=10, move_constructed=10, destructed=20)


def test23_vec_return_copyable(clean):
    for i, x in enumerate(t.vec_return_copyable()):
        assert x.value == i
    del x
    assert_stats(value_constructed=10, copy_constructed=20, destructed=30)


def test24_vec_movable_in_value(clean):
    t.vec_movable_in_value([t.Movable(i) for i in range(10)])
    assert_stats(value_constructed=10, copy_constructed=10, destructed=20)

    # Test that None values don't cause a crash
    with pytest.raises(TypeError):
        t.vec_movable_in_value([None])


def test25_vec_movable_in_value(clean):
    t.vec_copyable_in_value([t.Copyable(i) for i in range(10)])
    assert_stats(value_constructed=10, copy_constructed=10, destructed=20)


def test26_vec_movable_in_lvalue_ref(clean):
    t.vec_movable_in_lvalue_ref([t.Movable(i) for i in range(10)])
    assert_stats(value_constructed=10, copy_constructed=10, destructed=20)


def test27_vec_movable_in_ptr_2(clean):
    t.vec_movable_in_ptr_2([t.Movable(i) for i in range(10)])
    assert_stats(value_constructed=10, destructed=10)

    # Test that None values are permitted when casting to pointer;
    # instead we reach 'if (x.size() != 10) fail();' in the bound function
    with pytest.raises(RuntimeError):
        t.vec_movable_in_ptr_2([None])


def test28_vec_movable_in_rvalue_ref(clean):
    t.vec_movable_in_rvalue_ref([t.Movable(i) for i in range(10)])
    assert_stats(value_constructed=10, copy_constructed=10, destructed=20)


def test29_opaque_vector():
    f = t.float_vec()
    assert f.size() == 0
    assert isinstance(f, t.float_vec)
    f.push_back(1)
    assert f.size() == 1


def test30_std_function():
    assert t.return_empty_function() is None
    assert t.return_function()(3) == 8
    assert t.call_function(lambda x: 5 + x, 3) == 8

    with pytest.raises(TypeError) as excinfo:
        assert t.call_function(5, 3) == 8
    assert "incompatible function arguments" in str(excinfo.value)

    with pytest.raises(TypeError) as excinfo:
        assert t.call_function(lambda x, y: x + y, 3) == 8
    assert "missing 1 required positional argument" in str(excinfo.value)

    l = []

    def f():
        l.append(1)

    f2 = t.return_void_function(f)
    f2()
    assert l == [1]


def test31_std_function_roundtrip():
    def f():
        l.append(1)

    f2 = t.return_void_function(f)
    assert f2 is f


# cpyext reference cycles are not supported, see https://foss.heptapod.net/pypy/pypy/-/issues/3849
@skip_on_pypy
def test32_std_function_gc():
    # Test class -> function -> class cyclic reference
    class A(t.FuncWrapper):
        pass

    assert t.FuncWrapper.alive == 0
    a = A()
    assert t.FuncWrapper.alive == 1
    del a
    collect()
    assert t.FuncWrapper.alive == 0

    # t.FuncWrapper is a C extension type with a custom property 'f', which
    # can store Python function objects It implements the tp_traverse
    # callback so that reference cycles arising from this function object
    # can be detected.

    class Test(t.FuncWrapper):
        def __init__(self):
            super().__init__()

            # The constructor creates a closure, which references 'self'
            # and assigns it to the 'self.f' member.
            # This creates a cycle self -> self.f -> self
            def f():
                print(self.f)

            self.f = f


    # The Test class declared above inherits from 'FuncWrapper'.
    # This class keeps track of how many references are alive at
    # any point to help track down leak issues.
    assert t.FuncWrapper.alive == 0
    b = Test()
    assert t.FuncWrapper.alive == 1
    del b
    collect()
    assert t.FuncWrapper.alive == 0


def test33_vec_type_check():
    with pytest.raises(TypeError) as excinfo:
        t.vec_movable_in_value(0)


def test34_list():
    assert t.identity_list([]) == []
    assert t.identity_list([1, 2, 3]) == [1, 2, 3]
    assert t.identity_list(()) == []
    assert t.identity_list((1, 2, 3)) == [1, 2, 3]


def test35_string_and_string_view():
    assert t.identity_string("") == ""
    assert t.identity_string("orange") == "orange"
    assert t.identity_string("橘子") == "橘子"
    assert t.identity_string("ส้ม") == "ส้ม"
    assert t.identity_string("البرتقالي") == "البرتقالي"
    assert t.identity_string("🍊") == "🍊"

    assert t.identity_string_view("") == ""
    assert t.identity_string_view("orange") == "orange"
    assert t.identity_string_view("橘子") == "橘子"
    assert t.identity_string_view("ส้ม") == "ส้ม"
    assert t.identity_string_view("البرتقالي") == "البرتقالي"
    assert t.identity_string_view("🍊") == "🍊"


def test36_std_optional_copyable(clean):
    t.optional_copyable(t.Copyable())
    opt_copyable = optional("test_stl_ext.Copyable")
    assert t.optional_copyable.__doc__ == (
        f"optional_copyable(x: {opt_copyable}) -> None"
    )
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test37_std_optional_copyable_ptr(clean):
    t.optional_copyable_ptr(t.Copyable())
    opt_copyable = optional("test_stl_ext.Copyable")
    assert t.optional_copyable_ptr.__doc__ == (
        f"optional_copyable_ptr(x: {opt_copyable}) -> None"
    )
    assert_stats(default_constructed=1, destructed=1)


def test38_std_optional_none():
    t.optional_none(None)
    assert t.optional_cstr(None) == "none"
    assert t.optional_cstr("hi") == "hi"
    assert t.optional_non_assignable(None) == None


def test38b_std_optional_implicit_convert():
    # Regression test: std::optional<> with lossy implicit conversion on a
    # sibling argument and no explicit nb::arg annotations (issue #1293)
    assert t.optional_implicit_convert(16777217, 10) == 16777216.0 + 10
    assert t.optional_implicit_convert(16777217, None) == 16777216.0


def test39_std_optional_ret_opt_movable(clean):
    assert t.optional_ret_opt_movable().value == 5
    opt_movable = optional("test_stl_ext.Movable")
    assert t.optional_ret_opt_movable.__doc__ == (
        f"optional_ret_opt_movable() -> {opt_movable}"
    )
    assert_stats(default_constructed=1, move_constructed=2, destructed=3)


def test40_std_optional_ret_opt_movable_ptr(clean):
    assert t.optional_ret_opt_movable_ptr().value == 5
    assert_stats(default_constructed=1, destructed=1)


def test41_std_optional_ret_opt_none():
    assert t.optional_ret_opt_none() is None


def test42_std_optional_unbound_type():
    for method_name in (
        "optional_unbound_type",
        "optional_unbound_type_with_nullopt_as_default",
    ):
        method = getattr(t, method_name)
        assert method(3) == 3
        assert method(None) is None
        assert method() is None
        opt_int = optional("int")
        assert method.__doc__ == (f"{method_name}(x: {opt_int} = None) -> {opt_int}")


def test42a_std_optional_non_assignable():
    assert t.optional_non_assignable(t.NonAssignable()).value == 5


def test43_std_variant_copyable(clean):
    t.variant_copyable(t.Copyable())
    t.variant_copyable(5)
    arg_t = union('test_stl_ext.Copyable', 'int')
    assert t.variant_copyable.__doc__ == (
        f"variant_copyable(arg: {arg_t}, /) -> None"
    )
    assert_stats(default_constructed=3, copy_assigned=1, destructed=3)


def test44_std_variant_copyable_none(clean):
    t.variant_copyable_none(t.Copyable())
    t.variant_copyable_none(5)
    t.variant_copyable_none(None)
    arg_t = optional(union("int", "test_stl_ext.Copyable"))
    assert t.variant_copyable_none.__doc__ == (
        f"variant_copyable_none(x: {arg_t}) -> None"
    )
    assert_stats(default_constructed=1, copy_constructed=1, destructed=2)


def test45_std_variant_copyable_ptr(clean):
    t.variant_copyable_ptr(t.Copyable())
    t.variant_copyable_ptr(5)
    arg_t = union("test_stl_ext.Copyable", "int")
    assert t.variant_copyable_ptr.__doc__ == (
        f"variant_copyable_ptr(arg: {arg_t}, /) -> None"
    )
    assert_stats(default_constructed=1, destructed=1)


def test46_std_variant_copyable_ptr_none(clean):
    t.variant_copyable_ptr_none(t.Copyable())
    t.variant_copyable_ptr_none(5)
    t.variant_copyable_ptr_none(None)
    arg_t = optional(union("test_stl_ext.Copyable", "int"))
    assert t.variant_copyable_ptr_none.__doc__ == (
        f"variant_copyable_ptr_none(x: {arg_t}) -> None"
    )
    assert_stats(default_constructed=1, destructed=1)


def test47_std_variant_ret_var_copyable():
    assert t.variant_ret_var_copyable().value == 5
    rv_t = union("test_stl_ext.Copyable", "int")
    assert t.variant_ret_var_copyable.__doc__ == (
        f"variant_ret_var_copyable() -> {rv_t}"
    )


def test48_std_variant_ret_var_none():
    assert t.variant_ret_var_none() is None

    rv_t = union("None", "test_stl_ext.Copyable", "int")
    assert t.variant_ret_var_none.__doc__ == (f"variant_ret_var_none() -> {rv_t}")


def test49_std_variant_unbound_type():
    assert t.variant_unbound_type() is None
    assert t.variant_unbound_type(None) is None
    assert t.variant_unbound_type([5]) == [5]
    assert t.variant_unbound_type((1, 2, 3)) == (1, 2, 3)
    assert t.variant_unbound_type(5) == 5
    arg_t = optional(union("list", "tuple", "int"))
    rv_t = union("None", "list", "tuple", "int")
    assert t.variant_unbound_type.__doc__ == (
        f"variant_unbound_type(x: {arg_t} = None) -> {rv_t}"
    )

def test49b_std_variant_nondefault(clean):
    assert t.variant_nondefault(t.NonDefaultConstructible(10)) == 10
    assert t.variant_nondefault(20) == -20
    assert_stats(
        value_constructed=1, # constructed in NonDefaultConstructible pyobject
        copy_constructed=1, # copied into type_caster variant member
        move_constructed=1, # moved into function argument value
        destructed=3,
    )

def test50_map_return_movable_value():
    for i, (k, v) in enumerate(sorted(t.map_return_movable_value().items())):
        assert k == chr(ord("a") + i)
        assert v.value == i
        assert t.map_return_movable_value.__doc__ == (
            "map_return_movable_value() -> dict[str, test_stl_ext.Movable]"
        )


def test51_map_return_copyable_value():
    for i, (k, v) in enumerate(sorted(t.map_return_copyable_value().items())):
        assert k == chr(ord("a") + i)
        assert v.value == i
        assert t.map_return_copyable_value.__doc__ == (
            "map_return_copyable_value() -> dict[str, test_stl_ext.Copyable]"
        )


def test52_map_movable_in_value():
    t.map_movable_in_value(dict([(chr(ord("a") + i), t.Movable(i)) for i in range(10)]))
    assert t.map_movable_in_value.__doc__ == (
        "map_movable_in_value(x: collections.abc.Mapping[str, test_stl_ext.Movable]) -> None"
    )


def test53_map_copyable_in_value():
    t.map_copyable_in_value(
        dict([(chr(ord("a") + i), t.Copyable(i)) for i in range(10)])
    )
    assert t.map_copyable_in_value.__doc__ == (
        "map_copyable_in_value(x: collections.abc.Mapping[str, test_stl_ext.Copyable]) -> None"
    )


def test54_map_movable_in_lvalue_ref():
    t.map_movable_in_lvalue_ref(
        dict([(chr(ord("a") + i), t.Movable(i)) for i in range(10)])
    )
    assert t.map_movable_in_lvalue_ref.__doc__ == (
        "map_movable_in_lvalue_ref(x: collections.abc.Mapping[str, test_stl_ext.Movable]) -> None"
    )


def test55_map_movable_in_rvalue_ref():
    t.map_movable_in_rvalue_ref(
        dict([(chr(ord("a") + i), t.Movable(i)) for i in range(10)])
    )
    assert t.map_movable_in_rvalue_ref.__doc__ == (
        "map_movable_in_rvalue_ref(x: collections.abc.Mapping[str, test_stl_ext.Movable]) -> None"
    )


def test56_map_movable_in_ptr():
    t.map_movable_in_ptr(dict([(chr(ord("a") + i), t.Movable(i)) for i in range(10)]))
    assert t.map_movable_in_ptr.__doc__ == (
        "map_movable_in_ptr(x: collections.abc.Mapping[str, test_stl_ext.Movable]) -> None"
    )


def test57_map_return_readonly_value():
    for i, (k, v) in enumerate(sorted(t.map_return_readonly_value().map.items())):
        assert k == chr(ord("a") + i)
        assert v == i
    assert t.map_return_readonly_value.__doc__ == (
        "map_return_readonly_value() -> test_stl_ext.StructWithReadonlyMap"
    )


def test58_array():
    o = t.array_out()
    assert isinstance(o, list) and o == [1, 2, 3]
    assert t.array_in([1, 2, 3]) == 6
    assert t.array_in((1, 2, 3)) == 6
    with pytest.raises(TypeError) as excinfo:
        assert t.array_in((1, 2, 3, 4)) == 6
    assert "incompatible function arguments" in str(excinfo.value)


def test59_map_movable_in_failure():
    with pytest.raises(TypeError) as excinfo:
        t.map_copyable_in_value({1: 2})
    assert "incompatible function arguments" in str(excinfo.value)


def test60_set_return_value():
    for i, k in enumerate(sorted(t.set_return_value())):
        assert k == chr(ord("a") + i)
    for i, k in enumerate(sorted(t.unordered_set_return_value())):
        assert k == chr(ord("a") + i)
    assert t.set_return_value.__doc__ == "set_return_value() -> set[str]"
    assert t.unordered_set_return_value.__doc__ == (
        "unordered_set_return_value() -> set[str]"
    )


def test61_set_in_value():
    t.set_in_value(set([chr(ord("a") + i) for i in range(10)]))
    t.unordered_set_in_value(set([chr(ord("a") + i) for i in range(10)]))
    assert t.set_in_value.__doc__ == (
        "set_in_value(x: collections.abc.Set[str]) -> None"
    )
    assert t.unordered_set_in_value.__doc__ == (
        "unordered_set_in_value(x: collections.abc.Set[str]) -> None"
    )


def test62_set_in_lvalue_ref():
    t.set_in_lvalue_ref(set([chr(ord("a") + i) for i in range(10)]))
    assert t.set_in_lvalue_ref.__doc__ == (
        "set_in_lvalue_ref(x: collections.abc.Set[str]) -> None"
    )


def test63_set_in_rvalue_ref():
    t.set_in_rvalue_ref(set([chr(ord("a") + i) for i in range(10)]))
    assert t.set_in_rvalue_ref.__doc__ == (
        "set_in_rvalue_ref(x: collections.abc.Set[str]) -> None"
    )


def test64_set_in_failure():
    with pytest.raises(TypeError) as excinfo:
        t.set_in_value(set([i for i in range(10)]))
    assert "incompatible function arguments" in str(excinfo.value)


def test65_class_with_movable_field(clean):
    cwmf = t.ClassWithMovableField()
    m1 = t.Movable(1)
    m2 = t.Movable(2)

    assert_stats(value_constructed=2)

    cwmf.movable = [m1, m2]

    assert_stats(value_constructed=2, copy_constructed=2)

    del m1, m2
    collect()

    assert_stats(value_constructed=2, copy_constructed=2, destructed=2)

    del cwmf
    collect()

    assert_stats(value_constructed=2, copy_constructed=2, destructed=4)


def test66_replace_extension():
    from pathlib import Path

    if not hasattr(t, "replace_extension"):
        pytest.skip("filesystem tests not compiled")

    filename = Path("test.txt")
    assert t.replace_extension(filename, ".obj") == filename.with_suffix(".obj")

    filename = Path("🍊.html")
    assert t.replace_extension(filename, ".svg") == filename.with_suffix(".svg")

    class PseudoStrPath:
        def __fspath__(self):
            return "foo/bar"

    class PseudoBytesPath:
        def __fspath__(self):
            return b"foo/bar"

    assert t.parent_path(Path("foo/bar")) == Path("foo")
    assert t.parent_path("foo/bar") == Path("foo")
    assert t.parent_path(b"foo/bar") == Path("foo")
    assert t.parent_path(PseudoStrPath()) == Path("foo")
    assert t.parent_path(PseudoBytesPath()) == Path("foo")


def test67_vector_bool():
    bool_vector = [True, False, True, False]
    result = t.flip_vector_bool(bool_vector)
    assert result == [not x for x in bool_vector]


def test68_complex_value():
    # double: 64bits
    assert t.complex_value_double(1.0) == complex(1.0)
    assert t.complex_value_double(1.0j) == 1.0j
    assert t.complex_value_double(0.0) == complex(0.0)
    assert t.complex_value_double(0.0j) == 0.0j
    assert t.complex_value_double(0) == complex(0.0)
    assert t.complex_value_double_nc(1.0 + 2.0j) == 1.0 + 2.0j
    assert t.complex_value_double_nc(1.0j) == 1.0j
    assert t.complex_value_double_nc(0.0 + 0.0j) == complex(0.0)
    assert t.complex_value_double_nc(0.0j) == 0.0j
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_double_nc(0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_double_nc(0.0)

    # float: 32bits
    assert t.complex_value_float(1.0) == complex(1.0)
    assert t.complex_value_float(1.0j) == 1.0j
    assert t.complex_value_float(0.0) == complex(0.0)
    assert t.complex_value_float(0.0j) == 0.0j
    assert t.complex_value_float(0) == complex(0.0)
    assert t.complex_value_float_nc(1.0 + 2.0j) == 1.0 + 2.0j
    assert t.complex_value_float_nc(1.0j) == 1.0j
    assert t.complex_value_float_nc(0.0 + 0.0j) == complex(0.0)
    assert t.complex_value_float_nc(0.0j) == 0.0j
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(0.0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(1.1 + 2.0j)  # Inexact narrowing conversion
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(1.0 + 2.1j)  # Inexact narrowing conversion

    val_64 = 2.7 - 3.2j
    val_32 = 2.700000047683716 - 3.200000047683716j
    assert val_64 != val_32

    assert t.complex_value_double(val_32) == val_32
    assert t.complex_value_double(val_64) == val_64
    assert t.complex_value_double_nc(val_32) == val_32
    assert t.complex_value_double_nc(val_64) == val_64

    assert t.complex_value_float(val_32) == val_32
    assert t.complex_value_float(val_64) == val_32
    assert t.complex_value_float_nc(val_32) == val_32
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(val_64)  # Inexact narrowing conversion

    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float([])

    class MyInt(int):
        def __new__(cls, value):
            return super().__new__(cls, value)

    assert t.complex_value_double(MyInt(7)) == complex(7.0)
    assert t.complex_value_float(MyInt(7)) == complex(7.0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_double_nc(MyInt(7))
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(MyInt(7))

    class MyComplex:
        def __init__(self, real, imag):
            self.re = real
            self.im = imag
        def __complex__(self):
            return complex(self.re, self.im)

    assert t.complex_value_double(MyComplex(1, 2)) == complex(1.0, 2.0)
    assert t.complex_value_float(MyComplex(1, 2)) == complex(1.0, 2.0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_double_nc(MyComplex(1, 2))
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(MyComplex(1, 2))

    class MyComplexSubclass(complex): ...

    assert t.complex_value_double(MyComplexSubclass(1, 2)) == complex(1.0, 2.0)
    assert t.complex_value_float(MyComplexSubclass(1, 2)) == complex(1.0, 2.0)
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_double_nc(MyComplexSubclass(1, 2))
    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_value_float_nc(MyComplexSubclass(1, 2))

    try:
        import numpy as np

        assert t.complex_value_float(np.complex64(val_32)) == val_32
        assert t.complex_value_float(np.complex64(val_64)) == val_32
        assert t.complex_value_double(np.complex64(val_32)) == val_32
        assert t.complex_value_double(np.complex64(val_64)) == val_32

        assert t.complex_value_float(np.complex128(val_32)) == val_32
        assert t.complex_value_float(np.complex128(val_64)) == val_32
        assert t.complex_value_double(np.complex128(val_32)) == val_32
        assert t.complex_value_double(np.complex128(val_64)) == val_64

        with pytest.raises(TypeError, match="incompatible function arguments"):
            t.complex_value_double_nc(np.complex128(val_64))
        with pytest.raises(TypeError, match="incompatible function arguments"):
            t.complex_value_float_nc(np.complex64(val_32))

    except ImportError:
        pass


def test69_complex_array():
    val1_64 = 2.7 - 3.2j
    val1_32 = 2.700000047683716 - 3.200000047683716j
    val2_64 = 3.1415
    val2_32 = 3.1414999961853027 + 0j

    # test 64 bit casts
    assert t.complex_array_double([val1_64, -1j, val2_64]) == [
        val1_64,
        -0 - 1j,
        val2_64,
    ]

    # test 32 bit casts
    assert t.complex_array_float([val1_64, -1j, val2_64]) == [
        val1_32,
        (-0 - 1j),
        val2_32,
    ]

    with pytest.raises(TypeError, match="incompatible function arguments"):
        t.complex_array_float([[]])

    try:
        import numpy as np

        # test 64 bit casts
        assert t.complex_array_double(np.array([val1_64, -1j, val2_64])) == [
            val1_64,
            -0 - 1j,
            val2_64,
        ]
        assert t.complex_array_double(
            np.array([val1_64, -1j, val2_64], dtype=np.complex128)
        ) == [val1_64, -0 - 1j, val2_64]
        assert t.complex_array_double(
            np.array([val1_64, -1j, val2_64], dtype=np.complex64)
        ) == [val1_32, -0 - 1j, val2_32]

        # test 32 bit casts
        assert t.complex_array_float(np.array([val1_64, -1j, val2_64])) == [
            val1_32,
            (-0 - 1j),
            val2_32,
        ]
        assert t.complex_array_float(
            np.array([val1_64, -1j, val2_64], dtype=np.complex128)
        ) == [val1_32, (-0 - 1j), val2_32]
        assert t.complex_array_float(
            np.array([val1_64, -1j, val2_64], dtype=np.complex64)
        ) == [val1_32, (-0 - 1j), val2_32]
    except ImportError:
        pass


def test70_vec_char():
    assert isinstance(t.vector_str("123"), str)
    assert isinstance(t.vector_str(["123", "345"]), list)
    assert t.vector_optional_str(["abc", None]) == ["abc", None]


def test71_null_input():
    with pytest.raises(TypeError):
        t.vec_movable_in_value([None])
    with pytest.raises(TypeError):
        t.map_copyable_in_value({"a": None})

@skip_on_pypy # PyPy fails this test on Windows :-(
def test72_wstr():
    assert t.pass_wstr('🎈') == '🎈'

def test73_bad_input_to_set():
    with pytest.raises(TypeError):
        t.set_in_value(None)

def test74_variant_implicit_conversions():
    event = t.IDHavingEvent()
    assert event.id is None
    event.id = t.BasicID1(78)
    assert type(event.id) is t.BasicID1