Skip to content

Commit

Permalink
minor test cleanups. conform stuff to pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed Oct 10, 2017
1 parent f158021 commit 70c5f20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
10 changes: 4 additions & 6 deletions test/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 29 additions & 22 deletions test/test_forkchoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import forkchoice
import random as r


class TestForkchoice(unittest.TestCase):

def test_single_validator_correct_forkchoice(self):
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -111,6 +119,5 @@ def test_max_weight_indexes_tie(self):
self.assertEqual(set(weight.keys()), max_weight_indexes)



if __name__ == "__main__":
unittest.main()

0 comments on commit 70c5f20

Please sign in to comment.