I am trying out some TDD on Mat Buckland’s AI example code from “Programming AI by Example”. I translated Telegram.h to Telegram.css:
// ----------------------------------------------------------------------- // Telegram.cs // ----------------------------------------------------------------------- namespace WestWorld { using System; using System.Collections.Generic; using System.Linq; using System.Text; /// <summary> /// TODO: Update summary. /// </summary> public class Telegram { /// <summary> /// These telegrams will be stored in a priority queue. Therefore the > /// operator needs to be overloaded so that the PQ can sort the telegrams /// by time priority. Note how the times must be smaller than /// SmallestDelay apart before two Telegrams are considered unique. /// </summary> private const double SmallestDelay = 0.25; /// <summary> /// the entity that sent this telegram /// </summary> private int sender; /// <summary> /// the entity that is to receive this telegram /// </summary> private int receiver; /// <summary> /// the message itself. These are all enumerated in the file "MessageTypes.h" /// </summary> private int msg; /// <summary> /// Messages can be dispatched immediately or delayed for a specified amount /// of time. If a delay is necessary this field is stamped with the time /// the message should be dispatched. /// </summary> private double dispatchTime; // any additional information that may accompany the message // void* ExtraInfo; /// <summary> /// Initializes a new instance of the <see cref="Telegram"/> class. /// </summary> public Telegram() { this.sender = 0; this.receiver = 0; this.msg = 0; this.dispatchTime = 0; } /// <summary> /// Gets or sets the sender. /// </summary> /// <value> /// The sender. /// </value> public int Sender { get { return this.sender; } set { this.sender = value; } } /// <summary> /// Gets or sets the receiver. /// </summary> /// <value> /// The receiver. /// </value> public int Receiver { get { return this.receiver; } set { this.receiver = value; } } /// <summary> /// Gets or sets the message. /// </summary> /// <value> /// The message. /// </value> public int Message { get { return this.msg; } set { this.msg = value; } } /// <summary> /// Gets or sets the dispatch time. /// </summary> /// <value> /// The dispatch time. /// </value> public double DispatchTime { get { return this.dispatchTime; } set { this.dispatchTime = value; } } /// <summary> /// Implements the operator ==. /// </summary> /// <param name="t1">The t1.</param> /// <param name="t2">The t2.</param> /// <returns> /// The result of the operator. /// </returns> public static bool operator ==(Telegram t1, Telegram t2) { double temp = Math.Abs(t1.DispatchTime - t2.DispatchTime); return Math.Abs(t1.DispatchTime - t2.DispatchTime) < Telegram.SmallestDelay && t1.DispatchTime == t2.DispatchTime; } /// <summary> /// Implements the operator !=. /// </summary> /// <param name="t1">The t1.</param> /// <param name="t2">The t2.</param> /// <returns> /// The result of the operator. /// </returns> public static bool operator !=(Telegram t1, Telegram t2) { return Math.Abs(t1.DispatchTime - t2.DispatchTime) > Telegram.SmallestDelay && t1.DispatchTime != t2.DispatchTime; } /// <summary> /// Implements the operator <. /// </summary> /// <param name="t1">The t1.</param> /// <param name="t2">The t2.</param> /// <returns> /// The result of the operator. /// </returns> public static bool operator <(Telegram t1, Telegram t2) { if (t1 == t2) { return false; } else { return t1.DispatchTime < t2.DispatchTime; } } /// <summary> /// Implements the operator >. /// </summary> /// <param name="t1">The t1.</param> /// <param name="t2">The t2.</param> /// <returns> /// The result of the operator. /// </returns> public static bool operator >(Telegram t1, Telegram t2) { if (t1 == t2) { return false; } else { return t1.DispatchTime > t2.DispatchTime; } } /// <summary> /// Determines whether the specified <see cref="System.Object"/> is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { return (Telegram)obj == this; } /// <summary> /// Returns a hash code for this instance. /// </summary> /// <returns> /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// </returns> public override int GetHashCode() { return base.GetHashCode(); } } }
namespace TestWestWorld { using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using WestWorld; /// <summary> /// Tests for the Telegram class /// </summary> [TestClass] public class TestTelegram { /// <summary> /// Test context /// </summary> private TestContext testContextInstance; /// <summary> /// Initializes a new instance of the <see cref="TestTelegram"/> class. /// </summary> public TestTelegram() { // TODO: Add constructor logic here } /// <summary> /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// </summary> public TestContext TestContext { get { return this.testContextInstance; } set { this.testContextInstance = value; } } #region Additional test attributes // You can use the following additional attributes as you write your tests: // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } #endregion /// <summary> /// Tests the telegram equals == operator /// </summary> [TestMethod] public void TestTelegramEqualsOperator() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); Assert.IsTrue(telegram1 == telegram2); } /// <summary> /// Tests the telegram not equals operator. /// </summary> [TestMethod] public void TestTelegramNotEqualsOperator() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); Assert.IsFalse(telegram1 != telegram2); } /// <summary> /// Tests the telegram equals method. /// </summary> [TestMethod] public void TestTelegramEqualsMethod() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); Assert.IsTrue(telegram1.Equals(telegram2)); } /// <summary> /// Tests the telegram not equals method. /// </summary> [TestMethod] public void TestTelegramNotEqualsMethod() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram1.DispatchTime = 25; Assert.IsFalse(telegram1.Equals(telegram2)); } /// <summary> /// Tests the telegram not equals operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramEqualsOperatorWhenObjectNotSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram1.DispatchTime = 25; Assert.IsFalse(telegram1 == telegram2); } /// <summary> /// Tests the telegram not equals operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramNotEqualsOperatorWhenObjectNotSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram1.DispatchTime = 25; Assert.IsTrue(telegram1 != telegram2); } /// <summary> /// Tests the telegram less than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramLessThanOperatorWhenObjectIsSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); Assert.IsFalse(telegram1 < telegram2); } /// <summary> /// Tests the telegram less than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramLessThanOperatorWhenObjectIsNotSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram1.DispatchTime = 25; Assert.IsFalse(telegram1 < telegram2); } /// <summary> /// Tests the telegram less than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramLessThanOperatorWhenObjectNotSameButLess() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram2.DispatchTime = 25; Assert.IsTrue(telegram1 < telegram2); } /// <summary> /// Tests the telegram greater than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramGreaterThanOperatorWhenObjectIsSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); Assert.IsFalse(telegram1 > telegram2); } /// <summary> /// Tests the telegram greater than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramGreaterThanOperatorWhenObjectIsNotSame() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram1.DispatchTime = 25; Assert.IsFalse(telegram1 < telegram2); } /// <summary> /// Tests the telegram greater than operator when the object members /// have been set to different values. /// </summary> [TestMethod] public void TestTelegramGreaterThanOperatorWhenObjectNotSameButLess() { Telegram telegram1 = new Telegram(); Telegram telegram2 = new Telegram(); telegram2.DispatchTime = 25; Assert.IsTrue(telegram1 < telegram2); } } }
No comments:
Post a Comment