def test_invalid_string_alloc(self): with self.assertRaises(ValueError): data_type = EclDataType(EclTypeEnum.ECL_STRING_TYPE) with self.assertRaises(ValueError): data_type = EclDataType(EclTypeEnum.ECL_STRING_TYPE, -1) with self.assertRaises(ValueError): data_type = EclDataType(EclTypeEnum.ECL_STRING_TYPE, 1000)
def test_equals(self): test_base = zip(self.TYPES, self.SIZES) for ecl_type, elem_size in test_base: a = EclDataType(ecl_type, elem_size) b = EclDataType(ecl_type, elem_size) self.assertTrue(a.is_equal(b)) self.assertEqual(a, b) for otype, osize in set(test_base)-set([(ecl_type, elem_size)]): self.assertFalse(a.is_equal(EclDataType(otype, osize))) self.assertNotEqual(a, EclDataType(otype, osize))
def test_is_numeric(self): numeric_types = [ EclTypeEnum.ECL_INT_TYPE, EclTypeEnum.ECL_FLOAT_TYPE, EclTypeEnum.ECL_DOUBLE_TYPE ] for ecl_type in numeric_types: self.assertTrue(EclDataType(ecl_type).is_numeric()) for ecl_type in set(get_const_size_types()) - set(numeric_types): self.assertFalse(EclDataType(ecl_type).is_numeric()) for elem_size in self.STRING_SIZES: data_type = EclDataType(EclTypeEnum.ECL_STRING_TYPE, elem_size) self.assertFalse(data_type.is_numeric())
def test_hash(self): all_types = set() test_base = list(zip(self.TYPES, self.SIZES)) for index, (ecl_type, elem_size) in enumerate(test_base): all_types.add(EclDataType(ecl_type, elem_size)) self.assertEqual(index + 1, len(all_types)) for index, (ecl_type, elem_size) in enumerate(test_base): all_types.add(EclDataType(ecl_type, elem_size)) for index, ecl_type in enumerate(get_const_size_types()): all_types.add(EclDataType(ecl_type)) self.assertEqual(len(test_base), len(all_types))
def test_is_numeric(self): numeric_types = [ EclTypeEnum.ECL_INT_TYPE, EclTypeEnum.ECL_FLOAT_TYPE, EclTypeEnum.ECL_DOUBLE_TYPE ] for ecl_type in numeric_types: self.assertTrue(EclDataType(ecl_type).is_numeric()) for ecl_type in set(get_const_size_types())-set(numeric_types): self.assertFalse(EclDataType(ecl_type).is_numeric()) for elem_size in self.STRING_SIZES: data_type = EclDataType(EclTypeEnum.ECL_STRING_TYPE, elem_size) self.assertFalse(data_type.is_numeric())
def test_create_from_type_name(self): test_base = zip(self.TYPES, self.SIZES, self.NAMES) for (ecl_type, elem_size, type_name) in test_base: data_type = EclDataType.create_from_type_name(type_name) self.assertEqual(ecl_type, data_type.type) self.assertEqual(elem_size, data_type.element_size) self.assertEqual(type_name, data_type.type_name)
def test_sum(self): for ecl_type in [EclDataType.ECL_CHAR, EclDataType.ECL_STRING(42)]: kw_string = EclKW("STRING", 100, ecl_type) with self.assertRaises(ValueError): kw_string.sum() kw_int = EclKW("INT", 4, EclDataType.ECL_INT) kw_int[0] = 1 kw_int[1] = 2 kw_int[2] = 3 kw_int[3] = 4 self.assertEqual(kw_int.sum(), 10) kw_d = EclKW("D", 4, EclDataType.ECL_DOUBLE) kw_d[0] = 1 kw_d[1] = 2 kw_d[2] = 3 kw_d[3] = 4 self.assertEqual(kw_d.sum(), 10) kw_f = EclKW("F", 4, EclDataType.ECL_FLOAT) kw_f[0] = 1 kw_f[1] = 2 kw_f[2] = 3 kw_f[3] = 4 self.assertEqual(kw_f.sum(), 10) kw_b = EclKW("F", 4, EclDataType.ECL_BOOL) kw_b[0] = False kw_b[1] = True kw_b[2] = False kw_b[3] = True self.assertEqual(kw_b.sum(), 2)
def test_ecl_index(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW("KW1", 100, EclDataType.ECL_INT) kw2 = EclKW("KW2", 100, EclDataType.ECL_FLOAT) kw3 = EclKW("KW3", 100, EclDataType.ECL_CHAR) kw4 = EclKW("KW4", 100, EclDataType.ECL_STRING(23)) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) kw2.fwrite(f) kw3.fwrite(f) kw4.fwrite(f) ecl_file = EclFile("TEST") ecl_file.write_index("INDEX_FILE") ecl_file.close() ecl_file_index = EclFile("TEST", 0, "INDEX_FILE") for kw in ["KW1", "KW2", "KW3", "KW4"]: self.assertIn(kw, ecl_file_index) with self.assertRaises(IOError): ecl_file.write_index("does-not-exist/INDEX") with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "index_does_not_exist") os.mkdir("path") shutil.copyfile("TEST", "path/TEST") ecl_file = EclFile("path/TEST") ecl_file.write_index("path/index") with CWDContext("path"): ecl_file = EclFile("TEST", 0, "index")
def test_string_padding(self): kw = EclKW("TEST_KW", 1, EclDataType.ECL_STRING(4)) kw[0] = "AB" self.assertEqual(kw[0], "AB ") kw = EclKW("TEST_KW", 1, EclDataType.ECL_CHAR) kw[0] = "ABCD" self.assertEqual(kw[0], "ABCD ")
def test_string_alloc(self): kw = EclKW("KW", 10, EclDataType.ECL_STRING(30)) for i in range(10): kw[i] = str(i) * 30 for i in range(10): self.assertEqual(str(i) * 30, kw[i])
def test_initialization_validation(self): invalid_args = [(None, 0, self.CONST_NAMES[0]), (1, None, self.CONST_NAMES[0]), (1, 0, self.CONST_NAMES[0]), (None, None, None), (None, 12, None)] for inv_arg in invalid_args: with self.assertRaises(ValueError): EclDataType(inv_arg[0], inv_arg[1], inv_arg[2])
def test_fprintf(self): with TestAreaContext("python.ecl_kw"): self.kw_test(EclDataType.ECL_INT, [0, 1, 2, 3, 4, 5], "%4d\n") self.kw_test(EclDataType.ECL_FLOAT, [0.0, 1.1, 2.2, 3.3, 4.4, 5.5], "%12.6f\n") self.kw_test(EclDataType.ECL_DOUBLE, [0.0, 1.1, 2.2, 3.3, 4.4, 5.5], "%12.6f\n") self.kw_test(EclDataType.ECL_BOOL, [True, True, True, False, True], "%4d\n") self.kw_test(EclDataType.ECL_CHAR, ["1", "22", "4444", "666666", "88888888"], "%-8s\n") for str_len in range(1000): self.kw_test(EclDataType.ECL_STRING(str_len), [str(i)*str_len for i in range(10)], "%s\n")
def warn_and_cast_data_type(data_type): if isinstance(data_type, EclDataType): return data_type if isinstance(data_type, EclTypeEnum): if not constant_size_data_type(data_type): raise ValueError("Cannot cast EclTypeEnum (%d) to EclDataType due " "to non-constant size. Please provide an EclDataType instead.") dump_type_deprecation_warning() return EclDataType(data_type)
def test_add_squared(self): kw1 = EclKW("TEST_KW", 3, EclDataType.ECL_STRING(4)) kw2 = EclKW("TEST_KW", 3, EclDataType.ECL_STRING(4)) with self.assertRaises(TypeError): kw1.add_squared(kw2) kw1 = EclKW("T1", 10, EclDataType.ECL_INT) kw2 = EclKW("T2", 11, EclDataType.ECL_INT) with self.assertRaises(ValueError): kw1.add_squared(kw2) kw2 = EclKW("T", 10, EclDataType.ECL_FLOAT) with self.assertRaises(ValueError): kw1.add_squared(kw2) kw2 = EclKW("T2", 10, EclDataType.ECL_INT) kw2.assign(2) kw1.add_squared(kw2) for elm in kw1: self.assertEqual(elm, 4)
def test_equals(self): test_base = zip(self.TYPES, self.SIZES) for ecl_type, elem_size in test_base: a = EclDataType(ecl_type, elem_size) b = EclDataType(ecl_type, elem_size) self.assertTrue(a.is_equal(b)) self.assertEqual(a, b) for otype, osize in set(test_base) - set([(ecl_type, elem_size)]): self.assertFalse(a.is_equal(EclDataType(otype, osize))) self.assertNotEqual(a, EclDataType(otype, osize))
def test_string_write_read_formatted(self): for str_len in range(1000): with TestAreaContext("my_space"): kw = EclKW("TEST_KW", 10, EclDataType.ECL_STRING(str_len)) for i in range(10): kw[i] = str(i)*str_len file_name = "ecl_kw_test" with openFortIO(file_name, mode=FortIO.WRITE_MODE, fmt_file=True) as fortio: kw.fwrite(fortio) with openFortIO(file_name, fmt_file=True) as fortio: loaded_kw = EclKW.fread(fortio) self.assertEqual(kw, loaded_kw)
def test_abs(self): for ecl_type in [ EclDataType.ECL_CHAR, EclDataType.ECL_BOOL, EclDataType.ECL_STRING(32) ]: kw = EclKW("NAME", 10, ecl_type) with self.assertRaises(TypeError): abs_kw = abs(kw) kw = EclKW("NAME", 10, EclDataType.ECL_INT) for i in range(len(kw)): kw[i] = -i abs_kw = abs(kw) for i in range(len(kw)): self.assertEqual(kw[i], -i) self.assertEqual(abs_kw[i], i)
def test_numpy(self): kw1 = EclKW("DOUBLE", 10, EclDataType.ECL_DOUBLE) view = kw1.numpyView() copy = kw1.numpyCopy() self.assertTrue(copy[0] == kw1[0]) self.assertTrue(view[0] == kw1[0]) kw1[0] += 1 self.assertTrue(view[0] == kw1[0]) self.assertTrue(copy[0] == kw1[0] - 1) for ecl_type in [ EclDataType.ECL_CHAR, EclDataType.ECL_BOOL, EclDataType.ECL_STRING(19) ]: kw2 = EclKW("TEST_KW", 10, ecl_type) with self.assertRaises(ValueError): kw2.numpyView()
def test_alloc(self): for (ecl_type, element_size) in zip(self.TYPES, self.SIZES): data_type = EclDataType(ecl_type, element_size) self.assertEqual(ecl_type, data_type.type) self.assertEqual(element_size, data_type.element_size)
def test_get_type_name(self): test_base = zip(self.TYPES, self.SIZES, self.NAMES) for (ecl_type, elem_size, type_name) in test_base: data_type = EclDataType(ecl_type, elem_size) self.assertEqual(type_name, data_type.type_name)
def test_type_verifiers(self): test_base = zip(self.TYPES, self.SIZES, self.CONST_VERIFIERS) for (ecl_type, elem_size, verifier) in test_base: data_type = EclDataType(ecl_type, elem_size) self.assertTrue(verifier(data_type))
def test_alloc_from_type(self): types, sizes = get_const_size_types(), self.CONST_SIZES for (ecl_type, element_size) in zip(types, sizes): data_type = EclDataType(ecl_type) self.assertEqual(ecl_type, data_type.type) self.assertEqual(element_size, data_type.element_size)