def test_sums(self): # Calculates the sum: def sign_sum(array, signs): s = 0 for i in range(len(array)): s += array[i] * signs[i] return s # Checks the different lengths for N in range(1, 20, 1): for j in range(0,10): # Checks the different arrays array = [random.randint(-100,100) for x in range(N)] # Generates the permutations: minimum = sys.maxsize multiple_signs = itertools.product([-1,1], repeat=N) # Checks the real minimum sum for sign in multiple_signs: s = abs(sign_sum(array, sign)) if s < minimum: minimum = s # Checks the answer self.assertEqual(minimum, main.calculate(array))
def test_harder(self): self.assertEqual(calculate(usb_size2, memes2), (10850, {'a', 'c', 'f', 'g'}))
def test_simple(self): self.assertEqual( calculate(usb_size, memes), (22, {'yodeling_kid.avi', 'sad_pepe_compilation.gif'}))
def test_answer(): print("Start of loading files...") inverted_file = operation_file(datafolder, filename, map) print("End of loading files.") algoF, algoN, algoFT, algoFTE = init(inverted_file) print("End of init.") ans = [0, 0, 0, 0] t = [0, 0, 0, 0] rate = [0.0, 0.0, 0.0, 0.0] print("Start of running the tests...") bar.start() list_nbterms = [] list_k = [] list_e = [] list_times = [[], [], [], []] list_results = [[], [], [], []] k = 0 while k < NbTest: #while True: bar.update(k + 1) N = k_fagins ep = epsilon nterms = N_terms if test_k: N = random.randint(1, 100) terms = [] if test_nbterms: nterms = random.randint(1, 10) if test_epsilon: ep = random.random() #ep = random.randint(0,1000) # N_terms = 1 # if test_epsilon: # terms = ["youth", "young"] # else: for i in range(nterms): term = random.choice(list(inverted_file.inverted_file.keys()) ) # get random terms from dictionary while inverted_file.inverted_file[term]['size'] < 500: term = random.choice(list(inverted_file.inverted_file.keys())) terms.append(term) #print("-------------ans--------------") #print(k) #print(terms) for op_algo in [0, 1, 2, 3]: t1 = time.time() #import timeit #t[op_algo] = timeit.timeit("calculate(op_algo,N,terms,algoF,algoN,algoFT,algoFTE)", globals=globals()) #cProfile.run('calculate(op_algo,N,terms,algoF,algoN,algoFT,algoFTE)') ans[op_algo] = calculate(op_algo, N, terms, algoF, algoN, algoFT, algoFTE, ep) t2 = time.time() t[op_algo] = t2 - t1 #list_times[op_algo].append(t[op_algo]) #pprint.pprint(ans[op_algo]) #Sprint(t[op_algo]) for j in range(len(ans[3])): for i in [1, 2]: #assert(ans[i][j].score == ans[0][j].score) if abs(ans[i][j].score - ans[0][j].score) > 0.00001: # if ans[i][j].score != ans[0][j].score : print("bad results for algo...." + str(i) + " " + str(terms) + " difference=" + str(ans[i][j].score - ans[0][j].score)) for j in range(len(ans[3])): if abs(ans[i][j].score - ans[0][j].score) / ans[0][j].score > EPSILON: print("bad results for algo...." + str(i) + " " + str(terms) + " difference rate=" + str((ans[i][j].score - ans[0][j].score) / ans[0][j].score)) if len(ans[0]) > 50: k += 1 rate[1] += t[1] / t[0] rate[2] += t[2] / t[0] rate[3] += t[3] / t[0] list_times[0].append(1) list_times[1].append(t[1] / t[0]) list_times[3].append(t[3] / t[0]) list_times[2].append(t[2] / t[0]) list_nbterms.append(nterms) list_k.append(N) list_e.append(ep) list_results[0].append(len(ans[0])) list_results[1].append(len(ans[1])) list_results[2].append(len(ans[2])) list_results[3].append(len(ans[3])) if t[1] / t[0] > 1: print("time too long for algo 1 " + str(terms) + str(t[1] / t[0])) if t[2] / t[0] > 1: print("time too long for algo 2 " + str(terms) + str(t[2] / t[0])) if t[3] / t[0] > 1: print("time too long for algo 3 " + str(terms) + str(t[3] / t[0])) #print("-------------ans--------------\n") bar.finish() Nb = len(list_nbterms) if Nb == 0: print("no results") else: print("Fagins :") print(" " + str(int((1 - rate[1] / Nb) * 100)) + "% acceleration compared with naive algo.") print("Fagins Threshold :") print(" " + str(int((1 - rate[2] / Nb) * 100)) + "% acceleration compared with naive algo.") print(" " + str(int((1 - rate[2] / rate[1]) * 100)) + "% acceleration compared with Fagins.") print("Fagins Threshold With Epsilon:") print(" " + str(int((1 - rate[3] / Nb) * 100)) + "% acceleration compared with naive algo.") print(" " + str(int((1 - rate[3] / rate[1]) * 100)) + "% acceleration compared with Fagins.") print(" " + str(int((1 - rate[3] / rate[2]) * 100)) + "% acceleration compared with Fagins Threshold.") plt.title("4 algo") plt.plot(list_nbterms, list_times[0], 'ro') list_nbterms = [x + 0.1 for x in list_nbterms] plt.plot(list_nbterms, list_times[1], 'go') list_nbterms = [x + 0.1 for x in list_nbterms] plt.plot(list_nbterms, list_times[2], 'bo') list_nbterms = [x + 0.1 for x in list_nbterms] plt.plot(list_nbterms, list_times[3], 'ko') plt.show() plt.title("k for fagins") plt.plot(list_k, list_times[1], 'go') list_k = [x + 0.1 for x in list_k] plt.plot(list_k, list_times[2], 'bo') list_k = [x + 0.1 for x in list_k] plt.plot(list_k, list_times[3], 'ko') plt.show() plt.title("e for fagins_e") plt.plot(list_e, list_times[3], 'ko') plt.show() plt.title("lenght results") plt.plot(list_results[0], list_times[0], 'ro') list_nbterms = [x + 0.1 for x in list_results[1]] plt.plot(list_results[1], list_times[1], 'go') list_nbterms = [x + 0.2 for x in list_results[2]] plt.plot(list_results[2], list_times[2], 'bo') list_nbterms = [x + 0.3 for x in list_results[3]] plt.plot(list_results[3], list_times[3], 'ko') plt.show()
print( f"Test Generated in {end_test_creating_time}, amount of memes = {len(tests[-1].memes)}" ) # Saving All Informations in two files # In test_passed_file, only information if testpassed # in test_log informations about every test test_passed_file = open("testpassed.txt", "w+") test_log = open("testlog.txt", "w+") test_position = 0 passed = 0 not_passed = 0 passed_but_problem = 0 for test in tests: start_time = time.time() algorithm_answer = calculate(test.usb_size_GiB, test.rawMemes) calculate_elapsed_time = round(time.time() - start_time, 2) if algorithm_answer == test.expected: test_passed_file.write(f" Test {test_position} passed \n") passed += 1 elif algorithm_answer[0] == test.expected[0]: test_passed_file.write( f" Test {test_position} passed Same USB Stick Value: {test.expected[0]}, but not same names\n" ) passed_but_problem += 1 else: test_passed_file.write( f" Test {test_position} not passed ======== FLAG ========= \n") not_passed += 1
def test_calculate_two_same_meme(self): usb_size = 165 / 1024. memes = [('a', 23, 92), ('a', 23, 92)] self.assertEqual((92, {'a'}), calculate(usb_size, memes))
def test_calculate_two_meme(self): usb_size = 165 / 1024. memes = [('a', 33, 92), ('b', 33, 92)] self.assertEqual((184, {'a', 'b'}), calculate(usb_size, memes))
def test_calculate(self): self.assertEqual(main.calculate('2 + 2'), 4) self.assertEqual(main.calculate('3 * 4 * 5'), 60) self.assertEqual(main.calculate('5 * (2 + 2)'), 20) self.assertEqual(main.calculate('1 + 1 / 2'), 1.5) self.assertEqual(main.calculate('pi * 2'), math.pi * 2)
def test_empty_list(self): """ memes or usb_size is 0 -> 0 """ self.assertEqual(0, calculate(123, [])) self.assertEqual(0, calculate(0, [1, 2, 3])) self.assertEqual(0, calculate(0, 0))
def cct(): topString.set(main.calculate(topString.get()))
def on_button_click(self): main.calculate(self.MiddlestNum.get(), self.NumofDice.get())
def test_duplicate(self): self.assertEqual( calculate(self.usb_size, self.good_memes * 2), (22, {"sad_pepe_compilation.gif", "yodeling_kid.avi"}), )
def test_no_data(self): self.assertEqual(calculate(0, []), (0, set()))
def test_calculate(usb_size, memes, result): assert result == calculate(usb_size, memes)
def test_calculate_for_empty_memes_list(self): usb_size = 1 memes = [] results = calculate(usb_size, memes) self.assertEqual((0, frozenset()), results, "Calculate function do not return empty set")
#!/usr/bin/python3 from main import calculate with open('test_file') as file: capacity = int(file.readline()) memes = [] for line in file: meme = line.split() meme[1] = int(meme[1]) meme[2] = int(meme[2]) meme = tuple(meme) memes.append(meme) print(calculate(capacity, memes))