Skip to content

Instantly share code, notes, and snippets.

@handcraftsman
Created October 18, 2012 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handcraftsman/3913788 to your computer and use it in GitHub Desktop.
Save handcraftsman/3913788 to your computer and use it in GitHub Desktop.
sample code breaker
// this breaker was used on @scichelli's
// collection comparer
// https://gist.github.com/3836918/
[Test]
public void Breaker()
{
var legal = GetIntLegals();
for (int i = 0; i < 1000000; i++)
{
var expected = legal
.Concat(legal)
.Shuffle()
.Take(4)
.Concat(new[] { "...", "..." }.Random(1, 2))
.Shuffle()
.ToArray();
var actual = expected.Select(x => x == "..." ? legal.Random(0, 3) : new[] { x })
.SelectMany(x => x)
.ToArray();
var expectedText = " var expected = new[] { \"" + String.Join("\", \"", expected) + "\" };";
var actualText = " var actual = new[] { \"" + String.Join("\", \"", actual) + "\" };";
try
{
AssertStructure(actual, expected);
}
catch (Exception exception)
{
Console.WriteLine("Broke it with:" + exception.Message);
Console.WriteLine(expectedText);
Console.WriteLine(actualText);
break;
}
}
}
public IList<string> GetIntLegals()
{
return "1234567890".Select(x => "" + x).ToList();
}
public IList<string> GetIntLetterLegals()
{
var legal1 = "1234567890".Select(x => "" + x).ToList();
var legal2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Select(x => "" + x).ToList();
var legal = (from n in legal1
from m in legal2
select n + m).ToList();
return legal;
}
public IList<string> GetVisibleTextLegals()
{
var legal = @"qwertyuiop[]\asdfghjkl;'zxcvbnm,./`1234567890-=~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:""ZXCVBNM<>?".Select(x => "" + x).ToList();
return legal;
}
public static class Extensions
{
private static readonly Random Rnd = new Random();
public static IList<T> Random<T>(this IEnumerable<T> list, int min, int max)
{
var items = list.ToList();
var rand = Rnd.Next(min, max + 1);
return items.Shuffle().Take(rand).ToArray();
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
{
var toreturn = new List<T>(list);
for (var i = 1; i < toreturn.Count; i++)
{
var pos = Rnd.Next(i + 1);
var x = toreturn[i];
toreturn[i] = toreturn[pos];
toreturn[pos] = x;
}
return toreturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment