Skip to content

Commit cf994b5

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 9262e18 commit cf994b5

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
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

+3
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

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
22

3+
using System.Reflection;
4+
using System.Text;
5+
using System.Text.Json;
6+
using Newtonsoft.Json;
7+
38
[TestClass]
49
public class ArraysLeftRotationTest
510
{
611
public class ArraysLeftRotationTestCase
712
{
8-
public List<int> input = []; public List<int> expected = [];
13+
public List<Int32> input = []; public List<Int32> expected = [];
914
}
1015

1116
public class ArraysLeftRotationsTestCase
1217
{
1318
public List<int> input = []; public int d; public List<int> expected = [];
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 static List<ArraysLeftRotationTestCase> testCases = [];
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+
var info = Assembly.GetExecutingAssembly().GetName();
31+
var name = info.Name;
32+
using var stream = Assembly
33+
.GetExecutingAssembly()
34+
.GetManifestResourceStream($"{name}.Resources.hackerrank.interview_preparation_kit.arrays.ctci_array_left_rotation.testcases.json")!;
35+
36+
using var streamReader = new StreamReader(stream, Encoding.UTF8);
37+
38+
testCases = JsonConvert.DeserializeObject<List<ArraysLeftRotationTestCase>>(streamReader.ReadToEnd()) ?? [];
39+
}
40+
2841
[TestMethod]
2942
public void testRotLeftOne()
3043
{
3144
List<int> result;
3245

33-
foreach (ArraysLeftRotationTestCase test in tests)
46+
foreach (ArraysLeftRotationTestCase test in testCases)
3447
{
3548
result = ArraysLeftRotation.rotLeftOne(test.input);
3649
CollectionAssert.AreEquivalent(test.expected, result);

0 commit comments

Comments
 (0)