Skip to content

Commit 6f26778

Browse files
committed
Initial commit map_indexing_suite tests
[SVN r19599]
1 parent 834d815 commit 6f26778

2 files changed

Lines changed: 278 additions & 0 deletions

File tree

test/map_indexing_suite.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
2+
#include <boost/python/module.hpp>
3+
#include <boost/python/def.hpp>
4+
#include <boost/python/implicit.hpp>
5+
6+
using namespace boost::python;
7+
8+
struct X // a container element
9+
{
10+
std::string s;
11+
X():s("default") {}
12+
X(std::string s):s(s) {}
13+
std::string repr() const { return s; }
14+
void reset() { s = "reset"; }
15+
void foo() { s = "foo"; }
16+
bool operator==(X const& x) const { return s == x.s; }
17+
bool operator!=(X const& x) const { return s != x.s; }
18+
};
19+
20+
std::string x_value(X const& x)
21+
{
22+
return "gotya " + x.s;
23+
}
24+
25+
std::string
26+
print_xmap_entry(std::pair<std::string const, X> const& e)
27+
{
28+
std::string r;
29+
r += '(';
30+
r += e.first;
31+
r += ", ";
32+
r += e.second.s;
33+
r += ')';
34+
return r;
35+
}
36+
37+
X&
38+
get_xmap_entry(std::pair<std::string const, X>& e)
39+
{
40+
return e.second;
41+
}
42+
43+
BOOST_PYTHON_MODULE(map_indexing_suite_ext)
44+
{
45+
class_<X>("X")
46+
.def(init<>())
47+
.def(init<X>())
48+
.def(init<std::string>())
49+
.def("__repr__", &X::repr)
50+
.def("reset", &X::reset)
51+
.def("foo", &X::foo)
52+
;
53+
54+
def("x_value", x_value);
55+
implicitly_convertible<std::string, X>();
56+
57+
class_<std::pair<std::string const, X> >("XMapEntry")
58+
.def("__repr__", &print_xmap_entry)
59+
.def("data", &get_xmap_entry, return_internal_reference<>())
60+
;
61+
62+
class_<std::map<std::string, X> >("XMap")
63+
.def(map_indexing_suite<std::map<std::string, X> >())
64+
;
65+
66+
// Compile check only...
67+
class_<std::map<int, int> >("IntMap")
68+
.def(map_indexing_suite<std::map<int, int> >())
69+
;
70+
}
71+

