Source for file index.php

Documentation is available at index.php

  1. <?php
  2. /**
  3. * PHPTuring test file
  4. *
  5. * @author Simon Harris - pointbeing at users.sourceforge.net
  6. * @package PHPTuring
  7. * @subpackage Tests
  8. * @version $Id: index.php,v 1.2 2005/11/15 10:30:40 pointbeing Exp $
  9. * @todo Unbundle test cases into seperate files
  10. */
  11.  
  12. error_reporting(E_ALL);
  13.  
  14. /**#@+*/
  15. ('/usr/local/simpletest/unit_tester.php');
  16. require_once('/usr/local/simpletest/mock_objects.php');
  17. require_once('/usr/local/simpletest/reporter.php');
  18.  
  19. #require_once('/etc/simpletest/unit_tester.php');
  20. #require_once('/etc/simpletest/mock_objects.php');
  21. #require_once('/etc/simpletest/reporter.php');
  22.  
  23.  
  24.  
  25. require_once '../all.php';
  26. require_once '../implementation/simple.php';
  27. /**#@-*/* TapeTestCase class
  28. *
  29. * @package PHPTuring
  30. * @subpackage Tests
  31. */
  32. class TapeTestCase extends UnitTestCase {
  33.  
  34. public function testTapeStoresCellValuesCorrectly()
  35. {
  36. $tape = new Tape();
  37. $tape->write(3, 'House');
  38. $this->assertIdentical($tape->read(3), 'House');
  39. $tape->write(-1, 'Car');
  40. $this->assertIdentical($tape->read(-1), 'Car');
  41. $tape->write(400, 456);
  42. $this->assertIdentical($tape->read(400), 456);
  43. }
  44. public function testTapeReturnsBlankForUnsetCells()
  45. {
  46. $tape = new Tape();
  47. $this->assertIdentical($tape->read(400), '');
  48. $this->assertIdentical($tape->read(-345), '');
  49. }
  50. }
  51.  
  52.  
  53. /**
  54. * HeadTestCase class
  55. *
  56. * @package PHPTuring
  57. * @subpackage Tests
  58. */
  59. class HeadTestCase extends UnitTestCase {
  60.  
  61. public function testReadandWriteWithBlankTape()
  62. {
  63. $head = new Head();
  64. $head->shiftLeft();
  65. $this->assertIdentical($head->read(), '');
  66. $head->shiftLeft();
  67. $head->shiftLeft();
  68. $head->shiftLeft();
  69. $head->shiftLeft();
  70. $head->shiftLeft();
  71. $head->shiftLeft();
  72. $this->assertIdentical($head->read(), '');
  73.  
  74. $tape = new Tape();
  75. $head->shiftRight();
  76. $this->assertIdentical($head->read(), '');
  77. $head->shiftRight();
  78. $head->shiftRight();
  79. $head->shiftRight();
  80. $head->shiftRight();
  81. $head->shiftRight();
  82. $head->shiftRight();
  83. $this->assertIdentical($head->read(), '');
  84. $head->write('car');
  85. $this->assertIdentical($head->read(), 'car');
  86. $head->shiftRight();
  87. $head->shiftRight();
  88. $head->shiftLeft();
  89. $head->shiftLeft();
  90. $this->assertIdentical($head->read(), 'car');
  91. }
  92.  
  93. public function testReadWithPredefinedTape()
  94. {
  95. $head = new Head();
  96. $tape = new Tape();
  97. $tape->write(1, 'Hi');
  98. $tape->write(-1, 'Bellows');
  99. $tape->write(-10, '*');
  100. $head->setTape($tape);
  101. $head->shiftRight();
  102. $this->assertIdentical($head->read(), 'Hi');
  103. $head->shiftLeft();
  104. $head->shiftLeft();
  105. $this->assertIdentical($head->read(), 'Bellows');
  106. $head->shiftLeft();
  107. $head->shiftLeft();
  108. $head->shiftLeft();
  109. $head->shiftLeft();
  110. $head->shiftLeft();
  111. $head->shiftLeft();
  112. $head->shiftLeft();
  113. $head->shiftLeft();
  114. $head->shiftLeft();
  115. $this->assertIdentical($head->read(), '*');
  116. }
  117. }
  118.  
  119.  
  120. /**
  121. * InstructionTestCase class
  122. *
  123. * @package PHPTuring
  124. * @subpackage Tests
  125. */
  126. class InstructionTestCase extends UnitTestCase {
  127.  
  128. public function testInstructionValuesInitialisedCorrectly()
  129. {
  130. $inst = new Instruction('Inst1', 0, 1, Instruction::MOVE_R, 'Inst2');
  131. $this->assertIdentical($inst->getInitialState(), 'Inst1');
  132. $this->assertIdentical($inst->getPrerequisite(), 0);
  133. $this->assertIdentical($inst->getSymbolToWrite(), 1);
  134. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_R);
  135. $this->assertIdentical($inst->getNextState(), 'Inst2');
  136.  
  137. $inst = new Instruction('Spongebob', 1, 0, Instruction::MOVE_L, 'Turtles');
  138. $this->assertIdentical($inst->getInitialState(), 'Spongebob');
  139. $this->assertIdentical($inst->getPrerequisite(), 1);
  140. $this->assertIdentical($inst->getSymbolToWrite(), 0);
  141. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_L);
  142. $this->assertIdentical($inst->getNextState(), 'Turtles');
  143. }
  144. }
  145.  
  146.  
  147. /**
  148. * ProgramTestCase class
  149. *
  150. * @package PHPTuring
  151. * @subpackage Tests
  152. */
  153. class ProgramTestCase extends UnitTestCase {
  154.  
  155. public function testFindingInstructionsByStateandCurrentCellValue()
  156. {
  157. $program = new Program();
  158.  
  159. $cmd = new Instruction('S1', 1, 1, Instruction::MOVE_R, 'S2');
  160. $program->addInstruction($cmd);
  161. $cmd2 = new Instruction('S2', 0, 'B', Instruction::MOVE_L, 'S1');
  162. $program->addInstruction($cmd2);
  163. $inst = $program->getInstruction('S1', 1);
  164. $this->assertIdentical($inst->getSymbolToWrite(), 1);
  165. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_R);
  166. $this->assertIdentical($inst->getNextState(), 'S2');
  167. $inst = $program->getInstruction('S2', 0);
  168. $this->assertIsA($inst, 'Instruction');
  169. $this->assertIdentical($inst->getSymbolToWrite(), 'B');
  170. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_L);
  171. $this->assertIdentical($inst->getNextState(), 'S1');
  172. $this->assertFalse($program->getInstruction('s13', 4));
  173. }
  174. }
  175.  
  176.  
  177. /**
  178. * CompilerTestCase class
  179. *
  180. * @package PHPTuring
  181. * @subpackage Tests
  182. */
  183. class CompilerTestCase extends UnitTestCase {
  184.  
  185. public function testCompilingOneLineIntoProgram()
  186. {
  187. $compiler = new SimpleCompiler();
  188. $program = $compiler->compile("a|B|0|R|b");
  189. $this->assertIsA($program, 'Program');
  190. $inst = $program->getInstruction('a', 'B');
  191. $this->assertIsA($inst, 'Instruction');
  192. $this->assertIdentical($inst->getSymbolToWrite(), '0');
  193. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_R);
  194. $this->assertIdentical($inst->getNextState(), 'b');
  195. }
  196.  
  197. public function testCompilingSeveralLinesIntoProgram()
  198. {
  199. $compiler = new SimpleCompiler();
  200. $program = $compiler->compile("a|0|0|R|b\nb|0|0|R|c\nc|0|1|L|d\nd|0|0|R|a");
  201. $this->assertIsA($program, 'Program');
  202. $inst = $program->getInstruction('c', '0');
  203. $this->assertIsA($inst, 'Instruction');
  204. $this->assertIdentical($inst->getSymbolToWrite(), '1');
  205. $this->assertIdentical($inst->getNextMove(), Instruction::MOVE_L);
  206. $this->assertIdentical($inst->getNextState(), 'd');
  207. }
  208. }
  209.  
  210.  
  211. /**
  212. * MachineTestCase class
  213. *
  214. * @package PHPTuring
  215. * @subpackage Tests
  216. */
  217. class MachineTestCase extends UnitTestCase {
  218.  
  219. public function testDefaultInitialStateisZero()
  220. {
  221. $machine = new Machine();
  222. $this->assertIdentical($machine->getState(), '0');
  223. }
  224. public function testInitialStateCanBeOverriden()
  225. {
  226. $machine = new Machine('s0');
  227. $this->assertIdentical($machine->getState(), 's0');
  228. }
  229. public function testRunningMachine()
  230. {
  231. $machine = new Machine('a');
  232. $compiler = new SimpleCompiler();
  233. $machine->run($compiler->compile("a||0|R|b"));
  234. $this->assertIdentical($machine->getState(), 'b');
  235.  
  236. $machine = new Machine('s0');
  237. $machine->run($compiler->compile("s0||1||s0"));
  238. $this->assertIdentical($machine->getState(), 's0');
  239.  
  240. $machine = new Machine('s0');
  241. $machine->run($compiler->compile("s0||1|R|s1\ns1||1|L|s2\ns2|1|||s3"));
  242. $this->assertIdentical($machine->getState(), 's3');
  243. // would be nice to add a couple more examples here
  244. }
  245. }
  246.  
  247.  
  248. /**
  249. * TapeParserTestCase class
  250. *
  251. * @package PHPTuring
  252. * @subpackage Tests
  253. */
  254. class TapeParserTestCase extends UnitTestCase {
  255.  
  256. public function testMyExampleStringisParsedCorrectly()
  257. {
  258. $tp = new SimpleTapeParser();
  259. $tape = $tp->parse('|Foo|||1|0||1|0|0');
  260. $this->assertIsA($tape, 'Tape');
  261. $head = new Head();
  262. $head->setTape($tape);
  263. $this->assertIdentical($head->read(), '');
  264. $head->shiftRight();
  265. $this->assertIdentical($head->read(), 'Foo');
  266. $head->shiftRight();
  267. $this->assertIdentical($head->read(), '');
  268. $head->shiftRight();
  269. $this->assertIdentical($head->read(), '');
  270. $head->shiftRight();
  271. $this->assertIdentical($head->read(), '1');
  272. $head->shiftRight();
  273. $this->assertIdentical($head->read(), '0');
  274. $head->shiftRight();
  275. $this->assertIdentical($head->read(), '');
  276. $head->shiftRight();
  277. $this->assertIdentical($head->read(), '1');
  278. $head->shiftRight();
  279. $this->assertIdentical($head->read(), '0');
  280. $head->shiftRight();
  281. $this->assertIdentical($head->read(), '0');
  282. $head->shiftRight();
  283. $this->assertIdentical($head->read(), '');
  284. $head->shiftRight();
  285. $this->assertIdentical($head->read(), '');
  286. }
  287. }
  288.  
  289.  
  290. /**
  291. * ObservationTestCase class
  292. *
  293. * @package PHPTuring
  294. * @subpackage Tests
  295. */
  296. class ObservationTestCase extends UnitTestCase {
  297. public function testObserverIsNotifiedCorrectly_OneStep()
  298. {
  299. $prog = "0||1||0";
  300.  
  301. $machine = new Machine();
  302. Mock::generate('SimpleDebugger');
  303. $observer = new MockSimpleDebugger($this);
  304. $observer2 = new MockSimpleDebugger($this);
  305. $machine->registerObserver($observer);
  306. $machine->registerObserver($observer2);
  307.  
  308. $compiler = new SimpleCompiler();
  309. $parser = new SimpleTapeParser();
  310. $observer->expectOnce('notify');
  311. $observer2->expectOnce('notify');
  312. $machine->run($compiler->compile($prog));
  313. $observer->tally();
  314. $observer2->tally();
  315. }
  316.  
  317. public function testObserverIsNotifiedCorrectly_ThreeSteps()
  318. {
  319. $prog = "0||1||0\n0|1|0||1\n1|0|1||stop";
  320.  
  321. $machine = new Machine();
  322. Mock::generate('SimpleDebugger');
  323. $observer = new MockSimpleDebugger($this);
  324. $observer2 = new MockSimpleDebugger($this);
  325. $machine->registerObserver($observer);
  326. $machine->registerObserver($observer2);
  327.  
  328. $compiler = new SimpleCompiler();
  329. $observer->expectCallCount('notify', 3);
  330. $observer2->expectCallCount('notify', 3);
  331. $machine->run($compiler->compile($prog));
  332. $observer->tally();
  333. $observer2->tally();
  334. }
  335.  
  336. // this is pretty partial, rather than become brittle
  337. public function testDebugger()
  338. {
  339. $debugger = new SimpleDebugger();
  340. $prog = "0||1||0\n0|1|0||1\n1|0|1||stop";
  341. $machine = new Machine();
  342. $compiler = new SimpleCompiler();
  343. $debugger->watch($machine);
  344. }
  345. }
  346.  
  347.  
  348.  
  349. $test = new GroupTest('All Tests');
  350. $test->addTestClass(new TapeTestCase());
  351. $test->addTestClass(new HeadTestCase());
  352. $test->addTestClass(new InstructionTestCase());
  353. $test->addTestClass(new ProgramTestCase());
  354. $test->addTestClass(new CompilerTestCase());
  355. $test->addTestClass(new MachineTestCase());
  356. $test->addTestClass(new TapeParserTestCase());
  357. $test->addTestClass(new ObservationTestCase());
  358. $test->run(new HtmlReporter());
  359.  
  360.  
  361. ?>

Documentation generated on Tue, 15 Nov 2005 10:37:50 +0000 by phpDocumentor 1.3.0RC3