Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/json testcases #112

Merged
merged 8 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
},
"omnisharp.enableEditorConfigSupport": true,
"[github-actions-workflow]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand Down
8 changes: 8 additions & 0 deletions algorithm_exercises_csharp/algorithm_exercises_csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<!-- Static Analysis -->
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>

Expand All @@ -18,4 +19,11 @@
<ItemGroup>
<ProjectReference Include="../algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]

namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<!-- Static Analysis -->
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>

Expand All @@ -23,6 +24,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
Expand Down
2 changes: 1 addition & 1 deletion algorithm_exercises_csharp_base/src/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void error(string message, params object?[] args)
#pragma warning restore CA2254 // Template should be a static expression
}

public static void debu(string message, params object?[] args)
public static void debug(string message, params object?[] args)
{
#pragma warning disable CA2254 // Template should be a static expression
LoggerSingleton.Instance.Logger.LogDebug(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{"input": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]},
{"input": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]},
{"input": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]},
{"input": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]},
{"input": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"title": "Sample Test Case 0",
"magazine": ["give", "me", "one", "grand", "today", "night"],
"note": ["give", "one", "grand", "today"],
"expected": "Yes"
},
{
"title": "Sample Test Case 1",
"magazine": ["two", "times", "three", "is", "not", "four"],
"note": ["two", "times", "two", "is", "four"],
"expected": "No"
},
{
"title": "Sample Test",
"magazine": ["two", "two", "times", "three", "is", "not", "four"],
"note": ["two", "times", "two", "is", "four"],
"expected": "Yes"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"title": "Example 1",
"s1": "and",
"s2": "art",
"expected": "Yes"
},
{
"title": "Example 2",
"s1": "be",
"s2": "cat",
"expected": "No"
},
{
"title": "Sample Test Case 0",
"s1": "hello",
"s2": "world",
"expected": "Yes"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"title": "Sample Test case 0",
"k": 3,
"contests": [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
"expected": 29
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<!-- Static Analysis -->
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
Expand All @@ -29,6 +30,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
Expand All @@ -50,8 +55,13 @@
</ItemGroup>

<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../coverage-report"
ReportTypes="TextSummary;Html"/>
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../coverage-report" ReportTypes="TextSummary;Html" />
</Target>

<ItemGroup>
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json" />
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@ public class ArraysLeftRotationTest
{
public class ArraysLeftRotationTestCase
{
public List<int> input = []; public List<int> expected = [];
public List<int> input { get; set; } = default!;
public List<int> expected { get; set; } = default!;
}

public class ArraysLeftRotationsTestCase
{
public List<int> input = []; public int d; public List<int> expected = [];
public List<int> input { get; set; } = default!;
public int d { get; set; }
public List<int> expected { get; set; } = default!;
}

private static readonly ArraysLeftRotationTestCase[] tests = [
new() { input = [1, 2, 3, 4, 5], expected = [2, 3, 4, 5, 1] },
new() { input = [2, 3, 4, 5, 1], expected = [3, 4, 5, 1, 2] },
new() { input = [3, 4, 5, 1, 2], expected = [4, 5, 1, 2, 3] },
new() { input = [4, 5, 1, 2, 3], expected = [5, 1, 2, 3, 4] },
new() { input = [5, 1, 2, 3, 4], expected = [1, 2, 3, 4, 5] }
];
private List<ArraysLeftRotationTestCase> testCases { get; set; } = default!;

private static readonly ArraysLeftRotationsTestCase[] testRotationsCases = [
new() { input = [1, 2, 3, 4, 5], d = 4, expected = [5, 1, 2, 3, 4] }
];

[TestInitialize]
public void testInitialize()
{
testCases = JsonLoader.resourceLoad<List<ArraysLeftRotationTestCase>>(
"hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json"
) ?? [];
}

[TestMethod]
public void testRotLeftOne()
{
List<int> result;

foreach (ArraysLeftRotationTestCase test in tests)
foreach (ArraysLeftRotationTestCase test in testCases)
{
result = ArraysLeftRotation.rotLeftOne(test.input);
CollectionAssert.AreEquivalent(test.expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,28 @@ public class RansomNoteTest
{
public class RansomNoteTestCase
{
public string title = "";
public List<string> magazine = [];
public List<string> note = [];
public string expected = "";
public string title { get; set; } = default!;
public List<string> magazine { get; set; } = default!;
public List<string> note { get; set; } = default!;
public string expected { get; set; } = default!;
}

public class ArraysLeftRotationsTestCase
private List<RansomNoteTestCase> testCases { get; set; } = default!;

[TestInitialize]
public void testInitialize()
{
public List<int> input = []; public int d; public List<int> expected = [];
testCases = JsonLoader.resourceLoad<List<RansomNoteTestCase>>(
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json"
) ?? [];
}

private static readonly RansomNoteTestCase[] tests = [
new()
{
title = "Sample Test Case 0",
magazine = ["give", "me", "one", "grand", "today", "night"],
note = ["give", "one", "grand", "today"],
expected = "Yes"
},
new()
{
title = "Sample Test Case 1",
magazine = ["two", "times", "three", "is", "not", "four"],
note = ["two", "times", "two", "is", "four"],
expected = "No"
},
new()
{
title = "Sample Test",
magazine = ["two", "two", "times", "three", "is", "not", "four"],
note = ["two", "times", "two", "is", "four"],
expected = "Yes"
},
];

[TestMethod]
public void testCheckMagazine()
{
string result;

foreach (RansomNoteTestCase test in tests)
foreach (RansomNoteTestCase test in testCases)
{
result = RansomNote.checkMagazine(test.magazine, test.note);
Assert.AreEqual(test.expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,31 @@ public class TwoStringsTest
{
public class TwoStringsTestCase
{
public string title = "";
public string s1 = "";
public string s2 = "";
public string expected = "Yes";
public string title { get; set; } = default!;

public string s1 { get; set; } = default!;

public string s2 { get; set; } = default!;

public string expected { get; set; } = default!;
}

private static readonly TwoStringsTestCase[] tests = [
new()
{
title = "Example 1",
s1 = "and",
s2 = "art",
expected = "Yes"
},
new()
{
title = "Example 2",
s1 = "be",
s2 = "cat",
expected = "No"
},
new()
{
title = "Sample Test Case 0",
s1 = "hello",
s2 = "world",
expected = "Yes"
},
];
private List<TwoStringsTestCase> testCases { get; set; } = default!;

[TestInitialize]
public void testInitialize()
{
testCases = JsonLoader.resourceLoad<List<TwoStringsTestCase>>(
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json"
) ?? [];
}

[TestMethod]
public void testTwoStrings()
{
string result;

foreach (TwoStringsTestCase test in tests)
foreach (TwoStringsTestCase test in testCases)
{
result = TwoStrings.twoStrings(test.s1, test.s2);
Assert.AreEqual(test.expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,22 @@ public class LuckBalanceTestCase
public int expected;
}

private static readonly LuckBalanceTestCase[] tests = [
new()
{
title = "Sample Test case 0",
k = 3,
contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
expected = 29
},
new()
{
title = "Sample Test case 1",
k = 5,
contests = [[13, 1], [10, 1], [9, 1], [8, 1], [13, 1], [12, 1], [18, 1], [13, 1]],
expected = 42
},
new()
{
title = "Sample Test case 2",
k = 2,
contests = [[5, 1], [4, 0], [6, 1], [2, 1], [8, 0]],
expected = 21
}
];
private List<LuckBalanceTestCase> testCases { get; set; } = default!;

[TestInitialize]
public void testInitialize()
{
testCases = JsonLoader.resourceLoad<List<LuckBalanceTestCase>>(
"hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json"
) ?? [];
}

[TestMethod]
public void testLuckBalance()
{
int result;

foreach (LuckBalanceTestCase test in tests)
foreach (LuckBalanceTestCase test in testCases)
{
result = LuckBalance.luckBalance(test.k, test.contests);
Assert.AreEqual(
Expand Down
Loading