summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/li_std_vector_extra_runme.py
blob: 59e729a27d3e7c6cf52624fd249a785847270ad9 (plain)
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
from li_std_vector_extra import *

iv = IntVector(4)
for i in range(0, 4):
    iv[i] = i

x = average(iv)
y = average([1, 2, 3, 4])

a = half([10, 10.5, 11, 11.5])

dv = DoubleVector(10)
for i in range(0, 10):
    dv[i] = i / 2.0

halve_in_place(dv)


bv = BoolVector(4)
bv[0] = bool(1)
bv[1] = bool(0)
bv[2] = bool(4)
bv[3] = bool(0)

if bv[0] != bv[2]:
    raise RuntimeError, "bad std::vector<bool> mapping"

b = B(5)
va = VecA([b, None, b, b])

if va[0].f(1) != 6:
    raise RuntimeError, "bad std::vector<A*> mapping"

if vecAptr(va) != 6:
    raise RuntimeError, "bad std::vector<A*> mapping"

b.val = 7
if va[3].f(1) != 8:
    raise RuntimeError, "bad std::vector<A*> mapping"


ip = PtrInt()
ap = new_ArrInt(10)

ArrInt_setitem(ip, 0, 123)
ArrInt_setitem(ap, 2, 123)

vi = IntPtrVector((ip, ap, None))
if ArrInt_getitem(vi[0], 0) != ArrInt_getitem(vi[1], 2):
    raise RuntimeError, "bad std::vector<int*> mapping"

delete_ArrInt(ap)


a = halfs([10, 8, 4, 3])

v = IntVector()
v[0:2] = [1, 2]
if v[0] != 1 or v[1] != 2:
    raise RuntimeError, "bad setslice"

if v[0:-1][0] != 1:
    raise RuntimeError, "bad getslice"

if v[0:-2].size() != 0:
    raise RuntimeError, "bad getslice"

v[0:1] = [2]
if v[0] != 2:
    raise RuntimeError, "bad setslice"

v[1:] = [3]
if v[1] != 3:
    raise RuntimeError, "bad setslice"

v[2:] = [3]
if v[2] != 3:
    raise RuntimeError, "bad setslice"

if v[0:][0] != v[0]:
    raise RuntimeError, "bad getslice"


del v[:]
if v.size() != 0:
    raise RuntimeError, "bad getslice"

del v[:]
if v.size() != 0:
    raise RuntimeError, "bad getslice"


v = vecStr(["hello ", "world"])
if v[0] != "hello world":
    raise RuntimeError, "bad std::string+std::vector"


pv = pyvector([1, "hello", (1, 2)])

if pv[1] != "hello":
    raise RuntimeError


iv = IntVector(5)
for i in range(0, 5):
    iv[i] = i

iv[1:3] = []
if iv[1] != 3:
    raise RuntimeError

# Overloading checks
if overloaded1(iv) != "vector<int>":
    raise RuntimeError

if overloaded1(dv) != "vector<double>":
    raise RuntimeError

if overloaded2(iv) != "vector<int>":
    raise RuntimeError

if overloaded2(dv) != "vector<double>":
    raise RuntimeError

if overloaded3(iv) != "vector<int> *":
    raise RuntimeError

if overloaded3(None) != "vector<int> *":
    raise RuntimeError

if overloaded3(100) != "int":
    raise RuntimeError


# vector pointer checks
ip = makeIntPtr(11)
dp = makeDoublePtr(33.3)
error = 0
try:
    # check vector<int *> does not accept double * element
    vi = IntPtrVector((ip, dp))
    error = 1
except:
    pass

if error:
    raise RuntimeError

vi = IntPtrVector((ip, makeIntPtr(22)))
if extractInt(vi[0]) != 11:
    raise RuntimeError

if extractInt(vi[1]) != 22:
    raise RuntimeError

# vector const pointer checks
csp = makeConstShortPtr(111)

error = 0
try:
    # check vector<const unsigned short *> does not accept double * element
    vcs = ConstShortPtrVector((csp, dp))
    error = 1
except:
    pass

if error:
    raise RuntimeError

vcs = ConstShortPtrVector((csp, makeConstShortPtr(222)))
if extractConstShort(vcs[0]) != 111:
    raise RuntimeError

if extractConstShort(vcs[1]) != 222:
    raise RuntimeError

for p in vcs[0:1]:
    if extractConstShort2(p) != 111:
        raise RuntimeError

for p in vcs[1:2]:
    if extractConstShort2(p) != 222:
        raise RuntimeError