dt, p_i = self.some_particle(ParticleType.Fluid) p_e = copy(p_i) # Act p_i = i.predict(dt, p_i, 0) p_i = i.correct(dt, p_i, 0) p_e = e.predict(dt, p_e) p_e = e.correct(dt, p_e) # Verify self.assertAlmostEqual(p_i['x'], p_e['x']) self.assertAlmostEqual(p_i['y'], p_e['y']) self.assertAlmostEqual(p_i['vx'], p_e['vx']) self.assertAlmostEqual(p_i['vy'], p_e['vy']) self.assertAlmostEqual(p_i['rho'], p_e['rho']) def some_particle(self, label: int): dt = 2.0 p = np.zeros(1, dtype=particle_dtype) p['label'] = label p['vx'] = 1.0 p['vy'] = 3.0 p['ax'] = 5.0 p['ay'] = 0 p['drho'] = 10 return (dt, p) if __name__ == "__main__": unittest.test()
def printHist(aHistogram): import math theMax = max(aHistogram) numDigits = math.ceil(math.log10(len(aHistogram))) formatter = "%" + str(numDigits) + "d " for i, count in enumerate(aHistogram): print((formatter % i) + ("#" * int(round(10.0 * count / theMax)))) if __name__ == "__main__": from unittest import test test(8, collatzify(16)) test(14, collatzify(28)) test(10, collatzify(3)) test([1], collatzSequence(1)) test([5,16,8,4,2,1], collatzSequence(5)) test([7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1], collatzSequence(7)) test(0, frequency(5,[1,2,3,4])) test(1, frequency(3,[1,2,3,4])) test(5, frequency(6,[1,6,6,6,6,7,6])) test([1,1,1,1,1,1,1,1,1,1], histogram(range(10))) test([1,2,2,0,1,0,1,3,1,1,0,0,0,0,0,0,0,0,0,1], histogram([6,4,2,8,7,7,9,0,1,1,2,7,19])) test([0]*25 + [1], histogram([25]))
for (suitor, suited) in itertools.product(suitors, suiteds): if suitor not in marriage[suited] and suitorPrefers( suitor, suited) and suitedPrefers(suited, suitor): return False return True if __name__ == "__main__": from unittest import test suitors = [Suitor(0, [0, 1]), Suitor(1, [1, 0])] suiteds = [Suited(0, [0, 1], 1), Suited(1, [1, 0], 1)] marriage = stableMarriage(suitors, suiteds) test({suiteds[0]: [suitors[0]], suiteds[1]: [suitors[1]]}, marriage) test(True, verifyStable(suitors, suiteds, marriage)) suitors = [Suitor(0, [0]), Suitor(1, [0]), Suitor(2, [0]), Suitor(3, [0])] suiteds = [Suited(0, [0, 1, 2, 3], 4)] marriage = stableMarriage(suitors, suiteds) test({suiteds[0]: suitors}, marriage) test(True, verifyStable(suitors, suiteds, marriage)) suitors = [ Suitor(0, [0, 1]), Suitor(1, [0, 1]), Suitor(2, [0, 1]), Suitor(3, [0, 1]) ] suiteds = [Suited(0, [0, 1, 2, 3], 2), Suited(1, [3, 2, 1, 0], 2)]
from unittest import test # integers are houses, chars are agents agents = {'a','b','c','d','e','f'} houses = {1,2,3,4,5,6,} # test extracting agents from a cycle G = Graph([1,'a',2,'b',3,'c',4,'d',5,'e',6,'f']) G.addEdges([(1,'a'), ('a',2), (2,'b'), ('b',3), (3,'c'), ('c',1), (4,'d'), ('d',5), (5,'e'), ('e',4), (6,'f'), ('f',6)]) oneCycle = G[6] twoCycle = G[4] threeCycle = G[1] test({G['f']}, getAgents(G, oneCycle, agents)) test({G['d'],G['e']}, getAgents(G, twoCycle, agents)) test({G['a'],G['b'],G['c']}, getAgents(G, threeCycle, agents)) # test getting an arbitrary cycle G = Graph([1,'a',2,'b',3,'c',4,'d',5,'e',6,'f']) # a graph which is a single cycle plus a tail G.addEdges([(1,'a'), ('a',2), (2,'b'), ('b',3), (3,'c'), ('c',4), (4,'d'), ('d',5), (5,'e'), ('e',6), (6,'f'), ('f',4)]) test({G['f'], G['d'], G['e']}, getAgents(G, anyCycle(G), agents)) # preferences form a disjoint union of cycles # on the very first round agentPreferences = { 'a': [2,3,4,5,6,1],
self.__connection.getresponse().read() self.__connection.request( "GET", self.__BASE_URI + "/add_poi?name=Churrascaria&x=28&y=2") self.__connection.getresponse().read() def test_list_pois(self): # type: () -> None """ Verify that all inserted points are listed. """ self.__connection.request("GET", self.__BASE_URI + "/list_pois") response = self.__connection.getresponse().read() # remove trailing spaces, convert to string and make a list from each line response = str(response.strip(), "utf-8").splitlines() self.assertEqual(response, self.__EXPECTED_LIST) def test_list_nearby(self): # type: () -> None """ Verify that the proper nearby points are determined. """ self.__connection.request( "GET", self.__BASE_URI + "/list_nearby?x=20&y=10&d_max=10") response = self.__connection.getresponse().read() # remove trailing spaces, convert to string and make a list from each line response = str(response.strip(), "utf-8").splitlines() self.assertEqual(response, self.__EXPECTED_NEARBY) if __name__ == "__main__": test()
from unittest import test from notes import * test(29, greet("Jeremy"))
agentPreferences = agents[:] random.shuffle(agentPreferences) allocation = dict() availableHouses = set(objects[:]) for agentIndex, preference in enumerate(agentPreferences): allocation[agentIndex] = max(availableHouses, key=preference.index) availableHouses.remove(allocation[agentIndex]) return allocation if __name__ == "__main__": from unittest import test agents = [["a", "b", "c", "d"], ["a", "b", "c", "d"], ["a", "b", "c", "d"], ["a", "b", "c", "d"]] objects = ["a", "b", "c", "d"] allocation = serialDictatorship(agents, objects, seed=1) test({0: "d", 1: "c", 2: "b", 3: "a"}, allocation) agents = [ ["d", "a", "c", "b"], # 4th ["a", "d", "c", "b"], # 3rd ["a", "d", "b", "c"], # 2nd ["d", "a", "c", "b"], ] # 1st objects = ["a", "b", "c", "d"] allocation = serialDictatorship(agents, objects, seed=1) test({0: "b", 1: "c", 2: "d", 3: "a"}, allocation)
return any(map(lambda x: precedes(school.prefList, student.id, x.id), marriage[school])) for (student, school) in itertools.product(students, schools): if student not in marriage[school] and studentPrefers(student, school) and schoolPrefers(school, student): return False return True if __name__ == "__main__": from unittest import test students = [Student(0, [0,1]), Student(1, [1,0]),Student(2, [0,1])] schools = [School(0, [0,1,2], 1), School(1, [1,0,2], 2)] marriage = IAMarriage(students, schools) test({schools[0]:[students[0]], schools[1]:[students[1]]}, marriage) test(True, verifyStable(students, schools, marriage)) students = [Student(0, [0]), Student(1, [0]), Student(2, [0]), Student(3, [0])] schools = [School(0, [0,1,2,3], 4)] marriage = IAMarriage(students, schools) test({schools[0]: students}, marriage) test(True, verifyStable(students, schools, marriage)) students = [Student(0, [0,1,2]), Student(1, [0,1,2]), Student(2, [0,1,2]), Student(3, [0,1,2]),Student(4, [1,0,2]), Student(5, [1,0,2])] schools = [School(0, [0,1,2,3,4,5], 2), School(1, [3,2,1,0,4,5], 2),School(2, [3,2,1,0,4,5], 2)] marriage = IAMarriage(students, schools, verbose = True) test({schools[0]: students[:2], schools[1]: students[2:]}, marriage) test(True, verifyStable(students, schools, marriage))
agentPreferences = agents[:] random.shuffle(agentPreferences) allocation = dict() availableHouses = set(objects[:]) for agentIndex, preference in enumerate(agentPreferences): allocation[agentIndex] = max(availableHouses, key=preference.index) availableHouses.remove(allocation[agentIndex]) return allocation if __name__ == "__main__": from unittest import test agents = [['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']] objects = ['a', 'b', 'c', 'd'] allocation = serialDictatorship(agents, objects, seed=1) test({0: 'd', 1: 'c', 2: 'b', 3: 'a'}, allocation) agents = [ ['d', 'a', 'c', 'b'], # 4th ['a', 'd', 'c', 'b'], # 3rd ['a', 'd', 'b', 'c'], # 2nd ['d', 'a', 'c', 'b'] ] # 1st objects = ['a', 'b', 'c', 'd'] allocation = serialDictatorship(agents, objects, seed=1) test({0: 'b', 1: 'c', 2: 'd', 3: 'a'}, allocation)
for (student, school) in itertools.product(students, schools): if student not in marriage[school] and studentPrefers( student, school) and schoolPrefers(school, student): return False return True if __name__ == "__main__": from unittest import test students = [Student(0, [0, 1]), Student(1, [1, 0]), Student(2, [0, 1])] schools = [School(0, [0, 1, 2], 1), School(1, [1, 0, 2], 2)] marriage = IAMarriage(students, schools) test({schools[0]: [students[0]], schools[1]: [students[1]]}, marriage) test(True, verifyStable(students, schools, marriage)) students = [ Student(0, [0]), Student(1, [0]), Student(2, [0]), Student(3, [0]) ] schools = [School(0, [0, 1, 2, 3], 4)] marriage = IAMarriage(students, schools) test({schools[0]: students}, marriage) test(True, verifyStable(students, schools, marriage)) students = [ Student(0, [0, 1, 2]),
from structurechecker import * from unittest import test import a test([], checkHierarchy(a, [ 'f1', 'f2', 'f3', ], depth="a")) test(['a.f4'], checkHierarchy(a, [ 'f1', 'f2', 'f4', ], depth="a")) import b test([], checkHierarchy(b, {'Dog': [], 'Cat': ['meow']}, depth="b")) test(['b.Llama'], checkHierarchy(b, {'Dog': [], 'Llama': ['meow']}, depth="b")) test(['b.Cat.woof'], checkHierarchy(b, { 'Dog': [], 'Cat': ['meow', 'woof'] }, depth="b")) test(['b.Dog.Head'], checkHierarchy(b, { 'Dog': { 'Head': [] }, 'Cat': ['meow'] }, depth="b")) test(['b.bye'],
from structurechecker import * from unittest import test import a test([], checkHierarchy(a, ["f1", "f2", "f3"], depth="a")) test(["a.f4"], checkHierarchy(a, ["f1", "f2", "f4"], depth="a")) import b test([], checkHierarchy(b, {"Dog": [], "Cat": ["meow"]}, depth="b")) test(["b.Llama"], checkHierarchy(b, {"Dog": [], "Llama": ["meow"]}, depth="b")) test(["b.Cat.woof"], checkHierarchy(b, {"Dog": [], "Cat": ["meow", "woof"]}, depth="b")) test(["b.Dog.Head"], checkHierarchy(b, {"Dog": {"Head": []}, "Cat": ["meow"]}, depth="b")) test(["b.bye"], checkHierarchy(b, {"Dog": [], "Cat": ["meow"], "bye": [], "hi": []}, depth="b")) test( ["a.f4", "b.Dog.woof", "b.bye", "c"], checkStructure( [a, b], { "a": ["f1", "f2", "f4"], "b": {"Dog": {"Tail": [], "woof": []}, "Cat": ["meow"], "bye": [], "hi": []}, "c": [], }, ), )
from unittest import test # greet: string -> string # return a string which greets the user whose name is input def greet(name): return "Hello, " + name # test(expected, actual) test("Hello, ", greet("")) test("Hello, Jeremy", greet("Jeremy")) test("Hello, liae!", greet("liae!")) #listA = [1,2,3,4] #for element in listA: # print(element + 1) # #listA[2] # add1: [int] -> [int] # create a new list which adds1 to each element of the input list def add1(myList): outputList = [] for x in myList: outputList.append(x+1)