From 3ff6f9dfa6568356c5eff49ab7b0c9a75985faa8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 17 Mar 2011 14:53:16 +0100 Subject: [PATCH] Solucion Kata Enero @jomarmen --- jomarmen/StringCalculator.cs | 90 ++++++++++++++++++++++++ jomarmen/StringCalculatorTest.cs | 114 +++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 jomarmen/StringCalculator.cs create mode 100644 jomarmen/StringCalculatorTest.cs diff --git a/jomarmen/StringCalculator.cs b/jomarmen/StringCalculator.cs new file mode 100644 index 0000000..e4574e5 --- /dev/null +++ b/jomarmen/StringCalculator.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; + +namespace KataStringCalculator +{ + public class StringCalculator + { + private const String SEPARADOR_DEFECTO = ","; + private const String SEPARADOR_DEFECTO_2 = "\n"; + private const int VALOR_DEFECTO = 0; + private const string SEPARADOR_INICIAL = "//["; + private const string SEPARADOR_FINAL = "]"; + private const int NUMERO_MAXIMO = 1000; + + public int Add(string numbers) + { + List separadores = RecuperaSeparadores(numbers); + numbers = LimpeaDefinicionSeparadorOpcional(numbers, separadores); + + return Suma(numbers, separadores); + } + + private int Suma(string cadena, List separadores) + { + int resultado = VALOR_DEFECTO; + foreach (string number in cadena.Split(separadores.ToArray(), StringSplitOptions.RemoveEmptyEntries)) + { + int numero = Convert.ToInt32(number); + if (ValidarNumeros(Convert.ToInt32(numero))) + { + resultado += numero; + } + } + return resultado; + } + + private bool ValidarNumeros(int numero) + { + bool resultado = true; + if (numero < 0) + { + throw new ArgumentException("Negatives not allowed"); + } + if (numero > NUMERO_MAXIMO) + { + resultado = false; + } + return resultado; + } + private List RecuperaSeparadores(string cadena) + { + List separadores = new List(); + separadores.Add(SEPARADOR_DEFECTO); + separadores.Add(SEPARADOR_DEFECTO_2); + + string[] separadorOpcional = RecuperaDelimitadorOpcional(cadena); + separadores.AddRange(separadorOpcional); + + return separadores; + } + + private string LimpeaDefinicionSeparadorOpcional(string cadena, List separadores) + { + + foreach(string separador in separadores) + { + cadena = cadena.Replace(SEPARADOR_INICIAL + separador, "").Replace(SEPARADOR_FINAL, "").Replace("["+separador,""); + } + return cadena; + } + + public string[] RecuperaDelimitadorOpcional(string cadena) + { + if (cadena.StartsWith(SEPARADOR_INICIAL)) + { + string[] parametros = cadena.Split('\n'); + string parteConDelimitador = parametros[0]; + parteConDelimitador = parteConDelimitador.Replace(SEPARADOR_INICIAL, ""); + parteConDelimitador = parteConDelimitador.Replace(SEPARADOR_FINAL, ""); + + string[] delimitadores = parteConDelimitador.Split('['); + + return delimitadores; + + } + return null; + } + + } +} diff --git a/jomarmen/StringCalculatorTest.cs b/jomarmen/StringCalculatorTest.cs new file mode 100644 index 0000000..096ce59 --- /dev/null +++ b/jomarmen/StringCalculatorTest.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using NUnit.Framework; + + +namespace KataStringCalculator +{ + [TestFixture] + public class StringCalcultorTests + { + + [Test] + public void TestEmpty() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(0, sc.Add("")); + + } + + [Test] + public void Test1N() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(1, sc.Add("1")); + + } + + [Test] + public void Test2N() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(3, sc.Add("1,2")); + } + + [Test] + public void TestNParametros() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(10,sc.Add("1,2,3,4")); + } + + [Test] + public void TestMultipleSeparador() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(15, sc.Add("1\n2,3\n4,5")); + } + + [Test] + public void TestSuperMultipleSeparador() + { + StringCalculator sc = new StringCalculator(); + string parametro = ""; + int resultado = 0; + for (int i = 0; i < 100; i++) + { + string separador = ","; + if ((i % 2) == 2) + { + separador = "\n"; + } + if (i == 0) + { + parametro += i; + } + else + { + parametro += separador + i; + } + + resultado += i; + + Assert.AreEqual(resultado, sc.Add(parametro)); + } + } + + [Test] + public void TestConDelimitadorOpcional() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(10, sc.Add("//[$]\n1,2\n3$4")); + } + + [Test, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Negatives not allowed")] + public void TestExceptionNumerosNegativos() + { + StringCalculator sc = new StringCalculator(); + sc.Add("//[$]\n1,2\n-3$4"); + } + + [Test] + public void TestIgnorarNumerosSuperioresAMil() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(7, sc.Add("//[$]\n1,2\n1001$4")); + } + [Test] + public void TestDelimitadoresLargo() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(6, sc.Add("//[***]\n1***2***3")); + } + + [Test] + public void TestVariosDelimitadores() + { + StringCalculator sc = new StringCalculator(); + Assert.AreEqual(6, sc.Add("//[*][%]\n1*2%3")); + } + } +}