def test_len(self): r = [2.0, 2.0, 3.0, 4.0] offset = 0.5 it = sm.RelativeOffsetIterator(r, offset, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(len([i for i in copy.deepcopy(it)]), len(it)) self.assertEqual( 9, # 3 * 3 len(it)) offset = 5 it = sm.RelativeOffsetIterator(r, offset, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(len([i for i in copy.deepcopy(it)]), len(it)) offset = 7 it = sm.RelativeOffsetIterator(r, offset, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(len([i for i in copy.deepcopy(it)]), len(it)) # Test len with power hop # Example of WZL data r = [ 1, 2.2, 60.0, 20.0, 60.0, 20.0, 60.0, 20.0, 1, 1, 2, 22.5, 23.6, 30.2, 1, 1, 40.0, 165.0, 0.08 ] rounding = [0, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 3, 3, 0, 0, 3] id_len = 17 offset = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 400] it = sm.VariableOffsetIterator(r, offset, True, rounding, id_len) self.assertEqual(701, len(it))
def test_rel_offset_iterator(self): r = [2.0, 2.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 5, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(441, len([i for i in it])) r = [20.0, 20.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 0.5, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(9, len([i for i in it])) r = [200.0, 2.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 0.05, rounding_vec=[3, 3], record_id_length=2) self.assertEqual([(200.0, 2.0, 3.0, 4.0)], [i for i in it])
def test_split(self): r = [200.0, 2.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 2, rounding_vec=[3, 3], record_id_length=2) l1 = [str(i) for i in it] l = [str(i) for i in it] with self.assertRaises(RuntimeError): it.split(5) it = sm.RelativeOffsetIterator(r, 2, rounding_vec=[3, 3], record_id_length=2) its = it.split(5) self.assertGreaterEqual(len(its), 5) l2 = [] for it in its: l2.extend([str(i) for i in it]) self.assertEqual(sorted(l1), sorted(l2)) it = sm.RelativeOffsetIterator(r, 2, rounding_vec=[3, 3], record_id_length=2) its = it.split(5) for i in its: length = len(list(i)) self.assertIn(length, [9, 18]) # split on second it = sm.AbsoluteOffsetIterator(r, 0.5, rounding_vec=[2, 2], record_id_length=2) it2 = copy.deepcopy(it) l1 = [i for i in it2] its = it.split(5) self.assertEqual(4, len(its)) l2 = [] for it in its: l2.extend([i for i in it]) self.assertEqual(l1, l2)
def test_comp_offset_num(self): # Relative r = [2.0, 2.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 5, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(441, sm.comp_offset_num(it)) r = [20.0, 20.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 0.5, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(9, sm.comp_offset_num(it)) # Absolute r = [2.0, 2.0, 3.0, 4.0] it = sm.AbsoluteOffsetIterator(r, 0.1, rounding_vec=[3, 3], record_id_length=2) self.assertEqual(441, sm.comp_offset_num(it))
def test_record_iterator_len(self): r = [100.0, 1.0, 3.0, 4.0] it = sm.RecordIterator([r], b"key") self.assertEqual(1, len(it)) m = sm.RelativeOffsetIterator(r, 5, rounding_vec=[3, 3], record_id_length=2) it = sm.RecordIterator(m, b"key") self.assertEqual(3136, len(it)) it = sm.RecordIterator(iter([1]), b"key") self.assertEqual(0, len(it))
def test_offset(self): r = [2.0, 2.0, 3.0, 4.0] self.assertEqual(1, len(list(sm.AbsoluteOffsetIterator(r, 0, [3, 3], 2)))) self.assertEqual( 441, len(list(sm.AbsoluteOffsetIterator(r, 0.1, [3, 3], 2)))) r2 = [2000.0, 20000.0, 3.0, 4.0] self.assertEqual( 3, len(list(sm.AbsoluteOffsetIterator(r2, 10, [3, 3], 2)))) r = [200.0, 200.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 0.1, rounding_vec=[3, 3], record_id_length=2) self.assertEqual([(200.0, 200.0, 3.0, 4.0)], [i for i in it])
def test_normal_case(self): # Basically 2 identical Iterators it = sm.VariableOffsetIterator(self.target, [10 for _ in range(5)]) it2 = sm.RelativeOffsetIterator(self.target, 10) self.assertEqual(len(it), len(it2)) self.assertEqual(list(it), list(it2))
def test_eval_offsets(self): r = [float(i) for i in range(1, 101)] rounding = [3 for _ in range(10)] for offset in range(1, 6): it = sm.RelativeOffsetIterator(r, offset, rounding, 10)
def test_copy(self): r = [1.0, 1.0, 3.0, 4.0] it = sm.RelativeOffsetIterator(r, 1, [2, 2], 2) it2 = copy.copy(it) self.assertEqual(len(it), len(it2)) self.assertEqual(list(it), list(it2))