def test_name_mapping_multi_index(self): t = pp.SplittableInt( 33214, { 'weird_number': slice(None, None, 2), 'last_few': slice(-2, -5, -2), 'backwards': slice(None, None, -1) }) self.assertEqual(t.weird_number, 324) self.assertEqual(t.last_few, 13) self.assertRaises(ValueError, setattr, t, 'backwards', 1) self.assertRaises(ValueError, setattr, t, 'last_few', 1) self.assertEqual(t.backwards, 41233) self.assertEqual(t, 33214) t.weird_number = 99 # notice that this will zero the 5th number self.assertEqual(t, 3919) t.weird_number = 7899 self.assertEqual(t, 7083919) t.foo = 1 t = pp.SplittableInt(33214, {'ix': slice(None, 2), 'iy': slice(2, 4)}) self.assertEqual(t.ix, 14) self.assertEqual(t.iy, 32) t.ix = 21 self.assertEqual(t, 33221) t = pp.SplittableInt(33214, {'ix': slice(-1, 2)}) self.assertEqual(t.ix, 0) t = pp.SplittableInt(4, {'ix': slice(None, 2), 'iy': slice(2, 4)}) self.assertEqual(t.ix, 4) self.assertEqual(t.iy, 0)
def test_not_implemented(self): class Terry(object): pass sin = pp.SplittableInt(0) self.assertIs(sin.__eq__(Terry()), NotImplemented) self.assertIs(sin.__ne__(Terry()), NotImplemented)
def test_grow_str_list(self): t = pp.SplittableInt(3) t[1] = 3 self.assertEqual(t[1], 3) t[5] = 4 self.assertEqual(t[5], 4) self.assertEqual(int(t), 400033) self.assertEqual(t, 400033) self.assertNotEqual(t, 33) self.assertTrue(t >= 400033) self.assertFalse(t >= 400034) self.assertTrue(t <= 400033) self.assertFalse(t <= 400032) self.assertTrue(t > 400032) self.assertFalse(t > 400034) self.assertTrue(t < 400034) self.assertFalse(t < 400032)
def test_LBC_header(self): bzx, bzy = -10, 15 # stash m01s00i001 lbuser = [None, None, 121416, 1, None, None, 1] field = self._mock_lbc( lbegin=0, lbrow=10, lbnpt=12, bdx=1, bdy=1, bzx=bzx, bzy=bzy, lbuser=lbuser, ) with self.mock_for_extract_field([field]) as ff2pp: ff2pp._ff_header.dataset_type = 5 result = list(ff2pp._extract_field()) self.assertEqual([field], result) self.assertEqual(field.lbrow, 10 + 14 * 2) self.assertEqual(field.lbnpt, 12 + 16 * 2) name_mapping_dict = dict( rim_width=slice(4, 6), y_halo=slice(2, 4), x_halo=slice(0, 2) ) boundary_packing = pp.SplittableInt(121416, name_mapping_dict) self.assertEqual(field.boundary_packing, boundary_packing) self.assertEqual(field.bzy, bzy - boundary_packing.y_halo * field.bdy) self.assertEqual(field.bzx, bzx - boundary_packing.x_halo * field.bdx)
def test_negative_number(self): self.assertRaises(ValueError, pp.SplittableInt, -5) try: _ = pp.SplittableInt(-5) except ValueError as err: self.assertEqual( str(err), 'Negative numbers not supported with splittable integers object' )
def test_33214(self): t = pp.SplittableInt(33214) self.assertEqual(t[4], 3) self.assertEqual(t[3], 3) self.assertEqual(t[2], 2) self.assertEqual(t[1], 1) self.assertEqual(t[0], 4) # The rest should be zero for i in range(5, 100): self.assertEqual(t[i], 0)
def test_name_mapping(self): t = pp.SplittableInt(33214, {'ones': 0, 'tens': 1, 'hundreds': 2}) self.assertEqual(t.ones, 4) self.assertEqual(t.tens, 1) self.assertEqual(t.hundreds, 2) t.ones = 9 t.tens = 4 t.hundreds = 0 self.assertEqual(t.ones, 9) self.assertEqual(t.tens, 4) self.assertEqual(t.hundreds, 0)
def test_name_mapping(self): t = pp.SplittableInt(33214, {"ones": 0, "tens": 1, "hundreds": 2}) self.assertEqual(t.ones, 4) self.assertEqual(t.tens, 1) self.assertEqual(t.hundreds, 2) t.ones = 9 t.tens = 4 t.hundreds = 0 self.assertEqual(t.ones, 9) self.assertEqual(t.tens, 4) self.assertEqual(t.hundreds, 0)
def setUp(self): # Patch FFHeader to produce a mock header instead of opening a file. self.mock_ff_header = mock.Mock() self.mock_ff_header.dataset_type = 5 self.mock_ff = self.patch('iris.fileformats._ff.FFHeader', return_value=self.mock_ff_header) # Create a mock LBC type PPField. self.mock_field = mock.Mock() field = self.mock_field field.lbtim = 0 field.lblev = 7777 field.lbvc = 0 field.lbnpt = 1001 field.lbrow = 2001 field.lbuser = (None, None, 80504) field.lbpack = pp.SplittableInt(0) field.boundary_packing = None field.bdx = 1.0 field.bzx = 0.0 field.bdy = 1.0 field.bzy = 0.0
def test_3(self): t = pp.SplittableInt(3) self.assertEqual(t[0], 3)
def create_lbpack(self, value): name_mapping = dict(n5=slice(4, None), n4=3, n3=2, n2=1, n1=0) return pp.SplittableInt(value, name_mapping)