Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. Usage:
  2. ./driver.sh intersection.s
  3.  
  4. == driver.sh ==
  5. #!/bin/bash
  6.  
  7. ## Test driver for CS3421 Lab 3.
  8. # Generates two linked lists and appends them to the given source file, then
  9. # runs the whole thing in MARS and verifies the output for correctness.
  10. #
  11. # MARS output is expected to take the following format:
  12. # [list a]
  13. # [list b]
  14. # [intersection]
  15. #
  16. # Any output before the last four lines as above will be ignored (note that
  17. # MARS inserts a blank line at the end of output, making it four lines).
  18. # Lists should be output by MARS in the following form:
  19. # [0 1 2 3 4 5 6 ]
  20.  
  21. ## Known limitations:
  22. # - Lists dumped will always be in order in memory, so verify your list
  23. # traversal code actually follows the pointers.
  24.  
  25. ## Settings ##
  26. # Path to MARS jar
  27. MARS="../Mars_4_1.jar"
  28. # Python version to use, needs at least python 2.6 (_not_ 3.x)
  29. # Most distros should just use "python", but some (such as Arch) need
  30. # python2 here.
  31. PYTHON="python2"
  32. # Number of test lists to run
  33. LIMIT=500
  34.  
  35. TEMP_ASM=`mktemp`
  36. TEMP_LST=`mktemp`
  37.  
  38. for i in {1..$LIMIT}
  39. do
  40. cat $1 > $TEMP_ASM
  41.  
  42. $PYTHON driver.py gen > $TEMP_LST
  43. $PYTHON driver.py asm < $TEMP_LST >> $TEMP_ASM
  44.  
  45. RESULT=`java -jar $MARS $TEMP_ASM | tail -n4 | head -n3`
  46. EXPECTED=`cat $TEMP_LST`
  47. echo -e Expected\\t$EXPECTED
  48. echo -e Got\\t\\t$RESULT
  49. if [ "$RESULT" != "$EXPECTED" ]
  50. then
  51. echo 'Test failed!'
  52. exit 1
  53. else
  54. echo
  55. fi
  56. done
  57.  
  58. rm $TEMP_LST $TEMP_ASM
  59.  
  60. == driver.py ==
  61. #!/usr/bin/env python2
  62.  
  63. from random import randint
  64. import sys
  65.  
  66. def main(argv):
  67. if argv[0] == 'gen':
  68. a = rlist_gen(size=16, maxval=32)
  69. b = rlist_gen(size=16, maxval=32)
  70. print(list_2_string(a))
  71. print(list_2_string(b))
  72. intersection = list(set(a).intersection(b))
  73. intersection.sort()
  74. print(list_2_string(intersection))
  75. elif argv[0] == 'asm':
  76. a = string_2_list(sys.stdin.readline())
  77. b = string_2_list(sys.stdin.readline())
  78. print(list_dump([int(x) for x in a], 'a'))
  79. print(list_dump([int(x) for x in b], 'b'))
  80.  
  81. def list_2_string(l):
  82. s = "["
  83. for i in l:
  84. s += "%i " %i
  85. return s + "]"
  86.  
  87. def string_2_list(s):
  88. l = []
  89. for i in s.lstrip('[').split():
  90. try:
  91. l.append(int(i))
  92. except ValueError:
  93. pass
  94. return l
  95.  
  96. def rlist_gen(size=64, maxval=128):
  97. """Creates a sorted list of integers in the range (0, maxval-1) of size
  98. elements. Breaks if size > maxval."""
  99. output = []
  100. values = range(maxval)
  101. while len(output) < size:
  102. i = randint(0, len(values)-1)
  103. output.append(values[i])
  104. del values[i]
  105. output.sort()
  106. return output
  107.  
  108. def list_dump(list, name):
  109. """Dumps list to a string suitable for insertion into the intersection test
  110. driver.
  111. Name should be either 'a' or 'b' for my test driver."""
  112. prefix = "list%s" %name
  113. s = "%s:\n" %prefix
  114. def _dump(index, value, head=False, tail=False):
  115. s = "%s_%i:\n" %(prefix, index)
  116. s += "\t.word %i\n" %value
  117. s += "\t.word -1\n" if head else "\t.word %s_%i\n" %(prefix, index - 1)
  118. s += "\t.word -1\n" if tail else "\t.word %s_%i\n" %(prefix, index + 1)
  119. return s
  120. s += _dump(0, 0, head=True)
  121. for i in xrange(len(list)):
  122. s += _dump(i + 1, list[i])
  123. s += "%s_tail:\n" %prefix
  124. s += _dump(len(list) + 1, 0, tail=True)
  125. return s
  126.  
  127. if __name__ == '__main__':
  128. if len(sys.argv) < 2:
  129. print("Argument expected: either gen or asm")
  130. sys.exit(1)
  131. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement