def test_sortingpipe(self): iterations = 100 forward = sortingPipe() reverse = sortingPipe(reverse=True) [forward.push(random.random()) for _ in xrange(iterations)] [reverse.push(random.random()) for _ in xrange(iterations)] self.assertTrue(is_sorted(forward)) self.assertTrue(is_sorted(reverse, reverse=True))
def test_extsorted_stress(self): file_size = 2 ** 20 # 1 MB file_count = 2.5 typical_size = sys.getsizeof(1, random.random()) count = int((file_size * file_count)) / typical_size data = ((i, random.random()) for i in xrange(count)) sorted_data = list(extsorted(data, key=itemgetter(1), reverse=True, max_mem=file_size)) self.assertEqual(len(sorted_data), count) self.assertTrue(is_sorted(sorted_data, key=itemgetter(1), reverse=True, expected_len=None))
def test_indexed(self): tf = tempfile.TemporaryFile() with IndexedKVWriter(tf, reverse=True) as writer: for _ in xrange(20): writer.write(random.random(), random.random()) tf.seek(0) ikvr = IndexedKVReader(tf) self.assertRaises(IndexNotLoaded, ikvr.get) self.assertTrue(is_sorted(IndexedKVReader(tf), reverse=True, key=itemgetter(0))) tf.seek(0) self.assertEqual(20, len(iter(IndexedKVReader(tf))), 'length does not match')
def test_is_sorted(self): self.assertTrue(is_sorted([1,2,3,4,5])) self.assertTrue(is_sorted([1,2,3,4,5], expected_len=5)) self.assertFalse(is_sorted([1,2,3,2])) self.assertFalse(is_sorted([1,2,3,4,5], expected_len=1))
def test_extsorted_all_at_once(self): data = list(random.random() for _ in xrange(10)) max_mem = sys.getsizeof(data) * 2 # definitely big enough for whole list self.assertTrue(is_sorted(extsorted(data, max_mem=max_mem)))
def test_extsorted_small(self): data = list(random.random() for _ in xrange(10)) max_mem = sys.getsizeof(0) - 1 # too small for even one value self.assertTrue(is_sorted(extsorted(data, max_mem=max_mem)))