test/map_indexing_suite.py

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
'''
2+
3+
#####################################################################
4+
# Check an object that we will use as container element
5+
#####################################################################
6+
7+
>>> from map_indexing_suite_ext import *
8+
>>> x = X('hi')
9+
>>> x
10+
hi
11+
>>> x.reset() # a member function that modifies X
12+
>>> x
13+
reset
14+
>>> x.foo() # another member function that modifies X
15+
>>> x
16+
foo
17+
18+
# test that a string is implicitly convertible
19+
# to an X
20+
>>> x_value('bochi bochi')
21+
'gotya bochi bochi'
22+
23+
#####################################################################
24+
# Iteration
25+
#####################################################################
26+
>>> def print_xmap(xmap):
27+
... s = '[ '
28+
... for x in xmap:
29+
... s += repr(x)
30+
... s += ' '
31+
... s += ']'
32+
... print s
33+
34+
#####################################################################
35+
# Setting (adding entries)
36+
#####################################################################
37+
>>> xm = XMap()
38+
>>> xm['joel'] = 'apple'
39+
>>> xm['tenji'] = 'orange'
40+
>>> xm['mariel'] = 'grape'
41+
>>> xm['tutit'] = 'banana'
42+
>>> xm['kim'] = 'kiwi'
43+
44+
>>> print_xmap(xm)
45+
[ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
46+
47+
#####################################################################
48+
# Changing an entry
49+
#####################################################################
50+
>>> xm['joel'] = 'pineapple'
51+
>>> print_xmap(xm)
52+
[ (joel, pineapple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
53+
54+
#####################################################################
55+
# Deleting an entry
56+
#####################################################################
57+
>>> del xm['joel']
58+
>>> print_xmap(xm)
59+
[ (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
60+
61+
#####################################################################
62+
# adding an entry
63+
#####################################################################
64+
>>> xm['joel'] = 'apple'
65+
>>> print_xmap(xm)
66+
[ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
67+
68+
#####################################################################
69+
# Indexing
70+
#####################################################################
71+
>>> len(xm)
72+
5
73+
>>> xm['joel']
74+
apple
75+
>>> xm['tenji']
76+
orange
77+
>>> xm['mariel']
78+
grape
79+
>>> xm['tutit']
80+
banana
81+
>>> xm['kim']
82+
kiwi
83+
84+
#####################################################################
85+
# Calling a mutating function of a container element
86+
#####################################################################
87+
>>> xm['joel'].reset()
88+
>>> xm['joel']
89+
reset
90+
91+
#####################################################################
92+
# Copying a container element
93+
#####################################################################
94+
>>> x = X(xm['mariel'])
95+
>>> x
96+
grape
97+
>>> x.foo()
98+
>>> x
99+
foo
100+
>>> xm['mariel'] # should not be changed to 'foo'
101+
grape
102+
103+
#####################################################################
104+
# Referencing a container element
105+
#####################################################################
106+
>>> x = xm['mariel']
107+
>>> x
108+
grape
109+
>>> x.foo()
110+
>>> x
111+
foo
112+
>>> xm['mariel'] # should be changed to 'foo'
113+
foo
114+
115+
>>> xm['mariel'] = 'grape' # take it back
116+
>>> xm['joel'] = 'apple' # take it back
117+
118+
#####################################################################
119+
# Contains
120+
#####################################################################
121+
>>> assert 'joel' in xm
122+
>>> assert 'mariel' in xm
123+
>>> assert 'tenji' in xm
124+
>>> assert 'tutit' in xm
125+
>>> assert 'kim' in xm
126+
>>> assert not 'X' in xm
127+
>>> assert not 12345 in xm
128+
129+
#####################################################################
130+
# Some references to the container elements
131+
#####################################################################
132+
133+
>>> z0 = xm['joel']
134+
>>> z1 = xm['mariel']
135+
>>> z2 = xm['tenji']
136+
>>> z3 = xm['tutit']
137+
>>> z4 = xm['kim']
138+
139+
>>> z0 # proxy
140+
apple
141+
>>> z1 # proxy
142+
grape
143+
>>> z2 # proxy
144+
orange
145+
>>> z3 # proxy
146+
banana
147+
>>> z4 # proxy
148+
kiwi
149+
150+
#####################################################################
151+
# Delete some container element
152+
#####################################################################
153+
154+
>>> del xm['tenji']
155+
>>> print_xmap(xm)
156+
[ (joel, apple) (kim, kiwi) (mariel, grape) (tutit, banana) ]
157+
158+
>>> del xm['tutit']
159+
>>> print_xmap(xm)
160+
[ (joel, apple) (kim, kiwi) (mariel, grape) ]
161+
162+
#####################################################################
163+
# Show that the references are still valid
164+
#####################################################################
165+
>>> z0 # proxy
166+
apple
167+
>>> z1 # proxy
168+
grape
169+
>>> z2 # proxy detached
170+
orange
171+
>>> z3 # proxy detached
172+
banana
173+
>>> z4 # proxy
174+
kiwi
175+
176+
#####################################################################
177+
# Show that iteration allows mutable access to the elements
178+
#####################################################################
179+
>>> for x in xm:
180+
... x.data().reset()
181+
>>> print_xmap(xm)
182+
[ (joel, reset) (kim, reset) (mariel, reset) ]
183+
184+
#####################################################################
185+
# END....
186+
#####################################################################
187+
188+
'''
189+
190+
191+
def run(args = None):
192+
import sys
193+
import doctest
194+
195+
if args is not None:
196+
sys.argxm = args
197+
return doctest.testmod(sys.modules.get(__name__))
198+
199+
if __name__ == '__main__':
200+
print 'running...'
201+
import sys
202+
sys.exit(run()[0])
203+
204+
205+
206+
207+

0 commit comments

Comments
 (0)