From 778ec18f01ccb1a3d8c8bae6593c4cdee88ee0cd Mon Sep 17 00:00:00 2001 From: LuukvH Date: Fri, 5 Jan 2024 19:01:21 +0100 Subject: [PATCH] feat: Wrap result in GroupAnalysisResult object 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. --- BKRCalculator/GroupAnalysisResult.cs | 13 ++ BKRCalculator/GroupAnalyzer.cs | 11 +- BKRCalculatorTest/GroupAnalyzerTest.cs | 181 +++---------------------- 3 files changed, 34 insertions(+), 171 deletions(-) create mode 100644 BKRCalculator/GroupAnalysisResult.cs diff --git a/BKRCalculator/GroupAnalysisResult.cs b/BKRCalculator/GroupAnalysisResult.cs new file mode 100644 index 0000000..795b616 --- /dev/null +++ b/BKRCalculator/GroupAnalysisResult.cs @@ -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; + } +} \ No newline at end of file diff --git a/BKRCalculator/GroupAnalyzer.cs b/BKRCalculator/GroupAnalyzer.cs index d1b54ac..beb1ee4 100644 --- a/BKRCalculator/GroupAnalyzer.cs +++ b/BKRCalculator/GroupAnalyzer.cs @@ -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) { diff --git a/BKRCalculatorTest/GroupAnalyzerTest.cs b/BKRCalculatorTest/GroupAnalyzerTest.cs index 1aede8d..259fca6 100644 --- a/BKRCalculatorTest/GroupAnalyzerTest.cs +++ b/BKRCalculatorTest/GroupAnalyzerTest.cs @@ -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); } -} \ No newline at end of file +}