Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test suite for LCDConverter
using System;
namespace CodingKata
{
class LCDConverter
{
public string ConvertToLCD (int number)
{
if ( number < 0 ) throw new ArgumentOutOfRangeException("Only positive numbers please");
var result = "";
int i = 0;
foreach ( var c in number.ToString() )
{
string lcdDigit = "";
int n = (int) char.GetNumericValue(c);
switch ( n )
{
#region switch assign to lcdDigit
case 0: lcdDigit =
" _ \n"+
"| |\n"+
"|_|\n";
break;
case 1: lcdDigit =
" \n"+
" |\n"+
" |\n";
break;
case 2: lcdDigit =
" _ \n"+
" _|\n"+
"|_ \n";
break;
case 3: lcdDigit =
" _ \n"+
" _|\n"+
" _|\n";
break;
case 4: lcdDigit =
" \n"+
"|_|\n"+
" |\n";
break;
case 5: lcdDigit =
" _ \n"+
"|_ \n"+
" _|\n";
break;
case 6: lcdDigit =
" _ \n"+
"|_ \n"+
"|_|\n";
break;
case 7: lcdDigit =
" _ \n"+
" |\n"+
" |\n";
break;
case 8: lcdDigit =
" _ \n"+
"|_|\n"+
"|_|\n";
break;
case 9: lcdDigit =
" _ \n"+
"|_|\n"+
" _|\n";
break;
#endregion
}
result = i == 0 ? lcdDigit : result
.Insert( 9 * i+2 , lcdDigit.Substring (8,3))
.Insert( 6 * i+1 , lcdDigit.Substring (4,3))
.Insert( 3 * i , lcdDigit.Substring (0,3)) ;
i++;
}
return result;
}
public static void Main()
{
Console.WriteLine ( new LCDConverter().ConvertToLCD(1234567890) );
Console.WriteLine ( new LCDConverter().ConvertToLCD(-1) );
}
}
}
using NUnit.Framework;
namespace CodingKata
{
[TestFixture()]
public class LCDConverterTest
{
private LCDConverter main;
[TestFixtureSetUpAttribute]
public void SetUp()
{
main = new LCDConverter();
}
[Test()]
public void TestOne()
{
// Original test:
// Assert.AreEqual( numeros[1], main.ConvertToLCD( 1 ) );
// later refactored to:
testNumberInArray ( 1 );
// after the second test "TestSeven"
}
[Test()]
public void TestSeven()
{
// Second test.
// Refactor to extract method "testNumberInArray"
testNumberInArray ( 7 );
}
private void testNumberInArray( int n )
{
Assert.AreEqual( numeros[n], main.ConvertToLCD( n ) );
}
[Test()]
public void Test0To9 ()
{
// Third test, everything was easy up to this point
// because a simple switch did the trick
for ( var i = 0 ; i < numeros.Length; i++ )
{
testNumberInArray ( i );
}
}
[Test()]
public void TestGreaterThan9()
{
Assert.AreEqual(
" _ \n"+
" || |\n" +
" ||_|\n",
main.ConvertToLCD(10)
);
Assert.AreEqual(
" _ \n"+
"|_| _|\n"+
" ||_ \n",
main.ConvertToLCD( 42 )
);
}
[Test()]
public void TestGreaterThan99()
{
Assert.AreEqual(
" _ _ \n"+
" || || |\n"+
" ||_||_|\n",
main.ConvertToLCD(100)
);
}
[Test()]
public void TestDadaaaaa()
{
Assert.AreEqual(
" _ _ _ _ _ _ _ _ \n"+
" | _| _||_||_ |_ ||_||_|| |\n"+
" ||_ _| | _||_| ||_| _||_|\n",
main.ConvertToLCD(1234567890)
);
}
[Test()]
public void TestNegative()
{
try
{
main.ConvertToLCD( -1 );
Assert.Fail("Should've failed with negative");
}
catch( System.ArgumentOutOfRangeException ){}
catch
{
Assert.Fail("Shouldn't have thrown anything else");
}
}
// using verbatim string didn't look as I expected
string [] numeros = new string [] {
@" _
| |
|_|
",
@"
|
|
",
@" _
_|
|_
",
@" _
_|
_|
",
@"
|_|
|
",
@" _
|_
_|
",
@" _
|_
|_|
",
@" _
|
|
",
@" _
|_|
|_|
",
@" _
|_|
_|
"
};
}
}
@oscarryz

This comment has been minimized.

Copy link
Owner Author

commented Mar 20, 2012

package main
import (
    "fmt"
    "strings"
    "strconv"
)
func main() {
    fmt.Printf("%s\n", convertToLCD( 1234567890 ) )
}
func convertToLCD( n int ) string {
    i , result , numbers  := 0 , "" , strings.Split( all_numbers, ".\n" )

    for   _, char :=  range strconv.Itoa( n )  {
        current := numbers[ char  - '0'  ]

        if  i == 0   {
            result = current
        } else {
            r1, r2, r3 := 3*i, 6*i+1, 9*i+2
            result = result[0    : r1] + current[0:4] +
                     result[r1+1 : r2] + current[4:8] +
                     result[r2+1 : r3] + current[8:12]
        }
        i++
    }
    return result
}
const all_numbers = 
` _ 
| |
|_|
.

  |
  |
.
 _ 
 _|
|_ 
.
 _ 
 _|
 _|
.

|_|
  |
.
 _ 
|_ 
 _|
.
 _ 
|_ 
|_|
.
 _ 
  |
  |
.
 _ 
|_|
|_|
.
 _ 
|_|
 _|
`
@yngwie74

This comment has been minimized.

Copy link

commented Mar 22, 2012

Solo un comentario: el mensaje de la excepción en la validación inicial dice "solo > 0", cuando 0 es un argumento válido =)

Me llama la atención lo compacta y "tersa" que resulta la versión en Go. Necesito ponerme a estudiar, ya que hay algunos detalles sintácticos que todavía no me entran en la cabeza.

Muy buena solución... Y con pruebas además!!

@oscarryz

This comment has been minimized.

Copy link
Owner Author

commented Mar 22, 2012

je! :) si... corregido.

Si.. esta padré Go , verdad? Más aún, esa versión fue una transliteración de la de C# pero hice una pregunta en SO y me hicieron una revisión que es mucho más eficiente
Ahí la dejo:

http://stackoverflow.com/a/9780750/20654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.