def test(test_str): """ Test function for the srange class. """ print '\n---------------------------------------------' print "The test input: %r, type=%r" % (test_str,type(test_str)) try: sr = srange(test_str) mystr = '' for val in sr: # this tests the srange.next() method, iterator mystr += str(val) + ', ' print 'Elements of str1: ', mystr print 'Python list of elements in str1:', sr.list() print ' internal representation: ', sr.l print 'String representation:', repr(sr) print 'String value:', str(sr) print 'first and last elements are [%g, %g], %g elements' % (sr.first(), sr.last(), sr.len()) print 'Next element after 5:', sr.after(5) print 'Next element after 16:', sr.after(16) print 'Test if 5 is in range:', sr.is_in_range(5) print 'Test if 6 is in range:', sr.is_in_range(6) print 'Subrange from -1 with 100 elements: ', sr.sub_range(-1,100) print 'Subrange from 3 with 5 elements: ', sr.sub_range(3,5) print 'Index of 5 in string range:', sr.val2index(5) print 'Value at index 3 in string range:', sr.index(3) except Exception as err: print 'This test returned an ERROR!' print err
test('1,2,3,4,5') test('0,2,4,6,8') test('1-7:2, 9-13:2') test('1-7:2, 9-13:2, 15-19:2') test('1,3-7:2,9') test('1-5:1') test('1-5:2, 7-7:1') test('1,3,5,7, 10,12,14,16, 18-20:2') test('1-5:2, 7-7:0') # a zero stride test('1-5:2, 7-7:-1') # a negative stride test('1-5:1.1') # a non-integral stride test('0,3,6,9,12') # strides > 2 if testGroup & 4: # tests of auto_reset print '\n\n========== Tests of string range with auto_reset ==========\n\n' s = srange('5-7',auto_reset=True) for i in s: print ' ',i for i in s: print ' ',i print '\n---------------------------------------------' s = srange('1-5',auto_reset=False) print ' start first loop, "for in s:"' for i in s: if i == 2: print ' break at i =',i break print ' ',i print 'at end of first loop, previous_item = %r, start second loop "for i in s:"' % s.previous_item for i in s: