simplify date operators by making SDate.GetHashCode() return unique ordered values, expand unit tests (#307)

This commit is contained in:
Jesse Plamondon-Willard 2017-06-18 20:24:32 -04:00
parent 7e815911e2
commit 0a8c07cc07
2 changed files with 196 additions and 164 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -15,10 +16,35 @@ namespace StardewModdingAPI.Tests
** Properties ** Properties
*********/ *********/
/// <summary>All valid seasons.</summary> /// <summary>All valid seasons.</summary>
private static string[] ValidSeasons = { "spring", "summer", "fall", "winter" }; private static readonly string[] ValidSeasons = { "spring", "summer", "fall", "winter" };
/// <summary>All valid days of a month.</summary> /// <summary>All valid days of a month.</summary>
private static int[] ValidDays = Enumerable.Range(1, 28).ToArray(); private static readonly int[] ValidDays = Enumerable.Range(1, 28).ToArray();
/// <summary>Sample relative dates for test cases.</summary>
private static class Dates
{
/// <summary>The base date to which other dates are relative.</summary>
public const string Now = "02 summer Y2";
/// <summary>The day before <see cref="Now"/>.</summary>
public const string PrevDay = "01 summer Y2";
/// <summary>The month before <see cref="Now"/>.</summary>
public const string PrevMonth = "02 spring Y2";
/// <summary>The year before <see cref="Now"/>.</summary>
public const string PrevYear = "02 summer Y1";
/// <summary>The day after <see cref="Now"/>.</summary>
public const string NextDay = "03 summer Y2";
/// <summary>The month after <see cref="Now"/>.</summary>
public const string NextMonth = "02 fall Y2";
/// <summary>The year after <see cref="Now"/>.</summary>
public const string NextYear = "02 summer Y3";
}
/********* /*********
@ -63,7 +89,7 @@ namespace StardewModdingAPI.Tests
[TestCase("01 winter Y1", ExpectedResult = "01 winter Y1")] [TestCase("01 winter Y1", ExpectedResult = "01 winter Y1")]
public string ToString(string dateStr) public string ToString(string dateStr)
{ {
return this.ParseDate(dateStr).ToString(); return this.GetDate(dateStr).ToString();
} }
/**** /****
@ -80,58 +106,132 @@ namespace StardewModdingAPI.Tests
[TestCase("01 spring Y3", -(28 * 7 + 17), ExpectedResult = "12 spring Y1")] // negative year transition [TestCase("01 spring Y3", -(28 * 7 + 17), ExpectedResult = "12 spring Y1")] // negative year transition
public string AddDays(string dateStr, int addDays) public string AddDays(string dateStr, int addDays)
{ {
return this.ParseDate(dateStr).AddDays(addDays).ToString(); return this.GetDate(dateStr).AddDays(addDays).ToString();
} }
[Test(Description = "Assert that the equality operators work as expected")] /****
public void EqualityOperators() ** GetHashCode
****/
[Test(Description = "Assert that GetHashCode returns a unique ordered value for every date.")]
public void GetHashCode_ReturnsUniqueOrderedValue()
{ {
SDate s1 = new SDate(1, "spring", 2); IDictionary<int, SDate> hashes = new Dictionary<int, SDate>();
SDate s2 = new SDate(1, "spring", 2); int lastHash = int.MinValue;
SDate s3 = new SDate(1, "spring", 3); for (int year = 1; year <= 4; year++)
SDate s4 = new SDate(12, "spring", 2); {
SDate s5 = new SDate(1, "summer", 2); foreach (string season in SDateTests.ValidSeasons)
{
foreach (int day in SDateTests.ValidDays)
{
SDate date = new SDate(day, season, year);
int hash = date.GetHashCode();
if (hashes.TryGetValue(hash, out SDate otherDate))
Assert.Fail($"Received identical hash code {hash} for dates {otherDate} and {date}.");
if (hash < lastHash)
Assert.Fail($"Received smaller hash code for date {date} ({hash}) relative to {hashes[lastHash]} ({lastHash}).");
Assert.AreEqual(true, s1 == s2); lastHash = hash;
Assert.AreNotEqual(true, s1 == s3); hashes[hash] = date;
Assert.AreNotEqual(true, s1 == s4); }
Assert.AreNotEqual(true, s1 == s5); }
}
} }
[Test(Description = "Assert that the comparison operators work as expected")] [Test(Description = "Assert that the == operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
public void ComparisonOperators() [TestCase(Dates.Now, null, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
public bool Operators_Equals(string now, string other)
{ {
SDate s1 = new SDate(1, "spring", 2); return this.GetDate(now) == this.GetDate(other);
SDate s2 = new SDate(1, "spring", 2);
SDate s3 = new SDate(1, "spring", 3);
SDate s4 = new SDate(12, "spring", 2);
SDate s5 = new SDate(1, "summer", 2);
SDate s6 = new SDate(1, "winter", 1);
SDate s7 = new SDate(13, "fall", 1);
Assert.AreEqual(true, s1 <= s2);
Assert.AreEqual(true, s1 >= s2);
Assert.AreEqual(true, s1 < s4);
Assert.AreEqual(true, s1 <= s4);
Assert.AreEqual(true, s4 > s1);
Assert.AreEqual(true, s4 >= s1);
Assert.AreEqual(true, s5 > s7);
Assert.AreEqual(true, s5 >= s7);
Assert.AreEqual(true, s6 < s5);
Assert.AreEqual(true, s6 <= s5);
Assert.AreEqual(true, s1 < s5);
Assert.AreEqual(true, s1 <= s5);
Assert.AreEqual(true, s5 > s1);
Assert.AreEqual(true, s5 >= s1);
} }
[Test(Description = "Assert that the != operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
[TestCase(Dates.Now, null, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
public bool Operators_NotEquals(string now, string other)
{
return this.GetDate(now) != this.GetDate(other);
}
[Test(Description = "Assert that the < operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
[TestCase(Dates.Now, null, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
public bool Operators_LessThan(string now, string other)
{
return this.GetDate(now) < this.GetDate(other);
}
[Test(Description = "Assert that the <= operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
[TestCase(Dates.Now, null, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
public bool Operators_LessThanOrEqual(string now, string other)
{
return this.GetDate(now) <= this.GetDate(other);
}
[Test(Description = "Assert that the > operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
[TestCase(Dates.Now, null, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
public bool Operators_MoreThan(string now, string other)
{
return this.GetDate(now) > this.GetDate(other);
}
[Test(Description = "Assert that the > operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
[TestCase(Dates.Now, null, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
[TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
[TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
public bool Operators_MoreThanOrEqual(string now, string other)
{
return this.GetDate(now) > this.GetDate(other);
}
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Convert a string date into a game date, to make unit tests easier to read.</summary> /// <summary>Convert a string date into a game date, to make unit tests easier to read.</summary>
/// <param name="dateStr">The date string like "dd MMMM yy".</param> /// <param name="dateStr">The date string like "dd MMMM yy".</param>
private SDate ParseDate(string dateStr) private SDate GetDate(string dateStr)
{ {
if (dateStr == null)
return null;
void Fail(string reason) => throw new AssertionException($"Couldn't parse date '{dateStr}' because {reason}."); void Fail(string reason) => throw new AssertionException($"Couldn't parse date '{dateStr}' because {reason}.");
// parse // parse

View File

@ -13,6 +13,9 @@ namespace StardewModdingAPI.Utilities
/// <summary>The internal season names in order.</summary> /// <summary>The internal season names in order.</summary>
private readonly string[] Seasons = { "spring", "summer", "fall", "winter" }; private readonly string[] Seasons = { "spring", "summer", "fall", "winter" };
/// <summary>The number of seasons in a year.</summary>
private int SeasonsInYear => this.Seasons.Length;
/// <summary>The number of days in a season.</summary> /// <summary>The number of days in a season.</summary>
private readonly int DaysInSeason = 28; private readonly int DaysInSeason = 28;
@ -77,10 +80,8 @@ namespace StardewModdingAPI.Utilities
// handle season transition // handle season transition
if (day > this.DaysInSeason || day < 1) if (day > this.DaysInSeason || day < 1)
{ {
// get current season index // get season index
int curSeasonIndex = Array.IndexOf(this.Seasons, this.Season); int curSeasonIndex = this.GetSeasonIndex();
if (curSeasonIndex == -1)
throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
// get season offset // get season offset
int seasonOffset = day / this.DaysInSeason; int seasonOffset = day / this.DaysInSeason;
@ -94,7 +95,7 @@ namespace StardewModdingAPI.Utilities
} }
// validate // validate
if(year < 1) if (year < 1)
throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}."); throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}.");
// return new date // return new date
@ -116,157 +117,88 @@ namespace StardewModdingAPI.Utilities
/********* /*********
** Operator methods ** Operator methods
*********/ *********/
/// <summary>Get whether one date is equal to another.</summary>
/// <summary> /// <param name="date">The base date to compare.</param>
/// Equality operator. Tests the date being equal to each other /// <param name="other">The other date to compare.</param>
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>The equality of the dates</returns> /// <returns>The equality of the dates</returns>
public static bool operator ==(SDate s1, SDate s2) public static bool operator ==(SDate date, SDate other)
{ {
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season) return date?.GetHashCode() == other?.GetHashCode();
return true;
else
return false;
} }
/// <summary> /// <summary>Get whether one date is not equal to another.</summary>
/// Inequality operator. Tests the date being not equal to each other /// <param name="date">The base date to compare.</param>
/// </summary> /// <param name="other">The other date to compare.</param>
/// <param name="s1">The first date being compared</param> public static bool operator !=(SDate date, SDate other)
/// <param name="s2">The second date being compared</param>
/// <returns>The inequality of the dates</returns>
public static bool operator !=(SDate s1, SDate s2)
{ {
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season) return date?.GetHashCode() != other?.GetHashCode();
return false;
else
return true;
} }
/// <summary> /// <summary>Get whether one date is more than another.</summary>
/// Less than operator. Tests the date being less than to each other /// <param name="date">The base date to compare.</param>
/// </summary> /// <param name="other">The other date to compare.</param>
/// <param name="s1">The first date being compared</param> public static bool operator >(SDate date, SDate other)
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than</returns>
public static bool operator >(SDate s1, SDate s2)
{ {
if (s1.Year > s2.Year) return date?.GetHashCode() > other?.GetHashCode();
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day > s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
} }
/// <summary> /// <summary>Get whether one date is more than or equal to another.</summary>
/// Less or equal than operator. Tests the date being less than or equal to each other /// <param name="date">The base date to compare.</param>
/// </summary> /// <param name="other">The other date to compare.</param>
/// <param name="s1">The first date being compared</param> public static bool operator >=(SDate date, SDate other)
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than or equal than</returns>
public static bool operator >=(SDate s1, SDate s2)
{ {
if (s1.Year > s2.Year) return date?.GetHashCode() >= other?.GetHashCode();
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day >= s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
} }
/// <summary> /// <summary>Get whether one date is less than or equal to another.</summary>
/// Greater or equal than operator. Tests the date being greater than or equal to each other /// <param name="date">The base date to compare.</param>
/// </summary> /// <param name="other">The other date to compare.</param>
/// <param name="s1">The first date being compared</param> public static bool operator <=(SDate date, SDate other)
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than or equal than</returns>
public static bool operator <=(SDate s1, SDate s2)
{ {
if (s1.Year < s2.Year) return date?.GetHashCode() <= other?.GetHashCode();
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day <= s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
} }
/// <summary> /// <summary>Get whether one date is less than another.</summary>
/// Greater than operator. Tests the date being greater than to each other /// <param name="date">The base date to compare.</param>
/// </summary> /// <param name="other">The other date to compare.</param>
/// <param name="s1">The first date being compared</param> public static bool operator <(SDate date, SDate other)
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than</returns>
public static bool operator <(SDate s1, SDate s2)
{ {
if (s1.Year < s2.Year) return date?.GetHashCode() < other?.GetHashCode();
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day < s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
} }
/// <summary> /// <summary>Overrides the equals function.</summary>
/// Overrides the equals function.
/// </summary>
/// <param name="obj">Object being compared.</param> /// <param name="obj">Object being compared.</param>
/// <returns>The equalaity of the object.</returns> /// <returns>The equalaity of the object.</returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return base.Equals(obj); return obj is SDate other && this == other;
} }
/// <summary> /// <summary>Get a hash code which uniquely identifies a date.</summary>
/// This returns the hashcode of the object
/// </summary>
/// <returns>The hashcode of the object.</returns>
public override int GetHashCode() public override int GetHashCode()
{ {
return base.GetHashCode(); // return the number of days since 01 spring Y1
int yearIndex = this.Year - 1;
return
yearIndex * this.DaysInSeason * this.SeasonsInYear
+ this.GetSeasonIndex() * this.DaysInSeason
+ this.Day;
} }
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Get the current season index.</summary>
/// <exception cref="InvalidOperationException">The current season wasn't recognised.</exception>
private int GetSeasonIndex()
{
int index = Array.IndexOf(this.Seasons, this.Season);
if (index == -1)
throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
return index;
}
/// <summary>Get the real index in an array which should be treated as a two-way loop.</summary> /// <summary>Get the real index in an array which should be treated as a two-way loop.</summary>
/// <param name="index">The index in the looped array.</param> /// <param name="index">The index in the looped array.</param>
/// <param name="length">The number of elements in the array.</param> /// <param name="length">The number of elements in the array.</param>