def test_filterFromGeneratorReinstantiatesProperly(self): s = stream(ItrFromFunc(lambda: (i for i in xrange(5)))) s = s.filter(lambda e: e % 2 == 0) self.assertEquals(s.toList(), [0, 2, 4]) self.assertEquals(s.toList(), [0, 2, 4]) s = stream(xrange(5)).filter(lambda e: e % 2 == 0) self.assertEquals(s.toList(), [0, 2, 4]) self.assertEquals(s.toList(), [0, 2, 4])
def testStream(self): s = self.s self.assertEquals(list(ifilter(lambda i: i % 2 == 0, s())), [2]) self.assertEquals(list(s().filter(lambda i: i % 2 == 0)), [2]) self.assertEquals(s().filter(lambda i: i % 2 == 0).toList(), [2]) self.assertEquals(s()[1], 2) self.assertEquals(s()[1:].toList(), [2, 3]) self.assertEqual(s().take(2).toList(), [1, 2]) self.assertAlmostEqual(stream((0, 1, 2, 3)).filter(lambda x: x > 0).entropy(), 1.4591479) self.assertEquals(stream([(1, 2), (3, 4)]).zip().toList(), [(1, 3), (2, 4)])
def test_stream_add(self): s1 = stream([1, 2]) s2 = stream([3, 4]) s3 = s1 + s2 ll = s3.toList() self.assertEquals(s3.toList(), [1, 2, 3, 4]) self.assertEquals(s3.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug s1 = s1 + s2 self.assertEquals(s1.toList(), [1, 2, 3, 4]) self.assertEquals(s1.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug
def testStream(self): s = self.s self.assertEquals(list(ifilter(lambda i: i % 2 == 0, s())), [2]) self.assertEquals(list(s().filter(lambda i: i % 2 == 0)), [2]) self.assertEquals(s().filter(lambda i: i % 2 == 0).toList(), [2]) self.assertEquals(s()[1], 2) self.assertEquals(s()[1:].toList(), [2, 3]) self.assertEqual(s().take(2).toList(), [1, 2]) self.assertAlmostEqual( stream((0, 1, 2, 3)).filter(lambda x: x > 0).entropy(), 1.4591479) self.assertEquals( stream([(1, 2), (3, 4)]).zip().toList(), [(1, 3), (2, 4)])
def test_stream_add(self): s1 = stream([1, 2]) s2 = stream([3, 4]) s3 = s1 + s2 ll = s3.toList() self.assertEquals(s3.toList(), [1, 2, 3, 4]) self.assertEquals( s3.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug s1 = s1 + s2 self.assertEquals(s1.toList(), [1, 2, 3, 4]) self.assertEquals( s1.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug
def test_flatMap_reiteration(self): l = stream( ItrFromFunc(lambda: (xrange(i) for i in xrange(5)))).flatMap() self.assertEquals(l.toList(), [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]) self.assertEquals( l.toList(), [0, 0, 1, 0, 1, 2, 0, 1, 2, 3 ]) # second time to assert the regeneration of generator
def test_stream_iadd(self): s1 = stream([1, 2]) s1 += [3, 4] self.assertEquals(s1.toList(), [1, 2, 3, 4]) self.assertEquals( s1.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug self.assertEquals(s1.toList(), [1, 2, 3, 4])
def test_flatMap_basics(self): l = stream(({1: 2, 3: 4}, {5: 6, 7: 8})) self.assertEquals( l.flatMap(dict.itervalues).toSet(), set((2, 4, 6, 8))) self.assertEquals(l.flatMap(dict.iterkeys).toSet(), set((1, 3, 5, 7))) self.assertEquals( l.flatMap(dict.iteritems).toSet(), set(((1, 2), (5, 6), (3, 4), (7, 8))))
def testStreamsFromGenerator(self): sg = stream(ItrFromFunc(lambda: (i for i in range(4)))) self.assertEqual(sg.size(), 4) self.assertEqual(sg.size(), 4) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual(sg.head(), 0) self.assertEqual(sg.head(), 0) self.assertEqual(sg.map(lambda i: i ** 2).enumerate().toList(), [(0, 0), (1, 1), (2, 4), (3, 9)]) self.assertEqual(sg.reduce(lambda x, y: x + y, 5), 11)
def testStreamsFromGenerator(self): sg = stream(ItrFromFunc(lambda: (i for i in range(4)))) self.assertEqual(sg.size(), 4) self.assertEqual(sg.size(), 4) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual( sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual( sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual(sg.head(), 0) self.assertEqual(sg.head(), 0) self.assertEqual( sg.map(lambda i: i**2).enumerate().toList(), [(0, 0), (1, 1), (2, 4), (3, 9)]) self.assertEqual(sg.reduce(lambda x, y: x + y, 5), 11)
def testStreamToJson(self): from Json import Json j = stream((("a", 2), (3, 4))).toJson() self.assertIsInstance(j, Json) self.assertEqual(j.a, 2)
def test_flatMap_basics(self): l = stream(({1: 2, 3: 4}, {5: 6, 7: 8})) self.assertEquals(l.flatMap(dict.itervalues).toSet(), set((2, 4, 6, 8))) self.assertEquals(l.flatMap(dict.iterkeys).toSet(), set((1, 3, 5, 7))) self.assertEquals(l.flatMap(dict.iteritems).toSet(), set(((1, 2), (5, 6), (3, 4), (7, 8))))
def test_streamExists(self): s = stream(xrange(2)) self.assertEqual(s.exists(lambda e: e == 0), True) self.assertEqual(s.exists(lambda e: e == 2), False)
def testGroupBy(self): s = stream(iter((1, 2, 3, 4))) l = s.groupBy(lambda k: k % 2).sorted(key=itemgetter(0)).toList() self.assertEquals([(0, [2, 4]), (1, [1, 3])], l)
def test_stream_iadd(self): s1 = stream([1, 2]) s1 += [3, 4] self.assertEquals(s1.toList(), [1, 2, 3, 4]) self.assertEquals(s1.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug self.assertEquals(s1.toList(), [1, 2, 3, 4])
def test_mins(self): self.assertEquals(stream(['abc', 'a', 'abcd', 'defg', 'cde']).mins(lambda s: len(s)), ['a'])
def setUp(self): self.s = lambda: stream((1, 2, 3))
def test_mins(self): self.assertEquals( stream(['abc', 'a', 'abcd', 'defg', 'cde']).mins(lambda s: len(s)), ['a'])
def test_flatMap_reiteration(self): l = stream(ItrFromFunc(lambda: (xrange(i) for i in xrange(5)))).flatMap() self.assertEquals(l.toList(), [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]) self.assertEquals(l.toList(), [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]) # second time to assert the regeneration of generator