p.zaba p.zaba - 10 days ago 6
C# Question

Unit test of "Gaderypoluki" algorithm

I need to encode/decode a string by "Gaderypoluki" algorithm.
Everything works, if I do it in my own solution, a code:

using System;
using System.Linq;
using System.Text;

namespace algorithm
{
public class Gaderypoluki
{
public static Tuple<string, int[]> Encode(string key, string toEncrypt)
{
int[] iterations = new int[] { 0, 0, 0, 0, 0, 0 };
if (key.Length % 2 == 0)
{
if (key.Length == key.Distinct().Count())
{
var encodedText = new StringBuilder(toEncrypt);
for (int i = 0; i < toEncrypt.Length ; i++)
{

for (int j = 0; j < key.Length; j += 2)
{

if (key[j] == toEncrypt[i])
{
encodedText[i] = key[j + 1];
iterations[j / 2] += 1;

}
else if (key[j+1] == toEncrypt[i])
{
encodedText[i] = key[j];
iterations[j / 2] += 1;
}
}
}

return Tuple.Create(encodedText.ToString(), iterations);
}
else throw new ArgumentException("Key cannot contain the same chars");
}
else throw new ArgumentException("You have to put a key which is dividable by 2");
}



static void Main(string[] args)
{
var key = "gaderypoluki";
var encodedText = "gug mg iptg";
try
{
Console.WriteLine(Encode(key, encodedText).Item1);
Console.ReadLine();
}
catch (ArgumentException ex)
{
throw ex;
}
}
}
}


In this case, solution mades everything well.
Result will be "ala ma kota", so it's a good result.
But the problem starts, when I want to test it by auto test such as:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace algorithm
{
[TestClass]
public class GaderypolukiTests
{
[TestMethod]
public void TestMethod1()
{
var key = "GADERYPOLUKI";
var encodedText = "gug mg iptg";
var expectedText = "ala ma kota";
Tuple<string, int[]> actual = Gaderypoluki.Encode(key, encodedText);


Assert.AreEqual(expectedText.ToLower(), actual.Item1.ToLower());
}
}
}


Now, if I start a test, a method
Encode()
does completely nothing. While debugging, even if
if (key[j] == toEncrypt[i])
equals, compiler says it doesn't and goes on.
I really don't know why, was searching a lot about this issue but this problem is not resolved yet(or I could search better).
Thanks for help!

Answer

Your problem is in your unit test, you define your key as "GADERYPOLUKI" and in your implementation you do key[j] == toEncrypt[i], which is case sensitive. This works in your application because you define the key in all lower case. So either:

  • Update your test to have the key be in all lower case
  • Update your implementation to do a case insensitive comparison
Comments