Example #1
0
 def test_resolve_tie_1(self):
     test_tabulation = self.make_irv_01()
     test_tabulation.tabulate(stop_after_status_update=1)
     self.assertEqual(test_tabulation.tie_breaker, {'A': 0, 'B': 1, 'C': 2})
     self.assertEqual(test_tabulation.resolve_tie(set(str_tuple(' A B C'))),
                      'A')
     self.assertEqual(test_tabulation.resolve_tie(set(str_tuple(' B C'))),
                      'B')
Example #2
0
 def test_defeat_candidate(self):
     test_tabulation = self.make_irv_01()
     test_tabulation.tabulate(stop_after_status_update=1)
     self.assertEqual(test_tabulation.status['B'].status,
                      K.STATUS_CONTINUING)
     self.assertEqual(test_tabulation.continuing(),
                      set(str_tuple(' A B C')))
     self.assertEqual(test_tabulation.elected(), set())
     test_tabulation.defeat_candidates(set(['B']))
     self.assertEqual(test_tabulation.status['B'].status, K.STATUS_DEFEATED)
     self.assertEqual(test_tabulation.continuing(), set(str_tuple(' A C')))
     self.assertEqual(test_tabulation.elected(), set())
Example #3
0
 def test_ballots_invalid(self):
   candidates = validate.str_tuple(' A B C D E F G H I J K L')
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'ballots is not a list or tuple:',
         validate.ballots, (' A B', candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'A ballot is not a list or tuple:',
         validate.ballots, ((' A B',), candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'A ballot is not a pair of values:',
         validate.ballots, ([('A', 'B', 'C')], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'A ballot multiple is not an int:',
         validate.ballots, ([(0.12,' A B C')], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'A ballot multiple is zero or less:',
         validate.ballots, ([(-1,' A B C')], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'A ballot multiple is zero or less:',
         validate.ballots, ([(0,' A B C')], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'Invalid ballot rankings type:',
         validate.ballots, ([(1, 77)], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'Invalid ballot rankings type:',
         validate.ballots, ([(2, ('A', 77))], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'Ballot rankings is too long:',
         validate.ballots, ([(2, ' A B C D')], candidates, 3))
   _test_aids.assertRaises_with_message(self, errors.RcvValueError,
         'Invalid ballot ranking code:',
         validate.ballots, ([(2, ('A', 'Z'))], candidates, 3))
Example #4
0
 def test_str_tuple_from_str(self):
   self.assertEqual(validate.str_tuple(''), tuple())
   self.assertEqual(validate.str_tuple(' '), ('',))
   self.assertEqual(validate.str_tuple('#'), ('',))
   self.assertEqual(validate.str_tuple(' A'), ('A',))
   self.assertEqual(validate.str_tuple(' AA'), ('AA',))
   self.assertEqual(validate.str_tuple(' A B'), ('A', 'B'))
   self.assertEqual(validate.str_tuple(' A  B'), ('A', '', 'B'))
   self.assertEqual(validate.str_tuple('|A|B'), ('A', 'B'))
Example #5
0
 def test_ballot_update_transfer_value_1(self):
     test_ballot = ballot.Ballot(5, str_tuple(' A B C'))
     self.assertEqual(test_ballot.update_transfer_value(Decimal(5) / 7),
                      Decimal(0.71428))
     self.assertEqual(test_ballot.update_transfer_value(Decimal(5) / 7),
                      Decimal(0.51019))
     self.assertEqual(test_ballot.update_transfer_value(Decimal(5) / 7),
                      Decimal(0.36441))
Example #6
0
def run_test_spec(test_case, input_json):
    """
  Run a test case using test specs from a JSON text file
  """
    tabulate_args, test_spec = with_json.build_tabulate_args(
        input_json, 'all-tests-spec.json')
    if 'maxDiff' in test_spec:
        maxDiff = test_spec['maxDiff']
        if maxDiff is None:
            test_case.maxDiff = None
        if type(maxDiff) == int and maxDiff >= 0:
            test_case.maxDiff = maxDiff

    try:
        print_description = test_spec['print_description']
    except KeyError:
        print_description = False
    if print_description:
        print('\n  """' + test_spec['description'] + '"""')

    if 'exception' in test_spec:
        exception_type, exception_message = test_spec['exception']
        _test_aids.assertRaises_with_message(test_case, u2s(exception_type),
                                             u2s(exception_message),
                                             rcv.tabulate, tabulate_args)
    else:
        expected_elected = validate.str_tuple(test_spec['elected'])
        expected_status = _test_aids.build_expected_status(
            test_spec['status_codes'])
        expected_tally = {
            _test_aids.u2s(candidate): [
                K.Decimal(vote_total)
                if test_spec['nbr_seats_to_fill'] > 1 else vote_total
                for vote_total in votes
            ]
            for candidate, votes in test_spec['tally'].items()
        }
        elected, status, tally = rcv.Tabulation(*tabulate_args).tabulate()
        if 'print_results' in test_spec and test_spec['print_results']:
            print_elected(elected)
            print_status(status)
            print_tally(tally)
            try:
                description = test_spec['description']
            except KeyError:
                description = None
            jason_str = with_json.results_to_json(elected, status, tally,
                                                  description)
            print(jason_str)
        status_dict = {
            candidate: status.as_dict()
            for candidate, status in status.items()
        }
        test_case.assertEqual(tally, expected_tally)
        test_case.assertEqual(status_dict, expected_status)
        test_case.assertEqual(set(elected), set(expected_elected))
Example #7
0
 def test_ballot_get_hrcc_dups_1(self):
     test_ballot = ballot.Ballot(5, str_tuple(' A A A'))
     self.assertEqual(test_ballot.get_hrcc(('A', 'C'), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('A', 'C'), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('B', 'C'), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('B', 'C'), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 5), ':Abstentions')
Example #8
0
 def test_ballot_get_hrcc_skipped_2(self):
     test_ballot = ballot.Ballot(5, str_tuple(' A   C'))
     self.assertEqual(test_ballot.get_hrcc(('A', ), 4), 'A')
     self.assertEqual(test_ballot.get_hrcc(('A', ), 4), 'A')
     self.assertEqual(test_ballot.get_hrcc(('B', 'C'), 4), 'C')
     self.assertEqual(test_ballot.get_hrcc(('B', 'C'), 4), 'C')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 4), 'C')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 4), 'C')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 5), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 5), ':Abstentions')
