Skip to content

Commit c18c6f5

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 fb10e7d commit c18c6f5

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
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
@@ -54,4 +54,7 @@
5454
ReportTypes="TextSummary;Html"/>
5555
</Target>
5656

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

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
22

3+
using System.Reflection;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
37
[TestClass]
48
public class ArraysLeftRotationTest
59
{
@@ -13,24 +17,32 @@ public class ArraysLeftRotationsTestCase
1317
public List<int> input = []; public int d; public List<int> expected = [];
1418
}
1519

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-
];
20+
private static List<ArraysLeftRotationTestCase> testCases = [];
2321

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

26+
[TestInitialize]
27+
public void testInitialize()
28+
{
29+
var info = Assembly.GetExecutingAssembly().GetName();
30+
var name = info.Name;
31+
using var stream = Assembly
32+
.GetExecutingAssembly()
33+
.GetManifestResourceStream($"{name}.Resources.hackerrank.interview_preparation_kit.arrays.ctci_array_left_rotation.testcases.json")!;
34+
35+
using var streamReader = new StreamReader(stream, Encoding.UTF8);
36+
37+
testCases = JsonConvert.DeserializeObject<List<ArraysLeftRotationTestCase>>(streamReader.ReadToEnd()) ?? [];
38+
}
39+
2840
[TestMethod]
2941
public void testRotLeftOne()
3042
{
3143
List<int> result;
3244

33-
foreach (ArraysLeftRotationTestCase test in tests)
45+
foreach (ArraysLeftRotationTestCase test in testCases)
3446
{
3547
result = ArraysLeftRotation.rotLeftOne(test.input);
3648
CollectionAssert.AreEquivalent(test.expected, result);

0 commit comments

Comments
 (0)