def testAppend (self): self.assertEquals (List (1), Nil.append (1)) self.assertEquals (List (1, 2, 3, 4), List (1, 2).append (3, 4)) self.assertEquals (List (1, 2), List (1, 2).append ()) self.assertEquals (List (1, 2, 3), List (1, 2) << 3) pass
def testForeach (self): class Trace: def __init__ (self): self._args = [] pass def __call__ (self, args): self._args.append (args) pass pass tr1 = Trace () Nil.foreach (tr1) self.assertEquals ([], tr1._args) tr2 = Trace () List (1, 2, 3).foreach (tr2) self.assertEquals ([1, 2, 3], tr2._args) pass
def testDrop (self): self.assertEquals (Nil, Nil.drop (3)) self.assertEquals (Nil, List (1, 2, 3).drop (3)) self.assertEquals (List (4, 5), List (1, 2, 3, 4, 5).drop (3)) self.assertEquals (List (1, 2, 3), List (1, 2, 3).drop (0)) pass
def testMap (self): self.assertEquals (Nil, Nil.map (lambda x: x)) self.assertEquals (List (2, 4, 6), List (1, 2, 3).map (_ * 2)) pass
def testForall (self): self.assertEquals (True, Nil.forall (lambda x: x == 0)) self.assertEquals (False, List (1, 2, 3).forall (_ % 2 == 0)) self.assertEquals (True, List (2, 4).forall (_ % 2 == 0)) pass
def testFold (self): self.assertEquals (1, Nil.fold (1, operator.add)) self.assertEquals (3, List (2).fold (1, operator.add)) self.assertEquals (6, List (2, 3).fold (1, _[0] + _[1])) pass
def testFilter (self): self.assertEquals (Nil, Nil.filter (lambda x: True)) self.assertEquals (List (2, 4), List (1, 2, 3, 4, 5).filter (_ % 2 == 0)) self.assertEquals (Nil, List (1, 2, 3, 4, 5).filter (_ > 10)) self.assertEquals (List, List (1, 2, 3, 4, 5).filter (lambda x: x > 10).__class__) pass
def testExists (self): self.assertEquals (False, Nil.exists (lambda x: x == 0)) self.assertEquals (True, List (1, 2, 3).exists (lambda x: x % 2 == 0)) self.assertEquals (True, List (1, 2, 3, 4, 5).exists (_ % 2 == 0)) self.assertEquals (False, List (1, 2, 3, 4, 5).exists (_ > 10)) pass
def testTake (self): self.assertEquals (List (1, 2, 3), List (1, 2, 3, 4, 5).take (3)) self.assertEquals (List (1, 2), List (1, 2).take (3)) self.assertEquals (Nil, Nil.take (3)) pass
def testPrepend (self): self.assertEquals (List (1), Nil.prepend (1)) self.assertEquals (List (1, 2, 3), List (2, 3).prepend (1)) self.assertEquals (List (1, 2, 3), 1 >> List (2, 3)) pass
def testEndsWith (self): self.assertTrue (List (1, 2, 3).endsWith (List (2, 3))) self.assertFalse (List (1, 2, 3).endsWith (List (1, 2))) self.assertFalse (Nil.endsWith (List (1, 2))) self.assertTrue (Nil.endsWith (Nil)) pass
def testContains (self): self.assertFalse (Nil.contains (1)) self.assertTrue (List (1, 2, 3).contains (2)) self.assertFalse (List (1, 2, 3).contains (4)) pass
def testStartsWith (self): self.assertTrue (List (1, 2, 3).startsWith (List (1, 2))) self.assertTrue (Nil.startsWith (Nil)) self.assertFalse (List (1, 2, 3).startsWith (List (2, 3))) self.assertFalse (List (1).startsWith (List (1, 2))) pass
def testReverse (self): self.assertEquals (Nil, Nil.reverse ()) self.assertEquals (List (1), List (1).reverse ()) self.assertEquals (List (3, 2, 1), List (1, 2, 3).reverse ()) pass