PageRenderTime 624ms CodeModel.GetById 162ms app.highlight 271ms RepoModel.GetById 183ms app.codeStats 1ms

/examples/libtest/ListTest.py

http://pyjamas.googlecode.com/
Python | 110 lines | 87 code | 23 blank | 0 comment | 0 complexity | 6b996bb44d55bb4f85ca5d5b27deb475 MD5 | raw file
  1from UnitTest import UnitTest
  2from __pyjamas__ import String
  3
  4class ListTest(UnitTest):
  5    def __init__(self):
  6        UnitTest.__init__(self)
  7
  8    def getName(self):
  9        return "List"
 10
 11    def testSliceGet(self):
 12        value = [0, 1, 2, 3, 4]
 13        
 14        self.assertTrue(value[-1]==4)
 15        self.assertTrue(value[1]==1)
 16        self.assertTrue(value[4]==4)
 17        self.assertTrue(value[-3]==2)
 18    
 19    def testSliceRange(self):
 20        value = [0, 1, 2, 3, 4]
 21        
 22        self.assertTrue(value[1:3][0]==1)
 23        self.assertTrue(value[1:3][1]==2)
 24        self.assertTrue(len(value[1:2])==1)
 25        self.assertTrue(len(value[1:3])==2)
 26        
 27        self.assertTrue(value[:2][0]==0)
 28        self.assertTrue(value[:2][1]==1)
 29        self.assertTrue(len(value[:2])==2)
 30        self.assertTrue(len(value[:1])==1)
 31
 32        self.assertTrue(value[:-1][0]==0)
 33        self.assertTrue(value[:-1][3]==3)
 34        self.assertTrue(len(value[:-1])==4)
 35        
 36        self.assertTrue(value[:][3]==3)
 37        self.assertTrue(len(value[:])==5)
 38        
 39        self.assertTrue(value[0:][3]==3)
 40        self.assertTrue(value[1:][0]==1)
 41        self.assertTrue(len(value[1:])==4)
 42        
 43        self.assertTrue(value[-1:][0]==4)
 44        self.assertTrue(len(value[-1:3])==0)
 45        
 46    def testDelete(self):
 47        value = [0, 1, 2, 3, 4]
 48        del value[4]
 49        self.assertTrue(len(value)==4)
 50        self.assertTrue(value[3]==3)
 51
 52        del value[-1]
 53        self.assertTrue(len(value)==3)
 54        self.assertTrue(value[2]==2)
 55
 56    def testPop(self):
 57        a = ['a']
 58        b = ['b']
 59        c = ['c']
 60        d = ['d']
 61        e = ['e']
 62        
 63        value = [a, b, c, d, e]
 64        
 65        x = value.pop(4)
 66        self.assertTrue(x==e)
 67        self.assertTrue(len(value)==4)
 68        
 69        x = value.pop(-1)
 70        self.assertTrue(x==d)
 71        self.assertTrue(len(value)==3)
 72        
 73        x = value.pop()
 74        self.assertTrue(x==c)
 75        self.assertTrue(len(value)==2)
 76
 77        x = value.pop(0)
 78        self.assertTrue(x==a)
 79        self.assertTrue(len(value)==1)
 80
 81    def testSort(self):
 82        l1 = ['c', 'd', 'a', 'b']
 83        l1.sort()
 84        self.assertTrue(l1[0] == 'a')
 85        self.assertTrue(l1[1] == 'b')
 86        self.assertTrue(l1[2] == 'c')
 87        self.assertTrue(l1[3] == 'd')
 88        
 89        l2 = ['C', 'd', 'A', 'b']
 90        def toLower(x):
 91            return x.lower()
 92        l2.sort(None, toLower)
 93        self.assertTrue(l2[0] == 'A')
 94        self.assertTrue(l2[1] == 'b')
 95        self.assertTrue(l2[2] == 'C')
 96        self.assertTrue(l2[3] == 'd')
 97        
 98        l3 = ['C', 'd', 'A', 'b']
 99        l3.sort(None, toLower, True)
100        self.assertTrue(l3[0] == 'd')
101        self.assertTrue(l3[1] == 'C')
102        self.assertTrue(l3[2] == 'b')
103        self.assertTrue(l3[3] == 'A')
104        
105        l4 = ['c', 'd', 'a', 'b']
106        l4.sort(None, None, True)
107        self.assertTrue(l4[0] == 'd')
108        self.assertTrue(l4[1] == 'c')
109        self.assertTrue(l4[2] == 'b')
110        self.assertTrue(l4[3] == 'a')