-
Notifications
You must be signed in to change notification settings - Fork 153
/
test_stl.py
832 lines (673 loc) · 23.7 KB
/
test_stl.py
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
import test_stl_ext as t
import pytest
import sys
from common import collect, skip_on_pypy
@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
)
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
)
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():
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
# A class -> function -> class cyclic reference
class B(t.FuncWrapper):
def __init__(self):
super().__init__()
def f():
print(self.f)
self.f = f
assert t.FuncWrapper.alive == 0
b = B()
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())
assert t.optional_copyable.__doc__ == (
"optional_copyable(x: Optional[test_stl_ext.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())
assert t.optional_copyable_ptr.__doc__ == (
"optional_copyable_ptr(x: Optional[test_stl_ext.Copyable]) -> None"
)
assert_stats(
default_constructed=1,
destructed=1
)
def test38_std_optional_none():
t.optional_none(None)
def test39_std_optional_ret_opt_movable(clean):
assert t.optional_ret_opt_movable().value == 5
assert t.optional_ret_opt_movable.__doc__ == (
"optional_ret_opt_movable() -> Optional[test_stl_ext.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():
assert t.optional_unbound_type(3) == 3
assert t.optional_unbound_type(None) is None
assert t.optional_unbound_type() is None
assert t.optional_unbound_type.__doc__ == (
"optional_unbound_type(x: Optional[int] = None) -> Optional[int]"
)
def test43_std_variant_copyable(clean):
t.variant_copyable(t.Copyable())
t.variant_copyable(5)
assert t.variant_copyable.__doc__ == (
"variant_copyable(arg: Union[test_stl_ext.Copyable, int], /) -> 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)
assert t.variant_copyable_none.__doc__ == (
"variant_copyable_none(x: Optional[Union[int, test_stl_ext.Copyable]]) -> 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)
assert t.variant_copyable_ptr.__doc__ == (
"variant_copyable_ptr(arg: Union[test_stl_ext.Copyable, int], /) -> 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)
assert t.variant_copyable_ptr_none.__doc__ == (
"variant_copyable_ptr_none(x: Optional[Union[test_stl_ext.Copyable, int]]) -> None"
)
assert_stats(
default_constructed=1,
destructed=1
)
def test47_std_variant_ret_var_copyable():
assert t.variant_ret_var_copyable().value == 5
assert t.variant_ret_var_copyable.__doc__ == (
"variant_ret_var_copyable() -> Union[test_stl_ext.Copyable, int]"
)
def test48_std_variant_ret_var_none():
assert t.variant_ret_var_none() is None
assert t.variant_ret_var_none.__doc__ == (
"variant_ret_var_none() -> Union[None, test_stl_ext.Copyable, int]"
)
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
assert t.variant_unbound_type.__doc__ == (
"variant_unbound_type(x: Optional[Union[list, tuple, int]] = None)"
" -> Union[None, list, tuple, int]"
)
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
if sys.version_info < (3, 9):
assert t.map_return_movable_value.__doc__ == (
"map_return_movable_value() -> Dict[str, test_stl_ext.Movable]"
)
else:
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
if sys.version_info < (3, 9):
assert t.map_return_copyable_value.__doc__ == (
"map_return_copyable_value() -> Dict[str, test_stl_ext.Copyable]"
)
else:
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)]))
if sys.version_info < (3, 9):
assert t.map_movable_in_value.__doc__ == (
"map_movable_in_value(x: Dict[str, test_stl_ext.Movable]) -> None"
)
else:
assert t.map_movable_in_value.__doc__ == (
"map_movable_in_value(x: dict[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)]))
if sys.version_info < (3, 9):
assert t.map_copyable_in_value.__doc__ == (
"map_copyable_in_value(x: Dict[str, test_stl_ext.Copyable]) -> None"
)
else:
assert t.map_copyable_in_value.__doc__ == (
"map_copyable_in_value(x: dict[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)]))
if sys.version_info < (3, 9):
assert t.map_movable_in_lvalue_ref.__doc__ == (
"map_movable_in_lvalue_ref(x: Dict[str, test_stl_ext.Movable]) -> None"
)
else:
assert t.map_movable_in_lvalue_ref.__doc__ == (
"map_movable_in_lvalue_ref(x: dict[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)]))
if sys.version_info < (3, 9):
assert t.map_movable_in_rvalue_ref.__doc__ == (
"map_movable_in_rvalue_ref(x: Dict[str, test_stl_ext.Movable]) -> None"
)
else:
assert t.map_movable_in_rvalue_ref.__doc__ == (
"map_movable_in_rvalue_ref(x: dict[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)]))
if sys.version_info < (3, 9):
assert t.map_movable_in_ptr.__doc__ == (
"map_movable_in_ptr(x: Dict[str, test_stl_ext.Movable]) -> None"
)
else:
assert t.map_movable_in_ptr.__doc__ == (
"map_movable_in_ptr(x: dict[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)
if sys.version_info < (3, 9):
assert t.set_return_value.__doc__ == (
"set_return_value() -> Set[str]")
assert t.unordered_set_return_value.__doc__ == (
"unordered_set_return_value() -> Set[str]")
else:
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)]))
if sys.version_info < (3, 9):
assert t.set_in_value.__doc__ == (
"set_in_value(x: Set[str]) -> None")
assert t.unordered_set_in_value.__doc__ == (
"unordered_set_in_value(x: Set[str]) -> None")
else:
assert t.set_in_value.__doc__ == (
"set_in_value(x: set[str]) -> None")
assert t.unordered_set_in_value.__doc__ == (
"unordered_set_in_value(x: set[str]) -> None")
def test62_set_in_lvalue_ref():
t.set_in_lvalue_ref(set([chr(ord("a") + i) for i in range(10)]))
if sys.version_info < (3, 9):
assert t.set_in_lvalue_ref.__doc__ == (
"set_in_lvalue_ref(x: Set[str]) -> None"
)
else:
assert t.set_in_lvalue_ref.__doc__ == (
"set_in_lvalue_ref(x: set[str]) -> None"
)
def test63_set_in_rvalue_ref():
t.set_in_rvalue_ref(set([chr(ord("a") + i) for i in range(10)]))
if sys.version_info < (3, 9):
assert t.set_in_rvalue_ref.__doc__ == (
"set_in_rvalue_ref(x: Set[str]) -> None"
)
else:
assert t.set_in_rvalue_ref.__doc__ == (
"set_in_rvalue_ref(x: 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) == 1.0
assert t.complex_value_double(1.0j) == 1.0j
assert t.complex_value_double(0.0) == 0.0
assert t.complex_value_double(0.0j) == 0.0j
assert t.complex_value_double(0) == 0
assert t.complex_value_float(1.0) == 1.0
assert t.complex_value_float(1.0j) == 1.0j
assert t.complex_value_float(0.0) == 0.0
assert t.complex_value_float(0.0j) == 0.0j
assert t.complex_value_float(0) == 0
val_64 = 2.7-3.2j
val_32 = 2.700000047683716-3.200000047683716j
assert val_64 != val_32
assert t.complex_value_float(val_32) == val_32
assert t.complex_value_float(val_64) == val_32
assert t.complex_value_double(val_32) == val_32
assert t.complex_value_double(val_64) == val_64
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
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]
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)
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})