Example #9
0
 def test_ballot_get_hrcc_ov(self):
     test_ballot = ballot.Ballot(5, str_tuple(' A # C'))
     self.assertEqual(test_ballot.get_hrcc(('A', ), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('A', ), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('B', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('B', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Overvotes')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Overvotes')
Example #10
0
 def test_ballot_get_hrcc_1(self):
     test_ballot = ballot.Ballot(5, str_tuple(' A B C'))
     self.assertEqual(test_ballot.get_hrcc(('A', ), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('A', ), 3), 'A')
     self.assertEqual(test_ballot.get_hrcc(('B', ), 3), 'B')
     self.assertEqual(test_ballot.get_hrcc(('B', ), 3), 'B')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), 'C')
     self.assertEqual(test_ballot.get_hrcc(('C', ), 3), 'C')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Other exhausted')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 3), ':Other exhausted')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Abstentions')
     self.assertEqual(test_ballot.get_hrcc(('D', ), 4), ':Abstentions')
Example #11
0
 def test_str_tuple_from_tuple(self):
   self.assertEqual(validate.str_tuple(tuple()), tuple())
   self.assertEqual(validate.str_tuple(('',)), ('',))
   self.assertEqual(validate.str_tuple(('#',)), ('#',))
   self.assertEqual(validate.str_tuple(('AA',)), ('AA',))
   self.assertEqual(validate.str_tuple(('A', 'B')), ('A', 'B'))
   self.assertEqual(validate.str_tuple(('A', '', 'B')), ('A', '', 'B'))
   _test_aids.assertRaises_with_message(self, TypeError,
         'Item in tuple is not a str:',
         validate.str_tuple, (('A', 7, 'B'),))
Example #12
0
 def test_str_tuple_from_list(self):
   self.assertEqual(validate.str_tuple([]), tuple())
   self.assertEqual(validate.str_tuple(['']), ('',))
   self.assertEqual(validate.str_tuple(['#']), ('#',))
   self.assertEqual(validate.str_tuple(['AA']), ('AA',))
   self.assertEqual(validate.str_tuple(['A', 'B']), ('A', 'B'))
   self.assertEqual(validate.str_tuple(['A', '', 'B']), ('A', '', 'B'))
   _test_aids.assertRaises_with_message(self, TypeError,
         'Item in list is not a str:',
         validate.str_tuple, (['A', 7, 'B'],))
Example #13
0
 def test_ballots_valid(self):
   candidates = validate.str_tuple(' A B C D E F G H I J K L')
   self.assertEqual(validate.ballots((), candidates, 3), ())
   self.assertEqual(validate.ballots([], candidates, 3), ())
   self.assertEqual(repr(validate.ballots(
         ((5, ('A',)),), candidates, 3)),
         "((5, 1.00000, ('A',)),)")
   self.assertEqual(repr(validate.ballots(
         [[5, ['A',]],], candidates, 3)),
         "((5, 1.00000, ('A',)),)")
   self.assertEqual(repr(validate.ballots(
         [[5, ' A'],], candidates, 3)),
         "((5, 1.00000, ('A',)),)")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A B'), (5, ' B A'),], candidates, 3)),
         "((3, 1.00000, ('A', 'B')), (5, 1.00000, ('B', 'A')))")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A  B'), (5, ' B # A'),], candidates, 3)),
         "((3, 1.00000, ('A', '', 'B')), (5, 1.00000, ('B', '#', 'A')))")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A   B'), (5, '   A'),], candidates, 4)),
         "((3, 1.00000, ('A', '', '', 'B')), (5, 1.00000, ('', '', 'A')))")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A   B'), (5, '   A'),], candidates, None)),
         "((3, 1.00000, ('A', '', '', 'B')), (5, 1.00000, ('', '', 'A')))")
   self.assertEqual(repr(validate.ballots(
         [(3, ''), (5, ' #'),], candidates, 3)),
         "((3, 1.00000, ()), (5, 1.00000, ('#',)))")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A B C D E F G H I J K L'),
         (5, ' L K J I H G F E D C B A'),], candidates, 12)),
         "((3, 1.00000, ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'" +
         ", 'J', 'K', 'L'))," +
         " (5, 1.00000, ('L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D'" +
         ", 'C', 'B', 'A')))")
   self.assertEqual(repr(validate.ballots(
         [(3, ' A B C D E F G H I J K L'),
         (5, ' L K J I H G F E D C B A'),], candidates, None)),
         "((3, 1.00000, ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'" +
         ", 'J', 'K', 'L'))," +
         " (5, 1.00000, ('L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D'" +
         ", 'C', 'B', 'A')))")