Skip to content

Add OCR Numbers Test Generator #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions exercises/ocr-numbers/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,67 @@ public static class OcrNumbers
public static string Convert(string input)
{
var lines = input.Split('\n');


if (lines.Length > CharacterHeight)
lines = GetTransformedInput(lines);

return Positions(lines).Aggregate("", (str, pos) => str + ConvertCharacter(lines, pos.Item1, pos.Item2));
}

/// <summary>
/// Transform multilines input into one line digits separate by comma
/// </summary>
/// <param name="lines"></param>
/// <returns></returns>
private static string[] GetTransformedInput(string[] lines)
{
string[] transformedLines = new string[CharacterHeight];

for (int x = 0, characterRow = 0; x < lines.Length; x++, characterRow++)
{
if (characterRow > 3)
characterRow = 0;

for (int y = 0; y < lines[x].Length; y++)
transformedLines[characterRow] += lines[x][y];

//for empty & last line add comma
if (string.IsNullOrWhiteSpace(lines[x]) && lines.Length - 1 != x)
{
for (int i = 0; i < CharacterHeight; i++)
transformedLines[i] += i == CharacterWidth - 1 ? " ," : " ";
}
}

return transformedLines;
}

private static IEnumerable<Tuple<int, int>> Positions(string[] lines)
{
return from x in Enumerable.Range(0, Rows(lines))
from y in Enumerable.Range(0, Cols(lines))
select Tuple.Create(x, y);
}

private static int Cols(string[] lines) => lines[0].Length / CharacterWidth;
private static int Cols(string[] lines)
{
return lines[0].Length % CharacterWidth == 0
? lines[0].Length / CharacterWidth
: throw new ArgumentException();
}

private static int Rows(string[] lines)
{
return lines.Length % CharacterHeight == 0
? lines.Length / CharacterHeight
: throw new ArgumentException();
}

private static bool IsEmptyLine(string line)
{
return string.IsNullOrWhiteSpace(line);
}

private static int Rows(string[] lines) => lines.Length / CharacterHeight;

private static char ConvertCharacter(string[] input, int row, int col) => MatchCharacter(Character(input, row, col));

private static string Character(string[] input, int row, int col)
Expand All @@ -41,9 +87,9 @@ private static char MatchCharacter(string character)
private static readonly IReadOnlyDictionary<string, char> CharactersMap = new Dictionary<string, char>
{
{
" _ " +
"| |" +
"|_|" +
" _ " +
"| |" +
"|_|" +
" ",
'0'
},
Expand Down Expand Up @@ -109,6 +155,13 @@ private static char MatchCharacter(string character)
" _|" +
" ",
'9'
},
{
" " +
" " +
" ," +
" ",
','
}
};
}
227 changes: 136 additions & 91 deletions exercises/ocr-numbers/OcrNumbersTest.cs
Original file line number Diff line number Diff line change
@@ -1,154 +1,199 @@
using Xunit;
// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;
using System;

