Ejemplo n.º 1
0
    def test_ADD(self):
        cardiac = Cardiac()
        cardiac.memory[42] = 9998

        cardiac._ADD(42)
        self.assertEqual(9998, cardiac.accumulator)

        cardiac._ADD(0)
        self.assertEqual(9999, cardiac.accumulator)

        cardiac._ADD(0)
        self.assertEqual(0, cardiac.accumulator)
Ejemplo n.º 2
0
 def test_init(self):
     cardiac = Cardiac()
     self.assertEqual([], cardiac.input)
     self.assertEqual([], cardiac.output)
     self.assertEqual([1] + [None] * 99, cardiac.memory)
     self.assertEqual(0, cardiac.accumulator)
     self.assertEqual(0, cardiac.program_counter)
Ejemplo n.º 3
0
    def test_TAC(self):
        cardiac = Cardiac()
        cardiac._TAC(42)
        self.assertEqual(0, cardiac.program_counter)

        cardiac.accumulator = -1
        cardiac._TAC(42)
        self.assertEqual(42, cardiac.program_counter)
Ejemplo n.º 4
0
    def test_CLA(self):
        cardiac = Cardiac()
        cardiac._CLA(0)
        self.assertEqual(1, cardiac.accumulator)

        cardiac.memory[42] = 100
        cardiac._CLA(42)
        self.assertEqual(100, cardiac.accumulator)
Ejemplo n.º 5
0
 def test_JMP(self):
     cardiac = Cardiac()
     cardiac.load(["842"])
     cardiac.step()
     cardiac.step()
     self.assertEqual(42, cardiac.program_counter)
     self.assertEqual([1, 842] + [None] * 97 + [802], cardiac.memory)
Ejemplo n.º 6
0
 def test_SUB(self):
     cardiac = Cardiac()
     cardiac._SUB(0)
     self.assertEqual(-1, cardiac.accumulator)
Ejemplo n.º 7
0
 def test_STO(self):
     cardiac = Cardiac()
     cardiac.accumulator = 100
     cardiac._STO(42)
     self.assertEqual([1] + [None] * 41 + [100] + [None] * 57,
                      cardiac.memory)
Ejemplo n.º 8
0
 def test_OUT(self):
     cardiac = Cardiac()
     cardiac.memory[42] = 100
     cardiac._OUT(0)
     cardiac._OUT(42)
     self.assertEqual(["001", "100"], cardiac.output)
Ejemplo n.º 9
0
    def test_SFT(self):
        cardiac = Cardiac()
        cardiac.accumulator = 42

        cardiac._SFT(0)
        self.assertEqual(42, cardiac.accumulator)

        cardiac._SFT(20)
        self.assertEqual(4200, cardiac.accumulator)

        cardiac._SFT(13)
        self.assertEqual(2, cardiac.accumulator)

        cardiac._SFT(1)
        self.assertEqual(0, cardiac.accumulator)
Ejemplo n.º 10
0
 def test_INP(self):
     cardiac = Cardiac()
     cardiac.load(["100"])
     cardiac._INP(42)
     self.assertEqual([1] + [None] * 41 + [100] + [None] * 57,
                      cardiac.memory)
Ejemplo n.º 11
0
 def test_run(self):
     cardiac = Cardiac()
     cardiac.load(["002", "003", "942"])
     cardiac.run()
     self.assertEqual([1, 2, 3, 942] + [None] * 96, cardiac.memory)
     self.assertEqual(42, cardiac.program_counter)
Ejemplo n.º 12
0
 def test_HRS(self):
     cardiac = Cardiac()
     with (self.assertRaises(HaltException)):
         cardiac._HRS(42)
     self.assertEqual(42, cardiac.program_counter)
Ejemplo n.º 13
0
from argparse import ArgumentParser

from cardiac import Cardiac

if __name__ == "__main__":
    parser = ArgumentParser(description="CARDIAC interpreter written in Python")
    parser.add_argument("program", type=str, help="Program")
    args = parser.parse_args()

    with open(args.program) as f:
        program = [line.strip() for line in f]

    cardiac = Cardiac()
    cardiac.load(program)
    output = cardiac.run()
    print("\n".join(output))