Skip to content

Commit

Permalink
Add test lin_rec module
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Nov 30, 2023
1 parent 2f38a40 commit 04a5aa0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ MYPYC = $(PYTHON) -m mypyc

compile : rust
$(MYPYC) --version
$(MYPYC) tm tools test/utils.py --exclude rust_stuff
$(MYPYC) tm tools test/utils.py test/lin_rec.py --exclude rust_stuff

TUR = test.test_turing.Fast
PROG = test.test_program
Expand Down
57 changes: 57 additions & 0 deletions test/lin_rec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import annotations

from typing import Self, TYPE_CHECKING

from tm.lin_rec import BeepHistory, HeadTape, LinRecMachine

if TYPE_CHECKING:
from tm.lin_rec import State, Slot, Tapes


class LinRecSampler(LinRecMachine):
history: BeepHistory

def run(
self,
sim_lim: int,
samples: Tapes,
) -> Self:
self.blanks = {}

comp = self.comp

self.tape = tape = HeadTape.init()

self.history = BeepHistory(tapes = samples)

step: int = 0
state: State = 0

for cycle in range(sim_lim or 1_000_000):
slot: Slot = state, tape.scan

if step in self.history.tapes:
self.history.add_state_at_step(step, state)
self.history.add_tape_at_step(step, tape)

if (instr := comp[slot]) is None:
self.undfnd = step, slot
break

color, shift, next_state = instr

step += tape.step(shift, color, False)

if (state := next_state) == -1:
self.halted = step
break

if not color and tape.blank and state not in self.blanks:
self.blanks[state] = step

else:
self.xlimit = step

self.cycles = cycle

return self
2 changes: 1 addition & 1 deletion test/test_turing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# pylint: disable-next = wildcard-import, unused-wildcard-import
from test.prog_data import *
from test.utils import LinRecSampler
from test.lin_rec import LinRecSampler
from test.test_num import assert_num_counts, clear_caches

from tm.reason import (
Expand Down
61 changes: 0 additions & 61 deletions test/utils.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,6 @@
from __future__ import annotations

from typing import Self, TYPE_CHECKING

from tm.lin_rec import BeepHistory, HeadTape, LinRecMachine

if TYPE_CHECKING:
from tm.lin_rec import State, Slot, Tapes

########################################

def read_progs(name: str) -> set[str]:
with open(f'test/data/{name}.prog') as holdouts:
return set(
prog.strip()
for prog in holdouts.readlines()
)

########################################

class LinRecSampler(LinRecMachine):
history: BeepHistory

def run(
self,
sim_lim: int,
samples: Tapes,
) -> Self:
self.blanks = {}

comp = self.comp

self.tape = tape = HeadTape.init()

self.history = BeepHistory(tapes = samples)

step: int = 0
state: State = 0

for cycle in range(sim_lim or 1_000_000):
slot: Slot = state, tape.scan

if step in self.history.tapes:
self.history.add_state_at_step(step, state)
self.history.add_tape_at_step(step, tape)

if (instr := comp[slot]) is None:
self.undfnd = step, slot
break

color, shift, next_state = instr

step += tape.step(shift, color, False)

if (state := next_state) == -1:
self.halted = step
break

if not color and tape.blank and state not in self.blanks:
self.blanks[state] = step

else:
self.xlimit = step

self.cycles = cycle

return self

0 comments on commit 04a5aa0

Please sign in to comment.