Skip to content

Commit c382e63

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 413e011 commit c382e63

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-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
@@ -55,4 +55,7 @@
5555
ReportTypes="TextSummary;Html"/>
5656
</Target>
5757

58+
<ItemGroup>
59+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
60+
</ItemGroup>
5861
</Project>

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
22

3+
using Newtonsoft.Json;
4+
35
[TestClass]
46
public class ArraysLeftRotationTest
57
{
68
public class ArraysLeftRotationTestCase
79
{
8-
public List<int> input = []; public List<int> expected = [];
10+
public List<int> input { get; set; } = default!;
11+
public List<int> expected { get; set; } = default!;
912
}
1013

1114
public class ArraysLeftRotationsTestCase
1215
{
13-
public List<int> input = []; public int d; public List<int> expected = [];
16+
public List<int> input { get; set; } = default!;
17+
public int d { get; set; }
18+
public List<int> expected { get; set; } = default!;
1419
}
1520

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-
];
21+
private List<ArraysLeftRotationTestCase> testCases { get; set; } = default!;
2322

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

27+
[TestInitialize]
28+
public void testInitialize()
29+
{
30+
testCases = JsonConvert.DeserializeObject<List<ArraysLeftRotationTestCase>>(
31+
JsonLoader.resourceLoad("hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json")
32+
) ?? [];
33+
}
34+
2835
[TestMethod]
2936
public void testRotLeftOne()
3037
{
3138
List<int> result;
3239

33-
foreach (ArraysLeftRotationTestCase test in tests)
40+
foreach (ArraysLeftRotationTestCase test in testCases)
3441
{
3542
result = ArraysLeftRotation.rotLeftOne(test.input);
3643
CollectionAssert.AreEquivalent(test.expected, result);

0 commit comments

Comments
 (0)