示例#1
0
文件: basic.py 项目: marthagb/voting
 def test_max_entropy_calculation(self):
     rules = ElectionRules()
     rules["debug"] = True
     rules["show_entropy"] = True
     rules["parties"] = ["A", "B"]
     rules["adjustment_method"] = "alternating-scaling"
     rules["constituency_names"] = ["I", "II"]
     rules["constituency_seats"] = [2, 3]
     rules["constituency_adjustment_seats"] = [1, 2]
     votes = [[500, 400], [300, 200]]
     election = Election(rules, votes)
     election.run()
     self.assertEqual(round(election.entropy(), 2), 42.95)
示例#2
0
class TestEntropy(unittest.TestCase):
    def setUp(self):
        self.rules = ElectionRules()
        self.rules["debug"] = True
        self.rules["show_entropy"] = True
        self.rules["parties"] = ["A", "B"]
        self.rules["adjustment_method"] = "alternating-scaling"
        self.rules["constituency_names"] = ["I", "II"]
        self.rules["constituency_seats"] = [2, 3]
        self.rules["constituency_adjustment_seats"] = [1, 2]
        self.votes = [[500, 400], [300, 200]]
        self.election = Election(self.rules, self.votes)
        self.election.run()

    def test_entropy_calculation(self):
        self.assertEqual(round(self.election.entropy(), 2), 42.95)

    def test_entropy_depenency_on_divisor(self):
        dd_entropy = self.election.entropy()
        ds_entropy = entropy(self.votes, self.election.results,
                             sainte_lague_gen)
        self.rules["primary_divider"] = "sainte-lague"
        self.rules["adj_determine_divider"] = "sainte-lague"
        self.rules["adj_alloc_divider"] = "sainte-lague"
        self.sl_election = Election(self.rules, self.votes)
        self.sl_election.run()
        ss_entropy = self.sl_election.entropy()
        sd_entropy = entropy(self.votes, self.sl_election.results, dhondt_gen)
        self.assertNotEqual(ds_entropy, dd_entropy)
        self.assertNotEqual(ss_entropy, dd_entropy)
        self.assertNotEqual(ss_entropy, sd_entropy)
        self.assertNotEqual(ds_entropy, sd_entropy)
        self.assertEqual(round(dd_entropy, 2), 42.95)
        self.assertEqual(round(ds_entropy, 2), 41.22)
        self.assertEqual(round(ss_entropy, 2), 41.22)
        self.assertEqual(round(sd_entropy, 2), 42.95)