def test_modify_data(tmpdir): import mmap import shutil test_path = Path(".") / "tests" / "data" / "binary_scalar" shutil.copy(test_path, tmpdir / "testfile") test_file = (tmpdir / "testfile").open(mode="r+b") mm = mmap.mmap(test_file.fileno(), 0) x = parse_bytes(foam_file, mm) assert x["preamble"] == { "name": "FoamFile", "content": { "version": 2.0, "format": "binary", "class": "volScalarField", "arch": "LSB;label=32;scalar=64", "location": "1", "object": "p" } } print(x["data"]) assert x["data"]["internalField"].size == 9200 x["data"]["internalField"][:10] = np.arange(10) y = parse_bytes(foam_file, mm) np.testing.assert_array_equal(y["data"]["internalField"][:10], np.arange(10))
def test_config(): data = b'' @using_config def p(x, config): config["hello"] = x return value(None) @using_config def q(config): return value(config["hello"]) assert parse_bytes(with_config(sequence(p("world"), q())), data) == "world" @using_config def set_cap(x, config): config["upper"] = (x == 1) return value(None) @using_config def get_text(config): if config["upper"]: return many_char(item, lambda x: x.decode().upper()) else: return many_char(item, lambda x: x.decode()) assert parse_bytes(with_config(sequence(integer >> set_cap, get_text())), b'0hello') == "hello" assert parse_bytes(with_config(sequence(integer >> set_cap, get_text())), b'1hello') == "HELLO"
def test_sequence(): p1 = sequence(*(char(c) for c in data), flush()) assert parse_bytes(p1, data) == data p2 = sequence(*(char(c) for c in data), item, flush()) with pytest.raises(EndOfInput): parse_bytes(p2, data)
def test_dimensions(): assert parse_bytes(dimensions, b"[0 2 -2 0 0 0 0]") \ == [0, 2, -2, 0, 0, 0, 0] with pytest.raises(Failure): # Wrongly formatted dimensions vector parse_bytes(dimensions, b"(0 2 -1 0 0 0 0 2)") with pytest.raises(Failure): # The number of dimensions should be 7 parse_bytes(dimensions, b"[0 2 -1 0 0 0]")
def test_email(): email_char = choice(ascii_alpha_num, ascii_underscore) email = named_sequence(user=some_char(email_char), server=sequence(text_literal("@"), flush(), some_char(email_char)), country=sequence(text_literal("."), flush(), some_char(email_char))) assert parse_bytes(email, b"*****@*****.**")['user'] == b"pab" assert parse_bytes(email, b"*****@*****.**")['server'] == b"rod" assert parse_bytes(email, b"*****@*****.**")['country'] == b"es"
def test_identifier(): assert parse_bytes(identifier, b"thisShouldWork0") \ == "thisShouldWork0" assert parse_bytes(identifier, b"this_should_also_work_1") \ == "this_should_also_work_1" with pytest.raises(Failure): parse_bytes(identifier, b"676test") assert parse_bytes(identifier, b"call-with-current-continuation") \ == "call"
def test_array(): import numpy as np numbers = np.random.normal(size=128) byte_data = numbers.data.tobytes() np.testing.assert_array_equal( parse_bytes(array(np.dtype(float), 128), byte_data), numbers) p = named_sequence(open=char("("), data=array(np.dtype(float), 128), close=char(")")) mixed_data = b'(' + byte_data + b')' np.testing.assert_array_equal(parse_bytes(p, mixed_data)["data"], numbers) with pytest.raises(Failure): parse_bytes(array(np.dtype(float), 129), byte_data)
def test_preamble(): test_file = Path(".") / "tests" / "data" / "ascii_scalar" data = test_file.open(mode="rb").read() assert parse_bytes(with_config(preamble), data) == { "name": "FoamFile", "content": { "version": 2.0, "format": "ascii", "class": "volScalarField", "location": "1", "object": "p" } }
def test_ascii_scalar(): test_file = Path(".") / "tests" / "data" / "ascii_scalar" data = test_file.open(mode="rb").read() x = parse_bytes(foam_file, data) assert x["preamble"] == { "name": "FoamFile", "content": { "version": 2.0, "format": "ascii", "class": "volScalarField", "location": "1", "object": "p" } } print(x["data"]) assert x["data"]["internalField"]["size"] == 10 assert len(x["data"]["internalField"]["data"]) == 10
def test_binary_vector(): test_file = Path(".") / "tests" / "data" / "binary_vector" data = test_file.open(mode="rb").read() x = parse_bytes(foam_file, data) assert x["preamble"] == { "name": "FoamFile", "content": { "version": 2.0, "format": "binary", "class": "volVectorField", "arch": "LSB;label=32;scalar=64", "location": "1", "object": "U" } } print(x["data"]) assert x["data"]["internalField"].shape == (9200, 3)
def test_binary_uniform(): test_file = Path(".") / "tests" / "data" / "binary_uniform" data = test_file.open(mode="rb").read() x = parse_bytes(foam_file, data) assert x["preamble"] == { "name": "FoamFile", "content": { "version": 2.0, "format": "binary", "class": "volSymmTensorField", "arch": "LSB;label=32;scalar=64", "location": "0.0001", "object": "UPrime2Mean" } } print(x["data"]) assert x["data"]["internalField"]["data"] == [0, 0, 0, 0, 0, 0]
def test_nested_dict(): assert parse_bytes(dictionary, b"{ a 5; b 6; c { d (3 4 5); e {} } }") \ == {"a": 5, "b": 6, "c": {"d": [3, 4, 5], "e": {}}}
def test_simple_list(): assert parse_bytes(with_config(foam_list(), format="ascii"), b"hello (1 2 3 4 5)") \ == {"name": "hello", "data": [1, 2, 3, 4, 5]}
def test_value(): assert parse_bytes(value(42), b"") == 42
def test_vector(): assert parse_bytes(vector(scientific_number), b"(3.4 10 -1.2 5e9 -4e-5)") \ == [3.4, 10, -1.2, 5e9, -4e-5]
def test_key_value_pair(): assert parse_bytes(key_value_pair, b"alpha 1;") == { "key": "alpha", "value": 1 }
def test_end_of_input(): with pytest.raises(EndOfInput): parse_bytes(item, b"")
def test_tokenize(): assert parse_bytes(some(tokenize(integer)), b"3 42 -67") == [3, 42, -67]
def test_literal(): assert parse_bytes(literal(data), data) == data with pytest.raises(Failure): parse_bytes(literal(b"Hello, Universe!"), data)
def test_block_comment(): x = "/* Hello, World! */".encode() assert parse_bytes(block_comment, x) == " Hello, World! "
def test_fail(): with pytest.raises(Failure): parse_bytes(fail("FAIL!"), data)
def test_pop(): p = sequence(push(0), pop(lambda x: 1 / x)) with pytest.raises(Failure): parse_bytes(p, b"")
def test_scientific(): assert parse_bytes(scientific_number, b"3.1415") == pytest.approx(3.1415) with pytest.raises(Failure): parse_bytes(scientific_number, b".890") with pytest.raises(Failure): parse_bytes(scientific_number, b"8.78e78.2")
def test_item(): assert parse_bytes(item, data) == data[0]
def test_many_char(): assert parse_bytes(many_char(item), data) == data
def test_quoted_string(): assert parse_bytes(quoted_string('"'), b"\"blahblah\"") == "blahblah"
def test_text_literal(): assert parse_bytes(text_literal("Hello"), data) == b"Hello" assert parse_bytes(ignore(text_literal("Hello")), data) is None with pytest.raises(Failure): parse_bytes(text_literal("Hi"), data)