Skip to content

Commit 4beeb96

Browse files
GKotfisErikSchierboom
authored andcommitted
Add Saddle Points test generator (#463)
* - fix file name (missing 's') SaddlePointsTest - add SaddlePoint test generator - add multidimensional value formater * - fix indentation for multidimensional array formatter - cleanup test generator
1 parent 307c8a2 commit 4beeb96

File tree

4 files changed

+154
-68
lines changed

4 files changed

+154
-68
lines changed

exercises/saddle-points/SaddlePointTest.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
3+
using Xunit;
4+
using System;
5+
6+
public class SaddlePointsTest
7+
{
8+
[Fact]
9+
public void Can_identify_single_saddle_point()
10+
{
11+
var input = new[,]
12+
{
13+
{ 9, 8, 7 },
14+
{ 5, 3, 2 },
15+
{ 6, 6, 7 }
16+
};
17+
var sut = new SaddlePoints(input);
18+
var actual = sut.Calculate();
19+
var expected = new[] { Tuple.Create(1, 0) };
20+
Assert.Equal(expected, actual);
21+
}
22+
23+
[Fact(Skip = "Remove to run test")]
24+
public void Can_identify_that_empty_matrix_has_no_saddle_points()
25+
{
26+
var input = new int[,] { };
27+
var sut = new SaddlePoints(input);
28+
var actual = sut.Calculate();
29+
Assert.Empty(actual);
30+
}
31+
32+
[Fact(Skip = "Remove to run test")]
33+
public void Can_identify_lack_of_saddle_points_when_there_are_none()
34+
{
35+
var input = new[,]
36+
{
37+
{ 1, 2, 3 },
38+
{ 3, 1, 2 },
39+
{ 2, 3, 1 }
40+
};
41+
var sut = new SaddlePoints(input);
42+
var actual = sut.Calculate();
43+
Assert.Empty(actual);
44+
}
45+
46+
[Fact(Skip = "Remove to run test")]
47+
public void Can_identify_multiple_saddle_points()
48+
{
49+
var input = new[,]
50+
{
51+
{ 4, 5, 4 },
52+
{ 3, 5, 5 },
53+
{ 1, 5, 4 }
54+
};
55+
var sut = new SaddlePoints(input);
56+
var actual = sut.Calculate();
57+
var expected = new[] { Tuple.Create(0, 1), Tuple.Create(1, 1), Tuple.Create(2, 1) };
58+
Assert.Equal(expected, actual);
59+
}
60+
61+
[Fact(Skip = "Remove to run test")]
62+
public void Can_identify_saddle_point_in_bottom_right_corner()
63+
{
64+
var input = new[,]
65+
{
66+
{ 8, 7, 9 },
67+
{ 6, 7, 6 },
68+
{ 3, 2, 5 }
69+
};
70+
var sut = new SaddlePoints(input);
71+
var actual = sut.Calculate();
72+
var expected = new[] { Tuple.Create(2, 2) };
73+
Assert.Equal(expected, actual);
74+
}
75+
}

generators/Exercises/SaddlePoints.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Generators.Input;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace Generators.Exercises
7+
{
8+
public class SaddlePoints : Exercise
9+
{
10+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
11+
{
12+
foreach (var canonicalDataCase in canonicalData.Cases)
13+
{
14+
canonicalDataCase.TestedMethodType = TestedMethodType.Instance;
15+
canonicalDataCase.Property = "Calculate";
16+
canonicalDataCase.SetConstructorInputParameters("input");
17+
canonicalDataCase.UseVariablesForConstructorParameters = true;
18+
canonicalDataCase.UseVariablesForInput = true;
19+
canonicalDataCase.UseVariableForTested = true;
20+
canonicalDataCase.UseVariableForExpected = true;
21+
22+
canonicalDataCase.Properties["input"] = (canonicalDataCase.Properties["input"] as JArray).ToObject<int[,]>();
23+
24+
var array = canonicalDataCase.Expected as Array;
25+
26+
if (array != null)
27+
{
28+
canonicalDataCase.Expected = ToTupleCollection(array);
29+
}
30+
}
31+
}
32+
33+
protected override HashSet<string> AddAdditionalNamespaces()
34+
{
35+
return new HashSet<string>
36+
{
37+
typeof(System.String).Namespace
38+
};
39+
}
40+
41+
private IEnumerable<Tuple<string, object>> ToTupleCollection(Array array)
42+
{
43+
for (int x = 0; x < array.GetLength(0); x++)
44+
{
45+
var current = ((Array)array).GetValue(x) as Dictionary<string, object>;
46+
yield return new Tuple<string, object>(current["row"].ToString(), current["column"].ToString());
47+
}
48+
}
49+
}
50+
}

generators/Output/ValueFormatter.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public static object Format(object val)
2222
return $"{enumeration.GetType().Name}.{enumeration}";
2323
case JArray jArray:
2424
return $"new[] {{ {string.Join(", ", jArray.Select(Format))} }}";
25+
case int[,] multidimensionalArray:
26+
return multidimensionalArray.GetLength(0) > 1
27+
? $"new[,]\r\n{{\r\n {string.Join(",\r\n ", Enumerable.Range(0, multidimensionalArray.GetUpperBound(0) + 1).Select(x => multidimensionalArray.SliceRow(x).ToNestedArray()))}\r\n}}"
28+
: "new int[,] { }";
29+
case IEnumerable<Tuple<string, object>> tuples:
30+
return $"new[] {{ {string.Join(", ", tuples.Select(Format))} }}";
31+
case Tuple<string, object> tuple:
32+
return $"Tuple.Create({tuple.Item1}, {tuple.Item2})";
2533
case string str:
2634
return str.FormatString();
2735
case IEnumerable<int> ints:
@@ -89,5 +97,26 @@ private static IEnumerable<string> AddTrailingSemicolon(this IEnumerable<string>
8997
array[array.Length - 1] += ";";
9098
return array;
9199
}
100+
101+
private static IEnumerable<T> SliceRow<T>(this T[,] array, int row)
102+
{
103+
for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
104+
{
105+
yield return array[row, i];
106+
}
107+
}
108+
109+
private static IEnumerable<T> SliceColumn<T>(this T[,] array, int column)
110+
{
111+
for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
112+
{
113+
yield return array[i, column];
114+
}
115+
}
116+
117+
private static string ToNestedArray<T>(this IEnumerable<T> enumerable)
118+
{
119+
return enumerable.Any() ? $"{{ {string.Join(", ", enumerable)} }}" : string.Empty;
120+
}
92121
}
93122
}

0 commit comments

Comments
 (0)