def test_send_items_then_clear_is_empty(self): collection = CollectingSink() sink = collection() sink.send(64) sink.send(128) sink.send(256) collection.clear() self.assertEqual(len(collection), 0)
def test_three_item_iterable_sends_three_items(self): items = [4, 7, 2, 1, 4] collection = CollectingSink() sink = collection() poisson_source(1e6, items, sink) result = list(collection) self.assertListEqual(result, items)
def test_multiple_items_are_retrievable(self): collection = CollectingSink() sink = collection() sink.send(64) sink.send(128) sink.send(256) result = list(collection) self.assertListEqual(result, [64, 128, 256])
def test_two_items_returns_two_element_list(self): collection = CollectingSink() transduce(Transducer, sending(), [23, 78], init=collection()) self.assertEqual(list(collection), [23, 78])
def test_zero_items_returns_initial_empty_collection(self): collection = CollectingSink() transduce(Transducer, sending(), empty_iter(), init=collection()) self.assertEqual(len(collection), 0)
def test_three_items_added_two_dequeued(self): collection = CollectingSink() sink = collection() sink.send(64) sink.send(128) sink.send(256) i = iter(collection) next(i) next(i) self.assertEqual(len(collection), 1)
def test_send_items_to_multiple_sinks(self): collection = CollectingSink() sink1 = collection() sink2 = collection() sink1.send(64) sink2.send(128) sink1.send(256) sink2.send(512) result = list(collection) self.assertListEqual(result, [64, 128, 256, 512])
def test_three_items_added_four_dequeued_raises_stop_iteration(self): collection = CollectingSink() sink = collection() sink.send(64) sink.send(128) sink.send(256) i = iter(collection) next(i) next(i) next(i) with self.assertRaises(StopIteration): next(i)
def test_chained_transducers(self): input = [0.0, 0.2, 0.8, 0.9, 1.1, 2.3, 2.6, 3.0, 4.1] output = CollectingSink() iterable_source(iterable=input, target=transduce(compose( pairwise(), mapping(lambda p: p[1] - p[0]), filtering(lambda d: d < 0.5), mapping(lambda _: "double-click")), target=output())) result = list(output) self.assertListEqual(result, [ 'double-click', 'double-click', 'double-click', 'double-click', 'double-click' ])
def test_empty_iterable_sends_no_items(self): collection = CollectingSink() sink = collection() iterable_source([], sink) self.assertEqual(len(collection), 0)
def test_all_consumed_exits_with_empty_iterator(self): items = [4, 7, 2, 1, 4] collection = CollectingSink() sink = collection() remaining = poisson_source(1e6, items, sink) self.assertIsNone(iterator_or_none(remaining))
def test_send_single_item_is_retrievable(self): collection = CollectingSink() sink = collection() sink.send(64) result = list(collection) self.assertListEqual(result, [64])
def test_send_single_item_has_len_one(self): collection = CollectingSink() sink = collection() sink.send(42) self.assertEqual(len(collection), 1)
def test_no_items_is_empty(self): collection = CollectingSink() self.assertEqual(len(collection), 0)
def test_closed_sink_raises_stop_iteration(self): collection = CollectingSink() sink = collection() sink.close() with self.assertRaises(StopIteration): sink.send(42)