diff --git a/test/test_block.py b/test/test_block.py index 90034dc..b845fb3 100644 --- a/test/test_block.py +++ b/test/test_block.py @@ -3,30 +3,29 @@ from justification import Justification import unittest import settings as s -import random as r import copy + class TestUtils(unittest.TestCase): def test_equality_of_copies_off_genesis(self): - s.update([10]) # necessary for now due to assertions during block creation + s.update([10]) # necessary due to assertions during block creation block = Block(None, Justification(), 0) - shallow_copy = copy.copy(block) + shallow_copy = copy.copy(block) deep_copy = copy.deepcopy(block) self.assertEqual(block, shallow_copy) self.assertEqual(block, deep_copy) self.assertEqual(shallow_copy, deep_copy) - def test_equality_of_copies_of_non_genesis(self): test_string = "B0-A S1-A B1-B S0-B B0-C S1-C B1-D S0-D H0-D" testLang = TestLangCBC(test_string, [10, 11]) testLang.parse() for b in testLang.blocks: - shallow_copy = copy.copy(b) + shallow_copy = copy.copy(b) deep_copy = copy.deepcopy(b) self.assertEqual(b, shallow_copy) @@ -56,7 +55,6 @@ def test_non_equality_of_copies_of_non_genesis(self): self.assertEqual(num_equal, len(testLang.blocks)) - def test_not_in_blockchain_off_genesis(self): s.update([10, 11]) block_0 = Block(None, Justification(), 0) diff --git a/test/test_forkchoice.py b/test/test_forkchoice.py index 5332a7b..592ce73 100644 --- a/test/test_forkchoice.py +++ b/test/test_forkchoice.py @@ -3,6 +3,7 @@ import forkchoice import random as r + class TestForkchoice(unittest.TestCase): def test_single_validator_correct_forkchoice(self): @@ -15,58 +16,68 @@ def test_single_validator_correct_forkchoice(self): testLang = TestLangCBC(test_string, [10]) testLang.parse() - def test_two_validators_round_robin_forkchoice(self): test_string = "B0-A S1-A B1-B S0-B B0-C S1-C B1-D S0-D H0-D R" testLang = TestLangCBC(test_string, [10, 11]) testLang.parse() - def test_many_val_round_robin_forkchoice(self): - """ This tests that during a perfect round robin, validators choose the one chain as their fork choice """ + """ + Tests that during a perfect round robin, + validators choose the one chain as their fork choice + """ test_string = "" for i in xrange(100): - test_string += "B" + str(i % 10) + "-" + str(i) + " " + "S" + str((i+1) % 10) \ - + "-" + str(i) + " " + "H" + str((i+1) % 10) + "-" + str(i) + " " + test_string += "B" + str(i % 10) + "-" + str(i) + " " \ + + "S" + str((i+1) % 10) + "-" + str(i) + " " \ + + "H" + str((i+1) % 10) + "-" + str(i) + " " test_string = test_string[:-1] - testLang = TestLangCBC(test_string, [10 - x + r.random() for x in xrange(10)]) + testLang = TestLangCBC( + test_string, + [x + r.random() for x in xrange(10, 0, -1)] + ) testLang.parse() def test_fail_on_tie(self): - """ This tests that if there are two subsets of the validator set with the same weight, the forkchoice fails """ + """ + Tests that if there are two subsets of the validator + set with the same weight, the forkchoice fails + """ test_string = "B1-A S0-A B0-B S1-B S2-A B2-C S1-C H1-C" testLang = TestLangCBC(test_string, [5, 6, 5]) with self.assertRaises(AssertionError): testLang.parse() - def test_ignore_zero_weight_validator(self): - """ This tests that a validator with zero weight will not affect the forkchoice """ + """ + Tests that a validator with zero weight + will not affect the forkchoice + """ test_string = "B0-A S1-A B1-B S0-B H1-A H0-A" testLang = TestLangCBC(test_string, [1, 0]) testLang.parse() - def test_ignore_zero_weight_block(self): """ Tests that the forkchoice ignores zero weight blocks """ - # for more info about test, see here: https://gist.github.com/naterush/8d8f6ec3509f50939d7911d608f912f4 - test_string = "B0-A1 B0-A2 H0-A2 B1-B1 B1-B2 S3-B2 B3-D1 H3-D1 S3-A2 H3-A2 B3-D2 S2-B1 H2-B1 B2-C1 H2-C1 S1-D1 S1-D2 S1-C1 H1-B2" + # for more info about test, see + # https://gist.github.com/naterush/8d8f6ec3509f50939d7911d608f912f4 + test_string = ("B0-A1 B0-A2 H0-A2 B1-B1 B1-B2 S3-B2 B3-D1 H3-D1 " + "S3-A2 H3-A2 B3-D2 S2-B1 H2-B1 B2-C1 H2-C1 S1-D1 " + "S1-D2 S1-C1 H1-B2") testLang = TestLangCBC(test_string, [10, 9, 8, .5]) testLang.parse() - def test_reverse_message_arrival_order_forkchoice_four_val(self): - test_string = "B0-A S1-A B1-B S0-B B0-C S1-C B1-D S0-D B1-E S0-E S2-E H2-E S3-A S3-B S3-C S3-D S3-E H3-E" + test_string = ("B0-A S1-A B1-B S0-B B0-C S1-C B1-D S0-D B1-E S0-E " + "S2-E H2-E S3-A S3-B S3-C S3-D S3-E H3-E") testLang = TestLangCBC(test_string, [5, 6, 7, 8.1]) testLang.parse() - def test_different_message_arrival_order_forkchoice_many_val(self): # TODO pass - def test_max_weight_indexes(self): weight = {i: i for i in xrange(10)} max_weight_indexes = forkchoice.get_max_weight_indexes(weight) @@ -87,18 +98,15 @@ def test_max_weight_indexes(self): self.assertEqual(len(max_weight_indexes), 2) self.assertEqual(set([4, 5]), max_weight_indexes) - def test_max_weight_indexes_empty(self): weight = dict() with self.assertRaises(ValueError): - max_weight_indexes = forkchoice.get_max_weight_indexes(weight) - + forkchoice.get_max_weight_indexes(weight) def test_max_weight_indexes_zero_score(self): weight = {i: 0 for i in xrange(10)} with self.assertRaises(AssertionError): - max_weight_indexes = forkchoice.get_max_weight_indexes(weight) - + forkchoice.get_max_weight_indexes(weight) def test_max_weight_indexes_tie(self): weight = dict() @@ -111,6 +119,5 @@ def test_max_weight_indexes_tie(self): self.assertEqual(set(weight.keys()), max_weight_indexes) - if __name__ == "__main__": unittest.main()