Lichen

tests/range.py

1029:b4d8e43dad4a
5 months ago Paul Boddie Merged changes from the trailing-data branch. value-replacement
     1 # Obtain a list computed by range.     2      3 l = range(0, 10)     4 print l                                     # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]     5 print len(l)                                # 10     6      7 # Test a descending xrange.     8      9 x = xrange(0, -10, -2)    10 print x                                     # __builtins__.span.xrange(0, -10, -2)    11 print len(x)                                # 5    12     13 for i in x:    14     print i                                 # 0    15                                             # -2    16                                             # -4    17                                             # -6    18                                             # -8    19     20 # Test an empty xrange.    21     22 x = xrange(0, -10, 2)    23 print x                                     # __builtins__.span.xrange(0, 0, 2)    24 print len(x)                                # 0    25     26 # Test a simple ascending xrange.    27     28 y = xrange(4, 8)    29 print y                                     # __builtins__.span.xrange(4, 8, 1)    30     31 # Test enumerate and sum.    32     33 print enumerate(y)                          # [(0, 4), (1, 5), (2, 6), (3, 7)]    34 print sum(y)                                # 22    35     36 # Test map and filter using lambdas.    37     38 print map(lambda x: x*2, y)                 # [8, 10, 12, 14]    39 print filter(lambda x: x%2, y)              # [5, 7]    40     41 # Test the limits of the range.    42     43 print max(y)                                # 7    44 print min(y)                                # 4    45     46 # Reproduce the sum function using reduce and a lambda.    47     48 print reduce(lambda x, y: x+y, y)           # 22    49 print reduce(lambda x, y: x+y, y, 0)        # 22    50     51 # Test a single element range.    52     53 single = xrange(3, 5, 2)    54 print list(single)                          # [3]    55 print reduce(lambda x, y: x+y, single)      # [3]    56     57 # Test a double element range.    58     59 double = xrange(3, 5, 1)    60 print list(double)                          # [3, 4]    61 print reduce(lambda x, y: x+y, double)      # [7]    62     63 # Test steps not coinciding with the end.    64     65 beyond = xrange(4, 9, 2)    66 print list(beyond)                          # [4, 6, 8]    67 print len(beyond)                           # 3