Exemplo n.º 1
0
def test_write_low(tmp_path) -> None:
    """Low bits set."""
    bit_string = "00000011"
    zoon = Zoon(bit_string)
    bit_path = tmp_path / "bits"
    zoon.write(bit_path)
    assert bit_path.read_bytes() == to_bytes(bit_string)
Exemplo n.º 2
0
def test_write_high(tmp_path) -> None:
    """High bits set."""
    bit_string = "11000000"
    zoon = Zoon(bit_string)
    bit_path = tmp_path / "bits"
    zoon.write(bit_path)
    assert bit_path.read_bytes() == to_bytes(bit_string)
Exemplo n.º 3
0
def test_write_missing(tmp_path) -> None:
    """0-pad on the right."""
    bit_string = "000011"
    zoon = Zoon(bit_string)
    bit_path = tmp_path / "bits"
    zoon.write(bit_path)
    assert bit_path.read_bytes() == to_bytes(bit_string)
Exemplo n.º 4
0
def survey_range(params) -> None:
    """Mutatate the wild type at each bit in a range and capture the results.
    :param args.Namespace params: All the args
    """
    bits: Union[range, List[int]]
    zoon = Zoon(params.wild_type)
    cmd_args = params.cmd_args
    if params.mutants:  # name the directory
        dir_path = Path(params.mutants)
        dir_path.mkdir(parents=True, exist_ok=True)
    else:  # neither specified
        tempdir = tempfile.mkdtemp()  # use a temporary directory, then cleanup
        atexit.register(shutil.rmtree, tempdir)
        dir_path = Path(tempdir)
    if params.bit_file:
        with open(params.bit_file) as infile:
            bits = [int(bit) for bit in infile]
    elif params.bit_range:
        bits = make_range(params.bit_range,
                          8 * params.wild_type.stat().st_size)
    else:
        bits = range(params.wild_type.stat().st_size)
    for bit in bits:
        result = zoon.mutate_and_run(position=bit,
                                     dir_path=dir_path,
                                     cmd_args=cmd_args)
        if params.loglevel > logging.WARNING:
            print(f"{bit}\t{result[0]}", flush=True)
        logging.warning("mutant at bit %d: %s", bit, result)
        if not params.mutants:
            (dir_path / str(bit)).unlink()  # cleanliness is next to Godliness
Exemplo n.º 5
0
def test_string_len() -> None:
    """len() works correctly on Zoon from string."""
    zoon = Zoon("00000011")  # low bits set
    assert len(zoon) == 8
    zoon = Zoon("11000000")  # high bits set
    assert len(zoon) == 8
    zoon = Zoon("11000000" * 100)  # longer strings
    assert len(zoon) == 800
    zoon = Zoon("110000")  # blank-pad
    assert len(zoon) == 8
    zoon = Zoon("0")  # blank-pad really a lot
    assert len(zoon) == 8
Exemplo n.º 6
0
def test_mutate() -> None:
    """Mutate a bit in a Zoon."""
    zoon_0 = Zoon("00000000")
    new_zoon = zoon_0.mutate(0)
    assert new_zoon.byteseq != zoon_0.byteseq
    zoon_1 = Zoon("00000001")
    new_zoon = zoon_1.mutate(7)
    assert new_zoon.byteseq == zoon_0.byteseq
Exemplo n.º 7
0
def test_delete() -> None:
    """Deletes delete correctly."""
    one = "00000001"
    ten = "00001010"
    zoon_0 = Zoon(one + ten + one)
    zoon_1 = Zoon(one * 2)
    assert zoon_0.delete(1, 2).byteseq == zoon_1.byteseq
    assert zoon_0.delete(0, 1).byteseq != zoon_1.byteseq
    assert zoon_0.delete(2, 3).byteseq != zoon_1.byteseq
    assert zoon_0.delete(0, 2).byteseq != zoon_1.byteseq
    assert zoon_0.delete(0, 3).byteseq != zoon_1.byteseq
Exemplo n.º 8
0
def test_file_len() -> None:
    """len() works correctly on Zoon from file."""
    true = pwhich("true")
    zoon = Zoon(true)
    assert len(zoon) == Path(true).stat().st_size * 8
Exemplo n.º 9
0
def test_bad_initializer() -> None:
    """Assert on attempt to create Zoon with wrong initializer type."""
    with pytest.raises(TypeError):
        Zoon({"a", "b"})  # type: ignore
Exemplo n.º 10
0
def test_bad_file() -> None:
    """Assert on attempt to create Zoon from non-file."""
    with pytest.raises(AssertionError):
        Zoon("mumblefrabitz")
Exemplo n.º 11
0
def test_init_from_zoon() -> None:
    """__init__() works correctly from Zoon."""
    zoon = Zoon("00001100")
    zoon2 = Zoon(zoon)
    assert zoon2.byteseq == zoon.byteseq
Exemplo n.º 12
0
def test_init_from_string() -> None:
    """__init__() works correctly from string."""
    zoon = Zoon("000011")
    assert "%r" % zoon == "zoon.Zoon('000011')"
Exemplo n.º 13
0
def test_mutate_and_run(tmpdir) -> None:
    """mutate_and_run does that."""
    zoon = Zoon(pwhich("true"))
    result = zoon.mutate_and_run(0, Path(tmpdir), "--help", 1)
    assert result[1] != "success"
Exemplo n.º 14
0
def test_run(tmp_path) -> None:
    """Run a zoon."""
    expected = (0, "success", "", "")
    true = pwhich("true")
    zoon = Zoon(true)
    assert zoon.run(tmp_path / "true") == expected
Exemplo n.º 15
0
def test_init_from_file() -> None:
    """__init__() works correctly from string."""
    true = pwhich("true")
    zoon = Zoon(true)
    assert "%r" % zoon == f"zoon.Zoon('{true}')"
Exemplo n.º 16
0
def test_write_bad_dir() -> None:
    """Write to a non-existent path."""
    true = pwhich("true")
    zoon = Zoon(true)
    with pytest.raises(FileNotFoundError):
        zoon.write(Path("/u/jane/me/tarzan"))
Exemplo n.º 17
0
def test_bad_string() -> None:
    """Assert on attempt to create Zoon from non-binary string."""
    with pytest.raises(AssertionError):
        Zoon("abcdefgh")