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