Skip to content

Commit 4a538fe

Browse files
authored
Merge pull request #112 from sir-gon/feature/json-testcases
Feature/json testcases
2 parents e8aa31c + 4f26bf6 commit 4a538fe

File tree

16 files changed

+171
-99
lines changed

16 files changed

+171
-99
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"[csharp]": {
88
"editor.defaultFormatter": "ms-dotnettools.csharp"
99
},
10+
"omnisharp.enableEditorConfigSupport": true,
1011
"[github-actions-workflow]": {
1112
"editor.defaultFormatter": "esbenp.prettier-vscode"
1213
},

algorithm_exercises_csharp/algorithm_exercises_csharp.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<!-- Static Analysis -->
1212
<EnableNETAnalyzers>true</EnableNETAnalyzers>
13+
<AnalysisLevel>latest</AnalysisLevel>
1314
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1415
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
1516

@@ -18,4 +19,11 @@
1819
<ItemGroup>
1920
<ProjectReference Include="../algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj" />
2021
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
<PrivateAssets>all</PrivateAssets>
27+
</PackageReference>
28+
</ItemGroup>
2129
</Project>

algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
22

33
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
44

algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<!-- Static Analysis -->
1414
<EnableNETAnalyzers>true</EnableNETAnalyzers>
15+
<AnalysisLevel>latest</AnalysisLevel>
1516
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1617
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
1718

@@ -23,6 +24,10 @@
2324
</ItemGroup>
2425

2526
<ItemGroup>
27+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
2631
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
2732
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
2833
<PackageReference Include="System.Text.Json" Version="8.0.4" />

algorithm_exercises_csharp_base/src/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void error(string message, params object?[] args)
6868
#pragma warning restore CA2254 // Template should be a static expression
6969
}
7070

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

algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<!-- Static Analysis -->
2121
<EnableNETAnalyzers>true</EnableNETAnalyzers>
22+
<AnalysisLevel>latest</AnalysisLevel>
2223
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
2324
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
2425
</PropertyGroup>
@@ -29,6 +30,10 @@
2930
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3031
<PrivateAssets>all</PrivateAssets>
3132
</PackageReference>
33+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
34+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
<PrivateAssets>all</PrivateAssets>
36+
</PackageReference>
3237
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
3338
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
3439
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
@@ -50,8 +55,13 @@
5055
</ItemGroup>
5156

5257
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
53-
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../coverage-report"
54-
ReportTypes="TextSummary;Html"/>
58+
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../coverage-report" ReportTypes="TextSummary;Html" />
5559
</Target>
5660

