Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/test/list.cpp @ 45

Last change on this file since 45 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.7 KB
Line 
1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6#include <boost/python/module.hpp>
7#include <boost/python/def.hpp>
8#include <boost/python/class.hpp>
9#include <boost/python/list.hpp>
10#include <boost/python/make_function.hpp>
11#include <boost/lexical_cast.hpp>
12#include <boost/assert.hpp>
13#include "test_class.hpp"
14
15using namespace boost::python;
16
17object new_list()
18{
19    return list();
20}
21
22list listify(object x)
23{
24    return list(x);
25}
26
27object listify_string(char const* s)
28{
29    return list(s);
30}
31
32std::string x_rep(test_class<> const& x)
33{
34    return "X("  + boost::lexical_cast<std::string>(x.value()) + ")";
35}
36
37object apply_object_list(object f, list x)
38{
39    return f(x);
40}
41
42list apply_list_list(object f, list x)
43{
44    return call<list>(f.ptr(), x);
45}
46
47void append_object(list& x, object y)
48{
49    x.append(y);
50}
51
52void append_list(list& x, list const& y)
53{
54    x.append(y);
55}
56
57typedef test_class<> X;
58
59int notcmp(object const& x, object const& y)
60{
61    return y < x ? -1 : y > x ? 1 : 0;
62}
63
64void exercise(list x, object y, object print)
65{
66    x.append(y);
67    x.append(5);
68    x.append(X(3));
69   
70    print("after append:");
71    print(x);
72   
73    print("number of", y, "instances:", x.count(y));
74   
75    print("number of 5s:", x.count(5));
76
77    x.extend("xyz");
78    print("after extend:");
79    print(x);
80    print("index of", y, "is:", x.index(y));
81    print("index of 'l' is:", x.index("l"));
82   
83    x.insert(4, 666);
84    print("after inserting 666:");
85    print(x);
86    print("inserting with object as index:");
87    x.insert(x[x.index(5)], "---");
88    print(x);
89   
90    print("popping...");
91    x.pop();
92    print(x);
93    x.pop(x[x.index(5)]);
94    print(x);
95    x.pop(x.index(5));
96    print(x);
97
98    print("removing", y);
99    x.remove(y);
100    print(x);
101    print("removing", 666);
102    x.remove(666);
103    print(x);
104
105    print("reversing...");
106    x.reverse();
107    print(x);
108
109    print("sorted:");
110    x.pop(2); // make sorting predictable
111    x.sort();
112    print(x);
113
114    print("reverse sorted:");
115    x.sort(&notcmp);
116    print(x);
117
118    list w;
119    w.append(5);
120    w.append(6);
121    w += "hi";
122    BOOST_ASSERT(w[0] == 5);
123    BOOST_ASSERT(w[1] == 6);
124    BOOST_ASSERT(w[2] == 'h');
125    BOOST_ASSERT(w[3] == 'i');
126}
127
128BOOST_PYTHON_MODULE(list_ext)
129{
130    def("new_list", new_list);
131    def("listify", listify);
132    def("listify_string", listify_string);
133    def("apply_object_list", apply_object_list);
134    def("apply_list_list", apply_list_list);
135       
136    def("append_object", append_object);
137    def("append_list", append_list);
138
139    def("exercise", exercise);
140   
141    class_<X>("X", init<int>())
142        .def( "__repr__", x_rep)
143        ;
144}
145
Note: See TracBrowser for help on using the repository browser.