コード例 #1
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_landslide(self):

        # Generate data
        input = [{
            "count": 56,
            "ballot": ["c1", "c2", "c3"]
        }, {
            "count": 40,
            "ballot": ["c2", "c3", "c1"]
        }, {
            "count": 20,
            "ballot": ["c3", "c1", "c2"]
        }]
        output = STV(input, required_winners=2).as_dict()

        # Run tests
        self.assertEqual(
            output, {
                'candidates':
                set(['c1', 'c2', 'c3']),
                'quota':
                39,
                'rounds': [{
                    'tallies': {
                        'c3': 20.0,
                        'c2': 40.0,
                        'c1': 56.0
                    },
                    'winners': set(['c2', 'c1'])
                }],
                'winners':
                set(['c2', 'c1'])
            })
コード例 #2
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_ian_jacobs_3(self):

        # Generate data
        input = [
            {
                "count": 3,
                "ballot": ["A", "X", "D"]
            },
            {
                "count": 3,
                "ballot": ["B", "X", "D"]
            },
            {
                "count": 2,
                "ballot": ["C", "X"]
            },
            {
                "count": 1,
                "ballot": ["D", "X"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(output["winners"], set(["A", "B", "C"]))
コード例 #3
0
    def test_stv_everyone_wins(self):

        # Generate data
        input = [
            {
                "count": 56,
                "ballot": ["c1", "c2", "c3"]
            },
            {
                "count": 40,
                "ballot": ["c2", "c3", "c1"]
            },
            {
                "count": 20,
                "ballot": ["c3", "c1", "c2"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output,
            {
                "candidates": set(["c1", "c2", "c3"]),
                "quota": 30,
                "rounds": [],
                "remaining_candidates": set(["c1", "c2", "c3"]),
                "winners": set(["c1", "c2", "c3"]),
            },
        )
コード例 #4
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_ian_jacobs_2(self):

        # Generate data
        input = [{
            "count": 1,
            "ballot": ["A", "B", "C"]
        }, {
            "count": 1,
            "ballot": ["A", "D", "B", "G"]
        }, {
            "count": 1,
            "ballot": ["D", "A", "C", "E", "B", "F"]
        }, {
            "count": 1,
            "ballot": ["E", "A", "C", "F", "B", "D"]
        }, {
            "count": 1,
            "ballot": ["F", "C", "B"]
        }, {
            "count": 1,
            "ballot": ["C", "F", "A", "G"]
        }, {
            "count": 1,
            "ballot": ["A", "E", "B", "D", "C", "F"]
        }, {
            "count": 1,
            "ballot": ["F", "E", "D", "A", "C", "B", "G"]
        }, {
            "count": 1,
            "ballot": ["C", "F", "D"]
        }, {
            "count": 1,
            "ballot": ["C", "A", "E", "F", "D", "B", "G"]
        }, {
            "count": 1,
            "ballot": ["F", "B", "A", "C", "E", "D"]
        }, {
            "count": 1,
            "ballot": ["D", "A", "B", "F", "E", "C", "G"]
        }, {
            "count": 1,
            "ballot": ["D", "B", "C", "F", "A", "E", "G"]
        }, {
            "count": 1,
            "ballot": ["A", "F", "C", "E", "B", "G"]
        }, {
            "count": 1,
            "ballot": ["F", "A", "B"]
        }]
        output = STV(input, required_winners=4).as_dict()

        # Run tests
        self.assertEqual(output["winners"], set(['A', 'C', 'D', 'F']))
コード例 #5
0
    def test_stv_single_ballot(self):

        # Generate data
        input = [
            {
                "count": 1,
                "ballot": ["c1", "c2", "c3", "c4"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output,
            {
                "candidates":
                set(["c1", "c2", "c3", "c4"]),
                "quota":
                1,
                "rounds": [
                    {
                        "tallies": {
                            "c1": 1.0,
                            "c2": 0,
                            "c3": 0,
                            "c4": 0
                        },
                        "winners": set(["c1"]),
                    },
                    {
                        "note": "reset",
                        "tallies": {
                            "c2": 1.0,
                            "c3": 0,
                            "c4": 0
                        },
                        "winners": set(["c2"]),
                    },
                    {
                        "note": "reset",
                        "tallies": {
                            "c3": 1.0,
                            "c4": 0
                        },
                        "winners": set(["c3"]),
                    },
                ],
                "winners":
                set(["c1", "c2", "c3"]),
            },
        )
コード例 #6
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_single_ballot(self):

        # Generate data
        input = [
            {
                "count": 1,
                "ballot": ["c1", "c2", "c3", "c4"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output, {
                'candidates':
                set(['c1', 'c2', 'c3', 'c4']),
                'quota':
                1,
                'rounds': [
                    {
                        'tallies': {
                            'c1': 1.0,
                            'c2': 0,
                            'c3': 0,
                            'c4': 0
                        },
                        'winners': set(['c1'])
                    },
                    {
                        'note': 'reset',
                        'tallies': {
                            'c2': 1.0,
                            'c3': 0,
                            'c4': 0
                        },
                        'winners': set(['c2'])
                    },
                    {
                        'note': 'reset',
                        'tallies': {
                            'c3': 1.0,
                            'c4': 0
                        },
                        'winners': set(['c3'])
                    },
                ],
                'winners':
                set(['c1', 'c2', 'c3'])
            })
コード例 #7
0
    def test_stv_landslide(self):

        # Generate data
        input = [
            {
                "count": 56,
                "ballot": ["c1", "c2", "c3"]
            },
            {
                "count": 40,
                "ballot": ["c2", "c3", "c1"]
            },
            {
                "count": 20,
                "ballot": ["c3", "c1", "c2"]
            },
        ]
        output = STV(input, required_winners=2).as_dict()

        # Run tests
        self.assertEqual(
            output,
            {
                "candidates":
                set(["c1", "c2", "c3"]),
                "quota":
                39,
                "rounds": [{
                    "tallies": {
                        "c3": 20.0,
                        "c2": 40.0,
                        "c1": 56.0
                    },
                    "winners": set(["c2", "c1"]),
                }],
                "winners":
                set(["c2", "c1"]),
            },
        )
コード例 #8
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_everyone_wins(self):

        # Generate data
        input = [{
            "count": 56,
            "ballot": ["c1", "c2", "c3"]
        }, {
            "count": 40,
            "ballot": ["c2", "c3", "c1"]
        }, {
            "count": 20,
            "ballot": ["c3", "c1", "c2"]
        }]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output, {
                'candidates': set(['c1', 'c2', 'c3']),
                'quota': 30,
                'rounds': [],
                'remaining_candidates': set(['c1', 'c2', 'c3']),
                'winners': set(['c1', 'c2', 'c3'])
            })
コード例 #9
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_wiki_example(self):

        # Generate data
        input = [{
            "count": 4,
            "ballot": ["orange"]
        }, {
            "count": 2,
            "ballot": ["pear", "orange"]
        }, {
            "count": 8,
            "ballot": ["chocolate", "strawberry"]
        }, {
            "count": 4,
            "ballot": ["chocolate", "sweets"]
        }, {
            "count": 1,
            "ballot": ["strawberry"]
        }, {
            "count": 1,
            "ballot": ["sweets"]
        }]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output, {
                'candidates':
                set(['orange', 'pear', 'chocolate', 'strawberry', 'sweets']),
                'quota':
                6,
                'rounds': [{
                    'tallies': {
                        'orange': 4.0,
                        'strawberry': 1.0,
                        'pear': 2.0,
                        'sweets': 1.0,
                        'chocolate': 12.0
                    },
                    'winners': set(['chocolate'])
                }, {
                    'tallies': {
                        'orange': 4.0,
                        'strawberry': 5.0,
                        'pear': 2.0,
                        'sweets': 3.0
                    },
                    'loser': 'pear'
                }, {
                    'tallies': {
                        'orange': 6.0,
                        'strawberry': 5.0,
                        'sweets': 3.0
                    },
                    'winners': set(['orange'])
                }, {
                    'tallies': {
                        'strawberry': 5.0,
                        'sweets': 3.0
                    },
                    'loser': 'sweets'
                }],
                'remaining_candidates':
                set(['strawberry']),
                'winners':
                set(['orange', 'strawberry', 'chocolate'])
            })
コード例 #10
0
ファイル: test_stv.py プロジェクト: zizieada/python-vote-core
    def test_stv_ian_jacobs(self):

        # Generate data
        input = [
            {
                "count": 1,
                "ballot": ["AB", "EF", "BC"]
            },
            {
                "count": 1,
                "ballot": ["AB", "KL", "EF", "HI"]
            },
            {
                "count": 1,
                "ballot": ["EF", "AB", "QR"]
            },
            {
                "count": 1,
                "ballot": ["KL", "AB", "BC", "ST", "EF", "QR"]
            },
            {
                "count": 1,
                "ballot": ["ST", "AB", "BC", "QR", "EF", "KL"]
            },
            {
                "count": 1,
                "ballot": ["QR", "BC", "EF"]
            },
            {
                "count": 1,
                "ballot": ["BC", "QR", "AB", "HI"]
            },
            {
                "count": 1,
                "ballot": ["BC", "AB", "ST", "QR", "KL", "EF", "HI"]
            },
            {
                "count": 1,
                "ballot": ["QR", "EF", "AB", "BC", "ST", "KL"]
            },
            {
                "count": 1,
                "ballot": ["KL", "AB", "EF", "QR", "ST", "BC", "HI"]
            },
            {
                "count": 1,
                "ballot": ["AB", "ST", "EF", "KL", "BC", "QR"]
            },
            {
                "count": 1,
                "ballot": ["QR", "ST", "KL", "AB", "BC", "EF", "HI"]
            },
            {
                "count": 1,
                "ballot": ["BC", "QR", "KL"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(output["winners"], set(["AB", "BC", "QR"]))
コード例 #11
0
    def test_stv_wiki_example(self):

        # Generate data
        input = [
            {
                "count": 4,
                "ballot": ["orange"]
            },
            {
                "count": 2,
                "ballot": ["pear", "orange"]
            },
            {
                "count": 8,
                "ballot": ["chocolate", "strawberry"]
            },
            {
                "count": 4,
                "ballot": ["chocolate", "sweets"]
            },
            {
                "count": 1,
                "ballot": ["strawberry"]
            },
            {
                "count": 1,
                "ballot": ["sweets"]
            },
        ]
        output = STV(input, required_winners=3).as_dict()

        # Run tests
        self.assertEqual(
            output,
            {
                "candidates":
                set(["orange", "pear", "chocolate", "strawberry", "sweets"]),
                "quota":
                6,
                "rounds": [
                    {
                        "tallies": {
                            "orange": 4.0,
                            "strawberry": 1.0,
                            "pear": 2.0,
                            "sweets": 1.0,
                            "chocolate": 12.0,
                        },
                        "winners": set(["chocolate"]),
                    },
                    {
                        "tallies": {
                            "orange": 4.0,
                            "strawberry": 5.0,
                            "pear": 2.0,
                            "sweets": 3.0,
                        },
                        "loser": "pear",
                    },
                    {
                        "tallies": {
                            "orange": 6.0,
                            "strawberry": 5.0,
                            "sweets": 3.0
                        },
                        "winners": set(["orange"]),
                    },
                    {
                        "tallies": {
                            "strawberry": 5.0,
                            "sweets": 3.0
                        },
                        "loser": "sweets"
                    },
                ],
                "remaining_candidates":
                set(["strawberry"]),
                "winners":
                set(["orange", "strawberry", "chocolate"]),
            },
        )