def human(self): for mem_i, (mem_b, mem_hs) in self._mems.items(): mem = Memory(mem_i) mem_h = Memory(mem_hs) mem_human = mem.human() self.assertEqual(mem_human, mem_h) self.assertEqual(mem_human.units(), mem_h.units()) self.assertEqual(str(mem_human), mem_hs)
def test_operations(self): mem0 = Memory("1048576b") mem1 = Memory("1m") mem2 = Memory("1023k") memz = Memory(0) self.assertEqual(mem0, mem1) self.assertNotEqual(mem0, mem2) self.assertGreaterEqual(mem0, mem1) self.assertLessEqual(mem0, mem1) self.assertGreater(mem0, mem2) self.assertLess(mem2, mem0) self.assertGreater(mem1, 10)
def compare_files(self, filename_a, filename_b): block_bytes = Memory('1gb').get_bytes() offset = 0 with open(filename_a, 'rb') as f_a, open(filename_b, 'rb') as f_b: block_a = f_a.read(block_bytes) block_b = f_b.read(block_bytes) if block_a != block_b: for count, (ca, cb) in enumerate(zip(block_a, block_b)): if ca != cb: return False, offset + count offset += len(block_a) return True, -1
def impl_read(self, shape, extractor, dtype, threshold_size): shape = Shape(shape) extractor = Extractor(extractor) threshold_size = Memory(threshold_size) dtype = cb.get_dtype(dtype) file_format = 'raw' out_filename_format = 'xtmp_{shape}_{dtype}.{format}' out_filename = out_filename_format.format(shape=shape, dtype=dtype.__name__, format=file_format) returncode, output, error = self.run_program( """-e 'cb.random_cube("{s}")' -o '{o}'""".format( s=shape, o=out_filename_format)) self.assertEqual(returncode, 0) self.assertFileExistsAndHasShape(out_filename, shape=shape, dtype=dtype) out1_filename_format = 'xtmp1_{shape}_{dtype}.{format}' out1_filename = out1_filename_format.format(shape=shape, dtype=dtype.__name__, format=file_format) returncode, output, error = self.run_program( """-i '{o}' -s {s} -x '{x}' --read-threshold-size=0 -o {o1}""". format(s=shape, x=extractor, o=out_filename_format, o1=out1_filename_format)) self.assertEqual(returncode, 0) out2_filename_format = 'xtmp2_{shape}_{dtype}.{format}' out2_filename = out2_filename_format.format(shape=shape, dtype=dtype.__name__, format=file_format) returncode, output, error = self.run_program( """-i '{o}' -s {s} -x '{x}' --read-threshold-size={ms} -o {o2}""". format(s=shape, x=extractor, o=out_filename_format, o2=out2_filename_format, ms=threshold_size)) self.assertEqual(returncode, 0)
def get_available_units(self): self.assertIn('m', Memory.get_available_units())
def test_units(self): mem = Memory('1.5g') self.assertEqual(mem.units(), "g")
def test_value(self): mem = Memory('1.5g') self.assertEqual(mem.value(), 1.5)
def test_bool(self): memz = Memory(0) memn = Memory(2) self.assertTrue(memn) self.assertFalse(memz)
def constructor(self): for mem_i, (mem_b, mem_hs) in self._mems.items(): mem = Memory(mem_i) self.assertEqual(mem.get_bytes(), mem_b)