Created
October 5, 2012 19:13
-
-
Save thefringeninja/3841781 to your computer and use it in GitHub Desktop.
Greg Young Probability Kata - Simple Testing Style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Probability : IEquatable<Probability> | |
{ | |
#region Equality members | |
public bool Equals(Probability other) | |
{ | |
return EqualsWithin(other, 5); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (obj.GetType() != this.GetType()) return false; | |
return Equals((Probability) obj); | |
} | |
private bool EqualsWithin(Probability other, int precision) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
return Math.Abs(Math.Round(p, precision) - Math.Round(other.p, precision)) < Single.Epsilon; | |
} | |
public override int GetHashCode() | |
{ | |
return p.GetHashCode(); | |
} | |
#endregion | |
private readonly float p; | |
public Probability(float p) | |
{ | |
this.p = p; | |
} | |
public static Probability Of(float p) | |
{ | |
return new Probability(p); | |
} | |
public static Probability Of(float chance, float@in) | |
{ | |
return new Probability(chance/@in); | |
} | |
public override string ToString() | |
{ | |
return string.Format("A probability of {0:p10}", p); | |
} | |
public Probability Either(Probability other) | |
{ | |
return new Probability((p + other.p) - (p*other.p)); | |
} | |
public Probability CombinedWith(Probability other) | |
{ | |
return new Probability(p * other.p); | |
} | |
public Probability InverseOf() | |
{ | |
return new Probability(1 - p); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProbabilitySpecifications | |
{ | |
public Specification equality() | |
{ | |
return new ConstructorSpecification<Probability> | |
{ | |
When = () => Probability.Of(1), | |
Expect = | |
{ | |
sut => sut.Equals(Probability.Of(1)) | |
} | |
}; | |
} | |
public IEnumerable<Specification> either() | |
{ | |
var data = new[] | |
{ | |
Tuple.Create(Probability.Of(1), Probability.Of(1), Probability.Of(1)), | |
Tuple.Create(Probability.Of(0), Probability.Of(1), Probability.Of(1)), | |
Tuple.Create(Probability.Of(1, 2), Probability.Of(1, 2), Probability.Of(3, 4)) | |
}; | |
return from row in data | |
select new QuerySpecification<Probability, Probability> | |
{ | |
Name = "Either " + row.Item1 + " or " + row.Item2, | |
On = () => row.Item1, | |
When = sut => sut.Either(row.Item2), | |
Expect = | |
{ | |
sut => sut.Equals(row.Item3) | |
} | |
}; | |
} | |
public IEnumerable<Specification> combined_with() | |
{ | |
var data = new[] | |
{ | |
Tuple.Create(Probability.Of(1), Probability.Of(1), Probability.Of(1)), | |
Tuple.Create(Probability.Of(0), Probability.Of(1), Probability.Of(0)), | |
Tuple.Create(Probability.Of(1, 3), Probability.Of(1, 3), Probability.Of(1,9)), | |
}; | |
return from row in data | |
select new QuerySpecification<Probability, Probability> | |
{ | |
Name = row.Item1 + " Combined with " + row.Item2, | |
On = () => row.Item1, | |
When = sut => sut.CombinedWith(row.Item2), | |
Expect = | |
{ | |
sut => sut.Equals(row.Item3) | |
} | |
}; | |
} | |
public IEnumerable<Specification> inverse_of() | |
{ | |
var data = new[] | |
{ | |
Tuple.Create(Probability.Of(0), Probability.Of(1)), | |
Tuple.Create(Probability.Of(1), Probability.Of(0)), | |
Tuple.Create(Probability.Of(1, 3), Probability.Of(2, 3)), | |
Tuple.Create(Probability.Of(1, 2), Probability.Of(1, 2)), | |
}; | |
return from row in data | |
select new QuerySpecification<Probability, Probability> | |
{ | |
Name = "Inverse of " + row.Item1, | |
On = () => row.Item1, | |
When = sut => sut.InverseOf(), | |
Expect = | |
{ | |
sut => sut.Equals(row.Item2) | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment