Skip to content

Commit 1fee6da

Browse files
committed
binary and trinary exercises didn't have tests verifying if values were valid or not
1 parent c808509 commit 1fee6da

4 files changed

Lines changed: 37 additions & 15 deletions

File tree

binary/BinaryTest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ public int Binary_converts_to_decimal(string binary)
1515
{
1616
return new Binary(binary).ToDecimal();
1717
}
18+
19+
[TestCase("carrot", Ignore = true)]
20+
[TestCase("2", Ignore = true)]
21+
[TestCase("5", Ignore = true)]
22+
[TestCase("9", Ignore = true)]
23+
[TestCase("134678", Ignore = true)]
24+
[TestCase("abc10z", Ignore = true)]
25+
public void Invalid_binary_is_decimal_0(string invalidValue)
26+
{
27+
Assert.That(new Binary(invalidValue).ToDecimal(), Is.EqualTo(0));
28+
}
1829

1930
[Ignore]
2031
[Test]
21-
public void Invalid_binary_is_decimal_0()
32+
public void Binary_can_convert_formatted_strings()
2233
{
23-
Assert.That(new Binary("carrot123").ToDecimal(), Is.EqualTo(0));
34+
Assert.That(new Binary("011").ToDecimal(), Is.EqualTo(3));
2435
}
2536
}

binary/Example.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33

44
public class Binary
55
{
6-
private readonly string number;
6+
private readonly string value;
77

8-
public Binary(string number)
8+
public Binary(string value)
99
{
10-
this.number = number;
10+
this.value = value;
1111
}
1212

1313
public int ToDecimal()
1414
{
15-
if (IsNotANumber()) return 0;
15+
if (IsNotValidBinary()) return 0;
1616

17-
return number
18-
.Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(number.Length - i - 1))
17+
return value
18+
.Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(value.Length - i - 1))
1919
.Sum();
2020
}
2121

22-
private bool IsNotANumber()
22+
private bool IsNotValidBinary()
2323
{
24-
return !number.All(char.IsDigit);
24+
return !value.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 2);
2525
}
2626

2727
private static int TwoToThePowerOf(int power)

trinary/Example.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ public Trinary(string value)
1212

1313
public int ToDecimal()
1414
{
15-
if (IsNotNumeric()) return 0;
15+
if (IsNotValidTrinary()) return 0;
1616

1717
return value
1818
.Select((c, i) => int.Parse(c.ToString()) * ThreeToThePowerOf(value.Length - i - 1))
1919
.Sum();
2020
}
2121

22-
private bool IsNotNumeric()
22+
private bool IsNotValidTrinary()
2323
{
24-
return !value.All(char.IsDigit);
24+
return !value.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 3);
2525
}
2626

2727
private static int ThreeToThePowerOf(int power)

trinary/TrinaryTest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ public int Trinary_converts_to_decimal(string value)
1717
return new Trinary(value).ToDecimal();
1818
}
1919

20+
[TestCase("carrot", Ignore = true)]
21+
[TestCase("3", Ignore = true)]
22+
[TestCase("6", Ignore = true)]
23+
[TestCase("9", Ignore = true)]
24+
[TestCase("124578", Ignore = true)]
25+
[TestCase("abc1z", Ignore = true)]
26+
public void Invalid_trinary_is_decimal_0(string invalidValue)
27+
{
28+
Assert.That(new Trinary(invalidValue).ToDecimal(), Is.EqualTo(0));
29+
}
30+
2031
[Ignore]
2132
[Test]
22-
public void Invalid_trinary_is_decimal_0()
33+
public void Trinary_can_convert_formatted_strings()
2334
{
24-
Assert.That(new Trinary("carrot").ToDecimal(), Is.EqualTo(0));
35+
Assert.That(new Trinary("011").ToDecimal(), Is.EqualTo(4));
2536
}
2637
}

0 commit comments

Comments
 (0)