PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lab_1b/lab1b-tester.pl

https://github.com/mvismonte/cs111_lab1
Perl | 345 lines | 248 code | 71 blank | 26 comment | 9 complexity | 30226c3bf1a3d1d6661b3be3b8b30b6f MD5 | raw file
  1. #! /usr/bin/perl -w
  2. open(FOO, "main-a.c") || die "Did you delete main-a.c?";
  3. $lines = 0;
  4. $lines++ while defined($_ = <FOO>);
  5. close FOO;
  6. $rev = 'rev';
  7. if (-d '/w/class.1/cs/cs111/cbin/bin') {
  8. $ENV{'PATH'} = "/w/class.1/cs/cs111/cbin/bin:" . $ENV{'PATH'};
  9. }
  10. @tests = (
  11. # Execute
  12. [ 'Test 1 (Basic Execution)',
  13. 'echo Hooray',
  14. 'Hooray' ],
  15. [ 'Test 2',
  16. 'echo Semi ;',
  17. 'Semi' ],
  18. [ 'Test 3',
  19. 'echo Line & sleep 1',
  20. 'Line' ],
  21. [ 'Test 4',
  22. 'echo "" """" EOL',
  23. 'EOL' ],
  24. [ 'Test 5',
  25. 'echo He"llo wo"rld',
  26. 'Hello world' ],
  27. [ 'Test 6',
  28. 'echo Hello World',
  29. 'Hello World' ],
  30. # Use a vertical tab to avoid tab completion
  31. [ 'Test 7',
  32. 'echo Hello World',
  33. 'Hello World' ],
  34. [ 'Test 8',
  35. 'echo Pipe | cat',
  36. 'Pipe' ],
  37. # Conditional
  38. [ 'Test 9 (Conditionals)',
  39. 'true && echo True',
  40. 'True' ],
  41. [ 'Test 10',
  42. 'echo True || echo False',
  43. 'True' ],
  44. [ 'Test 11',
  45. 'grep -cv NotThere main-a.c && echo False',
  46. "$lines False" ],
  47. [ 'Test 12',
  48. 'false || echo True',
  49. 'True' ],
  50. [ 'Test 13',
  51. 'true && false || true && echo Good',
  52. 'Good' ],
  53. [ 'Test 14',
  54. 'echo True && false || false && echo Bad',
  55. 'True' ],
  56. # Test that conditional status is inherited through parentheses
  57. [ 'Test 15',
  58. '( ( false || echo False && false ) || ( echo Inherited ) ) && echo Nested',
  59. 'False Inherited Nested' ],
  60. # Pipe
  61. [ 'Test 16 (Pipes)',
  62. 'echo Good | grep G',
  63. 'Good' ],
  64. [ 'Test 17',
  65. 'echo Bad | grep -c G',
  66. '0' ],
  67. [ 'Test 18',
  68. 'echo Line | cat | wc -l',
  69. '1' ],
  70. [ 'Test 19',
  71. "echo GoHangASalamiImALasagnaHog | $rev | tee temp.out | $rev | $rev",
  72. 'goHangasaLAmIimalaSAgnaHoG' ],
  73. [ 'Test 20',
  74. "$rev temp.out | $rev",
  75. 'goHangasaLAmIimalaSAgnaHoG' ],
  76. [ 'Test 21',
  77. 'cat temp.out | tr [A-Z] [a-z] | md5sum | tr -d -',
  78. '8e21d03f7955611616bcd2337fe9eac1' ],
  79. [ 'Test 22',
  80. "$rev temp.out | md5sum | tr [a-z] [A-Z] | tr -d -",
  81. '502B109B37EC769342948826736FA063' ],
  82. # Sequential
  83. [ 'Test 23 (Sequential Execution)',
  84. 'echo Hello ; echo There',
  85. 'Hello There' ],
  86. [ 'Test 24',
  87. 'echo Hello ; echo There ; echo Who ; echo Are ; echo You ; echo ?',
  88. 'Hello There Who Are You ?' ],
  89. [ 'Test 25',
  90. 'rm -f temp.out ; echo Removed',
  91. 'Removed' ],
  92. [ 'Test 26',
  93. 'sleep 2 ; ps | grep sleep | grep -v grep ; echo Done',
  94. 'Done' ],
  95. # Parentheses
  96. [ 'Test 27 (Parentheses)',
  97. '( echo Line )',
  98. 'Line' ],
  99. [ 'Test 28',
  100. '( echo Hello ; echo World ) | wc -l',
  101. '2' ],
  102. [ 'Test 29',
  103. '( echo Line ; echo Line ) | uniq | wc -l',
  104. '1' ],
  105. [ 'Test 30',
  106. '( echo Hello -" ) " World " ( "- ; echo Next ) | cat',
  107. 'Hello - ) World ( - Next' ],
  108. [ 'Test 31',
  109. '( true ) && echo True',
  110. 'True' ],
  111. [ 'Test 32',
  112. '( false || true ) && echo True',
  113. 'True' ],
  114. [ 'Test 33',
  115. '( sleep 1 ; echo Sleep ) & echo Wake ; sleep 2',
  116. 'Wake Sleep' ],
  117. # BuildMultiLine (setup for Multiple-lines case below)
  118. [ 'Test 34 (Multi-line Scripts)',
  119. 'echo echo Line 1 > temp.out ; echo echo Line 2 | cat temp.out - > temp2.out ; mv -f temp2.out temp.out ; echo echo Line 3 | cat temp.out - > temp2.out ; mv -f temp2.out temp.out ; echo Build',
  120. 'Build' ],
  121. # Multiple-lines
  122. [ 'Test 35',
  123. './ospsh -q < temp.out',
  124. 'Line 1 Line 2 Line 3' ],
  125. # Redirection
  126. [ 'Test 36 (Redirection)',
  127. 'echo Start ; echo File > temp.out',
  128. 'Start' ],
  129. [ 'Test 37',
  130. 'cat < temp.out ; echo Done',
  131. 'File Done' ],
  132. [ 'Test 38',
  133. 'rm file_that_is_not_there 2> temp.out ; wc -l temp.out ; rm -f temp.out',
  134. '1 temp.out' ],
  135. # Test simultaneous input and output redirection
  136. [ 'Test 39',
  137. '( echo Hello ; echo Bye ) > temp.out ; cat < temp.out > temp2.out ; cat temp.out temp2.out',
  138. 'Hello Bye Hello Bye' ],
  139. [ 'Test 40',
  140. 'sort < temp.out | ( head -2 ; echo First && echo Good )',
  141. 'Bye Hello First Good' ],
  142. [ 'Test 41',
  143. 'sort < temp.out > temp2.out ; tail -2 temp2.out ; rm -f temp.out temp2.out',
  144. 'Bye Hello' ],
  145. # Background
  146. # Test that sleep is backgrounded and later commands are run
  147. [ 'Test 42 (Background)',
  148. 'sleep 2 & ps | grep sleep | grep -v grep | wc -l',
  149. '1' ],
  150. # Test parentheses
  151. [ 'Test 43',
  152. '( sleep 2 ) & ps | grep sleep | grep -v grep | wc -l',
  153. '1' ],
  154. # Test multiple background commands
  155. [ 'Test 44',
  156. '( echo Hello ; sleep 2 ) & sleep 1 ; ps | grep sleep | grep -v grep | wc -l',
  157. 'Hello 1' ],
  158. [ 'Test 45',
  159. '( echo Hello ; sleep 2 & echo Bye ) ; ps | grep sleep | grep -v grep | wc -l',
  160. 'Hello Bye 1' ],
  161. # cd
  162. [ 'Test 46 (cd)',
  163. 'cd / ; pwd',
  164. '/' ],
  165. [ 'Test 47',
  166. 'cd / ; cd /usr ; pwd',
  167. '/usr' ],
  168. # cd without redirecting stdout
  169. [ 'Test 48',
  170. 'cd / ; cd /doesnotexist 2> /dev/null ; pwd',
  171. '/' ],
  172. [ 'Test 49',
  173. 'cd / ; ( cd /tmp ) ; pwd',
  174. '/' ],
  175. # Fancy conditionals
  176. [ 'Test 50',
  177. 'cd / && pwd',
  178. '/' ],
  179. [ 'Test 51',
  180. 'echo go ; cd /doesnotexist 2> /dev/null > /dev/null && pwd',
  181. 'go' ],
  182. [ 'Test 52',
  183. 'cd /doesnotexist 2> /dev/null > /dev/null || echo does not exist',
  184. 'does not exist' ],
  185. [ 'Test 53',
  186. 'cd /tmp && cd / && pwd',
  187. '/' ],
  188. [ 'Test 54',
  189. 'cd / ; ( cd /doesnotexist1 2> /dev/null > /dev/null || cd / || cd /doesnotexist2 2> /dev/null > /dev/null ) && pwd',
  190. '/' ],
  191. [ 'Test 55',
  192. 'cd / ; cd /doesnotexist 2> /dev/null > /dev/null ; pwd',
  193. '/' ],
  194. # BuildMultiLine (setup for Exit case below)
  195. [ 'Test 56 (exit)',
  196. 'echo echo Line 1 > temp.out ; echo exit | cat temp.out - > temp2.out ; mv -f temp2.out temp.out ; echo echo Line 3 | cat temp.out - > temp2.out ; mv -f temp2.out temp.out ; cat temp.out',
  197. 'echo Line 1 exit echo Line 3' ],
  198. # Exit
  199. [ 'Test 57',
  200. './ospsh -q < temp.out',
  201. 'Line 1' ],
  202. [ 'Test 58',
  203. 'echo Before ; exit ; echo After',
  204. 'Before' ],
  205. [ 'Test 59',
  206. '( exit ) ; echo After',
  207. 'After' ],
  208. # BuildMultiLine
  209. [ 'Test 60',
  210. 'rm -f temp.out ; echo echo Line 1 "&" sleep 1 > temp1.out ; echo echo Line 2 | cat temp1.out - > tempt.out ; mv -f tempt.out temp1.out ; cat temp1.out',
  211. 'echo Line 1 & sleep 1 echo Line 2' ],
  212. [ 'Test 61',
  213. 'echo "sleep 2 & ps | grep sleep | grep -v grep | wc -l" > temp2.out ; echo "sleep 3" | cat temp2.out - > tempt.out ; mv -f tempt.out temp2.out ; cat temp2.out',
  214. 'sleep 2 & ps | grep sleep | grep -v grep | wc -l sleep 3' ],
  215. [ 'Test 62',
  216. 'echo "ps | grep sleep | grep -v grep | wc -l" | cat temp2.out - > tempt.out ; mv -f tempt.out temp2.out ; cat temp2.out',
  217. 'sleep 2 & ps | grep sleep | grep -v grep | wc -l sleep 3 ps | grep sleep | grep -v grep | wc -l' ],
  218. # Zombie
  219. # Method 1
  220. [ 'Test 63 (Zombie Processes)',
  221. 'cat temp1.out | ./ospsh -q & sleep 2 ; ps | grep echo | grep -v grep | grep "<defunct>$"',
  222. 'Line 1 Line 2' ],
  223. # Method 2
  224. [ 'Test 64',
  225. './ospsh -q < temp2.out',
  226. '1 0' ],
  227. # CleanupMultiLine
  228. [ 'Test 65',
  229. 'rm -f temp1.out temp2.out ; echo clean',
  230. 'clean' ],
  231. );
  232. my($ntest) = 0;
  233. my($sh) = "./ospsh";
  234. my($tempfile) = "lab1btest.txt";
  235. my($ntestfailed) = 0;
  236. if (!-x $sh) {
  237. $editsh = $sh;
  238. $editsh =~ s,^\./,,;
  239. print STDERR "$sh does not exist, so I can't run any tests!\n(Try running \"make $editsh\" to create $sh.)\n";
  240. exit(1);
  241. }
  242. select STDOUT;
  243. $| = 1;
  244. foreach $test (@tests) {
  245. my($desc, $in, $want) = @$test;
  246. if ($ENV{'HOME'} =~ "/Users/") { # if we're in os x, change md5sum to md5
  247. $in =~ s/md5sum/md5/g;
  248. }
  249. $ntest++;
  250. print STDOUT "Running $desc...";
  251. open(F, ">$tempfile") || die;
  252. print F $in, "\n";
  253. close(F);
  254. $result = `$sh -q < $tempfile 2>&1`;
  255. print STDOUT " done\n";
  256. $result =~ s%^cs111_winter11\$ %%;
  257. $result =~ s%cs111_winter11\$ $%%;
  258. $result =~ s%^\[\d+\]\s+\d+$%%mg;
  259. $result =~ s|\[\d+\]||g;
  260. $result =~ s|^\s+||g;
  261. $result =~ s|\s+| |g;
  262. $result =~ s|\s+$||;
  263. next if $result eq $want;
  264. next if $want eq 'Syntax error [NULL]' && $result eq '[NULL]';
  265. next if $result eq $want;
  266. print STDERR "$desc FAILED!\n input was \"$in\"\n expected output like \"$want\"\n got \"$result\"\n";
  267. $ntestfailed += 1;
  268. }
  269. unlink($tempfile);
  270. my($ntestpassed) = $ntest - $ntestfailed;
  271. print "$ntestpassed of $ntest tests passed\n";
  272. exit(0);