Lichen

Annotated tests/range.py

511:b99c11afb6f5
2017-01-27 Paul Boddie Configure the Makefile using generated secondary Makefiles, replacing the debug-specific Makefile and permitting the selection of included source files.
paul@371 1
# Obtain a list computed by range.
paul@371 2
paul@294 3
l = range(0, 10)
paul@371 4
print l                                     # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
paul@371 5
print len(l)                                # 10
paul@371 6
paul@371 7
# Test a descending xrange.
paul@294 8
paul@294 9
x = xrange(0, -10, -2)
paul@371 10
print x                                     # __builtins__.span.xrange(0, -10, -2)
paul@371 11
print len(x)                                # 5
paul@294 12
paul@294 13
for i in x:
paul@371 14
    print i                                 # 0
paul@371 15
                                            # -2
paul@371 16
                                            # -4
paul@371 17
                                            # -6
paul@371 18
                                            # -8
paul@371 19
paul@371 20
# Test an empty xrange.
paul@294 21
paul@294 22
x = xrange(0, -10, 2)
paul@371 23
print x                                     # __builtins__.span.xrange(0, 0, 2)
paul@371 24
print len(x)                                # 0
paul@371 25
paul@371 26
# Test a simple ascending xrange.
paul@370 27
paul@370 28
y = xrange(4, 8)
paul@371 29
print y                                     # __builtins__.span.xrange(4, 8, 1)
paul@371 30
paul@371 31
# Test enumerate and sum.
paul@371 32
paul@371 33
print enumerate(y)                          # [(0, 4), (1, 5), (2, 6), (3, 7)]
paul@371 34
print sum(y)                                # 22
paul@371 35
paul@371 36
# Test map and filter using lambdas.
paul@371 37
paul@371 38
print map(lambda x: x*2, y)                 # [8, 10, 12, 14]
paul@371 39
print filter(lambda x: x%2, y)              # [5, 7]
paul@371 40
paul@371 41
# Test the limits of the range.
paul@371 42
paul@371 43
print max(y)                                # 7
paul@371 44
print min(y)                                # 4
paul@371 45
paul@371 46
# Reproduce the sum function using reduce and a lambda.
paul@371 47
paul@371 48
print reduce(lambda x, y: x+y, y)           # 22
paul@371 49
print reduce(lambda x, y: x+y, y, 0)        # 22
paul@371 50
paul@371 51
# Test a single element range.
paul@371 52
paul@371 53
single = xrange(3, 5, 2)
paul@371 54
print list(single)                          # [3]
paul@371 55
print reduce(lambda x, y: x+y, single)      # [3]
paul@371 56
paul@371 57
# Test a double element range.
paul@371 58
paul@371 59
double = xrange(3, 5, 1)
paul@371 60
print list(double)                          # [3, 4]
paul@371 61
print reduce(lambda x, y: x+y, double)      # [7]