diff --git a/test/test_morecollections.py b/test/test_morecollections.py deleted file mode 100644 index b739b24e7..000000000 --- a/test/test_morecollections.py +++ /dev/null @@ -1,310 +0,0 @@ -import unittest - -from util.morecollections import (FastIndexList, StreamingDiffTracker, - IndexedStreamingDiffTracker) - -class FastIndexListTests(unittest.TestCase): - def test_basic_usage(self): - indexlist = FastIndexList() - - # Add 1 - indexlist.add(1) - self.assertEquals([1], indexlist.values()) - self.assertEquals(0, indexlist.index(1)) - - # Add 2 - indexlist.add(2) - self.assertEquals([1, 2], indexlist.values()) - self.assertEquals(0, indexlist.index(1)) - self.assertEquals(1, indexlist.index(2)) - - # Pop nothing. - indexlist.pop_until(-1) - self.assertEquals([1, 2], indexlist.values()) - self.assertEquals(0, indexlist.index(1)) - self.assertEquals(1, indexlist.index(2)) - - # Pop 1. - self.assertEquals([1], indexlist.pop_until(0)) - self.assertEquals([2], indexlist.values()) - self.assertIsNone(indexlist.index(1)) - self.assertEquals(0, indexlist.index(2)) - - # Add 3. - indexlist.add(3) - self.assertEquals([2, 3], indexlist.values()) - self.assertEquals(0, indexlist.index(2)) - self.assertEquals(1, indexlist.index(3)) - - # Pop 2, 3. - self.assertEquals([2, 3], indexlist.pop_until(1)) - self.assertEquals([], indexlist.values()) - self.assertIsNone(indexlist.index(1)) - self.assertIsNone(indexlist.index(2)) - self.assertIsNone(indexlist.index(3)) - - def test_popping(self): - indexlist = FastIndexList() - indexlist.add('hello') - indexlist.add('world') - indexlist.add('you') - indexlist.add('rock') - - self.assertEquals(0, indexlist.index('hello')) - self.assertEquals(1, indexlist.index('world')) - self.assertEquals(2, indexlist.index('you')) - self.assertEquals(3, indexlist.index('rock')) - - indexlist.pop_until(1) - self.assertEquals(0, indexlist.index('you')) - self.assertEquals(1, indexlist.index('rock')) - - -class IndexedStreamingDiffTrackerTests(unittest.TestCase): - def test_basic(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 3) - tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) - tracker.push_old([('b', 1)]) - tracker.done() - - self.assertEquals(['a', 'c'], added) - - def test_multiple_done(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 3) - tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) - tracker.push_old([('b', 1)]) - tracker.done() - tracker.done() - - self.assertEquals(['a', 'c'], added) - - def test_same_streams(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 3) - tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) - tracker.push_old([('a', 0), ('b', 1), ('c', 2)]) - tracker.done() - - self.assertEquals([], added) - - def test_only_new(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 3) - tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) - tracker.push_old([]) - tracker.done() - - self.assertEquals(['a', 'b', 'c'], added) - - def test_pagination(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('a', 0), ('b', 1)]) - tracker.push_old([]) - - tracker.push_new([('c', 2)]) - tracker.push_old([]) - - tracker.done() - - self.assertEquals(['a', 'b', 'c'], added) - - def test_old_pagination_no_repeat(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('new1', 3), ('new2', 4)]) - tracker.push_old([('old1', 1), ('old2', 2)]) - - tracker.push_new([]) - tracker.push_old([('new1', 3)]) - - tracker.done() - - self.assertEquals(['new2'], added) - - def test_old_pagination(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('a', 10), ('b', 11)]) - tracker.push_old([('z', 1), ('y', 2)]) - - tracker.push_new([('c', 12)]) - tracker.push_old([('a', 10)]) - - tracker.done() - - self.assertEquals(['b', 'c'], added) - - def test_very_offset(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('a', 10), ('b', 11)]) - tracker.push_old([('z', 1), ('y', 2)]) - - tracker.push_new([('c', 12), ('d', 13)]) - tracker.push_old([('x', 3), ('w', 4)]) - - tracker.push_new([('e', 14)]) - tracker.push_old([('a', 10), ('d', 13)]) - - tracker.done() - - self.assertEquals(['b', 'c', 'e'], added) - - def test_many_old(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('z', 26), ('hello', 100)]) - tracker.push_old([('a', 1), ('b', 2)]) - - tracker.push_new([]) - tracker.push_old([('c', 1), ('d', 2)]) - - tracker.push_new([]) - tracker.push_old([('e', 3), ('f', 4)]) - - tracker.push_new([]) - tracker.push_old([('g', 5), ('z', 26)]) - - tracker.done() - - self.assertEquals(['hello'], added) - - def test_high_old_bound(self): - added = [] - - tracker = IndexedStreamingDiffTracker(added.append, 2) - tracker.push_new([('z', 26), ('hello', 100)]) - tracker.push_old([('end1', 999), ('end2', 1000)]) - - tracker.push_new([]) - tracker.push_old([]) - - tracker.done() - - self.assertEquals(['z', 'hello'], added) - - -class StreamingDiffTrackerTests(unittest.TestCase): - def test_basic(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 3) - tracker.push_new(['a', 'b', 'c']) - tracker.push_old(['b']) - tracker.done() - - self.assertEquals(['a', 'c'], added) - - def test_same_streams(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 3) - tracker.push_new(['a', 'b', 'c']) - tracker.push_old(['a', 'b', 'c']) - tracker.done() - - self.assertEquals([], added) - - def test_some_new(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 5) - tracker.push_new(['a', 'b', 'c', 'd', 'e']) - tracker.push_old(['a', 'b', 'c']) - tracker.done() - - self.assertEquals(['d', 'e'], added) - - def test_offset_new(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 5) - tracker.push_new(['b', 'c', 'd', 'e']) - tracker.push_old(['a', 'b', 'c']) - tracker.done() - - self.assertEquals(['d', 'e'], added) - - def test_multiple_calls(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 3) - tracker.push_new(['a', 'b', 'c']) - tracker.push_old(['b', 'd', 'e']) - - tracker.push_new(['f', 'g', 'h']) - tracker.push_old(['g', 'h']) - tracker.done() - - self.assertEquals(['a', 'c', 'f'], added) - - def test_empty_old(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 3) - tracker.push_new(['a', 'b', 'c']) - tracker.push_old([]) - - tracker.push_new(['f', 'g', 'h']) - tracker.push_old([]) - tracker.done() - - self.assertEquals(['a', 'b', 'c', 'f', 'g', 'h'], added) - - def test_more_old(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 2) - tracker.push_new(['c', 'd']) - tracker.push_old(['a', 'b']) - - tracker.push_new([]) - tracker.push_old(['c']) - tracker.done() - - self.assertEquals(['d'], added) - - def test_more_new(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 4) - tracker.push_new(['a', 'b', 'c', 'd']) - tracker.push_old(['r']) - - tracker.push_new(['e', 'f', 'r', 'z']) - tracker.push_old([]) - tracker.done() - - self.assertEquals(['a', 'b', 'c', 'd', 'e', 'f', 'z'], added) - - def test_more_new2(self): - added = [] - - tracker = StreamingDiffTracker(added.append, 4) - tracker.push_new(['a', 'b', 'c', 'd']) - tracker.push_old(['r']) - - tracker.push_new(['e', 'f', 'g', 'h']) - tracker.push_old([]) - - tracker.push_new(['i', 'j', 'r', 'z']) - tracker.push_old([]) - tracker.done() - - self.assertEquals(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'z'], added) - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/util/test/test_morecollections.py b/util/test/test_morecollections.py new file mode 100644 index 000000000..321829e64 --- /dev/null +++ b/util/test/test_morecollections.py @@ -0,0 +1,300 @@ +from util.morecollections import (FastIndexList, StreamingDiffTracker, + IndexedStreamingDiffTracker) + +def test_fastindexlist_basic_usage(): + indexlist = FastIndexList() + + # Add 1 + indexlist.add(1) + assert indexlist.values() == [1] + assert indexlist.index(1) == 0 + + # Add 2 + indexlist.add(2) + assert indexlist.values() == [1, 2] + assert indexlist.index(1) == 0 + assert indexlist.index(2) == 1 + + # Pop nothing. + indexlist.pop_until(-1) + assert indexlist.values() == [1, 2] + assert indexlist.index(1) == 0 + assert indexlist.index(2) == 1 + + # Pop 1. + assert indexlist.pop_until(0) == [1] + assert indexlist.values() == [2] + assert indexlist.index(1) is None + assert indexlist.index(2) == 0 + + # Add 3. + indexlist.add(3) + assert indexlist.values() == [2, 3] + assert indexlist.index(2) == 0 + assert indexlist.index(3) == 1 + + # Pop 2, 3. + assert indexlist.pop_until(1) == [2, 3] + assert indexlist.values() == [] + assert indexlist.index(1) is None + assert indexlist.index(2) is None + assert indexlist.index(3) is None + +def test_fastindexlist_popping(): + indexlist = FastIndexList() + indexlist.add('hello') + indexlist.add('world') + indexlist.add('you') + indexlist.add('rock') + + assert indexlist.index('hello') == 0 + assert indexlist.index('world') == 1 + assert indexlist.index('you') == 2 + assert indexlist.index('rock') == 3 + + indexlist.pop_until(1) + assert indexlist.index('you') == 0 + assert indexlist.index('rock') == 1 + + +def test_indexedstreamingdifftracker_basic(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 3) + tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) + tracker.push_old([('b', 1)]) + tracker.done() + + assert added == ['a', 'c'] + +def test_indexedstreamingdifftracker_multiple_done(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 3) + tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) + tracker.push_old([('b', 1)]) + tracker.done() + tracker.done() + + assert added == ['a', 'c'] + +def test_indexedstreamingdifftracker_same_streams(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 3) + tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) + tracker.push_old([('a', 0), ('b', 1), ('c', 2)]) + tracker.done() + + assert added == [] + +def test_indexedstreamingdifftracker_only_new(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 3) + tracker.push_new([('a', 0), ('b', 1), ('c', 2)]) + tracker.push_old([]) + tracker.done() + + assert added == ['a', 'b', 'c'] + +def test_indexedstreamingdifftracker_pagination(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('a', 0), ('b', 1)]) + tracker.push_old([]) + + tracker.push_new([('c', 2)]) + tracker.push_old([]) + + tracker.done() + + assert added == ['a', 'b', 'c'] + +def test_indexedstreamingdifftracker_old_pagination_no_repeat(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('new1', 3), ('new2', 4)]) + tracker.push_old([('old1', 1), ('old2', 2)]) + + tracker.push_new([]) + tracker.push_old([('new1', 3)]) + + tracker.done() + + assert added == ['new2'] + +def test_indexedstreamingdifftracker_old_pagination(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('a', 10), ('b', 11)]) + tracker.push_old([('z', 1), ('y', 2)]) + + tracker.push_new([('c', 12)]) + tracker.push_old([('a', 10)]) + + tracker.done() + + assert added == ['b', 'c'] + +def test_indexedstreamingdifftracker_very_offset(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('a', 10), ('b', 11)]) + tracker.push_old([('z', 1), ('y', 2)]) + + tracker.push_new([('c', 12), ('d', 13)]) + tracker.push_old([('x', 3), ('w', 4)]) + + tracker.push_new([('e', 14)]) + tracker.push_old([('a', 10), ('d', 13)]) + + tracker.done() + + assert added == ['b', 'c', 'e'] + +def test_indexedstreamingdifftracker_many_old(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('z', 26), ('hello', 100)]) + tracker.push_old([('a', 1), ('b', 2)]) + + tracker.push_new([]) + tracker.push_old([('c', 1), ('d', 2)]) + + tracker.push_new([]) + tracker.push_old([('e', 3), ('f', 4)]) + + tracker.push_new([]) + tracker.push_old([('g', 5), ('z', 26)]) + + tracker.done() + + assert added == ['hello'] + +def test_indexedstreamingdifftracker_high_old_bound(): + added = [] + + tracker = IndexedStreamingDiffTracker(added.append, 2) + tracker.push_new([('z', 26), ('hello', 100)]) + tracker.push_old([('end1', 999), ('end2', 1000)]) + + tracker.push_new([]) + tracker.push_old([]) + + tracker.done() + + assert added == ['z', 'hello'] + +def test_streamingdifftracker_basic(): + added = [] + + tracker = StreamingDiffTracker(added.append, 3) + tracker.push_new(['a', 'b', 'c']) + tracker.push_old(['b']) + tracker.done() + + assert added == ['a', 'c'] + +def test_streamingdifftracker_same_streams(): + added = [] + + tracker = StreamingDiffTracker(added.append, 3) + tracker.push_new(['a', 'b', 'c']) + tracker.push_old(['a', 'b', 'c']) + tracker.done() + + assert added == [] + +def test_streamingdifftracker_some_new(): + added = [] + + tracker = StreamingDiffTracker(added.append, 5) + tracker.push_new(['a', 'b', 'c', 'd', 'e']) + tracker.push_old(['a', 'b', 'c']) + tracker.done() + + assert added == ['d', 'e'] + +def test_streamingdifftracker_offset_new(): + added = [] + + tracker = StreamingDiffTracker(added.append, 5) + tracker.push_new(['b', 'c', 'd', 'e']) + tracker.push_old(['a', 'b', 'c']) + tracker.done() + + assert added == ['d', 'e'] + +def test_streamingdifftracker_multiple_calls(): + added = [] + + tracker = StreamingDiffTracker(added.append, 3) + tracker.push_new(['a', 'b', 'c']) + tracker.push_old(['b', 'd', 'e']) + + tracker.push_new(['f', 'g', 'h']) + tracker.push_old(['g', 'h']) + tracker.done() + + assert added == ['a', 'c', 'f'] + +def test_streamingdifftracker_empty_old(): + added = [] + + tracker = StreamingDiffTracker(added.append, 3) + tracker.push_new(['a', 'b', 'c']) + tracker.push_old([]) + + tracker.push_new(['f', 'g', 'h']) + tracker.push_old([]) + tracker.done() + + assert added == ['a', 'b', 'c', 'f', 'g', 'h'] + +def test_streamingdifftracker_more_old(): + added = [] + + tracker = StreamingDiffTracker(added.append, 2) + tracker.push_new(['c', 'd']) + tracker.push_old(['a', 'b']) + + tracker.push_new([]) + tracker.push_old(['c']) + tracker.done() + + assert added == ['d'] + +def test_streamingdifftracker_more_new(): + added = [] + + tracker = StreamingDiffTracker(added.append, 4) + tracker.push_new(['a', 'b', 'c', 'd']) + tracker.push_old(['r']) + + tracker.push_new(['e', 'f', 'r', 'z']) + tracker.push_old([]) + tracker.done() + + assert added == ['a', 'b', 'c', 'd', 'e', 'f', 'z'] + +def test_streamingdifftracker_more_new2(): + added = [] + + tracker = StreamingDiffTracker(added.append, 4) + tracker.push_new(['a', 'b', 'c', 'd']) + tracker.push_old(['r']) + + tracker.push_new(['e', 'f', 'g', 'h']) + tracker.push_old([]) + + tracker.push_new(['i', 'j', 'r', 'z']) + tracker.push_old([]) + tracker.done() + + assert added == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'z']