Skip to content

Instantly share code, notes, and snippets.

@serialseb
Created December 8, 2010 14:56
Show Gist options
  • Save serialseb/733369 to your computer and use it in GitHub Desktop.
Save serialseb/733369 to your computer and use it in GitHub Desktop.
A simple value object in .net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace DDD_1
{
class Program
{
static void Main(string[] args)
{
}
}
[TestFixture]
internal class probability_specs
{
[TestCase(0d)]
[TestCase(0.5d)]
[TestCase(1d)]
public void can_create(decimal value)
{
new Probability(value);
}
[TestCase(-1d)]
[TestCase(1.5d)]
public void incorrect_values_throw(decimal value)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new Probability(value));
}
[TestCase(0.5d, 0.5d, 0.25d)]
[TestCase(0.25d, 0.5d, 0.125d)]
public void should_combine_as_the_multiplication_of_prob(decimal first, decimal second, decimal result)
{
Assert.That(new Probability(first).CombinedWith(new Probability(second)), Is.EqualTo(new Probability(result)));
}
[TestCase(1d, 0d)]
[TestCase(0d, 1d)]
[TestCase(.5d, .5d)]
public void should_inverse_by_substracting_from_1(decimal value, decimal expected)
{
Assert.That(new Probability(value).Inverse(), Is.EqualTo(new Probability(expected)));
}
[TestCase(0d, 0d, 0d)]
[TestCase(0d, 1d, 1d)]
[TestCase(1d, 1d, 1d)]
[TestCase(.5d, .5d, .75d)]
public void should_apply_either_by_adding_both_minus_combined(decimal first, decimal second, decimal expected)
{
Assert.That(new Probability(first).Either(new Probability(second)), Is.EqualTo(new Probability(expected)));
}
}
public class Probability : IEquatable<Probability>
{
private readonly decimal _value;
public Probability(decimal value)
{
_value = value;
if (value < 0 || value > 1)
throw new ArgumentOutOfRangeException("value");
}
public Probability CombinedWith(Probability probability)
{
return new Probability(_value*probability._value);
}
public bool Equals(Probability other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other._value.Equals(_value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Probability)) return false;
return Equals((Probability) obj);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public static bool operator ==(Probability left, Probability right)
{
return Equals(left, right);
}
public static bool operator !=(Probability left, Probability right)
{
return !Equals(left, right);
}
public override string ToString()
{
return _value*100 + "%";
}
public Probability Inverse()
{
return new Probability(1 - _value);
}
public Probability Either(Probability probability)
{
return new Probability(_value + probability._value - CombinedWith(probability)._value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment