PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lab1b-tester.pl

https://bitbucket.org/jeremythec/lab1b
Perl | 217 lines | 170 code | 21 blank | 26 comment | 3 complexity | f2439bc5d18c2612646184bd5ec49af0 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. @tests = (
  7. # Execute
  8. [ 'echo Hooray',
  9. 'Hooray' ],
  10. [ 'echo Semi ;',
  11. 'Semi' ],
  12. [ 'echo Line & sleep 1',
  13. 'Line' ],
  14. [ 'echo "" """" EOL',
  15. 'EOL' ],
  16. [ 'echo He"llo wo"rld',
  17. 'Hello world' ],
  18. [ 'echo Hello World',
  19. 'Hello World' ],
  20. # Use a vertical tab to avoid tab completion
  21. [ 'echo Hello World',
  22. 'Hello World' ],
  23. [ 'echo Pipe | cat',
  24. 'Pipe' ],
  25. # Conditional
  26. [ 'true && echo True',
  27. 'True' ],
  28. [ 'echo True || echo False',
  29. 'True' ],
  30. [ 'grep -cv NotThere main-a.c && echo False',
  31. "$lines False" ],
  32. [ 'false || echo True',
  33. 'True' ],
  34. [ 'true && false || true && echo Good',
  35. 'Good' ],
  36. [ 'echo True && false || false && echo Bad',
  37. 'True' ],
  38. # Test that conditional status is inherited through parentheses
  39. [ '( ( false || echo False && false ) || ( echo Inherited ) ) && echo Nested',
  40. 'False Inherited Nested' ],
  41. # Pipe
  42. [ 'echo Good | grep G',
  43. 'Good' ],
  44. [ 'echo Bad | grep -c G',
  45. '0' ],
  46. [ 'echo Line | cat | wc -l',
  47. '1' ],
  48. [ 'echo GoHangASalamiImALasagnaHog | rev | tee temp.out | rev | rev',
  49. 'goHangasaLAmIimalaSAgnaHoG' ],
  50. [ 'rev temp.out | rev',
  51. 'goHangasaLAmIimalaSAgnaHoG' ],
  52. [ 'cat temp.out | tr [A-Z] [a-z] | md5sum',
  53. '8e21d03f7955611616bcd2337fe9eac1 -' ],
  54. [ 'rev temp.out | md5sum | tr [a-z] [A-Z]',
  55. '502B109B37EC769342948826736FA063 -' ],
  56. # Sequential
  57. [ 'echo Hello ; echo There',
  58. 'Hello There' ],
  59. [ 'echo Hello ; echo There ; echo Who ; echo Are ; echo You ; echo ?',
  60. 'Hello There Who Are You ?' ],
  61. [ 'rm -f temp.out ; echo Removed',
  62. 'Removed' ],
  63. [ 'sleep 2 ; ps -C sleep ; echo Done',
  64. 'PID TTY TIME CMD Done' ],
  65. # Parentheses
  66. [ '( echo Line )',
  67. 'Line' ],
  68. [ '( echo Hello ; echo World ) | wc -l',
  69. '2' ],
  70. [ '( echo Line ; echo Line ) | uniq | wc -l',
  71. '1' ],
  72. [ '( echo Hello -" ) " World " ( "- ; echo Next ) | cat',
  73. 'Hello - ) World ( - Next' ],
  74. [ '( true ) && echo True',
  75. 'True' ],
  76. [ '( false || true ) && echo True',
  77. 'True' ],
  78. [ '( sleep 1 ; echo Sleep ) & echo Wake ; sleep 2',
  79. 'Wake Sleep' ],
  80. # BuildMultiLine (setup for Multiple-lines case below)
  81. [ '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',
  82. 'Build' ],
  83. # Multiple-lines
  84. [ './ospsh -q < temp.out',
  85. 'Line 1 Line 2 Line 3' ],
  86. # Redirection
  87. [ 'echo Start ; echo File > temp.out',
  88. 'Start' ],
  89. [ 'cat < temp.out ; echo Done',
  90. 'File Done' ],
  91. [ 'rm file_that_is_not_there 2> temp.out ; wc -l temp.out ; rm -f temp.out',
  92. '1 temp.out' ],
  93. # Test simultaneous input and output redirection
  94. [ '( echo Hello ; echo Bye ) > temp.out ; cat < temp.out > temp2.out ; cat temp.out temp2.out',
  95. 'Hello Bye Hello Bye' ],
  96. [ 'sort < temp.out | ( head -n 2 ; echo First && echo Good )',
  97. 'Bye Hello First Good' ],
  98. [ 'sort < temp.out > temp2.out ; tail -n 2 temp2.out ; rm -f temp.out temp2.out',
  99. 'Bye Hello' ],
  100. # Background
  101. # Test that sleep is backgrounded and later commands are run
  102. [ 'sleep 2 & ps -C sleep | wc -l',
  103. '2' ],
  104. # Test parentheses
  105. [ '( sleep 2 ) & ps -C sleep | wc -l',
  106. '2' ],
  107. # Test multiple background commands
  108. [ '( echo Hello ; sleep 2 ) & sleep 1 ; ps -C sleep | wc -l',
  109. 'Hello 2' ],
  110. [ '( echo Hello ; sleep 2 & echo Bye ) ; ps -C sleep | wc -l',
  111. 'Hello Bye 2' ],
  112. # cd
  113. [ 'cd / ; pwd',
  114. '/' ],
  115. [ 'cd / ; cd /tmp ; pwd',
  116. '/tmp' ],
  117. # cd without redirecting stdout
  118. [ 'cd / ; cd /doesnotexist 2> /dev/null ; pwd',
  119. '/' ],
  120. [ 'cd / ; ( cd /tmp ) ; pwd',
  121. '/' ],
  122. # Fancy conditionals
  123. [ 'cd / && pwd',
  124. '/' ],
  125. [ 'echo go ; cd /doesnotexist 2> /dev/null > /dev/null && pwd',
  126. 'go' ],
  127. [ 'cd /doesnotexist 2> /dev/null > /dev/null || echo does not exist',
  128. 'does not exist' ],
  129. [ 'cd /tmp && cd / && pwd',
  130. '/' ],
  131. [ 'cd / ; ( cd /doesnotexist1 2> /dev/null > /dev/null || cd / || cd /doesnotexist2 2> /dev/null > /dev/null ) && pwd',
  132. '/' ],
  133. [ 'cd / ; cd /doesnotexist 2> /dev/null > /dev/null ; pwd',
  134. '/' ],
  135. # BuildMultiLine (setup for Exit case below)
  136. [ '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',
  137. 'echo Line 1 exit echo Line 3' ],
  138. # Exit
  139. [ './ospsh -q < temp.out',
  140. 'Line 1' ],
  141. [ 'echo Before ; exit ; echo After',
  142. 'Before' ],
  143. [ '( exit ) ; echo After',
  144. 'After' ],
  145. # BuildMultiLine
  146. [ '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',
  147. 'echo Line 1 & sleep 1 echo Line 2' ],
  148. [ 'echo "sleep 2 & ps -C sleep | wc -l" > temp2.out ; echo "sleep 3" | cat temp2.out - > tempt.out ; mv -f tempt.out temp2.out ; cat temp2.out',
  149. 'sleep 2 & ps -C sleep | wc -l sleep 3' ],
  150. [ 'echo "ps -C sleep | wc -l" | cat temp2.out - > tempt.out ; mv -f tempt.out temp2.out ; cat temp2.out',
  151. 'sleep 2 & ps -C sleep | wc -l sleep 3 ps -C sleep | wc -l' ],
  152. # Zombie
  153. # Method 1
  154. [ 'cat temp1.out | ./ospsh -q & sleep 2 ; ps -C echo | grep "<defunct>$"',
  155. 'Line 1 Line 2' ],
  156. # Method 2
  157. [ './ospsh -q < temp2.out',
  158. '2 1' ],
  159. # CleanupMultiLine
  160. [ 'rm -f temp1.out temp2.out ; echo clean',
  161. 'clean' ],
  162. );
  163. my($ntest) = 0;
  164. my($sh) = "./ospsh";
  165. my($tempfile) = "lab1btest.txt";
  166. my($ntestfailed) = 0;
  167. if (!-x $sh) {
  168. print STDERR "$sh does not exist, so I can't run any tests!\n(Try running \"gmake\" to create $sh.)\n";
  169. exit(1);
  170. }
  171. foreach $test (@tests) {
  172. $ntest++;
  173. print STDOUT "Starting test $ntest\n";
  174. my($in, $want) = @$test;
  175. open(F, ">$tempfile") || die;
  176. print F $in, "\n";
  177. close(F);
  178. $result = `$sh -q < $tempfile 2>&1`;
  179. $result =~ s%^cs111_fall07\$ %%;
  180. $result =~ s%cs111_fall07\$ $%%;
  181. $result =~ s|\[\d+\]||g;
  182. $result =~ s|^\s+||g;
  183. $result =~ s|\s+| |g;
  184. $result =~ s|\s+$||;
  185. next if $result eq $want;
  186. next if $want eq 'Syntax error [NULL]' && $result eq '[NULL]';
  187. next if $result eq $want;
  188. print STDERR "Test $ntest FAILED!\n input was \"$in\"\n expected output like \"$want\"\n got \"$result\"\n";
  189. $ntestfailed += 1;
  190. }
  191. unlink($tempfile);
  192. my($ntestpassed) = $ntest - $ntestfailed;
  193. print "$ntestpassed of $ntest tests passed\n";
  194. exit(0);