t164.py

00001 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING up one directory from this.
00012 
00013 #
00014 # System Test 164
00015 # Add support for list comprehensions
00016 #
00017 
00018 l = [x for x in range(5)]
00019 assert l == range(5)
00020 
00021 l = [x*2 for x in range(5)]
00022 print "[x*2 for x in range(5)] = ", l
00023 assert l == range(0,10,2)
00024 
00025 assert [ord(c) for c in "0123"] == [0x30, 0x31, 0x32, 0x33]
00026 
00027 assert [x for x in ('a', 'b', 'c')] == ['a', 'b', 'c']
00028 
00029 
00030 # Check 'if' after for comprehension
00031 assert [x for x in range(10) if x%3 != 0] == [1,2, 4,5, 7,8 ]
00032 
00033 
00034 # Check nested comprehensions
00035 mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]
00036 assert [[row[i] for row in mat] for i in [0, 1, 2]] == [[1,4,7],[2,5,8],[3,6,9]]

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9