Skip to content

Commit c04e43a

Browse files
hymccordErikSchierboom
authored andcommitted
Add dominoes test generator (#466)
Resolves #408
1 parent ce33150 commit c04e43a

File tree

2 files changed

+73
-45
lines changed

2 files changed

+73
-45
lines changed

exercises/dominoes/DominoesTest.cs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,91 @@
1-
using System;
1+
// This file was auto-generated based on version 2.0.0 of the canonical data.
2+
23
using Xunit;
4+
using System;
35

46
public class DominoesTest
57
{
68
[Fact]
7-
public void Empty_input_equals_empty_output()
9+
public void Empty_input_empty_output()
810
{
9-
var actual = new Tuple<int, int>[0];
10-
Assert.True(Dominoes.CanChain(actual));
11+
var input = new Tuple<int, int>[] { };
12+
Assert.True(Dominoes.CanChain(input));
1113
}
12-
14+
1315
[Fact(Skip = "Remove to run test")]
14-
public void Singleton_input_equals_singleton_output()
16+
public void Singleton_input_singleton_output()
1517
{
16-
var actual = new[] { Tuple.Create(1, 1) };
17-
Assert.True(Dominoes.CanChain(actual));
18+
var input = new Tuple<int, int>[] { Tuple.Create(1, 1) };
19+
Assert.True(Dominoes.CanChain(input));
1820
}
19-
21+
2022
[Fact(Skip = "Remove to run test")]
2123
public void Singleton_that_cant_be_chained()
2224
{
23-
var actual = new[] { Tuple.Create(1, 2) };
24-
Assert.False(Dominoes.CanChain(actual));
25+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2) };
26+
Assert.False(Dominoes.CanChain(input));
2527
}
26-
28+
2729
[Fact(Skip = "Remove to run test")]
2830
public void Three_elements()
2931
{
30-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(3, 1), Tuple.Create(2, 3) };
31-
Assert.True(Dominoes.CanChain(actual));
32+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(3, 1), Tuple.Create(2, 3) };
33+
Assert.True(Dominoes.CanChain(input));
3234
}
33-
35+
3436
[Fact(Skip = "Remove to run test")]
3537
public void Can_reverse_dominoes()
3638
{
37-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(1, 3), Tuple.Create(2, 3) };
38-
Assert.True(Dominoes.CanChain(actual));
39+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(1, 3), Tuple.Create(2, 3) };
40+
Assert.True(Dominoes.CanChain(input));
3941
}
40-
42+
4143
[Fact(Skip = "Remove to run test")]
4244
public void Cant_be_chained()
4345
{
44-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(4, 1), Tuple.Create(2, 3) };
45-
Assert.False(Dominoes.CanChain(actual));
46+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(4, 1), Tuple.Create(2, 3) };
47+
Assert.False(Dominoes.CanChain(input));
4648
}
47-
49+
4850
[Fact(Skip = "Remove to run test")]
4951
public void Disconnected_simple()
5052
{
51-
var actual = new[] { Tuple.Create(1, 1), Tuple.Create(2, 2) };
52-
Assert.False(Dominoes.CanChain(actual));
53+
var input = new Tuple<int, int>[] { Tuple.Create(1, 1), Tuple.Create(2, 2) };
54+
Assert.False(Dominoes.CanChain(input));
5355
}
54-
56+
5557
[Fact(Skip = "Remove to run test")]
5658
public void Disconnected_double_loop()
5759
{
58-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(2, 1), Tuple.Create(3, 4), Tuple.Create(4, 3) };
59-
Assert.False(Dominoes.CanChain(actual));
60+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(2, 1), Tuple.Create(3, 4), Tuple.Create(4, 3) };
61+
Assert.False(Dominoes.CanChain(input));
6062
}
61-
63+
6264
[Fact(Skip = "Remove to run test")]
6365
public void Disconnected_single_isolated()
6466
{
65-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(4, 4) };
66-
Assert.False(Dominoes.CanChain(actual));
67+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(4, 4) };
68+
Assert.False(Dominoes.CanChain(input));
6769
}
68-
70+
6971
[Fact(Skip = "Remove to run test")]
7072
public void Need_backtrack()
7173
{
72-
var actual = new[] { Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(2, 4), Tuple.Create(2, 4) };
73-
Assert.True(Dominoes.CanChain(actual));
74+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(2, 4), Tuple.Create(2, 4) };
75+
Assert.True(Dominoes.CanChain(input));
7476
}
75-
77+
7678
[Fact(Skip = "Remove to run test")]
7779
public void Separate_loops()
7880
{
79-
var actual = new[]
80-
{
81-
Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(1, 1), Tuple.Create(2, 2), Tuple.Create(3, 3)
82-
};
83-
Assert.True(Dominoes.CanChain(actual));
81+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(2, 3), Tuple.Create(3, 1), Tuple.Create(1, 1), Tuple.Create(2, 2), Tuple.Create(3, 3) };
82+
Assert.True(Dominoes.CanChain(input));
8483
}
8584

8685
[Fact(Skip = "Remove to run test")]
87-
public void Ten_elements()
86+
public void Nine_elements()
8887
{
89-
var actual = new[]
90-
{
91-
Tuple.Create(1, 2), Tuple.Create(5, 3), Tuple.Create(3, 1), Tuple.Create(1, 2), Tuple.Create(2, 4),
92-
Tuple.Create(1, 6), Tuple.Create(2, 3), Tuple.Create(3, 4), Tuple.Create(5, 6)
93-
};
94-
Assert.True(Dominoes.CanChain(actual));
88+
var input = new Tuple<int, int>[] { Tuple.Create(1, 2), Tuple.Create(5, 3), Tuple.Create(3, 1), Tuple.Create(1, 2), Tuple.Create(2, 4), Tuple.Create(1, 6), Tuple.Create(2, 3), Tuple.Create(3, 4), Tuple.Create(5, 6) };
89+
Assert.True(Dominoes.CanChain(input));
9590
}
9691
}

generators/Exercises/Dominoes.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Generators.Input;
5+
using Generators.Output;
6+
using Newtonsoft.Json.Linq;
7+
8+
namespace Generators.Exercises
9+
{
10+
public class Dominoes : Exercise
11+
{
12+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
13+
{
14+
foreach (var canonicalDataCase in canonicalData.Cases)
15+
{
16+
canonicalDataCase.UseVariablesForInput = true;
17+
canonicalDataCase.Properties["input"] = ConvertInput(canonicalDataCase.Properties["input"]);
18+
}
19+
}
20+
21+
protected override HashSet<string> AddAdditionalNamespaces() => new HashSet<string>() { typeof(Tuple).Namespace };
22+
23+
private UnescapedValue ConvertInput(dynamic input)
24+
{
25+
var dominoes = (input as JArray).Children();
26+
27+
// Manually format array of ints to array of tuples since the ValueFormatter doesn't handle Tuple<int,int>[]
28+
// Project each jtoken element to an int array, then format to a string that will create a tuple from the 2-element array (via UnescapedValues)
29+
var tuplesStringLiteral = dominoes.Select(s => s.ToObject<int[]>()).Select(s => $"Tuple.Create({s[0]}, {s[1]})");
30+
return new UnescapedValue($"new Tuple<int, int>[] {{ {string.Join(", ", tuplesStringLiteral)} }}");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)