public class OcrNumbersTest
{
[Fact]
public void Recognizes_zero()
public void Recognizes_0()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
"| |" + "\n" +
"|_|" + "\n" +
" ");
Assert.Equal("0", converted);
var input = " _ " + "\n" +
"| |" + "\n" +
"|_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("0", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_one()
public void Recognizes_1()
{
var converted = OcrNumbers.Convert(" " + "\n" +
" |" + "\n" +
" |" + "\n" +
" ");
Assert.Equal("1", converted);
var input = " " + "\n" +
" |" + "\n" +
" |" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("1", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_two()
public void Unreadable_but_correctly_sized_inputs_return_()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
" _|" + "\n" +
"|_ " + "\n" +
" ");
Assert.Equal("2", converted);
var input = " " + "\n" +
" _" + "\n" +
" |" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("?", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_three()
public void Input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
" _|" + "\n" +
" _|" + "\n" +
" ");
Assert.Equal("3", converted);
var input = " _ " + "\n" +
"| |" + "\n" +
" ";
Assert.Throws<ArgumentException>(() => OcrNumbers.Convert(input));
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_four()
public void Input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error()
{
var converted = OcrNumbers.Convert(" " + "\n" +
"|_|" + "\n" +
" |" + "\n" +
" ");
Assert.Equal("4", converted);
var input = " " + "\n" +
" |" + "\n" +
" |" + "\n" +
" ";
Assert.Throws<ArgumentException>(() => OcrNumbers.Convert(input));
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_five()
public void Recognizes_110101100()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
"|_ " + "\n" +
" _|" + "\n" +
" ");
Assert.Equal("5", converted);
var input = " _ _ _ _ " + "\n" +
" | || | || | | || || |" + "\n" +
" | ||_| ||_| | ||_||_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("110101100", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_six()
public void Garbled_numbers_in_a_string_are_replaced_with_()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
"|_ " + "\n" +
"|_|" + "\n" +
" ");
Assert.Equal("6", converted);
var input = " _ _ _ " + "\n" +
" | || | || | || || |" + "\n" +
" | | _| ||_| | ||_||_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("11?10?1?0", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_seven()
public void Recognizes_2()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
" |" + "\n" +
" |" + "\n" +
" ");
Assert.Equal("7", converted);
var input = " _ " + "\n" +
" _|" + "\n" +
"|_ " + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("2", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_eight()
public void Recognizes_3()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
"|_|" + "\n" +
"|_|" + "\n" +
" ");
Assert.Equal("8", converted);
var input = " _ " + "\n" +
" _|" + "\n" +
" _|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("3", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_nine()
public void Recognizes_4()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
"|_|" + "\n" +
" _|" + "\n" +
" ");
Assert.Equal("9", converted);
var input = " " + "\n" +
"|_|" + "\n" +
" |" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("4", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_garble()
public void Recognizes_5()
{
var converted = OcrNumbers.Convert(" " + "\n" +
"| |" + "\n" +
"| |" + "\n" +
" ");
Assert.Equal("?", converted);
var input = " _ " + "\n" +
"|_ " + "\n" +
" _|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("5", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_ten()
public void Recognizes_6()
{
var converted = OcrNumbers.Convert(" _ " + "\n" +
" || |" + "\n" +
" ||_|" + "\n" +
" ");
Assert.Equal("10", converted);
var input = " _ " + "\n" +
"|_ " + "\n" +
"|_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("6", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_110101100()
public void Recognizes_7()
{
var input = " _ " + "\n" +
" |" + "\n" +
" |" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("7", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_8()
{
var input = " _ " + "\n" +
"|_|" + "\n" +
"|_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("8", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_9()
{
var converted = OcrNumbers.Convert(" _ _ _ _ " + "\n" +
" | || | || | | || || |" + "\n" +
" | ||_| ||_| | ||_||_|" + "\n" +
" ");
Assert.Equal("110101100", converted);
var input = " _ " + "\n" +
"|_|" + "\n" +
" _|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("9", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_numbers_and_garble()
public void Recognizes_string_of_decimal_numbers()
{
var converted = OcrNumbers.Convert(" _ _ _ " + "\n" +
" | || | || | || || |" + "\n" +
" | | _| ||_| | ||_||_|" + "\n" +
" ");
Assert.Equal("11?10?1?0", converted);
var input = " _ _ _ _ _ _ _ _ " + "\n" +
" | _| _||_||_ |_ ||_||_|| |" + "\n" +
" ||_ _| | _||_| ||_| _||_|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("1234567890", actual);
}

[Fact(Skip = "Remove to run test")]
public void Recognizes_1234567890()
public void Numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas_()
{
var converted = OcrNumbers.Convert(" _ _ _ _ _ _ _ _ " + "\n" +
" | _| _||_||_ |_ ||_||_|| |" + "\n" +
" ||_ _| | _||_| ||_| _||_|" + "\n" +
" ");
Assert.Equal("1234567890", converted);
var input = " _ _ " + "\n" +
" | _| _|" + "\n" +
" ||_ _|" + "\n" +
" " + "\n" +
" _ _ " + "\n" +
"|_||_ |_ " + "\n" +
" | _||_|" + "\n" +
" " + "\n" +
" _ _ _ " + "\n" +
" ||_||_|" + "\n" +
" ||_| _|" + "\n" +
" ";
var actual = OcrNumbers.Convert(input);
Assert.Equal("123,456,789", actual);
}
}
Loading