Skip to content

Commit

Permalink
feat: Wrap result in GroupAnalysisResult object
Browse files Browse the repository at this point in the history
Instead of returning just a number we return an object with additional information. At a later point this can be extended to also include the reasoning behind the result.
  • Loading branch information
LuukvH committed Jan 5, 2024
1 parent aff7f83 commit 778ec18
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 171 deletions.
13 changes: 13 additions & 0 deletions BKRCalculator/GroupAnalysisResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class GroupAnalysisResult
{
public int TotalChildren { get; }
public bool HasSolution { get; }
public int Professionals { get; }

public GroupAnalysisResult(int totalChildren, bool hasSolution, int professionals)
{
TotalChildren = totalChildren;
HasSolution = hasSolution;
Professionals = professionals;
}
}
11 changes: 5 additions & 6 deletions BKRCalculator/GroupAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,27 @@ public GroupAnalyzer()
ageGroupRules = ageGroupRulesFactory.BuildAgeGroupRules();
}

public int CalculateBKR(AgeGroupCounts childrenCounts)
public GroupAnalysisResult CalculateBKR(AgeGroupCounts childrenCounts)
{
// Validate counts
if (childrenCounts.TotalCount <= 0)
{
return 0;
return new GroupAnalysisResult(childrenCounts.TotalCount, true, 0);
}

// Find ageRange
var ageRange = ageGroupRules.FirstOrDefault(ar => ar.MeetsConstraint(childrenCounts));
if (ageRange == null)
{
// Log or handle the case where ageRange is null if needed
return -1; // Fallback to 0
return new GroupAnalysisResult(childrenCounts.TotalCount, false, -1);
}

if (TryAllCombinationsWithOneChildLess(childrenCounts, ageRange.GetProfessionals(childrenCounts)))
{
return ageRange.GetProfessionals(childrenCounts) + 1;
return new GroupAnalysisResult(childrenCounts.TotalCount, true, ageRange.GetProfessionals(childrenCounts) + 1);
}

return ageRange.GetProfessionals(childrenCounts);
return new GroupAnalysisResult(childrenCounts.TotalCount, true, ageRange.GetProfessionals(childrenCounts));
}
private bool TryAllCombinationsWithOneChildLess(AgeGroupCounts childrenCounts, int original)
{
Expand Down
181 changes: 16 additions & 165 deletions BKRCalculatorTest/GroupAnalyzerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,180 +6,31 @@ namespace KDVManager.BKRCalculatorTest;
public class GroupAnalyzerTest
{
[TestMethod]
public void TestScenario2()
[DataRow(3, 5, 8, 0, true, 4)]
[DataRow(4, 0, 0, 12, true, 4)]
[DataRow(0, 4, 4, 7, true, 3)]
[DataRow(2, 0, 1, 2, true, 2)]
[DataRow(2, 2, 0, 1, true, 2)]
[DataRow(0, 1, 11, 1, true, 3)]
public void TestCalculation(int age0, int age1, int age2, int age3, bool expectedHasSolution, int expectedProfessionals)
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

AgeGroupCounts childrenCountByAge = new AgeGroupCounts
{
Age0Count = 3,
Age1Count = 5,
Age2Count = 8
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCountByAge);

// Assert
double expectedBKR = 4;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestScenario3()
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

AgeGroupCounts childrenCountByAge = new AgeGroupCounts
{
Age0Count = 4,
Age1Count = 0,
Age2Count = 0,
Age3Count = 12
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCountByAge);

// Assert
double expectedBKR = 4;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestScenario4()
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

var childrenCounts = new AgeGroupCounts
{
Age1Count = 4,
Age2Count = 4,
Age3Count = 7
// Add more age categories as needed for your scenario
Age0Count = age0,
Age1Count = age1,
Age2Count = age2,
Age3Count = age3
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCounts);
var result = groupAnalyzer.CalculateBKR(childrenCounts);

// Assert
double expectedBKR = 3;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestScenario5()
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

var childrenCounts = new AgeGroupCounts
{
Age0Count = 2,
Age2Count = 1,
Age3Count = 2
// Add more age categories as needed for your scenario
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCounts);

// Assert
double expectedBKR = 2;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestScenario6()
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

var childrenCounts = new AgeGroupCounts
{
Age0Count = 2,
Age1Count = 2,
Age3Count = 1
// Add more age categories as needed for your scenario
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCounts);

// Assert
double expectedBKR = 2;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestScenario7()
{
// Arrange
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

var childrenCounts = new AgeGroupCounts
{
Age1Count = 1,
Age2Count = 11,
Age3Count = 1
// Add more age categories as needed for your scenario
};

// Act
double actualBKR = groupAnalyzer.CalculateBKR(childrenCounts);

// Assert
double expectedBKR = 3;
double tolerance = 0.0001; // Adjust based on acceptable error margin
Assert.AreEqual(expectedBKR, actualBKR, tolerance);
}

[TestMethod]
public void TestCalculateBKRFromCounts_AllCombinations()
{
GroupAnalyzer groupAnalyzer = new GroupAnalyzer();

for (int count0 = 0; count0 <= 16; count0++)
{
for (int count1 = 0; count1 <= 16; count1++)
{
for (int count2 = 0; count2 <= 16; count2++)
{
for (int count3 = 0; count3 <= 16; count3++)
{
var childrenCountByAge = new AgeGroupCounts
{
Age0Count = count0,
Age1Count = count1,
Age2Count = count2,
Age3Count = count3
};

if (childrenCountByAge.TotalCount > 16)
{
continue;
}

try
{
double actualBKR = groupAnalyzer.CalculateBKR(childrenCountByAge);
Assert.IsTrue(actualBKR <= 4, $"BKR {actualBKR} exceeds 4 for counts: {count0}, {count1}, {count2}, {count3}");
}
catch (Exception ex)
{
Assert.Fail($"Unexpected exception for counts: {count0}, {count1}, {count2}, {count3} - {ex.Message}");
}
}
}
}
}
Assert.AreEqual(age0 + age1 + age2 + age3, result.TotalChildren);
Assert.AreEqual(expectedHasSolution, result.HasSolution);
Assert.AreEqual(expectedProfessionals, result.Professionals);
}
}
}

0 comments on commit 778ec18

Please sign in to comment.