Skip to content

Commit

Permalink
move get_bit_string() into the test
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaskula committed Jul 26, 2022
1 parent f39396d commit 2077c19
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
7 changes: 0 additions & 7 deletions software/contrib/turing_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
MAX_OUTPUT_VOLTAGE = europi.MAX_OUTPUT_VOLTAGE


def _mask(n):
return (1 << n) - 1


class TuringMachine:
def __init__(self, bit_count=DEFAULT_BIT_COUNT, max_output_voltage=MAX_OUTPUT_VOLTAGE):
"""Create a new TuringMachine with a shift register of the specified bit count. Default is 16, minimum is 8."""
Expand All @@ -58,9 +54,6 @@ def __init__(self, bit_count=DEFAULT_BIT_COUNT, max_output_voltage=MAX_OUTPUT_VO
self.write_getter = lambda: self._write
self.step_handler = lambda: None

def get_bit_string(self):
return f"{self.bits:0{self.bit_count}b}"

def rotate_bits(self):
self.bits = ((self.bits << 1) % (1 << self.bit_count)) | ((self.bits >> (self.length - 1)) & 1)

Expand Down
33 changes: 15 additions & 18 deletions software/tests/contrib/test_turing_machine.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import pytest

try:
from contrib.turing_machine import TuringMachine, MAX_OUTPUT_VOLTAGE, DEFAULT_BIT_COUNT
except ImportError:
from turing_machine import TuringMachine, MAX_OUTPUT_VOLTAGE, DEFAULT_BIT_COUNT


@pytest.fixture
def turing_machine():
tm = TuringMachine()
tm.bits = 0b1100110011110000 # set the bits to a known value
return tm
from turing_machine import TuringMachine, MAX_OUTPUT_VOLTAGE, DEFAULT_BIT_COUNT


def tm(bit_count, starting_bits):
Expand All @@ -19,6 +9,13 @@ def tm(bit_count, starting_bits):
return tm


@pytest.fixture
def turing_machine():
return tm(16, 0b1100110011110000) # set the bits to a known value

def get_bit_string(tm):
return f"{tm.bits:0{tm.bit_count}b}"

def test_bad_bit_count():
with pytest.raises(ValueError, match=r"4") as e:
tm = TuringMachine(4)
Expand All @@ -43,9 +40,9 @@ def test_rotate_bits(bit_count, length, starting_bits, expected1, expected2):
turing_machine = tm(bit_count, starting_bits)
turing_machine.length = length
turing_machine.rotate_bits()
assert turing_machine.get_bit_string() == expected1
assert get_bit_string(turing_machine) == expected1
turing_machine.rotate_bits()
assert turing_machine.get_bit_string() == expected2
assert get_bit_string(turing_machine) == expected2


@pytest.mark.parametrize(
Expand Down Expand Up @@ -98,25 +95,25 @@ def test_get_voltage(turing_machine):
def test_locked_loop(turing_machine):
for i in range(16 * 100): # step until we're back where we started many times
turing_machine.step()
assert turing_machine.get_bit_string() == "1100110011110000"
assert get_bit_string(turing_machine) == "1100110011110000"


def test_mobius_loop(turing_machine):
turing_machine.flip_probability = 100
for _ in range(100):
for i in range(16): # step halfway through
turing_machine.step()
assert turing_machine.get_bit_string() == "0011001100001111" # inverted
assert get_bit_string(turing_machine) == "0011001100001111" # inverted
for i in range(16): # step back to the beginning
turing_machine.step()
assert turing_machine.get_bit_string() == "1100110011110000"
assert get_bit_string(turing_machine) == "1100110011110000"


def test_random_loop(turing_machine):
turing_machine.flip_probability = 50
for _ in range(100):
turing_machine.step()
assert turing_machine.get_bit_string() != "1100110011110000"
assert get_bit_string(turing_machine) != "1100110011110000"
# technically, we _could_ randomply end up back at our starting sequence, but probably not in 100 steps


Expand Down Expand Up @@ -188,4 +185,4 @@ def test_write(turing_machine):
turing_machine.write = True
for _ in range(16):
turing_machine.step()
assert turing_machine.get_bit_string() == "0000000000000000"
assert get_bit_string(turing_machine) == "0000000000000000"

0 comments on commit 2077c19

Please sign in to comment.