Skip to content

Commit 1599ea2

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. Load test case data from JSON.
JSON load as Resource: https://khalidabuhakmeh.com/how-to-use-embedded-resources-in-dotnet
1 parent f5ee69f commit 1599ea2

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed
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+
]

algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@
5858
<ReportGenerator ReportFiles="@(CoverletReport)" TargetDirectory="../coverage-report" ReportTypes="TextSummary;Html" />
5959
</Target>
6060

61+
<ItemGroup>
62+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
63+
</ItemGroup>
6164
</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);

0 commit comments

Comments
 (0)