61+
<ItemGroup>
62+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
63+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
64+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json" />
65+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json" />
66+
</ItemGroup>
5767
</Project>

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,37 @@ public class ArraysLeftRotationTest
55
{
66
public class ArraysLeftRotationTestCase
77
{
8-
public List<int> input = []; public List<int> expected = [];
8+
public List<int> input { get; set; } = default!;
9+
public List<int> expected { get; set; } = default!;
910
}
1011

1112
public class ArraysLeftRotationsTestCase
1213
{
13-
public List<int> input = []; public int d; public List<int> expected = [];
14+
public List<int> input { get; set; } = default!;
15+
public int d { get; set; }
16+
public List<int> expected { get; set; } = default!;
1417
}
1518

16-
private static readonly ArraysLeftRotationTestCase[] tests = [
17-
new() { input = [1, 2, 3, 4, 5], expected = [2, 3, 4, 5, 1] },
18-
new() { input = [2, 3, 4, 5, 1], expected = [3, 4, 5, 1, 2] },
19-
new() { input = [3, 4, 5, 1, 2], expected = [4, 5, 1, 2, 3] },
20-
new() { input = [4, 5, 1, 2, 3], expected = [5, 1, 2, 3, 4] },
21-
new() { input = [5, 1, 2, 3, 4], expected = [1, 2, 3, 4, 5] }
22-
];
19+
private List<ArraysLeftRotationTestCase> testCases { get; set; } = default!;
2320

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

25+
[TestInitialize]
26+
public void testInitialize()
27+
{
28+
testCases = JsonLoader.resourceLoad<List<ArraysLeftRotationTestCase>>(
29+
"hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json"
30+
) ?? [];
31+
}
32+
2833
[TestMethod]
2934
public void testRotLeftOne()
3035
{
3136
List<int> result;
3237

33-
foreach (ArraysLeftRotationTestCase test in tests)
38+
foreach (ArraysLeftRotationTestCase test in testCases)
3439
{
3540
result = ArraysLeftRotation.rotLeftOne(test.input);
3641
CollectionAssert.AreEquivalent(test.expected, result);

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,28 @@ public class RansomNoteTest
55
{
66
public class RansomNoteTestCase
77
{
8-
public string title = "";
9-
public List<string> magazine = [];
10-
public List<string> note = [];
11-
public string expected = "";
8+
public string title { get; set; } = default!;
9+
public List<string> magazine { get; set; } = default!;
10+
public List<string> note { get; set; } = default!;
11+
public string expected { get; set; } = default!;
1212
}
1313

14-
public class ArraysLeftRotationsTestCase
14+
private List<RansomNoteTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
1518
{
16-
public List<int> input = []; public int d; public List<int> expected = [];
19+
testCases = JsonLoader.resourceLoad<List<RansomNoteTestCase>>(
20+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json"
21+
) ?? [];
1722
}
1823

19-
private static readonly RansomNoteTestCase[] tests = [
20-
new()
21-
{
22-
title = "Sample Test Case 0",
23-
magazine = ["give", "me", "one", "grand", "today", "night"],
24-
note = ["give", "one", "grand", "today"],
25-
expected = "Yes"
26-
},
27-
new()
28-
{
29-
title = "Sample Test Case 1",
30-
magazine = ["two", "times", "three", "is", "not", "four"],
31-
note = ["two", "times", "two", "is", "four"],
32-
expected = "No"
33-
},
34-
new()
35-
{
36-
title = "Sample Test",
37-
magazine = ["two", "two", "times", "three", "is", "not", "four"],
38-
note = ["two", "times", "two", "is", "four"],
39-
expected = "Yes"
40-
},
41-
];
42-
4324
[TestMethod]
4425
public void testCheckMagazine()
4526
{
4627
string result;
4728

48-
foreach (RansomNoteTestCase test in tests)
29+
foreach (RansomNoteTestCase test in testCases)
4930
{
5031
result = RansomNote.checkMagazine(test.magazine, test.note);
5132
Assert.AreEqual(test.expected, result);

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,31 @@ public class TwoStringsTest
55
{
66
public class TwoStringsTestCase
77
{
8-
public string title = "";
9-
public string s1 = "";
10-
public string s2 = "";
11-
public string expected = "Yes";
8+
public string title { get; set; } = default!;
9+
10+
public string s1 { get; set; } = default!;
11+
12+
public string s2 { get; set; } = default!;
13+
14+
public string expected { get; set; } = default!;
1215
}
1316

14-
private static readonly TwoStringsTestCase[] tests = [
15-
new()
16-
{
17-
title = "Example 1",
18-
s1 = "and",
19-
s2 = "art",
20-
expected = "Yes"
21-
},
22-
new()
23-
{
24-
title = "Example 2",
25-
s1 = "be",
26-
s2 = "cat",
27-
expected = "No"
28-
},
29-
new()
30-
{
31-
title = "Sample Test Case 0",
32-
s1 = "hello",
33-
s2 = "world",
34-
expected = "Yes"
35-
},
36-
];
17+
private List<TwoStringsTestCase> testCases { get; set; } = default!;
18+
19+
[TestInitialize]
20+
public void testInitialize()
21+
{
22+
testCases = JsonLoader.resourceLoad<List<TwoStringsTestCase>>(
23+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json"
24+
) ?? [];
25+
}
3726

3827
[TestMethod]
3928
public void testTwoStrings()
4029
{
4130
string result;
4231

43-
foreach (TwoStringsTestCase test in tests)
32+
foreach (TwoStringsTestCase test in testCases)
4433
{
4534
result = TwoStrings.twoStrings(test.s1, test.s2);
4635
Assert.AreEqual(test.expected, result);

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,22 @@ public class LuckBalanceTestCase
1111
public int expected;
1212
}
1313

14-
private static readonly LuckBalanceTestCase[] tests = [
15-
new()
16-
{
17-
title = "Sample Test case 0",
18-
k = 3,
19-
contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
20-
expected = 29
21-
},
22-
new()
23-
{
24-
title = "Sample Test case 1",
25-
k = 5,
26-
contests = [[13, 1], [10, 1], [9, 1], [8, 1], [13, 1], [12, 1], [18, 1], [13, 1]],
27-
expected = 42
28-
},
29-
new()
30-
{
31-
title = "Sample Test case 2",
32-
k = 2,
33-
contests = [[5, 1], [4, 0], [6, 1], [2, 1], [8, 0]],
34-
expected = 21
35-
}
36-
];
14+
private List<LuckBalanceTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
testCases = JsonLoader.resourceLoad<List<LuckBalanceTestCase>>(
20+
"hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json"
21+
) ?? [];
22+
}
3723

3824
[TestMethod]
3925
public void testLuckBalance()
4026
{
4127
int result;
4228

43-
foreach (LuckBalanceTestCase test in tests)
29+
foreach (LuckBalanceTestCase test in testCases)
4430
{
4531
result = LuckBalance.luckBalance(test.k, test.contests);
4632
Assert.AreEqual(

0 commit comments

Comments
 (0)