• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright David Abrahams 2004. Distributed under the Boost
2# Software License, Version 1.0. (See accompanying
3# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4from __future__ import print_function
5'''
6>>> from list_ext import *
7
8>>> new_list()
9[]
10
11>>> listify((1,2,3))
12[1, 2, 3]
13
14>>> letters = listify_string('hello')
15>>> letters
16['h', 'e', 'l', 'l', 'o']
17
18>>> X(22)
19X(22)
20
21>>> def identity(x):
22...     return x
23>>> assert apply_object_list(identity, letters) is letters
24
25  5 is not convertible to a list
26
27>>> try: result = apply_object_list(identity, 5)
28... except TypeError: pass
29... else: print('expected an exception, got', result, 'instead')
30
31>>> assert apply_list_list(identity, letters) is letters
32
33  5 is not convertible to a list as a return value
34
35>>> try: result = apply_list_list(len, letters)
36... except TypeError: pass
37... else: print('expected an exception, got', result, 'instead')
38
39>>> append_object(letters, '.')
40>>> letters
41['h', 'e', 'l', 'l', 'o', '.']
42
43  tuples do not automatically convert to lists when passed as arguments
44
45>>> try: append_list(letters, (1,2))
46... except TypeError: pass
47... else: print('expected an exception')
48
49>>> append_list(letters, [1,2])
50>>> letters
51['h', 'e', 'l', 'l', 'o', '.', [1, 2]]
52
53    Check that subclass functions are properly called
54
55>>> class mylist(list):
56...     def append(self, o):
57...         list.append(self, o)
58...         if not hasattr(self, 'nappends'):
59...             self.nappends = 1
60...         else:
61...             self.nappends += 1
62...
63>>> l2 = mylist()
64>>> append_object(l2, 'hello')
65>>> append_object(l2, 'world')
66>>> l2
67['hello', 'world']
68>>> l2.nappends
692
70
71>>> def printer(*args):
72...     for x in args: print( x,)
73...     print('')
74...
75
76>>> y = X(42)
77>>> exercise(letters, y, printer) #doctest: +NORMALIZE_WHITESPACE
78after append:
79['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3)]
80number of X(42) instances: 1
81number of 5s: 1
82after extend:
83['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
84index of X(42) is: 7
85index of 'l' is: 2
86after inserting 666:
87['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
88inserting with object as index:
89['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
90popping...
91['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
92['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
93['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), X(3), 'x', 'y']
94removing X(42)
95['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(3), 'x', 'y']
96removing 666
97['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(3), 'x', 'y']
98reversing...
99['y', 'x', X(3), [1, 2], '.', 'o', 'l', 'l', 'e', 'h']
100sorted:
101['.', 'e', 'h', 'l', 'l', 'o', 'x', 'y']
102reverse sorted:
103['y', 'x', 'o', 'l', 'l', 'h', 'e', '.']
104'''
105
106def run(args = None):
107    import sys
108    import doctest
109
110    if args is not None:
111        sys.argv = args
112    return doctest.testmod(sys.modules.get(__name__))
113
114if __name__ == '__main__':
115    print("running...")
116    import sys
117    status = run()[0]
118    if (status == 0): print("Done.")
119    sys.exit(status)
120