Skip to content

Commit 1f52158

Browse files
all-your-base: add generator
1 parent 2783eaf commit 1f52158

File tree

2 files changed

+38
-98
lines changed

2 files changed

+38
-98
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Xunit;
2+
3+
public class AllYourBaseTests
4+
{
5+
{{for testCase in testCases}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.testMethodName}}()
8+
{
9+
{{if testCase.expected.error}}
10+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase({{testCase.input.inputBase}}, {{testCase.input.digits}}, {{testCase.input.inputBase}}));
11+
{{else}}
12+
Assert.Equal({{testCase.expected}}, AllYourBase.Rebase({{testCase.input.inputBase}}, {{testCase.input.digits}}, {{testCase.input.inputBase}}));
13+
{{end}}
14+
}
15+
{{end}}
16+
}
Lines changed: 22 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,130 @@
1-
using System;
21
using Xunit;
32

43
public class AllYourBaseTests
54
{
65
[Fact]
76
public void Single_bit_one_to_decimal()
87
{
9-
var inputBase = 2;
10-
var digits = new[] { 1 };
11-
var outputBase = 10;
12-
var expected = new[] { 1 };
13-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
8+
Assert.Equal([1], AllYourBase.Rebase(2, [1], 2));
149
}
1510

1611
[Fact(Skip = "Remove this Skip property to run this test")]
1712
public void Binary_to_single_decimal()
1813
{
19-
var inputBase = 2;
20-
var digits = new[] { 1, 0, 1 };
21-
var outputBase = 10;
22-
var expected = new[] { 5 };
23-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
14+
Assert.Equal([5], AllYourBase.Rebase(2, [1, 0, 1], 2));
2415
}
2516

2617
[Fact(Skip = "Remove this Skip property to run this test")]
2718
public void Single_decimal_to_binary()
2819
{
29-
var inputBase = 10;
30-
var digits = new[] { 5 };
31-
var outputBase = 2;
32-
var expected = new[] { 1, 0, 1 };
33-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
20+
Assert.Equal([1, 0, 1], AllYourBase.Rebase(10, [5], 10));
3421
}
3522

3623
[Fact(Skip = "Remove this Skip property to run this test")]
3724
public void Binary_to_multiple_decimal()
3825
{
39-
var inputBase = 2;
40-
var digits = new[] { 1, 0, 1, 0, 1, 0 };
41-
var outputBase = 10;
42-
var expected = new[] { 4, 2 };
43-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
26+
Assert.Equal([4, 2], AllYourBase.Rebase(2, [1, 0, 1, 0, 1, 0], 2));
4427
}
4528

4629
[Fact(Skip = "Remove this Skip property to run this test")]
4730
public void Decimal_to_binary()
4831
{
49-
var inputBase = 10;
50-
var digits = new[] { 4, 2 };
51-
var outputBase = 2;
52-
var expected = new[] { 1, 0, 1, 0, 1, 0 };
53-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
32+
Assert.Equal([1, 0, 1, 0, 1, 0], AllYourBase.Rebase(10, [4, 2], 10));
5433
}
5534

5635
[Fact(Skip = "Remove this Skip property to run this test")]
5736
public void Trinary_to_hexadecimal()
5837
{
59-
var inputBase = 3;
60-
var digits = new[] { 1, 1, 2, 0 };
61-
var outputBase = 16;
62-
var expected = new[] { 2, 10 };
63-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
38+
Assert.Equal([2, 10], AllYourBase.Rebase(3, [1, 1, 2, 0], 3));
6439
}
6540

6641
[Fact(Skip = "Remove this Skip property to run this test")]
6742
public void Hexadecimal_to_trinary()
6843
{
69-
var inputBase = 16;
70-
var digits = new[] { 2, 10 };
71-
var outputBase = 3;
72-
var expected = new[] { 1, 1, 2, 0 };
73-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
44+
Assert.Equal([1, 1, 2, 0], AllYourBase.Rebase(16, [2, 10], 16));
7445
}
7546

7647
[Fact(Skip = "Remove this Skip property to run this test")]
77-
public void Number_15_bit_integer()
48+
public void 15_ bit_integer()
7849
{
79-
var inputBase = 97;
80-
var digits = new[] { 3, 46, 60 };
81-
var outputBase = 73;
82-
var expected = new[] { 6, 10, 45 };
83-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
50+
Assert.Equal([6, 10, 45], AllYourBase.Rebase(97, [3, 46, 60], 97));
8451
}
8552

8653
[Fact(Skip = "Remove this Skip property to run this test")]
8754
public void Empty_list()
8855
{
89-
var inputBase = 2;
90-
var digits = Array.Empty<int>();
91-
var outputBase = 10;
92-
var expected = new[] { 0 };
93-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
56+
Assert.Equal([0], AllYourBase.Rebase(2, [], 2));
9457
}
9558

9659
[Fact(Skip = "Remove this Skip property to run this test")]
9760
public void Single_zero()
9861
{
99-
var inputBase = 10;
100-
var digits = new[] { 0 };
101-
var outputBase = 2;
102-
var expected = new[] { 0 };
103-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
62+
Assert.Equal([0], AllYourBase.Rebase(10, [0], 10));
10463
}
10564

10665
[Fact(Skip = "Remove this Skip property to run this test")]
10766
public void Multiple_zeros()
10867
{
109-
var inputBase = 10;
110-
var digits = new[] { 0, 0, 0 };
111-
var outputBase = 2;
112-
var expected = new[] { 0 };
113-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
68+
Assert.Equal([0], AllYourBase.Rebase(10, [0, 0, 0], 10));
11469
}
11570

11671
[Fact(Skip = "Remove this Skip property to run this test")]
11772
public void Leading_zeros()
11873
{
119-
var inputBase = 7;
120-
var digits = new[] { 0, 6, 0 };
121-
var outputBase = 10;
122-
var expected = new[] { 4, 2 };
123-
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
74+
Assert.Equal([4, 2], AllYourBase.Rebase(7, [0, 6, 0], 7));
12475
}
12576

12677
[Fact(Skip = "Remove this Skip property to run this test")]
12778
public void Input_base_is_one()
12879
{
129-
var inputBase = 1;
130-
var digits = new[] { 0 };
131-
var outputBase = 10;
132-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
80+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(1, [0], 1));
13381
}
13482

