def dft(seq, s): Accounting.start_task("dft") n = len(seq) omega = cmath.exp((2 * cmath.pi * 1j) / n) res = sum(seq[k] * omega ** (k + s) for k in range(n)) Accounting.finish_task("dft") return res
def check_diophantine_invariant(A, B): Accounting.start_task('check_diophantine_invariant') v = len(A) a = sum(A) b = sum(B) # p.279 in "New Results..." # # By pre- and post-multiplying equation (1) with J_v, one obtains that # a^2 + b^2 = 4v−2, # where a and b are row sums of A and B # Accounting.finish_task('check_diophantine_invariant') return equal(a ** 2 + b ** 2, 4*v - 2)
class TestAccounting(unittest.TestCase): def setUp(self): self.a = Accounting() def tearDown(self): pass def test_add(self): self.assertEqual(self.a.add(1, 2, 3), 6, "Wrong additions") def test_sub(self): self.assertEqual(self.a.sub(1, 2, 3), -4, "Wrong subtractions") def test_mul(self): self.assertEqual(self.a.mul(1, 2, 3), 6, "Wrong multiplications") def test_div(self): self.assertAlmostEqual(self.a.div(1, 2, 3), 0.17, places=2, msg="Wrong divisions") # Not Unit Test # Reason: not single unit. involves opening a "instructions.yaml" # file and load as a list of dictionaries def test_process_ali(self): self.assertEqual(self.a.process("ali"), 5, "Wrong calculation") def test_process_ahhuat(self): self.assertEqual(self.a.process("ahhuat"), 33, "Wrong calculation") def test_process_mutu(self): self.assertEqual(self.a.process("mutu"), 0, "Wrong calculation") def test_process_paul(self): with self.assertRaisesRegex(Exception, "Haha Very Funny"): self.a.process("paul") # Unit Test @patch('builtins.open') @patch('yaml.load', return_value={'bob': [{ 'initial': 1 }, { 'add': [2, 3] }, { 'sub': 1 }]}) def test_process(self, mock_load, mock_open): self.a = Accounting() self.assertEqual(self.a.process("bob"), 5, "Wrong calculation")
def setUp(self): """ test up method. Returns: none (NoneType): None """ self.accounting = Accounting()
class TestAccounting(unittest.TestCase): def setUp(self): self.a = Accounting() def tearDown(self): pass def test_add(self): self.assertEqual(self.a.add(1,2,3), 6, "Wrong additions") def test_sub(self): self.assertEqual(self.a.sub(1,2,3), -4, "Wrong subtractions") def test_mul(self): self.assertEqual(self.a.mul(1,2,3), 6, "Wrong multiplications") def test_div(self): self.assertAlmostEqual(self.a.div(1,2,3), 0.17, places=2, msg="Wrong divisions") # Not Unit Test # Reason: not single unit. involves opening a "instructions.yaml" # file and load as a list of dictionaries def test_process_ali(self): self.assertEqual(self.a.process("ali"), 5, "Wrong calculation") def test_process_ahhuat(self): self.assertEqual(self.a.process("ahhuat"), 33, "Wrong calculation") def test_process_mutu(self): self.assertEqual(self.a.process("mutu"), 0, "Wrong calculation") def test_process_paul(self): with self.assertRaisesRegex(Exception, "Haha Very Funny"): self.a.process("paul") # Unit Test @patch('builtins.open') @patch('yaml.load', return_value={'bob': [{'initial': 1}, {'add': [2, 3]}, {'sub': 1}]}) def test_process(self, mock_load, mock_open): self.a = Accounting() self.assertEqual(self.a.process("bob"), 5, "Wrong calculation")
class TestAccounting(unittest.TestCase): accounting = Accounting() def test_start_connection(self): self.assertEqual(self.accounting.start_connection(), "Connection Successful") def test_calculate_income(self): self.assertEqual( self.accounting.calculate_ticket_income(2), "The total income from ticket sales for flight 2 is: £1180.00")
def setUp(self): """ test up method. Returns: none (NoneType): None """ self.options = { 'number': { 'thousand': ',', 'precision': 0, 'decimal': '.', 'grouping': 3 } } self.accounting = Accounting(self.options)
def check_sequence_invariants(aa, bb): Accounting.start_task('check_sequence_invariants') #r = check_diophantine_invariant(aa, bb) #if not r: # Accounting.finish_task('check_sequence_invariants') # return False r = check_paf_invariant(aa, bb) if not r: Accounting.finish_task('check_sequence_invariants') return False #r = check_psd_invariant(aa, bb) Accounting.finish_task('check_sequence_invariants') return r
def paf(seq, i): Accounting.start_task("paf") n = len(seq) res = sum(seq[k] * seq[(k + i) % n] for k in range(n)) Accounting.finish_task("paf") return res
def psd(seq, s): Accounting.start_task("psd") d = dft(seq, s) magnitude = d.real ** 2 + d.imag ** 2 Accounting.finish_task("psd") return magnitude
def check_psd_invariant(A, B): Accounting.start_task('check_psd_invariant') v = len(A) # p.280 in "New Results..." # # define a circulant D-optimal matrix implies that the sum # PSD_A(s) + PSD_B(s), s ≠ 0, is equal to the constant 2v − 2 # cond = 2*v - 2 for k in range(1, v+1): Accounting.start_task('check_psd_invariant_step') # trick here: # non negativity of PSD. we can discard before computing PSD both times # but first I have to deal with rounding error. psd > cond with very # low residual psd_a = psd(A, k) psd_b = psd(B, k) if not equal(psd_a + psd_b, cond): Accounting.finish_task('check_psd_invariant_step') Accounting.finish_task('check_psd_invariant') return False Accounting.finish_task('check_psd_invariant_step') Accounting.finish_task('check_psd_invariant') return True
Accounting.start_task('check_sequence_invariants') #r = check_diophantine_invariant(aa, bb) #if not r: # Accounting.finish_task('check_sequence_invariants') # return False r = check_paf_invariant(aa, bb) if not r: Accounting.finish_task('check_sequence_invariants') return False #r = check_psd_invariant(aa, bb) Accounting.finish_task('check_sequence_invariants') return r if __name__ == '__main__': Accounting.start_task('_program') N = int(sys.argv[1]) matches = [] iterations = 0 max_possible = (2**N)**2 print("max_possible:", max_possible) iter_mod = max_possible // 23 for aa in all_possible_sequences(N): for bb in all_possible_sequences(N): iterations += 1 percent_done = iterations / max_possible if iterations % iter_mod == 0 or iterations == max_possible: print("Percent checked: {0:>7.3f}% Matches found: {1}".format(
def setUp(self): self.a = Accounting()
def test_process(self, mock_load, mock_open): self.a = Accounting() self.assertEqual(self.a.process("bob"), 5, "Wrong calculation")
def cashflow_report(conn, cur): option = input( "Cashflow report phase > \nEnter 1.CAMPAIGN / 2.SUPPORTER option number: " ) if int(option) is 1: op = Accounting() op.op_list(cur) print("\nCampaign cashflow bar chart: ") op.op_graph(cur) elif int(option) is 2: sp = Accounting() sp.sp_list(cur) print("\nSupporter cashflow bar chart: ") sp.sp_graph(cur) return 1
def check_paf_invariant(A, B): # only check half of sequence due to PAF symmetry Accounting.start_task('check_paf_invariant') v = len(A) for s in range(1, len(A)//2+1): Accounting.start_task('check_paf_invariant_step') paf_a = paf(A, s) paf_b = paf(B, s) if not paf_a + paf_b == 2: Accounting.finish_task('check_paf_invariant_step') Accounting.finish_task('check_paf_invariant') return False Accounting.finish_task('check_paf_invariant_step') Accounting.finish_task('check_paf_invariant') return True