Skip to content

Instantly share code, notes, and snippets.

@rozap
Created March 17, 2014 21:38
Show Gist options
  • Save rozap/9608941 to your computer and use it in GitHub Desktop.
Save rozap/9608941 to your computer and use it in GitHub Desktop.
test script for cs416 assignment
import subprocess
import os
import re
import unittest
from unittest import TestSuite
from collections import defaultdict
class TestPrime(unittest.TestCase):
def __init__(self, num, pnum):
super(TestPrime, self).__init__('test_election')
self.num = num
self.pnum = pnum
def test_election(self):
ex = os.getcwd() + '/electleader'
p = subprocess.Popen(['mpiexec', '-n', str(self.num), ex, str(self.pnum)], stdout=subprocess.PIPE)
out, err = p.communicate()
uids = []
leaders = []
lines = out.split('\n')
lines = [l for l in lines if len(l) > 10]
for line in lines:
out_group = re.search('id=(\d+)', line)
leader_group = re.search('leader=(\d+)', line)
uid = int(out_group.group(1))
leader = int(leader_group.group(1))
uids.append(uid)
leaders.append(leader)
self.assertEqual(1, len(set(leaders)))
self.assertEqual(max(uids), leaders[0])
print "Done num: %d pnum: %d" % (self.num, self.pnum)
def main():
suite = TestSuite()
def farey( n, asc=True ):
pairs = []
"""Python function to print the nth Farey sequence, either ascending or descending."""
if asc:
a, b, c, d = 0, 1, 1 , n # (*)
else:
a, b, c, d = 1, 1, n-1 , n # (*)
while (asc and c <= n) or (not asc and a > 0):
k = int((n + b)/d)
a, b, c, d = c, d, k*c - a, k*d - b
pairs.append((a, b))
return pairs
for num, pnum in farey(30):
if num > 1 and pnum > 1:
suite.addTest(TestPrime(num, pnum))
unittest.TextTestRunner().run(suite)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment