summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/python/test/list.py
blob: 913032db8a53ff46e3951b39079fcaaf2bb42f6a (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
# Copyright David Abrahams 2004. 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)
from __future__ import print_function
'''
>>> from list_ext import *

>>> new_list()
[]

>>> listify((1,2,3))
[1, 2, 3]

>>> letters = listify_string('hello')
>>> letters
['h', 'e', 'l', 'l', 'o']

>>> X(22)
X(22)

>>> def identity(x):
...     return x
>>> assert apply_object_list(identity, letters) is letters

  5 is not convertible to a list

>>> try: result = apply_object_list(identity, 5)
... except TypeError: pass
... else: print('expected an exception, got', result, 'instead')

>>> assert apply_list_list(identity, letters) is letters

  5 is not convertible to a list as a return value

>>> try: result = apply_list_list(len, letters)
... except TypeError: pass
... else: print('expected an exception, got', result, 'instead')

>>> append_object(letters, '.')
>>> letters
['h', 'e', 'l', 'l', 'o', '.']

  tuples do not automatically convert to lists when passed as arguments
  
>>> try: append_list(letters, (1,2))
... except TypeError: pass
... else: print('expected an exception')

>>> append_list(letters, [1,2])
>>> letters
['h', 'e', 'l', 'l', 'o', '.', [1, 2]]

    Check that subclass functions are properly called
    
>>> class mylist(list):
...     def append(self, o):
...         list.append(self, o)
...         if not hasattr(self, 'nappends'):
...             self.nappends = 1
...         else:
...             self.nappends += 1
...
>>> l2 = mylist()
>>> append_object(l2, 'hello')
>>> append_object(l2, 'world')
>>> l2
['hello', 'world']
>>> l2.nappends
2

>>> def printer(*args):
...     for x in args: print( x,)
...     print('')
...

>>> y = X(42)
>>> exercise(letters, y, printer) #doctest: +NORMALIZE_WHITESPACE
after append:
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3)]
number of X(42) instances: 1
number of 5s: 1
after extend:
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
index of X(42) is: 7
index of 'l' is: 2
after inserting 666:
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
inserting with object as index:
['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
popping...
['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), X(3), 'x', 'y']
removing X(42)
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(3), 'x', 'y']
removing 666
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(3), 'x', 'y']
reversing...
['y', 'x', X(3), [1, 2], '.', 'o', 'l', 'l', 'e', 'h']
sorted:
['.', 'e', 'h', 'l', 'l', 'o', 'x', 'y']
reverse sorted:
['y', 'x', 'o', 'l', 'l', 'h', 'e', '.']
'''

def run(args = None):
    import sys
    import doctest

    if args is not None:
        sys.argv = args
    return doctest.testmod(sys.modules.get(__name__))
    
if __name__ == '__main__':
    print("running...")
    import sys
    status = run()[0]
    if (status == 0): print("Done.")
    sys.exit(status)