Skip to content

Commit

Permalink
[DigiTally] Fix bug related to missing votes
Browse files Browse the repository at this point in the history
  • Loading branch information
ermshiperete committed Apr 9, 2024
1 parent f64818d commit c294141
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
81 changes: 81 additions & 0 deletions DigiTallyTests/ReadBallotsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,86 @@ public void AddBallot_BothInvalid()
Assert.That(election2.Key.BallotsProcessed, Is.EqualTo(1));
Assert.That(election2.Key.Invalid, Is.EqualTo(1));
}

[Test]
public void GetResultString_Valid_OnlyOneNominee()
{
File.WriteAllText(_configFileName, @"[Wahlen]
Titel=The election
Wahl1=Election
Email=election@example.com
PublicKey=12345678.asc
[Election]
Text=Some description
Typ=Weighted
FehlendOk=true
Stimmen=2
Kandidat1=Mickey Mouse
");
var ballotFileName = Path.GetTempFileName();
File.WriteAllText(ballotFileName, "The election\r\n" +
"============\r\n" +
"\r\n" +
"Election\r\n" +
"--------\r\n" +
"(2 Stimmen; Wahl der Reihenfolge nach mit 1.-2. kennzeichnen)\r\n" +
"2. Mickey Mouse\r\n" +
"\r\n" +
"\r\n" +
"12345\r\n");
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.NumberOfInvalidBallots, Is.EqualTo(0));

var election1 = sut.Results.First();
Assert.That(election1.Key.BallotsProcessed, Is.EqualTo(1));
Assert.That(election1.Key.Invalid, Is.EqualTo(0));
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
--------
1. Mickey Mouse (1 points)
(1 ballots, thereof 0 invalid)
"));
}

[Test]
public void GetResultString_Invalid_TwoNomineesOneVote()
{
File.WriteAllText(_configFileName, @"[Wahlen]
Titel=The election
Wahl1=Election
Email=election@example.com
PublicKey=12345678.asc
[Election]
Text=Some description
Typ=Weighted
FehlendOk=false
Stimmen=2
Kandidat1=Mickey Mouse
Kandidat2=Dagobert Duck
");
var ballotFileName = Path.GetTempFileName();
File.WriteAllText(ballotFileName, "The election\r\n" +
"============\r\n" +
"\r\n" +
"Election\r\n" +
"--------\r\n" +
"(2 Stimmen; Wahl der Reihenfolge nach mit 1.-2. kennzeichnen)\r\n" +
"2. Mickey Mouse\r\n" +
"\r\n" +
"\r\n" +
"12345\r\n");
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.NumberOfInvalidBallots, Is.EqualTo(1));

var election1 = sut.Results.First();
Assert.That(election1.Key.BallotsProcessed, Is.EqualTo(1));
Assert.That(election1.Key.Invalid, Is.EqualTo(1));
}
}
}
32 changes: 32 additions & 0 deletions DigiTallyTests/ReadBallotsTests_Weighted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,38 @@ Donald Duck (0 points)
"));
}

[Test]
public void GetResultString_Abstains()
{
var ballotFileName = Path.GetTempFileName();
File.WriteAllText(ballotFileName, "The election\r\n" +
"============\r\n" +
"\r\n" +
"Election\r\n" +
"--------\r\n" +
"(2 Stimmen; Wahl der Reihenfolge nach mit 1.-2. kennzeichnen)\r\n" +
" Mickey Mouse\r\n" +
" Donald Duck\r\n" +
" Dagobert Duck\r\n" +
" Daisy Duck\r\n" +
"\r\n" +
"\r\n" +
"12345\r\n");
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
--------
Dagobert Duck (0 points)
Daisy Duck (0 points)
Donald Duck (0 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid)
"));
}

}

}
14 changes: 10 additions & 4 deletions DigitaleBriefwahl/Model/WeightedElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ protected override Dictionary<string, CandidateResult> ReadVotesFromBallotIntern
var nomineesSeen = new List<string>();
var rankSeen = new List<int>();
var invalid = false;
var lineCount = 0;

for (var line = stream.ReadLine(); !string.IsNullOrEmpty(line); line = stream.ReadLine())
{
lineCount++;

// 1. Mickey Mouse
CandidateResult res = null;
var regex = new Regex("(([0-9]+).| ) (.+)");
Expand Down Expand Up @@ -143,7 +146,7 @@ protected override Dictionary<string, CandidateResult> ReadVotesFromBallotIntern
}
}

if (invalid)
if (invalid || (lineCount < Votes && !MissingOk ))
{
Invalid++;
return null;
Expand All @@ -154,15 +157,15 @@ protected override Dictionary<string, CandidateResult> ReadVotesFromBallotIntern

private class FindIndexPredicate
{
private string Name;
private readonly string _name;
public FindIndexPredicate(string name)
{
Name = name;
_name = name;
}

public bool Find(KeyValuePair<string, CandidateResult> kv)
{
return kv.Key == Name;
return kv.Key == _name;
}
}

Expand All @@ -189,6 +192,9 @@ private static int GetRank(List<KeyValuePair<string, CandidateResult>> results,
candidate)
{
var index = results.FindIndex(new FindIndexPredicate(candidate).Find);
if (index < 0 || ((WeightedCandidateResult)results[index].Value).Points <= 0)
return Int32.MaxValue;

if (index > 0 && ((WeightedCandidateResult)results[index].Value).Points == ((WeightedCandidateResult)results[index-1].Value).Points)
{
return index;
Expand Down

0 comments on commit c294141

Please sign in to comment.