Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Usage:
- ./driver.sh intersection.s
- == driver.sh ==
- #!/bin/bash
- ## Test driver for CS3421 Lab 3.
- # Generates two linked lists and appends them to the given source file, then
- # runs the whole thing in MARS and verifies the output for correctness.
- #
- # MARS output is expected to take the following format:
- # [list a]
- # [list b]
- # [intersection]
- #
- # Any output before the last four lines as above will be ignored (note that
- # MARS inserts a blank line at the end of output, making it four lines).
- # Lists should be output by MARS in the following form:
- # [0 1 2 3 4 5 6 ]
- ## Known limitations:
- # - Lists dumped will always be in order in memory, so verify your list
- # traversal code actually follows the pointers.
- ## Settings ##
- # Path to MARS jar
- MARS="../Mars_4_1.jar"
- # Python version to use, needs at least python 2.6 (_not_ 3.x)
- # Most distros should just use "python", but some (such as Arch) need
- # python2 here.
- PYTHON="python2"
- # Number of test lists to run
- LIMIT=500
- TEMP_ASM=`mktemp`
- TEMP_LST=`mktemp`
- for i in {1..$LIMIT}
- do
- cat $1 > $TEMP_ASM
- $PYTHON driver.py gen > $TEMP_LST
- $PYTHON driver.py asm < $TEMP_LST >> $TEMP_ASM
- RESULT=`java -jar $MARS $TEMP_ASM | tail -n4 | head -n3`
- EXPECTED=`cat $TEMP_LST`
- echo -e Expected\\t$EXPECTED
- echo -e Got\\t\\t$RESULT
- if [ "$RESULT" != "$EXPECTED" ]
- then
- echo 'Test failed!'
- exit 1
- else
- echo
- fi
- done
- rm $TEMP_LST $TEMP_ASM
- == driver.py ==
- #!/usr/bin/env python2
- from random import randint
- import sys
- def main(argv):
- if argv[0] == 'gen':
- a = rlist_gen(size=16, maxval=32)
- b = rlist_gen(size=16, maxval=32)
- print(list_2_string(a))
- print(list_2_string(b))
- intersection = list(set(a).intersection(b))
- intersection.sort()
- print(list_2_string(intersection))
- elif argv[0] == 'asm':
- a = string_2_list(sys.stdin.readline())
- b = string_2_list(sys.stdin.readline())
- print(list_dump([int(x) for x in a], 'a'))
- print(list_dump([int(x) for x in b], 'b'))
- def list_2_string(l):
- s = "["
- for i in l:
- s += "%i " %i
- return s + "]"
- def string_2_list(s):
- l = []
- for i in s.lstrip('[').split():
- try:
- l.append(int(i))
- except ValueError:
- pass
- return l
- def rlist_gen(size=64, maxval=128):
- """Creates a sorted list of integers in the range (0, maxval-1) of size
- elements. Breaks if size > maxval."""
- output = []
- values = range(maxval)
- while len(output) < size:
- i = randint(0, len(values)-1)
- output.append(values[i])
- del values[i]
- output.sort()
- return output
- def list_dump(list, name):
- """Dumps list to a string suitable for insertion into the intersection test
- driver.
- Name should be either 'a' or 'b' for my test driver."""
- prefix = "list%s" %name
- s = "%s:\n" %prefix
- def _dump(index, value, head=False, tail=False):
- s = "%s_%i:\n" %(prefix, index)
- s += "\t.word %i\n" %value
- s += "\t.word -1\n" if head else "\t.word %s_%i\n" %(prefix, index - 1)
- s += "\t.word -1\n" if tail else "\t.word %s_%i\n" %(prefix, index + 1)
- return s
- s += _dump(0, 0, head=True)
- for i in xrange(len(list)):
- s += _dump(i + 1, list[i])
- s += "%s_tail:\n" %prefix
- s += _dump(len(list) + 1, 0, tail=True)
- return s
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- print("Argument expected: either gen or asm")
- sys.exit(1)
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement