summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/python/test/object.cpp
blob: b1f015187cbb140fcf6a60b9b2550312b8948d2f (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
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
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/class.hpp>

using namespace boost::python;

class NotCopyable
{
} not_copyable;

object ref_to_noncopyable()
{
  return object(boost::ref(not_copyable));
}

object call_object_3(object f)
{
    return f(3);
}

object message()
{
    return object("hello, world!");
}

object number()
{
    return object(42);
}

object obj_getattr(object x, char const* name)
{
    return x.attr(name);
}

object obj_objgetattr(object x, object const& name)
{
    return x.attr(name);
}

object obj_const_getattr(object const& x, char const* name)
{
    return x.attr(name);
}

object obj_const_objgetattr(object const& x, object const& name)
{
    return x.attr(name);
}

void obj_setattr(object x, char const* name, object value)
{
    x.attr(name) = value;
}

void obj_objsetattr(object x, object const& name, object value)
{
    x.attr(name) = value;
}

void obj_setattr42(object x, char const* name)
{
    x.attr(name) = 42;
}

void obj_objsetattr42(object x, object const& name)
{
    x.attr(name) = 42;
}

void obj_moveattr(object& x, char const* src, char const* dst)
{
    x.attr(dst) = x.attr(src);
}

void obj_objmoveattr(object& x, object const& src, object const& dst)
{
    x.attr(dst) = x.attr(src);
}

void obj_delattr(object x, char const* name)
{
    x.attr(name).del();
}

void obj_objdelattr(object x, object const& name)
{
    x.attr(name).del();
}

object obj_getitem(object x, object key)
{
    return x[key];
}

object obj_getitem3(object x)
{
    return x[3];
}

object obj_const_getitem(object const& x, object key)
{
    return x[key];
}

void obj_setitem(object x, object key, object value)
{
    x[key] = value;
}

void obj_setitem42(object x, object key)
{
    x[key] = 42;
}

void obj_moveitem(object& x, object src, object dst)
{
    x[dst] = x[src];
}

void obj_moveitem2(object const& x_src, object k_src, object& x_dst, object k_dst)
{
    x_dst[k_dst] = x_src[k_src];
}

bool test(object y)
{
    return y;
}

bool test_not(object y)
{
    return !y;
}

bool test_attr(object y, char* name)
{
    return y.attr(name);
}

bool test_objattr(object y, object& name)
{
    return y.attr(name);
}

bool test_not_attr(object y, char* name)
{
    return !y.attr(name);
}

bool test_not_objattr(object y, object& name)
{
    return !y.attr(name);
}

bool test_item(object y, object key)
{
    return y[key];
}

bool test_not_item(object y, object key)
{
    return !y[key];
}

bool check_string_slice()
{
    object s("hello, world");

    if (s.slice(_,-3) != "hello, wo")
        return false;
    
    if (s.slice(-3,_) != "rld")
        return false;
    
    if (s.slice(_,_) != s)
        return false;
    
    if (", " != s.slice(5,7))
        return false;

    return s.slice(2,-1).slice(1,-1)  == "lo, wor";
}

object test_call(object c, object args, object kwds) 
{ 
    return c(*args, **kwds); 
} 

bool check_binary_operators()
{
    int y;
    
    object x(3);

#define TEST_BINARY(op)                         \
    for (y = 1; y < 6; ++y)                     \
    {                                           \
        if ((x op y) != (3 op y))               \
            return false;                       \
    }                                           \
    for (y = 1; y < 6; ++y)                     \
    {                                           \
        if ((y op x) != (y op 3))               \
            return false;                       \
    }                                           \
    for (y = 1; y < 6; ++y)                     \
    {                                           \
        object oy(y);                           \
        if ((oy op x) != (oy op 3))             \
            return false;                       \
    }
    TEST_BINARY(>)
    TEST_BINARY(>=)
    TEST_BINARY(<)
    TEST_BINARY(<=)
    TEST_BINARY(==)
    TEST_BINARY(!=)

    TEST_BINARY(+)
    TEST_BINARY(-)
    TEST_BINARY(*)
    TEST_BINARY(/)
    TEST_BINARY(%)
    TEST_BINARY(<<)
    TEST_BINARY(>>)
    TEST_BINARY(&)
    TEST_BINARY(^)
    TEST_BINARY(|)
    return true;
}

bool check_inplace(object l, object o)
{
    int y;
#define TEST_INPLACE(op)                        \
    for (y = 1; y < 6; ++y)                     \
    {                                           \
        object x(666);                          \
        x op##= y;                              \
        if (x != (666 op y))                    \
            return false;                       \
    }                                           \
    for (y = 1; y < 6; ++y)                     \
    {                                           \
        object x(666);                          \
        x op##= object(y);                      \
        if (!(x == (666 op y)))                 \
            return false;                       \
    }
    TEST_INPLACE(+)
    TEST_INPLACE(-)
    TEST_INPLACE(*)
    TEST_INPLACE(/)
    TEST_INPLACE(%)
    TEST_INPLACE(<<)
    TEST_INPLACE(>>)
    TEST_INPLACE(&)
    TEST_INPLACE(^)
    TEST_INPLACE(|)
        
    l += l;
    for (y = 0; y < 6; ++y)
    {
        if (l[y] != y % 3)
            return false;
    }

#define TEST_ITEM_INPLACE(index, op, n, r1, r2)         \
    l[index] op##= n;                                   \
    if (l[index] != r1)                                 \
        return false;                                   \
    l[index] op##= object(n);                           \
    if (!(l[index] == r2))                              \
        return false;

    TEST_ITEM_INPLACE(0,+,7,7,14)
    TEST_ITEM_INPLACE(1,-,2,-1,-3)
    TEST_ITEM_INPLACE(2,*,3,6,18)
    TEST_ITEM_INPLACE(2,/,2,9,4)
    TEST_ITEM_INPLACE(0,%,4,2,2)
    l[0] += 1;
    TEST_ITEM_INPLACE(0,<<,2,12,48)
    TEST_ITEM_INPLACE(0,>>,1,24,12)
    l[4] = 15;
    TEST_ITEM_INPLACE(4,&,(16+4+1),5,5)
    TEST_ITEM_INPLACE(0,^,1,13,12)
    TEST_ITEM_INPLACE(0,|,1,13,13)

    o.attr("x0") = 0;
    o.attr("x1") = 1;
    o.attr("x2") = 2;
    o.attr("x3") = 0;
    o.attr("x4") = 1;
    
#define TEST_ATTR_INPLACE(index, op, n, r1, r2) \
    o.attr("x" #index) op##= n;                 \
    if (o.attr("x" #index) != r1)               \
        return false;                           \
    o.attr("x" #index) op##= object(n);         \
    if (o.attr("x" #index) != r2)               \
        return false;
    
    TEST_ATTR_INPLACE(0,+,7,7,14)
    TEST_ATTR_INPLACE(1,-,2,-1,-3)
    TEST_ATTR_INPLACE(2,*,3,6,18)
    TEST_ATTR_INPLACE(2,/,2,9,4)
    TEST_ATTR_INPLACE(0,%,4,2,2)
    o.attr("x0") += 1;
    TEST_ATTR_INPLACE(0,<<,2,12,48)
    TEST_ATTR_INPLACE(0,>>,1,24,12)
    o.attr("x4") = 15;
    TEST_ATTR_INPLACE(4,&,(16+4+1),5,5)
    TEST_ATTR_INPLACE(0,^,1,13,12)
    TEST_ATTR_INPLACE(0,|,1,13,13)

    if (l[0] != o.attr("x0"))
        return false;
    if (l[1] != o.attr("x1"))
        return false;
    if (l[2] != o.attr("x2"))
        return false;
    if (l[3] != o.attr("x3"))
        return false;
    if (l[4] != o.attr("x4"))
        return false;

    // set item 5 to be a list, by calling l.__class__
    l[5] = l.attr("__class__")();
    // append an element
    l[5].attr("append")(2);
    // Check its value
    if (l[5][0] != 2)
        return false;
    
    return true;
}

BOOST_PYTHON_MODULE(object_ext)
{
    class_<NotCopyable, boost::noncopyable>("NotCopyable", no_init);

    def("ref_to_noncopyable", ref_to_noncopyable);
    def("call_object_3", call_object_3);
    def("message", message);
    def("number", number);

    def("obj_getattr", obj_getattr);
    def("obj_objgetattr", obj_objgetattr);
    def("obj_const_getattr", obj_const_getattr);
    def("obj_const_objgetattr", obj_const_objgetattr);
    def("obj_setattr", obj_setattr);
    def("obj_objsetattr", obj_objsetattr);
    def("obj_setattr42", obj_setattr42);
    def("obj_objsetattr42", obj_objsetattr42);
    def("obj_moveattr", obj_moveattr);
    def("obj_objmoveattr", obj_objmoveattr);
    def("obj_delattr", obj_delattr);
    def("obj_objdelattr", obj_objdelattr);

    def("obj_getitem", obj_getitem);
    def("obj_getitem3", obj_getitem);
    def("obj_const_getitem", obj_const_getitem);
    def("obj_setitem", obj_setitem);
    def("obj_setitem42", obj_setitem42);
    def("obj_moveitem", obj_moveitem);
    def("obj_moveitem2", obj_moveitem2);

    def("test", test);
    def("test_not", test_not);

    def("test_attr", test_attr);
    def("test_objattr", test_objattr);
    def("test_not_attr", test_not_attr);
    def("test_not_objattr", test_not_objattr);

    def("test_item", test_item);
    def("test_not_item", test_not_item);

    def("test_call", test_call);
    def("check_binary_operators", check_binary_operators);
    def("check_inplace", check_inplace);
    def("check_string_slice", check_string_slice);
        ;
}

#include "module_tail.cpp"