You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p1gradingscript 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #!/bin/bash
  2. exec > >(tee -a ./p1grading_result) 2>&1 # Copy all output to ./p1grading_result
  3. # Parse any args
  4. nocolor=1
  5. while [ $# -gt 0 ]
  6. do
  7. if [[ $1 == "-nocolor" ]]
  8. then
  9. nocolor=0
  10. fi
  11. shift
  12. done
  13. ###############################################################################
  14. # Begin helper functions
  15. # generate() - Generates a matrix
  16. # perror() - prints error messages
  17. # failed() - prints failed test message
  18. # passed() - prints passed test message
  19. # result() - if $? == 0, calls passed(), otherwise calls failed()
  20. # comment() - prints comment to output
  21. # unit() - prints unit test headers
  22. ###############################################################################
  23. function generate(){
  24. # NAME
  25. # generate - generate a matrix of a specified size
  26. # SYNOPSIS
  27. # generate ROWS COLS RANGE
  28. # DESCRIPTION
  29. # Outputs a matrix with ROWS and COLS as dimensions. If RANGE is an integer, the entries
  30. # of the matrix will be random integers in (-RANGE,+RANGE). If RANGE is a string starting
  31. # with 'Z' or 'z', the matrix will be populated with zeroes. If RANGE is a string starting
  32. # with 'O' or 'o', the matrix will be populated with ones. If RANGE is a string starting
  33. # with 'D' or 'd', the matrix will be a diagonal matrix with ones along the diagonal entries.
  34. # AUTHOR
  35. # Written by Ryan Gambord (gambordr@oregonstate.edu)
  36. [ $# -ne 3 ] && echo "invalid number of arguments" >&2 # This is equivalent to using an if statement.
  37. # The right side of the && operator will not
  38. # be evaluated unless the left side is true.
  39. for arg in $1 $2
  40. do
  41. [[ $arg =~ [^0-9]+ ]] && echo "argument '$arg' is not an integer" >&2
  42. done
  43. y=0
  44. while [ $y -lt $1 ]
  45. do
  46. x=0
  47. ((y++))
  48. while [ $x -lt $2 ]
  49. do
  50. ((x++))
  51. case $3 in
  52. [oO]*) echo -n 1;;
  53. [zZ]*) echo -n 0;;
  54. [dD]*) [ $x -eq $y ] && echo -n 1 || echo -n 0;;
  55. *) if [[ "$3" =~ [^0-9]+ ]]
  56. then
  57. echo "invalid RANGE" >&2
  58. exit 1
  59. else
  60. echo -n $(( (RANDOM-32767/2) % $3 ))
  61. fi;;
  62. esac
  63. if [ $x -ne $2 ]
  64. then
  65. echo -ne "\t"
  66. else
  67. echo
  68. fi
  69. done
  70. done
  71. }
  72. function perror(){
  73. # NAME
  74. # perror - print a stack trace and error message
  75. # SYNOPSIS
  76. # perror [STRING]
  77. # DESCRIPTION
  78. # Echoes the STRING(s) to standard error with a stack trace for debugging.
  79. # The value of the status flag $? is not changed by perror.
  80. # AUTHOR
  81. # Written by Ryan Gambord (gambordr@oregonstate.edu)
  82. status=$? # Capture status of previous command
  83. echo -e "\e[36mTraceback (most recent call last):\e[0m" >&2
  84. i=${#BASH_LINENO[@]} # Get number of items in stack trace
  85. # This section prints a stack trace of the current execution stack
  86. while
  87. [ $i -gt 0 ] # Iterate to the top of the stack
  88. do
  89. until # Loop until we get to the bottom of caller stack (this is safer than offsetting $i)
  90. ((i--))
  91. info=($(caller $i)) # Get caller info
  92. do :; done # Do nothing
  93. echo " File \"${info[2]}\", line ${info[0]}, in ${info[1]}()" >&2 # Print caller info
  94. if [ $i -ne 0 ] # Check if we're at the top of the stack (perror call is at top)
  95. then
  96. echo " "$(head "${info[2]}" -n ${info[0]} | tail -n 1) >&2 # Print trace if not at top
  97. else
  98. echo -en "\e[31mERROR\e[0m: " >&2 # Print error message if at top
  99. [ $# -gt 0 ] && echo "$*" >&2 || echo "(no error message specified)" >&2
  100. fi
  101. done
  102. exit 1
  103. }
  104. function failed(){
  105. [ $nocolor -eq 0 ] || echo -ne "\033[31m"
  106. echo -e "\tFailed: $@"
  107. [ $nocolor -eq 0 ] || echo -ne "\033[0m"
  108. }
  109. function passed(){
  110. [ $nocolor -eq 0 ] || echo -ne "\033[32m"
  111. echo -e "\tPassed: $@"
  112. [ $nocolor -eq 0 ] || echo -ne "\033[0m"
  113. }
  114. function result(){
  115. val=$?
  116. pts=$2
  117. if [ $# -lt 2 ]
  118. then
  119. pts=1
  120. fi
  121. if [ $val -eq 0 ]
  122. then
  123. passed "$1" "($pts/$pts)"
  124. ((score += pts))
  125. ((total += pts))
  126. else
  127. failed "$1" "(0/$pts)"
  128. ((total += pts))
  129. fi
  130. }
  131. function comment(){
  132. [ $nocolor -eq 0 ] || echo -ne "\033[0;96m"
  133. echo -e "# $@"
  134. [ $nocolor -eq 0 ] || echo -ne "\033[0m"
  135. }
  136. unit_count=0
  137. function unit(){
  138. ((unit_count++))
  139. [ $nocolor -eq 0 ] || echo -ne "\033[4m"
  140. echo -e "Unit test $unit_count: $@"
  141. [ $nocolor -eq 0 ] || echo -ne "\033[0m"
  142. comment "(running score = $score/$total)"
  143. }
  144. ###############################################################################
  145. # End helper functions #
  146. # Begin script #
  147. ###############################################################################
  148. # Print banner
  149. if [ $nocolor -eq 0 ]
  150. then
  151. echo -e "┏┓ ┏━╸┏━╸╻┏┓╻┏┓╻╻┏┓╻┏━╸ ┏┳┓┏━┓╺┳╸┏━┓╻╻ ╻ ╺┳╸┏━╸┏━┓╺┳╸\n┣┻┓┣╸ ┃╺┓┃┃┗┫┃┗┫┃┃┗┫┃╺┓ ┃┃┃┣━┫ ┃ ┣┳┛┃┏╋┛ ┃ ┣╸ ┗━┓ ┃ \n┗━┛┗━╸┗━┛╹╹ ╹╹ ╹╹╹ ╹┗━┛ ╹ ╹╹ ╹ ╹ ╹┗╸╹╹ ╹ ╹ ┗━╸┗━┛ ╹"
  152. else
  153. echo -e "┏┓ ┏━╸┏━╸╻┏┓╻┏┓╻╻┏┓╻┏━╸ ┏┳┓┏━┓╺┳╸┏━┓╻╻ ╻ ╺┳╸┏━╸┏━┓╺┳╸\n┣┻┓┣╸ ┃╺┓┃┃┗┫┃┗┫┃┃┗┫┃╺┓ ┃┃┃┣━┫ ┃ ┣┳┛┃┏╋┛ ┃ ┣╸ ┗━┓ ┃ \n┗━┛┗━╸┗━┛╹╹ ╹╹ ╹╹╹ ╹┗━┛ ╹ ╹╹ ╹ ╹ ╹┗╸╹╹ ╹ ╹ ┗━╸┗━┛ ╹ "
  154. fi
  155. # Initialize score and total variables
  156. score=0
  157. total=0
  158. # Check for existence/executability of matrix file
  159. if [ ! -r matrix ] || [ ! -x matrix ]
  160. then
  161. perror "matrix: permission error"
  162. exit 0
  163. fi
  164. tmp1="$(mktemp --tmpdir p1grading_XXXXX)"
  165. tmp2="$(mktemp --tmpdir p1grading_XXXXX)"
  166. tmp3="$(mktemp --tmpdir p1grading_XXXXX)"
  167. tmp4="$(mktemp --tmpdir p1grading_XXXXX)"
  168. ###############################################################################
  169. # Begin unit tests #
  170. ###############################################################################
  171. unit "error messages"
  172. $(./matrix dims "$tmp1" "$tmp1" 1>"$tmp2" 2>"$tmp3")
  173. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  174. result "too many args to matrix dims" 1
  175. $(./matrix add 1>"$tmp2" 2>"$tmp3")
  176. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  177. result "no args to matrix add" 1
  178. generate 5 6 10 > "$tmp1"
  179. $(./matrix multiply "$tmp1" "$tmp1" 1>"$tmp2" 2>"$tmp3")
  180. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  181. result "multiplying incompatible matrices" 5
  182. generate 6 5 10 > "$tmp2"
  183. $(./matrix add "$tmp1" "$tmp2" 1>"$tmp2" 2>"$tmp3")
  184. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  185. result "adding incompatible matrices" 4
  186. chmod -r "$tmp1"
  187. $(./matrix transpose "$tmp1" 1>"$tmp2" 2>"$tmp3")
  188. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  189. result "tranpose unreadable file" 2
  190. chmod +r "$tmp1"
  191. $(./matrix dims $(mktemp -u) 1>"$tmp2" 2>"$tmp3")
  192. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  193. result "get dims of nonexistent file" 1
  194. $(./matrix badcommand 1>"$tmp2" 2>"$tmp3")
  195. [ $? -ne 0 ] && [ -s "$tmp3" ] && [ ! -s "$tmp2" ]
  196. result "matrix badcommand" 1
  197. ###############################################################################
  198. unit "matrix dims tests"
  199. for a in {0..2}
  200. do
  201. (( rows = RANDOM % 50 + 51 ))
  202. (( cols = RANDOM % 50 + 51 ))
  203. dims="$(generate $rows $cols 100 | ./matrix dims)"
  204. [[ "$dims" == "$rows $cols" ]]
  205. result "Stdin: (\"$dims\" == \"$rows $cols\")" 3
  206. done
  207. for a in {3..5}
  208. do
  209. (( rows = RANDOM % 50 + 51 ))
  210. (( cols = RANDOM % 50 + 51 ))
  211. generate $rows $cols 100 > "$tmp1"
  212. dims="$(./matrix dims $tmp1)"
  213. [[ "$dims" == "$rows $cols" ]]
  214. result "File input: (\"$dims\" == \"$rows $cols\")" 3
  215. done
  216. ###############################################################################
  217. unit "matrix transpose hardcoded test"
  218. m1="-93 -92 29 -45 -55 -87 -36 39 -88 71 7 -69 52 45 -22
  219. 50 -27 85 11 -76 -3 23 68 58 -5 34 7 -29 -49 41
  220. -61 2 -44 -62 47 -77 33 6 -7 55 -45 99 2 20 89
  221. 52 -97 57 -39 -76 62 24 69 -74 89 -76 1 -46 -27 -9
  222. 55 77 42 10 -98 -22 15 -48 26 33 -7 29 -34 78 -19
  223. 23 25 -40 16 -63 -12 42 45 -22 20 44 -23 78 -50 17
  224. -67 14 -9 -58 38 -78 2 99 -87 -92 -34 -29 -7 -31 11
  225. 65 -32 27 91 -46 -13 -71 37 24 -5 34 -92 6 15 -15
  226. -49 23 -52 -9 59 -57 -78 -10 17 -27 44 -34 -62 22 -94
  227. -45 -45 88 -12 -64 1 -60 -35 11 1 -10 52 5 52 -17
  228. -91 61 90 22 82 -9 82 85 10 56 18 4 -18 -92 -46
  229. 31 98 47 -12 -60 20 54 -8 92 24 -71 -23 24 91 37
  230. -12 98 -13 66 72 -14 88 51 75 5 40 -91 91 -94 26
  231. 0 60 -41 6 28 -54 97 56 40 -17 94 -92 -23 -3 -91
  232. 10 -26 78 -22 -55 73 -82 -49 -26 -63 -80 8 97 87 -27
  233. 80 17 90 22 6 45 23 91 16 -93 -38 -64 -75 35 61
  234. 21 -24 -38 92 -43 98 -14 35 39 -65 -20 65 65 19 -81
  235. 79 -32 62 -93 89 19 -83 -47 45 20 93 49 43 73 80
  236. 24 81 19 -15 48 -46 -23 -63 65 2 75 -16 1 -98 14
  237. -40 -68 -89 -10 90 29 3 -15 58 86 -85 36 -55 31 -79"
  238. m2="-93 50 -61 52 55 23 -67 65 -49 -45 -91 31 -12 0 10 80 21 79 24 -40
  239. -92 -27 2 -97 77 25 14 -32 23 -45 61 98 98 60 -26 17 -24 -32 81 -68
  240. 29 85 -44 57 42 -40 -9 27 -52 88 90 47 -13 -41 78 90 -38 62 19 -89
  241. -45 11 -62 -39 10 16 -58 91 -9 -12 22 -12 66 6 -22 22 92 -93 -15 -10
  242. -55 -76 47 -76 -98 -63 38 -46 59 -64 82 -60 72 28 -55 6 -43 89 48 90
  243. -87 -3 -77 62 -22 -12 -78 -13 -57 1 -9 20 -14 -54 73 45 98 19 -46 29
  244. -36 23 33 24 15 42 2 -71 -78 -60 82 54 88 97 -82 23 -14 -83 -23 3
  245. 39 68 6 69 -48 45 99 37 -10 -35 85 -8 51 56 -49 91 35 -47 -63 -15
  246. -88 58 -7 -74 26 -22 -87 24 17 11 10 92 75 40 -26 16 39 45 65 58
  247. 71 -5 55 89 33 20 -92 -5 -27 1 56 24 5 -17 -63 -93 -65 20 2 86
  248. 7 34 -45 -76 -7 44 -34 34 44 -10 18 -71 40 94 -80 -38 -20 93 75 -85
  249. -69 7 99 1 29 -23 -29 -92 -34 52 4 -23 -91 -92 8 -64 65 49 -16 36
  250. 52 -29 2 -46 -34 78 -7 6 -62 5 -18 24 91 -23 97 -75 65 43 1 -55
  251. 45 -49 20 -27 78 -50 -31 15 22 52 -92 91 -94 -3 87 35 19 73 -98 31
  252. -22 41 89 -9 -19 17 11 -15 -94 -17 -46 37 26 -91 -27 61 -81 80 14 -79"
  253. echo "$m1" > "$tmp1"
  254. echo "$m2" > "$tmp2"
  255. [[ "$m1" == "$(./matrix transpose $tmp2)" ]] && [[ "$m2" == "$(./matrix transpose $tmp1)" ]]
  256. result "m1 == T(m2) and T(m1) == m2" 10
  257. ###############################################################################
  258. unit "matrix transpose involution test"
  259. for a in {1..3}
  260. do
  261. (( rows = RANDOM % 50 + 51 ))
  262. (( cols = RANDOM % 50 + 51 ))
  263. m1="$(generate $rows $cols 100)"
  264. echo "$m1" > "$tmp1"
  265. [[ "$m1" == "$(./matrix transpose $tmp1 | ./matrix transpose)" ]]
  266. result "m$a == T(T(m$a))" 5
  267. done
  268. ###############################################################################
  269. unit "matrix mean test"
  270. m1="-28 91 29 -5 12 83
  271. -94 -16 41 -28 6 86
  272. -44 83 -9 64 92 -70
  273. 41 22 66 29 55 49
  274. 6 52 4 17 -29 52
  275. -8 -33 96 -73 -76 92
  276. -32 94 45 4 43 -97
  277. -57 36 86 90 35 75
  278. 46 8 4 -83 -94 -52
  279. 0 -6 -90 48 70 11
  280. -33 -41 -76 68 30 -19
  281. 70 96 -85 -1 -9 -62
  282. 58 63 -4 22 -69 -25
  283. -75 -65 -78 76 39 10
  284. -99 -69 63 53 35 67
  285. 73 51 55 -26 -14 9
  286. -90 -19 -19 -63 -96 23
  287. -62 89 93 98 48 -21
  288. -56 -86 24 45 -79 -65
  289. -79 -60 87 -74 44 20
  290. -38 50 -50 -38 -36 30
  291. -69 -66 -23 42 55 -5
  292. -62 98 47 -86 23 -85
  293. -2 -55 -79 -41 -12 -31
  294. -84 -10 46 16 -56 -26
  295. 70 -16 -17 99 24 -41
  296. 80 47 -59 -55 80 96
  297. -54 43 24 -82 1 -46
  298. 25 -1 92 16 44 -21
  299. 62 58 79 -97 -21 -62
  300. -46 -23 -68 59 -72 -6
  301. -78 59 11 52 -96 -77
  302. 5 77 64 -29 -98 69
  303. -68 69 74 32 -71 91
  304. 38 -75 25 -61 -29 73
  305. 40 25 29 93 93 -27
  306. -1 -16 -14 48 99 -36
  307. 81 6 35 -90 4 -57
  308. 80 84 63 -10 80 -99
  309. 96 -5 -81 11 37 -46"
  310. m2="-10 16 13 4 2 -4"
  311. generate 5 10 z > "$tmp1"
  312. [[ "$(generate 1 10 z)" == "$(./matrix mean < $tmp1)" ]]
  313. result "zero matrix" 2
  314. generate 5 10 o > "$tmp1"
  315. [[ "$(generate 1 10 o)" == "$(./matrix mean $tmp1)" ]]
  316. result "ones matrix" 5
  317. generate 5 10 d > "$tmp1"
  318. [[ "$(generate 1 10 z)" == "$(./matrix mean $tmp1)" ]]
  319. result "identity matrix" 5
  320. echo "$m1" > "$tmp1"
  321. [[ "$m2" == "$(./matrix mean $tmp1)" ]]
  322. result "hardcoded matrix" 15
  323. ###############################################################################
  324. unit "matrix add test"
  325. bigz="$(generate 30 20 z)"
  326. echo "$bigz" > "$tmp1"
  327. [[ "$bigz" == "$(./matrix add $tmp1 $tmp1)" ]]
  328. result "0 + 0 == 0" 2
  329. bigr="$(generate 30 20 100)"
  330. echo "$bigr" > "$tmp2"
  331. [[ "$bigr" == "$(./matrix add $tmp2 $tmp1)" ]]
  332. result "X + 0 == X" 5
  333. [[ "$bigr" == "$(./matrix add $tmp1 $tmp2)" ]]
  334. result "0 + X == X" 5
  335. m1="$(generate 10 10 100)"
  336. m2="$(generate 10 10 100)"
  337. echo "$m1" > "$tmp1"
  338. echo "$m2" > "$tmp2"
  339. [[ "$(./matrix add $tmp1 $tmp2)" == "$(./matrix add $tmp2 $tmp1)" ]]
  340. result "A + B == B + A"
  341. m1="68 86 -22 95 -97 44 68 -98 70 -65 69 94 -5 -84 3 83 71 31 -10 0
  342. -82 74 -87 94 56 27 -45 -45 12 -75 -76 0 42 -76 75 14 21 -30 87 -8
  343. -58 62 78 70 57 -93 2 -82 -2 -55 46 68 98 -91 40 73 -87 -45 58 78
  344. 45 45 42 93 47 -66 62 -70 84 92 82 -18 51 -55 -5 19 -16 58 -29 56
  345. -95 -24 -72 23 40 67 -8 58 41 -5 54 -11 -22 68 73 90 92 20 89 39
  346. 74 -7 59 27 39 -97 -97 75 40 21 13 36 -83 -85 -84 -92 44 -7 83 -88
  347. -33 -59 -91 -33 -23 -55 -77 -50 14 39 91 -39 58 -29 -75 -25 28 -84 -91 35
  348. 94 -78 97 24 94 -74 10 81 67 42 -67 -72 -66 -48 71 11 77 0 56 92
  349. 56 -36 70 -8 50 53 92 -81 99 45 96 -37 38 60 -37 61 -62 99 23 79
  350. 93 47 -87 -56 -60 -43 -7 75 82 -13 -81 -66 38 -23 30 -46 7 -30 -25 89
  351. -8 73 1 -25 -53 -82 -82 23 0 -5 26 -13 -13 -4 -57 -52 26 96 -40 47
  352. 66 -83 84 -51 6 -5 62 -51 -54 -50 31 -6 88 55 -73 35 32 -91 -23 2
  353. 73 37 89 90 -45 89 -56 -12 3 79 -26 -18 81 93 -92 3 -17 23 -64 -4
  354. -45 -57 59 -72 -84 44 38 70 47 -91 -10 -86 18 -40 -21 98 42 79 7 -80
  355. -33 -93 78 -16 91 -62 55 82 -93 1 72 -31 -52 -8 -55 -89 66 -99 10 -25
  356. 64 -66 -56 19 19 80 -32 -16 73 10 25 -20 -57 30 29 81 30 -90 33 -54
  357. 68 -7 53 52 -55 -44 -94 -20 -76 29 88 33 -26 30 -45 90 -56 85 60 38
  358. -28 17 90 -42 -35 87 -2 -47 94 -35 78 -33 59 -22 8 -5 0 -36 33 -27
  359. -92 75 91 -97 19 76 33 20 -10 -80 -20 3 98 -35 75 -28 -48 -73 -68 72
  360. 17 45 43 -90 -12 4 86 -49 88 -92 39 -18 5 -10 -69 -5 -56 3 -21 10"
  361. m2="24 52 55 -34 -9 74 -79 -78 5 -42 48 -53 -77 47 -53 97 -72 20 77 -28
  362. -66 -54 72 -46 -25 53 -95 -3 -75 -62 92 -43 -57 0 78 -63 -90 12 -60 -77
  363. 65 43 -17 65 25 -77 57 27 71 98 83 53 28 -87 -56 -20 67 -3 -16 -61
  364. -99 19 30 34 -10 -73 1 -18 7 99 -76 71 -72 -78 20 56 66 20 10 34
  365. -54 -37 54 18 -11 56 -81 -97 64 -56 68 -36 25 -26 -56 91 -60 12 -9 17
  366. -41 -78 -54 -19 -38 -76 17 -90 91 -5 82 -31 -92 93 -57 43 -56 14 29 -45
  367. 78 22 -94 12 -76 44 -63 -11 -11 -89 -81 53 56 6 27 30 -26 -79 -12 -50
  368. 52 15 -44 63 8 40 91 10 -26 -35 -61 21 -55 82 -97 -90 -25 10 -60 2
  369. -33 -48 -46 -64 -41 -61 84 54 0 26 -56 75 -81 -53 -68 -2 -93 -46 29 85
  370. 53 -93 -31 51 78 75 69 10 -20 -68 -67 -67 -54 23 76 32 -11 -86 -55 -14
  371. 5 93 35 41 -26 41 77 88 43 44 -89 91 66 -11 90 -44 -61 -10 -66 -51
  372. 82 83 -44 55 69 80 1 -29 8 -87 -93 -91 -53 44 3 -3 24 13 86 33
  373. 92 5 -75 58 -4 -52 -2 -3 78 9 83 59 82 -4 -57 76 25 -49 -44 15
  374. 75 64 44 -23 43 56 60 0 46 26 39 53 -85 53 14 81 77 -11 -32 33
  375. 75 0 68 -5 -78 -13 90 -51 -15 -40 77 -95 -37 59 -20 30 38 -32 4 -76
  376. -63 -28 58 -62 -42 93 15 79 32 -42 4 -98 85 -85 -31 -38 28 -31 -17 17
  377. -65 -66 -8 26 -7 79 18 33 21 66 41 -22 20 66 -9 -64 -71 -67 -16 68
  378. 19 12 55 6 -90 -60 1 26 64 -29 62 96 89 11 -77 -26 76 -62 -48 -36
  379. -76 -21 -93 85 -5 -99 41 -74 47 1 -44 47 16 83 30 -38 33 -53 80 -11
  380. 94 20 -76 -25 17 -5 3 -78 92 -33 -13 14 85 -52 -55 5 75 -89 9 -16"
  381. m3="92 138 33 61 -106 118 -11 -176 75 -107 117 41 -82 -37 -50 180 -1 51 67 -28
  382. -148 20 -15 48 31 80 -140 -48 -63 -137 16 -43 -15 -76 153 -49 -69 -18 27 -85
  383. 7 105 61 135 82 -170 59 -55 69 43 129 121 126 -178 -16 53 -20 -48 42 17
  384. -54 64 72 127 37 -139 63 -88 91 191 6 53 -21 -133 15 75 50 78 -19 90
  385. -149 -61 -18 41 29 123 -89 -39 105 -61 122 -47 3 42 17 181 32 32 80 56
  386. 33 -85 5 8 1 -173 -80 -15 131 16 95 5 -175 8 -141 -49 -12 7 112 -133
  387. 45 -37 -185 -21 -99 -11 -140 -61 3 -50 10 14 114 -23 -48 5 2 -163 -103 -15
  388. 146 -63 53 87 102 -34 101 91 41 7 -128 -51 -121 34 -26 -79 52 10 -4 94
  389. 23 -84 24 -72 9 -8 176 -27 99 71 40 38 -43 7 -105 59 -155 53 52 164
  390. 146 -46 -118 -5 18 32 62 85 62 -81 -148 -133 -16 0 106 -14 -4 -116 -80 75
  391. -3 166 36 16 -79 -41 -5 111 43 39 -63 78 53 -15 33 -96 -35 86 -106 -4
  392. 148 0 40 4 75 75 63 -80 -46 -137 -62 -97 35 99 -70 32 56 -78 63 35
  393. 165 42 14 148 -49 37 -58 -15 81 88 57 41 163 89 -149 79 8 -26 -108 11
  394. 30 7 103 -95 -41 100 98 70 93 -65 29 -33 -67 13 -7 179 119 68 -25 -47
  395. 42 -93 146 -21 13 -75 145 31 -108 -39 149 -126 -89 51 -75 -59 104 -131 14 -101
  396. 1 -94 2 -43 -23 173 -17 63 105 -32 29 -118 28 -55 -2 43 58 -121 16 -37
  397. 3 -73 45 78 -62 35 -76 13 -55 95 129 11 -6 96 -54 26 -127 18 44 106
  398. -9 29 145 -36 -125 27 -1 -21 158 -64 140 63 148 -11 -69 -31 76 -98 -15 -63
  399. -168 54 -2 -12 14 -23 74 -54 37 -79 -64 50 114 48 105 -66 -15 -126 12 61
  400. 111 65 -33 -115 5 -1 89 -127 180 -125 26 -4 90 -62 -124 0 19 -86 -12 -6"
  401. echo "$m1" > "$tmp1"
  402. echo "$m2" > "$tmp2"
  403. [[ "$m3" == "$(./matrix add $tmp1 $tmp2)" ]]
  404. result "m1 + m2 == m3 (hardcoded matrix)" 15
  405. ###############################################################################
  406. unit "matrix multiply test"
  407. m1="$(generate 10 10 d)"
  408. m2="$(generate 10 15 100)"
  409. echo "$m1" > "$tmp1"
  410. echo "$m2" > "$tmp2"
  411. [[ "$m2" == "$(./matrix multiply $tmp1 $tmp2)" ]]
  412. result "I * M == M" 5
  413. m2="$(generate 15 10 100)"
  414. echo "$m2" > "$tmp2"
  415. [[ "$m2" == "$(./matrix multiply $tmp2 $tmp1)" ]]
  416. result "M * I == M" 5
  417. m1="$(generate 30 14 z)"
  418. m2="$(generate 14 12 60)"
  419. m3="$(generate 30 12 z)"
  420. echo "$m1" > "$tmp1"
  421. echo "$m2" > "$tmp2"
  422. [[ "$m3" == "$(./matrix multiply $tmp1 $tmp2)" ]]
  423. result "0 * M == 0" 5
  424. m1="-80 85 -94 93 28 88 47 37 9 8 51 -39 -63 56 -72 -39 80 -77 -22 70 44 -47 57 62 -77
  425. -66 22 -30 10 -16 -26 16 -93 2 -87 32 -26 -52 99 -59 8 4 62 -66 -73 -46 -7 -88 -14 94
  426. -41 82 45 -37 -18 -66 0 99 -28 42 -13 -22 67 52 30 87 95 -14 -7 -68 2 -12 -98 -49 79
  427. -33 40 -76 80 97 18 -63 39 -74 99 60 -64 31 -88 32 74 97 -66 21 11 45 -5 -21 -4 -12
  428. -78 8 5 -13 -79 -67 -23 -40 -80 13 -73 -62 -38 -13 -77 40 -61 88 81 -6 72 -71 -40 53 -26
  429. -85 2 46 -79 -95 15 -86 35 -57 12 -94 -1 -9 -42 52 0 38 65 1 41 -52 -1 -92 -28 57
  430. 60 71 -22 -90 -3 -53 -16 -90 98 -88 53 -81 -67 -51 -24 -74 90 36 99 -57 59 -14 10 -90 -53
  431. -23 -84 -29 2 -19 96 4 -6 -54 -73 38 -6 99 -57 63 40 -83 19 -15 80 -90 34 68 -22 -78
  432. 2 -74 -18 -45 -16 12 -99 8 98 59 72 20 -51 37 -28 56 11 79 0 94 -48 10 69 60 -22
  433. 68 -48 12 -14 51 -11 10 -97 -88 -87 9 -77 -99 79 -38 75 -48 -78 38 -51 -3 -47 66 -5 -52
  434. -87 -93 1 93 13 77 31 79 -97 -73 -69 7 97 -24 -14 -7 -40 53 58 -14 -78 33 26 51 -48
  435. 17 -18 -90 -27 86 9 -97 51 27 -26 -81 -44 -34 -28 71 -56 -17 -88 3 89 14 59 -7 -45 -47
  436. -5 96 -80 30 -1 43 31 -21 70 94 2 16 49 46 -67 69 0 64 -53 32 -94 96 -76 87 21
  437. -1 72 35 67 -23 15 51 -3 -75 4 65 -44 -1 -43 66 -21 -88 -59 -29 -25 30 -57 21 -81 -76
  438. -36 94 59 -2 86 25 -10 53 63 -24 67 49 -37 -39 -39 -14 67 -77 -76 74 46 -34 -3 85 -54
  439. -26 -48 -34 -24 -75 67 33 6 43 -82 -25 58 82 -13 47 66 18 -39 57 -29 -8 85 15 -98 -83
  440. -21 9 -7 86 4 72 17 -73 -64 71 -48 65 -38 -51 -2 -10 7 -69 -21 -33 -96 -26 37 25 -23
  441. -52 -72 23 -46 7 70 12 11 -36 -38 28 71 -22 46 -40 -86 70 81 81 -34 76 -32 -90 92 77
  442. 35 -13 -63 42 63 97 -52 94 49 69 -66 25 21 42 98 93 15 -35 -13 90 -4 65 -35 34 -10
  443. -15 -36 -49 -2 -42 -64 43 78 -31 -56 -89 -9 37 -8 -55 -4 17 74 79 55 17 46 6 85 34
  444. 49 -53 81 36 -10 -82 -48 -4 -50 11 -11 62 46 -64 74 -18 13 -76 0 87 92 -65 -37 58 50
  445. -68 -6 62 8 93 58 80 35 55 45 21 41 0 88 -13 -47 36 45 43 -25 -40 -25 -81 -8 -40
  446. -85 -30 -4 -17 73 -30 15 97 -64 63 81 86 3 -40 -36 7 83 43 -91 42 0 77 96 -77 10
  447. 56 46 -10 12 0 -21 2 -6 7 72 65 14 -67 2 0 53 -36 92 -52 -30 59 -86 -13 -61 -94
  448. -74 -78 50 -78 36 81 -19 11 81 19 78 -55 -69 66 41 4 92 74 -58 -43 24 25 -74 -58 95"
  449. m2="-46 19 8 23 22 -11 34 10 -36 -90 58 -23 -73 -73 -92 40 78 -43 -59 34 0 -83 72 0 -23
  450. 89 -13 42 -49 52 15 37 68 -90 85 -29 37 53 67 92 -61 -69 16 -91 81 -96 -48 30 63 -4
  451. 60 70 92 -43 91 -21 85 -38 -13 16 73 50 55 -21 79 56 -39 -15 94 -32 -57 -3 -19 0 97
  452. 87 86 -58 -11 -47 -50 -36 -67 -47 -83 -79 -44 -63 75 -80 -67 -27 22 4 -50 30 -69 39 -12 6
  453. 51 -94 26 3 -36 -50 -65 -66 36 -11 62 -39 5 4 65 85 -9 -42 82 41 -19 -43 -86 -71 16
  454. 86 -30 -46 59 56 81 -95 -97 -42 71 28 52 -15 -36 3 -81 -33 -5 -75 -73 -3 -17 -83 70 -71
  455. -37 66 4 50 61 -92 86 -20 85 -74 -1 -41 39 73 -49 34 -41 29 -65 47 44 -48 -4 98 66
  456. -73 -34 32 17 3 80 -45 -20 65 59 -14 -15 -60 62 5 -56 -51 70 -98 5 55 67 -67 -95 -51
  457. 13 54 15 -72 -67 48 98 45 17 48 45 37 99 87 -96 -75 36 -82 65 -72 88 83 -29 -14 -62
  458. -43 -18 -77 73 -59 -42 4 -45 68 -15 15 71 -20 7 -80 21 -32 63 -11 73 34 3 63 68 60
  459. -75 65 88 90 -28 -68 -14 57 -11 12 -97 3 83 76 -53 -42 -35 24 -35 -89 -96 58 -3 70 52
  460. -71 18 -37 12 -72 14 93 32 63 47 64 -14 15 62 55 69 -75 -83 1 39 85 80 25 -98 -19
  461. -9 -71 -87 35 -47 -11 -48 -8 -80 -68 -53 59 -38 -64 6 5 14 60 -70 -18 28 44 -2 1 -71
  462. 3 89 0 17 -16 -24 30 58 -67 -29 8 -5 30 -75 31 -62 7 -81 89 66 92 71 -34 20 41
  463. -34 -11 -70 0 73 80 43 29 -67 -9 6 -94 -19 -46 44 -45 69 51 -78 -66 71 48 60 -51 -27
  464. 21 -90 -37 -35 21 -25 29 96 26 -90 -46 45 89 69 -20 99 98 -91 -40 -54 45 -42 51 -39 -71
  465. -32 3 10 40 63 42 -3 -55 -53 -8 -98 -48 -43 -50 34 95 -98 95 83 55 -75 -61 -91 28 43
  466. -88 66 9 -83 84 4 -47 -48 42 -11 -9 -85 0 -68 -45 -69 -52 68 94 34 4 94 -42 73 -50
  467. 26 -56 75 -14 -4 -92 72 -8 75 -79 49 47 -55 79 57 -53 8 98 85 12 -47 -54 -61 -24 55
  468. 72 31 18 -57 -85 -76 -65 46 14 -64 -61 81 92 89 -76 -96 29 -57 98 -21 -75 -66 14 19 -7
  469. 39 -24 -64 -85 80 -75 -48 57 -5 -52 -60 -73 27 -56 -90 -43 41 -55 -26 -47 -31 96 20 76 -4
  470. 17 -20 -98 28 87 70 54 -45 -61 90 2 48 0 97 53 -48 67 86 16 50 97 69 -8 48 -11
  471. 70 -26 -63 55 62 -70 -41 -28 -46 -93 -54 -69 90 -52 11 98 28 64 -31 76 -59 -67 44 -77 -85
  472. -65 54 57 90 0 6 -70 77 -90 31 -3 87 -24 28 -84 23 0 36 71 -76 -84 12 95 49 -95
  473. 77 32 -46 -1 -28 81 15 -58 30 92 -36 -29 7 57 58 -70 60 11 11 63 -72 -3 -29 23 76"
  474. m3="22965 9925 9346 19084 -9241 -22200 -36903 4840 -17439 -3760 -35169 5378 12600 18991 -22989 -14725 -32343 1170 -2937 -4396 -26805 -23470 -8985 20005 -7504
  475. 880 30761 12175 -10504 -1443 14140 9769 4237 -4832 26530 -6612 -12224 9775 -1188 14763 -14504 -5753 -19864 21746 7281 500 13720 -14866 23709 21727
  476. -9033 -13102 -124 -6371 9272 23489 19677 6070 1264 16454 -13560 922 -8722 1698 32792 4132 -10006 12433 -15426 17814 6717 12988 -14819 593 22482
  477. 11783 -40738 -14880 15690 -7899 -8331 -36285 -9546 -6428 -12619 -35913 2784 -17376 12908 -4454 9870 -4642 26274 -21466 -8609 -24396 -25865 -780 -25 448
  478. -1276 -1038 16703 -27882 7215 -26322 -10871 16460 17761 -17116 -2243 7755 -5261 -9371 -10386 -5338 -1291 2751 22528 -2512 -21509 -412 11762 17965 5066
  479. -5136 -7103 -255 -20259 8776 42398 77 -9822 7926 34747 5216 10583 -14472 -8855 31169 -17432 -7493 16197 10568 2730 -1897 13094 -12927 -1666 5970
  480. 360 -1799 34125 -26362 25516 -10741 23804 8095 -691 -2552 2953 -21933 8654 -14659 5222 -1985 -2520 6886 16187 6950 -31028 -7076 -21627 15455 15922
  481. 848 -17111 -17574 14713 -545 -685 -26151 -6875 -9975 -19816 -7325 8565 5982 -3161 -2395 -3037 16148 9460 -24889 -29229 12651 -5344 4650 -15432 -36871
  482. -16765 11515 8292 3744 -19397 -1659 -11255 14721 5488 -1125 -4057 18644 28033 8904 -30314 -5209 8153 -9628 39049 -12067 -1570 15887 3261 -5141 -24360
  483. 17593 -6852 20227 6133 12954 -34998 750 13276 -5882 -38695 11443 -10186 7710 -23947 6911 37162 24979 -29165 7330 -600 -14134 -39266 8709 -10992 10773
  484. -369 -11620 -10411 15335 4309 2728 -30188 -33425 964 -11467 844 3646 -31615 149 13207 -1787 -12522 34973 -975 -11384 17085 -3307 -17114 -19353 -25133
  485. 17332 -33041 -13076 -11537 -13530 19009 -18179 -2934 -5603 8477 11143 -190 -12810 304 6621 -16515 24148 -7021 -2448 -2873 14418 -5757 -7791 -30304 -16958
  486. -6475 10715 -13824 12207 -19061 15461 70 6461 -11482 23484 -6500 39111 7910 35755 -18773 -21063 -6039 546 4878 10977 19056 7629 7790 34095 -18612
  487. 13667 -1085 515 1508 12389 -21233 -1840 2936 -6831 -16663 -8074 -12920 3468 -4394 -27 -4065 -5894 1869 -50965 -14144 -6933 -15192 18645 4134 11128
  488. 10327 4857 28804 1641 -7760 -3323 -13194 17760 -12322 22858 -8966 16024 26871 25153 -5110 9878 -28011 -20824 7434 -18171 -32353 -3025 -4412 -830 -7018
  489. -1661 -23976 -25870 -1377 13205 13390 23373 1051 -1759 -9005 2316 1772 1038 4759 15361 1770 12214 3348 -26808 -13522 44371 13043 -12637 -17474 -22149
  490. 13006 -2049 -14014 26626 -10428 -172 -2331 -22127 -3373 575 8922 7956 -13718 3907 8380 27389 -17717 5587 -11905 7024 -389 -36078 16064 -7341 2178
  491. -21234 19490 21361 5777 7470 6996 -9582 -12574 14386 23977 7143 -6457 -19467 -10249 7108 -16446 -28066 9779 39085 -5292 -18801 30944 -34163 22496 17968
  492. 7388 -22586 -32992 4524 -13345 25762 -15259 939 -9852 1620 1859 17200 -8712 15878 -12332 -18121 20442 -9515 -8709 -12004 37161 4037 1846 -17026 -33204
  493. -11895 58 681 -7467 4107 -3420 -9721 -302 10751 -10808 -9672 2001 -16882 12518 -7593 -11014 3469 25696 20308 12852 -2101 2624 -5053 190 -14209
  494. -164 2607 -1822 -6818 -15689 -11559 -2459 13706 -3064 -18735 -5615 26 -10923 -3822 -11462 11007 12019 -10753 6484 -20223 -21451 -4704 27280 -15647 10788
  495. -10770 15639 21011 5213 -6157 -4369 12354 -17687 19721 11972 21356 5276 -897 7982 11080 -8174 -37729 817 28742 3704 18163 16758 -37289 8967 22292
  496. -13448 -11410 -18040 16558 2038 -2575 -16333 -23440 20123 9673 -26702 -18295 17314 13159 13797 25471 -26248 21741 -3577 28368 130 13603 -17888 -12320 5062
  497. -20432 7774 7523 -15477 5045 -21824 847 17301 15529 -18691 -616 -18991 12855 -13920 -29755 5548 -10819 -21413 -15771 -3257 5292 8643 15855 10524 -697
  498. -7616 14863 3840 -4490 17884 34495 2052 -19149 9604 39327 -1303 -14717 15286 -11968 8785 -20204 -4881 -3407 26948 -9991 7408 39516 -44128 22859 22855"
  499. echo "$m1" > "$tmp1"
  500. echo "$m2" > "$tmp2"
  501. [[ "$m3" == "$(./matrix multiply $tmp1 $tmp2)" ]]
  502. result "m3 == m1 * m2 (hardcoded matrix)" 20
  503. comment "Final grade: $score/$total"
  504. # Remove temp files
  505. for f in $tmp{1..4}
  506. do
  507. rm "$f"
  508. done
  509. exit $score
  510. echo
  511. echo