13583
[Fact(Skip = "Remove this Skip property to run this test")]
13684
public void Input_base_is_zero()
13785
{
138-
var inputBase = 0;
139-
var digits = Array.Empty<int>();
140-
var outputBase = 10;
141-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
86+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(0, [], 0));
14287
}
14388

14489
[Fact(Skip = "Remove this Skip property to run this test")]
14590
public void Input_base_is_negative()
14691
{
147-
var inputBase = -2;
148-
var digits = new[] { 1 };
149-
var outputBase = 10;
150-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
92+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(-2, [1], -2));
15193
}
15294

15395
[Fact(Skip = "Remove this Skip property to run this test")]
15496
public void Negative_digit()
15597
{
156-
var inputBase = 2;
157-
var digits = new[] { 1, -1, 1, 0, 1, 0 };
158-
var outputBase = 10;
159-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
98+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(2, [1, -1, 1, 0, 1, 0], 2));
16099
}
161100

162101
[Fact(Skip = "Remove this Skip property to run this test")]
163102
public void Invalid_positive_digit()
164103
{
165-
var inputBase = 2;
166-
var digits = new[] { 1, 2, 1, 0, 1, 0 };
167-
var outputBase = 10;
168-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
104+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(2, [1, 2, 1, 0, 1, 0], 2));
169105
}
170106

171107
[Fact(Skip = "Remove this Skip property to run this test")]
172108
public void Output_base_is_one()
173109
{
174-
var inputBase = 2;
175-
var digits = new[] { 1, 0, 1, 0, 1, 0 };
176-
var outputBase = 1;
177-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
110+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(2, [1, 0, 1, 0, 1, 0], 2));
178111
}
179112

180113
[Fact(Skip = "Remove this Skip property to run this test")]
181114
public void Output_base_is_zero()
182115
{
183-
var inputBase = 10;
184-
var digits = new[] { 7 };
185-
var outputBase = 0;
186-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
116+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(10, [7], 10));
187117
}
188118

189119
[Fact(Skip = "Remove this Skip property to run this test")]
190120
public void Output_base_is_negative()
191121
{
192-
var inputBase = 2;
193-
var digits = new[] { 1 };
194-
var outputBase = -7;
195-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
122+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(2, [1], 2));
196123
}
197124

198125
[Fact(Skip = "Remove this Skip property to run this test")]
199126
public void Both_bases_are_negative()
200127
{
201-
var inputBase = -2;
202-
var digits = new[] { 1 };
203-
var outputBase = -7;
204-
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
128+
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(-2, [1], -2));
205129
}
206130
}

0 commit comments

Comments
 (0)