def test_RecurTraversal_no_change(): data = [ 1, [], [2], Pair(3, 4), Pair('one', 'two'), Pair([Pair(5, [6, 7]), 256.0], 8), Pair(['three', Pair(9, 'four')], 'five'), ] lens = b.RecurTraversal(float) result = lens.over(data, lambda a: 512.0) assert data is not result for n in (0, 1, 2, 3, 4, 6): assert data[n] is result[n]
def test_RecurTraversal_no_change(): data = [ 1, [], [2], Pair(3, 4), Pair("one", "two"), Pair([Pair(5, [6, 7]), 256.0], 8), Pair(["three", Pair(9, "four")], "five"), ] lens = b.RecurTraversal(float) result = lens.over(data, lambda a: 512.0) assert data is not result for n in (0, 1, 2, 3, 4, 6): assert data[n] is result[n]
def test_RecurTraversal_memoizes_hashable(): depth = 100 width = 10 lens = b.RecurTraversal(int) hashable_data = frozenset([0]) for _ in range(depth): hashable_data = hashable_data, None hashable_data = [hashable_data] * width unhashable_data = set([0]) for _ in range(depth): unhashable_data = unhashable_data, None unhashable_data = [unhashable_data] * width hashable_time = timer(lens.over, hashable_data, lambda n: 1) unhashable_time = timer(lens.over, unhashable_data, lambda n: 1) assert hashable_time < unhashable_time
def test_RecurTraversal_over(): data = [ 1, [], [2], Pair(3, 4), Pair('one', 'two'), Pair([Pair(5, [6, 7]), 256.0], 8), Pair(['three', Pair(9, 'four')], 'five'), ] result = [ 2, [], [3], Pair(4, 5), Pair('one', 'two'), Pair([Pair(6, [7, 8]), 256.0], 9), Pair(['three', Pair(10, 'four')], 'five'), ] assert b.RecurTraversal(int).over(data, lambda n: n + 1) == result
def test_RecurTraversal_over(): data = [ 1, [], [2], Pair(3, 4), Pair("one", "two"), Pair([Pair(5, [6, 7]), 256.0], 8), Pair(["three", Pair(9, "four")], "five"), ] result = [ 2, [], [3], Pair(4, 5), Pair("one", "two"), Pair([Pair(6, [7, 8]), 256.0], 9), Pair(["three", Pair(10, "four")], "five"), ] assert b.RecurTraversal(int).over(data, lambda n: n + 1) == result
def test_RecurTraversal_over_with_frozenset(): data = [1, frozenset([2, 3]), 4] result = [11, frozenset([12, 13]), 14] lens = b.RecurTraversal(int) assert lens.over(data, lambda n: n + 10) == result
def test_RecurTraversal_to_list_of(): data = [1, [2], [3, 4], [[5], 6, [7, [8, 9]]]] result = [1, 2, 3, 4, 5, 6, 7, 8, 9] assert b.RecurTraversal(int).to_list_of(data) == result