test_stl_binders.py revision 11986
1def test_vector_int(): 2 from pybind11_tests import VectorInt 3 4 v_int = VectorInt([0, 0]) 5 assert len(v_int) == 2 6 assert bool(v_int) is True 7 8 v_int2 = VectorInt([0, 0]) 9 assert v_int == v_int2 10 v_int2[1] = 1 11 assert v_int != v_int2 12 13 v_int2.append(2) 14 v_int2.append(3) 15 v_int2.insert(0, 1) 16 v_int2.insert(0, 2) 17 v_int2.insert(0, 3) 18 assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]" 19 20 v_int.append(99) 21 v_int2[2:-2] = v_int 22 assert v_int2 == VectorInt([3, 2, 0, 0, 99, 2, 3]) 23 del v_int2[1:3] 24 assert v_int2 == VectorInt([3, 0, 99, 2, 3]) 25 del v_int2[0] 26 assert v_int2 == VectorInt([0, 99, 2, 3]) 27 28 29def test_vector_custom(): 30 from pybind11_tests import El, VectorEl, VectorVectorEl 31 32 v_a = VectorEl() 33 v_a.append(El(1)) 34 v_a.append(El(2)) 35 assert str(v_a) == "VectorEl[El{1}, El{2}]" 36 37 vv_a = VectorVectorEl() 38 vv_a.append(v_a) 39 vv_b = vv_a[0] 40 assert str(vv_b) == "VectorEl[El{1}, El{2}]" 41 42 43def test_vector_bool(): 44 from pybind11_tests import VectorBool 45 46 vv_c = VectorBool() 47 for i in range(10): 48 vv_c.append(i % 2 == 0) 49 for i in range(10): 50 assert vv_c[i] == (i % 2 == 0) 51 assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]" 52 53 54def test_map_string_double(): 55 from pybind11_tests import MapStringDouble, UnorderedMapStringDouble 56 57 m = MapStringDouble() 58 m['a'] = 1 59 m['b'] = 2.5 60 61 assert list(m) == ['a', 'b'] 62 assert list(m.items()) == [('a', 1), ('b', 2.5)] 63 assert str(m) == "MapStringDouble{a: 1, b: 2.5}" 64 65 um = UnorderedMapStringDouble() 66 um['ua'] = 1.1 67 um['ub'] = 2.6 68 69 assert sorted(list(um)) == ['ua', 'ub'] 70 assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)] 71 assert "UnorderedMapStringDouble" in str(um) 72 73 74def test_map_string_double_const(): 75 from pybind11_tests import MapStringDoubleConst, UnorderedMapStringDoubleConst 76 77 mc = MapStringDoubleConst() 78 mc['a'] = 10 79 mc['b'] = 20.5 80 assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}" 81 82 umc = UnorderedMapStringDoubleConst() 83 umc['a'] = 11 84 umc['b'] = 21.5 85 86 str(umc) 87 88 89def test_noncopyable_vector(): 90 from pybind11_tests import get_vnc 91 92 vnc = get_vnc(5) 93 for i in range(0, 5): 94 assert vnc[i].value == i + 1 95 96 for i, j in enumerate(vnc, start=1): 97 assert j.value == i 98 99 100def test_noncopyable_deque(): 101 from pybind11_tests import get_dnc 102 103 dnc = get_dnc(5) 104 for i in range(0, 5): 105 assert dnc[i].value == i + 1 106 107 i = 1 108 for j in dnc: 109 assert(j.value == i) 110 i += 1 111 112 113def test_noncopyable_map(): 114 from pybind11_tests import get_mnc 115 116 mnc = get_mnc(5) 117 for i in range(1, 6): 118 assert mnc[i].value == 10 * i 119 120 vsum = 0 121 for k, v in mnc.items(): 122 assert v.value == 10 * k 123 vsum += v.value 124 125 assert vsum == 150 126 127 128def test_noncopyable_unordered_map(): 129 from pybind11_tests import get_umnc 130 131 mnc = get_umnc(5) 132 for i in range(1, 6): 133 assert mnc[i].value == 10 * i 134 135 vsum = 0 136 for k, v in mnc.items(): 137 assert v.value == 10 * k 138 vsum += v.value 139 140 assert vsum == 150 141