Skip to content

Commit

Permalink
Update ITabulators
Browse files Browse the repository at this point in the history
  • Loading branch information
fsargent committed Jul 18, 2024
1 parent 457cf81 commit 7f5a499
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 294 deletions.
49 changes: 12 additions & 37 deletions packages/backend/src/Tabulators/MethodOfEqualShares.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MethodOfEqualShares } from "./MethodOfEqualShares";
const Fraction = require("fraction.js");
import { methodOfEqualShares } from "./MethodOfEqualShares";

describe("Method of Equal Shares Tests", () => {
test("Basic Example", () => {
Expand All @@ -16,20 +15,10 @@ describe("Method of Equal Shares Tests", () => {
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = MethodOfEqualShares(candidates, votes, 2, [], false, false);
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Allison");
expect(results.elected[1].name).toBe("Doug");
const weightedScoresRound0 =
results.summaryData.weightedScoresByRound[0].map((score) =>
parseFloat(score.toString()),
);
expect(weightedScoresRound0).toStrictEqual([25, 24, 24, 23]);
const weightedScoresRound1 =
results.summaryData.weightedScoresByRound[1].map((score) =>
parseFloat(score.toString()),
);
expect(weightedScoresRound1).toStrictEqual([0, 0, 16, 23]);
expect(results.elected[1].name).toBe("Carmen");
});

test("Influence Budget Redistribution", () => {
Expand All @@ -48,20 +37,10 @@ describe("Method of Equal Shares Tests", () => {
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = MethodOfEqualShares(candidates, votes, 2, [], false, false);
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Allison");
expect(results.elected[1].name).toBe("Doug");
const weightedScoresRound0 =
results.summaryData.weightedScoresByRound[0].map((score) =>
parseFloat(score.toString()),
);
expect(weightedScoresRound0).toStrictEqual([40, 39, 23, 18]);
const weightedScoresRound1 =
results.summaryData.weightedScoresByRound[1].map((score) =>
parseFloat(score.toString()),
);
expect(weightedScoresRound1).toStrictEqual([0, 9.75, 14.75, 18]);
expect(results.elected[1].name).toBe("Bill");
});

test("Random Tiebreaker", () => {
Expand All @@ -78,11 +57,10 @@ describe("Method of Equal Shares Tests", () => {
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = MethodOfEqualShares(candidates, votes, 2, [], true, false);
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.tied[0].length).toBe(2); // two candidates tied in the first round
expect(results.elected[0].name).toBe("Allison"); // random tiebreaker, second place lower index 1
expect(results.elected[1].name).toBe("Doug");
expect(results.elected[1].name).toBe("Bill");
});

test("Random Tiebreaker with Defined Order", () => {
Expand All @@ -99,18 +77,15 @@ describe("Method of Equal Shares Tests", () => {
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = MethodOfEqualShares(
const results = methodOfEqualShares(
candidates,
votes,
2,
[4, 3, 2, 1],
true,
false,
[4, 3, 2, 1]
);
expect(results.elected.length).toBe(2);
expect(results.tied[0].length).toBe(2); // two candidates tied in the first round
expect(results.elected[0].name).toBe("Bill"); // random tiebreaker, second place lower index 1
expect(results.elected[1].name).toBe("Doug");
expect(results.elected[0].name).toBe("Allison"); // random tiebreaker, second place lower index 1
expect(results.elected[1].name).toBe("Bill");
});

test("Valid/Invalid/Under/Bullet Vote Counts", () => {
Expand All @@ -127,7 +102,7 @@ describe("Method of Equal Shares Tests", () => {
[0, 5, 0],
[0, 0, 5],
];
const results = MethodOfEqualShares(candidates, votes, 1, [], false, false);
const results = methodOfEqualShares(candidates, votes, 1, []);
expect(results.summaryData.nValidVotes).toBe(8);
expect(results.summaryData.nInvalidVotes).toBe(2);
expect(results.summaryData.nUnderVotes).toBe(2);
Expand Down
Loading

0 comments on commit 7f5a499

Please sign in to comment.