Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
| 3 | from test_support import * |
| 4 | |
| 5 | print '6. Built-in types' |
| 6 | |
| 7 | print '6.1 Truth value testing' |
| 8 | if None: raise TestFailed, 'None is true instead of false' |
| 9 | if 0: raise TestFailed, '0 is true instead of false' |
| 10 | if 0L: raise TestFailed, '0L is true instead of false' |
| 11 | if 0.0: raise TestFailed, '0.0 is true instead of false' |
| 12 | if '': raise TestFailed, '\'\' is true instead of false' |
| 13 | if (): raise TestFailed, '() is true instead of false' |
| 14 | if []: raise TestFailed, '[] is true instead of false' |
| 15 | if {}: raise TestFailed, '{} is true instead of false' |
| 16 | if not 1: raise TestFailed, '1 is false instead of true' |
| 17 | if not 1L: raise TestFailed, '1L is false instead of true' |
| 18 | if not 1.0: raise TestFailed, '1.0 is false instead of true' |
| 19 | if not 'x': raise TestFailed, '\'x\' is false instead of true' |
| 20 | if not (1, 1): raise TestFailed, '(1, 1) is false instead of true' |
| 21 | if not [1]: raise TestFailed, '[1] is false instead of true' |
| 22 | if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' |
| 23 | def f(): pass |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 24 | class C: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 25 | import sys |
| 26 | x = C() |
| 27 | if not f: raise TestFailed, 'f is false instead of true' |
| 28 | if not C: raise TestFailed, 'C is false instead of true' |
| 29 | if not sys: raise TestFailed, 'sys is false instead of true' |
| 30 | if not x: raise TestFailed, 'x is false instead of true' |
| 31 | |
| 32 | print '6.2 Boolean operations' |
| 33 | if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' |
| 34 | if 1 and 1: pass |
| 35 | else: raise TestFailed, '1 and 1 is false instead of false' |
| 36 | if not 1: raise TestFailed, 'not 1 is true instead of false' |
| 37 | |
| 38 | print '6.3 Comparisons' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 39 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 40 | else: raise TestFailed, 'int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 41 | if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 42 | else: raise TestFailed, 'long int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 43 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 44 | else: raise TestFailed, 'float comparisons failed' |
| 45 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass |
| 46 | else: raise TestFailed, 'string comparisons failed' |
| 47 | if 0 in [0] and 0 not in [1]: pass |
| 48 | else: raise TestFailed, 'membership test failed' |
| 49 | if None is None and [] is not []: pass |
| 50 | else: raise TestFailed, 'identity test failed' |
| 51 | |
| 52 | print '6.4 Numeric types (mostly conversions)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 53 | if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons' |
| 54 | if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' |
| 55 | if -1 != -1L or -1 != -1.0 or -1L != -1.0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 56 | raise TestFailed, 'int/long/float value not equal' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 57 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
| 58 | else: raise TestFailed, 'int() does not round properly' |
| 59 | if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass |
| 60 | else: raise TestFailed, 'long() does not round properly' |
| 61 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass |
| 62 | else: raise TestFailed, 'float() does not work properly' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 63 | print '6.4.1 32-bit integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 64 | if 12 + 24 != 36: raise TestFailed, 'int op' |
| 65 | if 12 + (-24) != -12: raise TestFailed, 'int op' |
| 66 | if (-12) + 24 != 12: raise TestFailed, 'int op' |
| 67 | if (-12) + (-24) != -36: raise TestFailed, 'int op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 68 | if not 12 < 24: raise TestFailed, 'int op' |
| 69 | if not -24 < -12: raise TestFailed, 'int op' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 70 | # Test for a particular bug in integer multiply |
| 71 | xsize, ysize, zsize = 238, 356, 4 |
| 72 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 73 | raise TestFailed, 'int mul commutativity' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 74 | print '6.4.2 Long integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 75 | if 12L + 24L != 36L: raise TestFailed, 'long op' |
| 76 | if 12L + (-24L) != -12L: raise TestFailed, 'long op' |
| 77 | if (-12L) + 24L != 12L: raise TestFailed, 'long op' |
| 78 | if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 79 | if not 12L < 24L: raise TestFailed, 'long op' |
| 80 | if not -24L < -12L: raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 81 | x = sys.maxint |
| 82 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 83 | try: int(long(x)+1L) |
| 84 | except OverflowError: pass |
| 85 | else:raise TestFailed, 'long op' |
| 86 | x = -x |
| 87 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 88 | x = x-1 |
| 89 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 90 | try: int(long(x)-1L) |
| 91 | except OverflowError: pass |
| 92 | else:raise TestFailed, 'long op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 93 | print '6.4.3 Floating point numbers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 94 | if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' |
| 95 | if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op' |
| 96 | if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' |
| 97 | if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 98 | if not 12.0 < 24.0: raise TestFailed, 'float op' |
| 99 | if not -24.0 < -12.0: raise TestFailed, 'float op' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 100 | |
| 101 | print '6.5 Sequence types' |
| 102 | |
| 103 | print '6.5.1 Strings' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 104 | if len('') != 0: raise TestFailed, 'len(\'\')' |
| 105 | if len('a') != 1: raise TestFailed, 'len(\'a\')' |
| 106 | if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' |
| 107 | if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation' |
| 108 | if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' |
| 109 | if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' |
| 110 | if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 111 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass |
| 112 | else: raise TestFailed, 'in/not in string' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 113 | x = 'x'*103 |
| 114 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 115 | |
| 116 | print '6.5.2 Tuples' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 117 | if len(()) != 0: raise TestFailed, 'len(())' |
| 118 | if len((1,)) != 1: raise TestFailed, 'len((1,))' |
| 119 | if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))' |
| 120 | if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation' |
| 121 | if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3' |
| 122 | if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*' |
| 123 | if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed, 'min/max tuple' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 124 | if 0 in (0,1,2) and 1 in (0,1,2) and 2 in (0,1,2) and 3 not in (0,1,2): pass |
| 125 | else: raise TestFailed, 'in/not in tuple' |
| 126 | |
| 127 | print '6.5.3 Lists' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 128 | if len([]) != 0: raise TestFailed, 'len([])' |
| 129 | if len([1,]) != 1: raise TestFailed, 'len([1,])' |
| 130 | if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])' |
| 131 | if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation' |
| 132 | if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3' |
| 133 | if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L' |
| 134 | if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*' |
| 135 | if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*' |
| 136 | if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 137 | if 0 in [0,1,2] and 1 in [0,1,2] and 2 in [0,1,2] and 3 not in [0,1,2]: pass |
| 138 | else: raise TestFailed, 'in/not in list' |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 139 | a = [1, 2, 3, 4, 5] |
| 140 | a[:-1] = a |
| 141 | if a != [1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 142 | raise TestFailed, "list self-slice-assign (head)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 143 | a = [1, 2, 3, 4, 5] |
| 144 | a[1:] = a |
| 145 | if a != [1, 1, 2, 3, 4, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 146 | raise TestFailed, "list self-slice-assign (tail)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 147 | a = [1, 2, 3, 4, 5] |
| 148 | a[1:-1] = a |
| 149 | if a != [1, 1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 150 | raise TestFailed, "list self-slice-assign (center)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 152 | |
| 153 | print '6.5.3a Additional list operations' |
| 154 | a = [0,1,2,3,4] |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 155 | a[0L] = 1 |
| 156 | a[1L] = 2 |
| 157 | a[2L] = 3 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 158 | if a != [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 159 | a[0] = 5 |
| 160 | a[1] = 6 |
| 161 | a[2] = 7 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 162 | if a != [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 163 | a[-2L] = 88 |
| 164 | a[-1L] = 99 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 165 | if a != [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 166 | a[-2] = 8 |
| 167 | a[-1] = 9 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 168 | if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 169 | a[:2] = [0,4] |
| 170 | a[-3:] = [] |
| 171 | a[1:1] = [1,2,3] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 172 | if a != [0,1,2,3,4]: raise TestFailed, 'list slice assignment' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 173 | a[ 1L : 4L] = [7,8,9] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 174 | if a != [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 175 | del a[1:4] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 176 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 177 | del a[0] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 178 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 179 | del a[-1] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 180 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 181 | a=range(0,5) |
| 182 | del a[1L:4L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 183 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 184 | del a[0L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 185 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 186 | del a[-1L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 187 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 188 | a.append(0) |
| 189 | a.append(1) |
| 190 | a.append(2) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 191 | if a != [0,1,2]: raise TestFailed, 'list append' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 192 | a.insert(0, -2) |
| 193 | a.insert(1, -1) |
| 194 | a.insert(2,0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 195 | if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert' |
| 196 | if a.count(0) != 2: raise TestFailed, ' list count' |
| 197 | if a.index(0) != 2: raise TestFailed, 'list index' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 198 | a.remove(0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 199 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 200 | a.reverse() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 201 | if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 202 | a.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 203 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 204 | def revcmp(a, b): return cmp(b, a) |
| 205 | a.sort(revcmp) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 206 | if a != [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func' |
Guido van Rossum | d151d34 | 1998-02-25 17:51:50 +0000 | [diff] [blame] | 207 | # The following dumps core in unpatched Python 1.5: |
| 208 | def myComparison(x,y): |
| 209 | return cmp(x%3, y%7) |
| 210 | z = range(12) |
| 211 | z.sort(myComparison) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 212 | |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 213 | # Test extreme cases with long ints |
| 214 | a = [0,1,2,3,4] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 215 | if a[ -pow(2,128L): 3 ] != [0,1,2]: |
| 216 | raise TestFailed, "list slicing with too-small long integer" |
| 217 | if a[ 3: pow(2,145L) ] != [3,4]: |
| 218 | raise TestFailed, "list slicing with too-large long integer" |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 220 | print '6.6 Mappings == Dictionaries' |
| 221 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 222 | if d.keys() != []: raise TestFailed, '{}.keys()' |
| 223 | if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 224 | if ('a' in d) != 0: raise TestFailed, "'a' in {}" |
| 225 | if ('a' not in d) != 1: raise TestFailed, "'a' not in {}" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 226 | if len(d) != 0: raise TestFailed, 'len({})' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 227 | d = {'a': 1, 'b': 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 228 | if len(d) != 2: raise TestFailed, 'len(dict)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 229 | k = d.keys() |
| 230 | k.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 231 | if k != ['a', 'b']: raise TestFailed, 'dict keys()' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 232 | if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass |
| 233 | else: raise TestFailed, 'dict keys()' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 234 | if 'a' in d and 'b' in d and 'c' not in d: pass |
| 235 | else: raise TestFailed, 'dict keys() # in/not in version' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 236 | if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 237 | d['c'] = 3 |
| 238 | d['a'] = 4 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 239 | if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 240 | del d['b'] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 241 | if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 242 | # dict.clear() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 243 | d = {1:1, 2:2, 3:3} |
| 244 | d.clear() |
| 245 | if d != {}: raise TestFailed, 'dict clear' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 246 | # dict.update() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 247 | d.update({1:100}) |
| 248 | d.update({2:20}) |
| 249 | d.update({1:1, 2:2, 3:3}) |
| 250 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 251 | d.clear() |
| 252 | try: d.update(None) |
| 253 | except AttributeError: pass |
| 254 | else: raise TestFailed, 'dict.update(None), AttributeError expected' |
| 255 | class SimpleUserDict: |
| 256 | def __init__(self): |
| 257 | self.d = {1:1, 2:2, 3:3} |
| 258 | def keys(self): |
| 259 | return self.d.keys() |
| 260 | def __getitem__(self, i): |
| 261 | return self.d[i] |
| 262 | d.update(SimpleUserDict()) |
| 263 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)' |
| 264 | d.clear() |
| 265 | class FailingUserDict: |
| 266 | def keys(self): |
| 267 | raise ValueError |
| 268 | try: d.update(FailingUserDict()) |
| 269 | except ValueError: pass |
| 270 | else: raise TestFailed, 'dict.keys() expected ValueError' |
| 271 | class FailingUserDict: |
| 272 | def keys(self): |
| 273 | class BogonIter: |
| 274 | def __iter__(self): |
| 275 | raise ValueError |
| 276 | return BogonIter() |
| 277 | try: d.update(FailingUserDict()) |
| 278 | except ValueError: pass |
| 279 | else: raise TestFailed, 'iter(dict.keys()) expected ValueError' |
| 280 | class FailingUserDict: |
| 281 | def keys(self): |
| 282 | class BogonIter: |
| 283 | def __init__(self): |
| 284 | self.i = 1 |
| 285 | def __iter__(self): |
| 286 | return self |
| 287 | def next(self): |
| 288 | if self.i: |
| 289 | self.i = 0 |
| 290 | return 'a' |
| 291 | raise ValueError |
| 292 | return BogonIter() |
| 293 | def __getitem__(self, key): |
| 294 | return key |
| 295 | try: d.update(FailingUserDict()) |
| 296 | except ValueError: pass |
| 297 | else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError' |
| 298 | class FailingUserDict: |
| 299 | def keys(self): |
| 300 | class BogonIter: |
| 301 | def __init__(self): |
| 302 | self.i = ord('a') |
| 303 | def __iter__(self): |
| 304 | return self |
| 305 | def next(self): |
| 306 | if self.i <= ord('z'): |
| 307 | rtn = chr(self.i) |
| 308 | self.i += 1 |
| 309 | return rtn |
| 310 | raise StopIteration |
| 311 | return BogonIter() |
| 312 | def __getitem__(self, key): |
| 313 | raise ValueError |
| 314 | try: d.update(FailingUserDict()) |
| 315 | except ValueError: pass |
| 316 | else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError' |
| 317 | # dict.copy() |
| 318 | d = {1:1, 2:2, 3:3} |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 319 | if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy' |
| 320 | if {}.copy() != {}: raise TestFailed, 'empty dict copy' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 321 | # dict.get() |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 322 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 323 | if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg' |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 324 | if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 325 | d = {'a' : 1, 'b' : 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 326 | if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 327 | if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg' |
| 328 | if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg' |
| 329 | if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 330 | # dict.setdefault() |
| 331 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 332 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 333 | raise TestFailed, 'missing {} setdefault, no 2nd arg' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 334 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 335 | raise TestFailed, 'present {} setdefault, no 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 336 | d.setdefault('key', []).append(3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 337 | if d['key'][0] != 3: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 338 | raise TestFailed, 'missing {} setdefault, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 339 | d.setdefault('key', []).append(4) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 340 | if len(d['key']) != 2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 341 | raise TestFailed, 'present {} setdefault, w/ 2nd arg' |
Guido van Rossum | b822c61 | 2000-12-12 22:02:59 +0000 | [diff] [blame] | 342 | # dict.popitem() |
| 343 | for copymode in -1, +1: |
| 344 | # -1: b has same structure as a |
| 345 | # +1: b is a.copy() |
| 346 | for log2size in range(12): |
| 347 | size = 2**log2size |
| 348 | a = {} |
| 349 | b = {} |
| 350 | for i in range(size): |
| 351 | a[`i`] = i |
| 352 | if copymode < 0: |
| 353 | b[`i`] = i |
| 354 | if copymode > 0: |
| 355 | b = a.copy() |
| 356 | for i in range(size): |
| 357 | ka, va = ta = a.popitem() |
| 358 | if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta) |
| 359 | kb, vb = tb = b.popitem() |
| 360 | if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb) |
| 361 | if copymode < 0 and ta != tb: |
| 362 | raise TestFailed, "a.popitem != b.popitem: %s, %s" % ( |
| 363 | str(ta), str(tb)) |
| 364 | if a: raise TestFailed, 'a not empty after popitems: %s' % str(a) |
| 365 | if b: raise TestFailed, 'b not empty after popitems: %s' % str(b) |