Last active
September 11, 2020 17:51
-
-
Save arlm/e77794dc8b3831e921d2de04e3f94d3c to your computer and use it in GitHub Desktop.
Clock
This file contains hidden or 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
using System; | |
using System.Diagnostics.CodeAnalysis; | |
public partial class Clock : IComparable, IComparable<Clock>, | |
IComparable<int>, IComparable<long>, IComparable<short>, | |
IComparable<decimal>, IComparable<double>, IComparable<float> | |
{ | |
public int CompareTo(object other) | |
{ | |
if (ReferenceEquals(this, other)) | |
{ | |
return 0; | |
} | |
if (other is Clock clock) | |
{ | |
return CompareTo(clock); | |
} | |
throw new ArgumentException("Cannot compare objects that are not Clocks."); | |
} | |
public int CompareTo([AllowNull] Clock other) | |
{ | |
if (ReferenceEquals(this, other)) | |
{ | |
return 0; | |
} | |
if (this == other) | |
{ | |
return 0; | |
} | |
if (this > other) | |
{ | |
return 1; | |
} | |
return -1; | |
} | |
public int CompareTo([AllowNull] int other) | |
{ | |
var minutes = ToMinutes(); | |
return minutes.CompareTo(other); | |
} | |
public int CompareTo([AllowNull] float other) | |
{ | |
var minutes = unchecked((float)ToMinutes()); | |
return minutes.CompareTo(other); | |
} | |
public int CompareTo([AllowNull] short other) | |
{ | |
var minutes = unchecked((short) ToMinutes()); | |
return minutes.CompareTo(other); | |
} | |
public int CompareTo([AllowNull] long other) | |
{ | |
var minutes = unchecked((long)ToMinutes()); | |
return minutes.CompareTo(other); | |
} | |
public int CompareTo([AllowNull] double other) | |
{ | |
var minutes = unchecked((double)ToMinutes()); | |
return minutes.CompareTo(other); | |
} | |
public int CompareTo([AllowNull] decimal other) | |
{ | |
var minutes = unchecked((decimal)ToMinutes()); | |
return minutes.CompareTo(other); | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Diagnostics.CodeAnalysis; | |
public partial class Clock : IEquatable<Clock> | |
{ | |
/// <summary> | |
/// Compares the value of two different clocks. | |
/// </summary> | |
public bool Equals([AllowNull] Clock other) | |
{ | |
if (other is null) | |
{ | |
return false; | |
} | |
if (ReferenceEquals(this, other)) | |
{ | |
return true; | |
} | |
return Hours == other.Hours && Minutes == other.Minutes; | |
} | |
} |
This file contains hidden or 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
using System; | |
public partial class Clock : IEquatable<Clock> | |
{ | |
/// <summary> | |
/// Returns a positive clock. | |
/// </summary> | |
public static Clock operator +(Clock clock) => new Clock(clock.Hours, clock.Minutes); | |
/// <summary> | |
/// Returns a negative clock (makes both hours and minutes negative). | |
/// </summary> | |
public static Clock operator -(Clock clock) => new Clock(-clock.Hours, -clock.Minutes); | |
/// <summary> | |
/// Increments the clock in one minute. | |
/// </summary> | |
public static Clock operator ++(Clock clock) => | |
clock.Add(1); | |
/// <summary> | |
/// Decrements the clock in one minute. | |
/// </summary> | |
public static Clock operator --(Clock clock) => | |
clock.Subtract(1); | |
/// <summary> | |
/// Adds one clocktime to another. | |
/// </summary> | |
public static Clock operator +(Clock one, Clock another) => | |
new Clock(one.Hours + another.Hours, one.Minutes + another.Minutes); | |
/// <summary> | |
/// Subtracts the time of a clock from another clock time. | |
/// </summary> | |
public static Clock operator -(Clock one, Clock another) => | |
new Clock(one.Hours - another.Hours, one.Minutes - another.Minutes); | |
/// <summary> | |
/// Adds a specified minutes amount to a clock. | |
/// </summary> | |
public static Clock operator +(Clock clock, int minutes) => | |
new Clock(clock.Hours, clock.Minutes + minutes); | |
/// <summary> | |
/// Subtracts a specified minutes amount to a clock. | |
/// </summary> | |
public static Clock operator -(Clock clock, int minutes) => | |
new Clock(clock.Hours, clock.Minutes - minutes); | |
/// <summary> | |
/// Multiplies the total amount of minutes by a specified factor. | |
/// </summary> | |
public static Clock operator *(Clock clock, int value) => | |
new Clock(clock.ToMinutes() * value); | |
/// <summary> | |
/// Divides the total amount of minutes by a specified factor. | |
/// </summary> | |
public static Clock operator /(Clock clock, int value) => | |
new Clock(clock.ToMinutes() / value); | |
/// <summary> | |
/// Compares the equality of a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator ==(Clock one, Clock another) => | |
one.Equals(another); | |
/// <summary> | |
/// Compares the inequality of a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator !=(Clock one, Clock another) => | |
!one.Equals(another); | |
/// <summary> | |
/// Compares a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator <(Clock one, Clock another) => | |
one.ToMinutes() < another.ToMinutes(); | |
/// <summary> | |
/// Compares a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator >(Clock one, Clock another) => | |
one.ToMinutes() > another.ToMinutes(); | |
/// <summary> | |
/// Compares a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator <=(Clock one, Clock another) => | |
one.ToMinutes() <= another.ToMinutes(); | |
/// <summary> | |
/// Compares a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public static bool operator >=(Clock one, Clock another) => | |
one.ToMinutes() >= another.ToMinutes(); | |
} |
This file contains hidden or 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
using System; | |
using System.Runtime.CompilerServices; | |
/// <summary> | |
/// Class that represents a clock. | |
/// </summary> | |
public partial class Clock | |
{ | |
internal int Hours { get; } | |
internal int Minutes { get; } | |
/// <summary> | |
/// Creates a clock from a given number of minutes. | |
/// </summary> | |
/// <param name="minutes">Given minutes to set the clock.</param> | |
public Clock(int minutes):this(0, minutes) | |
{ | |
} | |
/// <summary> | |
/// Creates a clock from hours and minutes. | |
/// </summary> | |
/// <param name="hours">Given hours to create the clock.</param> | |
/// <param name="minutes">Given minutes to create the clock.</param> | |
public Clock(int hours, int minutes) | |
{ | |
Hours = (hours + (minutes / 60)) % 24; | |
Minutes = minutes % 60; | |
if (Minutes < 0) | |
{ | |
Hours--; | |
Minutes = 60 + Minutes; | |
} | |
if (Hours < 0) | |
{ | |
Hours = 24 + Hours; | |
} | |
} | |
public Clock Add(int minutesToAdd) => new Clock(Hours, Minutes + minutesToAdd); | |
public Clock Subtract(int minutesToSubtract) => new Clock(Hours, Minutes - minutesToSubtract); | |
public int ToMinutes() => Hours * 60 + Minutes; | |
/// <summary> | |
/// Compares a clock with any othe object, including clocks. | |
/// When comparing two clocks, compares the value of them. | |
/// </summary> | |
/// <remarks>When comparing two clocks, compares the value of them.</remarks> | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(this, obj)) | |
{ | |
return true; | |
} | |
return obj is Clock clock && Equals(clock); | |
} | |
///<summary> | |
///<para>If you are implementing a value type, you should consider overriding | |
///the <see cref="Equals"/> method to gain increased performance over the | |
///default implementation of the <see cref="Equals"/> method on <see cref="ValueType"/>.</para> | |
///<para>If you are implementing a reference type, you should consider overriding | |
///the <see cref="Equals"/> method if your type looks like a base type, | |
///such as <see cref="Point"/>, <see cref="String"/>, <see cref="BigNumber"/>, and so on.</para> | |
///<para>Override the <see cref="GetHashCode"/> method to allow a type to work | |
///correctly in a hash table.</para> | |
///<para> | |
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netcore-3.1#notes-to-inheritors">MS Documentation</seealso><br/> | |
/// <seealso href="http://www.isthe.com/chongo/tech/comp/fnv/">FNV hash implementation</seealso><br/> | |
/// <seealso href="https://stackoverflow.com/a/263416/412426">StackOverflow</seealso> | |
/// </para> | |
/// </summary> | |
/// <remarks> | |
/// Note that one thing to be aware of is that ideally you should prevent | |
/// your equality-sensitive (and thus hashcode-sensitive) state from | |
/// changing after adding it to a collection that depends on the hash code. | |
/// </remarks> | |
/// <example> | |
/// public override int GetHashCode() | |
/// { | |
/// unchecked // Overflow is fine, just wrap | |
/// { | |
/// int hash = (int)2166136261; | |
/// // Suitable nullity checks etc, of course :) | |
/// hash = (hash * 16777619) ^ Hours.GetHashCode(); | |
/// hash = (hash * 16777619) ^ Minutes.GetHashCode(); | |
/// return hash; | |
/// } | |
/// } | |
/// </example> | |
/// <returns></returns> | |
public override int GetHashCode() => HashCode.Combine(Hours, Minutes); | |
public override string ToString() => $"{Hours:D2}:{Minutes:D2}"; | |
} |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using Xunit; | |
public class ClockComparableTests | |
{ | |
[Fact] | |
public void Equality() | |
{ | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(3, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(2, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(1, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(0, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(75, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(51, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(27, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(26, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(25, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(24, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(21, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(20, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(19, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(18, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(0, 1_260))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(-21, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(-20, -60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(-19, -120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(-18, -180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(new Clock(0, -1_260))); | |
} | |
[Fact] | |
public void GreaterThan() | |
{ | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-22, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-22, -59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-22, -01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-23, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-23, -59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-23, -01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(-24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_261))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_380))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_320))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_410))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_437))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(0, -1_429))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(48, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(new Clock(72, 0))); | |
} | |
[Fact] | |
public void LesserThan() | |
{ | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(24, 59))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(24, 01))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(3, 0))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(2, 60))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(1, 120))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(0, 180))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(-24, -59))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(-24, -01))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(-3, 0))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(-2, -60))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(-1, -120))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(new Clock(0, -180))); | |
} | |
[Fact] | |
public void Equality_object() | |
{ | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(3, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(2, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(1, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(0, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(75, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(51, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(27, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(26, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(25, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(24, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(21, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(20, 60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(19, 120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(18, 180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo(-new Clock(0, 1_260))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(-21, 0))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(-20, -60))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(-19, -120))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(-18, -180))); | |
Assert.Equal(0, new Clock(3, 0).CompareTo((object)new Clock(0, -1_260))); | |
} | |
[Fact] | |
public void GreaterThan_object() | |
{ | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(22, 01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(23, 01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo(-new Clock(24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-22, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-22, -59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-22, -01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-23, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-23, -59))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-23, -01))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(-24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_261))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_380))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_320))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_410))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_437))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(0, -1_429))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(24, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(48, 0))); | |
Assert.Equal(1, new Clock(3, 0).CompareTo((object)new Clock(72, 0))); | |
} | |
[Fact] | |
public void LesserThan_object() | |
{ | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(24, 59))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(24, 01))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(3, 0))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(2, 60))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(1, 120))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo(-new Clock(0, 180))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(-24, -59))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(-24, -01))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(-3, 0))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(-2, -60))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(-1, -120))); | |
Assert.Equal(-1, new Clock(3, 0).CompareTo((object)new Clock(0, -180))); | |
} | |
[Fact] | |
public void CompareTo_Object_throws_ArgumentException() | |
{ | |
Assert.Throws<ArgumentException>(() => new Clock(3, 0).CompareTo(new object())); | |
} | |
[Fact] | |
public void CompareTo_List_throws_ArgumentException() | |
{ | |
Assert.Throws<ArgumentException>(() => new Clock(3, 0).CompareTo(new List<Clock> { new Clock(01,01) })); | |
} | |
[Fact] | |
public void CompareTo_String_throws_ArgumentException() | |
{ | |
Assert.Throws<ArgumentException>(() => new Clock(3, 0).CompareTo("teste")); | |
Assert.Throws<ArgumentException>(() => new Clock(3, 0).CompareTo(string.Empty)); | |
} | |
[Fact] | |
public void Equals_Object_throws_ArgumentException() | |
{ | |
Assert.False(new Clock(3, 0).Equals(new object())); | |
} | |
[Fact] | |
public void Equals_List_throws_ArgumentException() | |
{ | |
Assert.False(new Clock(3, 0).Equals(new List<Clock> { new Clock(01, 01) })); | |
} | |
[Fact] | |
public void Equals_String_throws_ArgumentException() | |
{ | |
Assert.False(new Clock(3, 0).Equals("teste")); | |
Assert.False(new Clock(3, 0).Equals(string.Empty)); | |
} | |
[Fact] | |
public void Default_sorting() | |
{ | |
var input = new List<Clock> { new Clock(03, 00), new Clock(27, 01), new Clock(01, 02), -new Clock(21, 30) }; | |
var expected = new List<Clock> { new Clock(01, 02), -new Clock(21, 30), new Clock(03, 00), new Clock(27, 01) }; | |
input.Sort(); | |
Assert.Equal(expected, input); | |
} | |
[Fact] | |
public void Ascending_sorting() | |
{ | |
var input = new List<Clock> { new Clock(03, 00), new Clock(27, 01), new Clock(01, 02), -new Clock(21, 30) }; | |
var expected = new List<Clock> { new Clock(01, 02), -new Clock(21, 30), new Clock(03, 00), new Clock(27, 01) }; | |
input.Sort(ClockComparer.Ascending); | |
Assert.Equal(expected, input); | |
} | |
[Fact] | |
public void Descending_sorting() | |
{ | |
var input = new List<Clock> { new Clock(03, 00), new Clock(27, 01), new Clock(01, 02), -new Clock(21, 30) }; | |
var expected = new List<Clock> { new Clock(27, 01), new Clock(03, 00) , - new Clock(21, 30), new Clock(01, 02) }; | |
input.Sort(ClockComparer.Descending); | |
Assert.Equal(expected, input); | |
} | |
[Fact] | |
public void Descending_hour_ascending_minute_sorting() | |
{ | |
var input = new List<Clock> { new Clock(27, 49), new Clock(03, 00), new Clock(27, 01), new Clock(01, 02), -new Clock(21, 30), new Clock(03, 15) }; | |
var expected = new List<Clock> { new Clock(03, 00), new Clock(27, 01), new Clock(03, 15), new Clock(27, 49), -new Clock(21, 30), new Clock(01, 02) }; | |
input.Sort(ClockComparer.DescendingHourAscendingMinutes); | |
Assert.Equal(expected, input); | |
} | |
} |
This file contains hidden or 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
using Xunit; | |
public class ClockOperatorTests | |
{ | |
[Fact] | |
public void Negative_without_minutes() | |
{ | |
var sut = new Clock(8, 0); | |
Assert.Equal("08:00", sut.ToString()); | |
Assert.Equal("16:00", (-sut).ToString()); | |
} | |
[Fact] | |
public void Negative_with_minutes() | |
{ | |
var sut = new Clock(0, 8); | |
Assert.Equal("00:08", sut.ToString()); | |
Assert.Equal("23:52", (-sut).ToString()); | |
} | |
[Fact] | |
public void Increment_does_not_change_hour() | |
{ | |
var sut = new Clock(8, 0); | |
Assert.Equal("08:00", sut.ToString()); | |
sut++; | |
Assert.Equal("08:01", sut.ToString()); | |
++sut; | |
Assert.Equal("08:02", sut.ToString()); | |
} | |
[Fact] | |
public void Increment_changes_hour() | |
{ | |
var sut = new Clock(8, 59); | |
Assert.Equal("08:59", sut.ToString()); | |
sut++; | |
Assert.Equal("09:00", sut.ToString()); | |
sut = new Clock(8, 59); | |
++sut; | |
Assert.Equal("09:00", sut.ToString()); | |
} | |
[Fact] | |
public void Decrement_does_not_change_hour() | |
{ | |
var sut = new Clock(8, 2); | |
Assert.Equal("08:02", sut.ToString()); | |
sut--; | |
Assert.Equal("08:01", sut.ToString()); | |
--sut; | |
Assert.Equal("08:00", sut.ToString()); | |
} | |
[Fact] | |
public void Decrement_changes_hour() | |
{ | |
var sut = new Clock(8, 0); | |
Assert.Equal("08:00", sut.ToString()); | |
sut--; | |
Assert.Equal("07:59", sut.ToString()); | |
--sut; | |
Assert.Equal("07:58", sut.ToString()); | |
} | |
[Fact] | |
public void Add_changes_day() | |
{ | |
var sut = new Clock(8, 0) + new Clock(17, 0); | |
Assert.Equal("01:00", sut.ToString()); | |
} | |
[Fact] | |
public void Add_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) + new Clock(7, 0); | |
Assert.Equal("15:00", sut.ToString()); | |
} | |
[Fact] | |
public void Subtract_changes_day() | |
{ | |
var sut = new Clock(8, 0) - new Clock(17, 0); | |
Assert.Equal("15:00", sut.ToString()); | |
} | |
[Fact] | |
public void Subtract_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) - new Clock(7, 0); | |
Assert.Equal("01:00", sut.ToString()); | |
} | |
[Fact] | |
public void Add_minutes_changes_day() | |
{ | |
var sut = new Clock(8, 0) + 1_020; | |
Assert.Equal("01:00", sut.ToString()); | |
} | |
[Fact] | |
public void Add_minutes_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) + 420; | |
Assert.Equal("15:00", sut.ToString()); | |
} | |
[Fact] | |
public void Subtract_minutes_changes_day() | |
{ | |
var sut = new Clock(8, 0) - 1_020; | |
Assert.Equal("15:00", sut.ToString()); | |
} | |
[Fact] | |
public void Subtract_minutes_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) - 420; | |
Assert.Equal("01:00", sut.ToString()); | |
} | |
[Fact] | |
public void Multiply_changes_day() | |
{ | |
var sut = new Clock(8, 0) * 3; | |
Assert.Equal("00:00", sut.ToString()); | |
sut = new Clock(8, 0) * 4; | |
Assert.Equal("08:00", sut.ToString()); | |
sut = new Clock(8, 0) * 5; | |
Assert.Equal("16:00", sut.ToString()); | |
} | |
[Fact] | |
public void Multiply_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) * 2; | |
Assert.Equal("16:00", sut.ToString()); | |
sut = new Clock(4, 22) * 3; | |
Assert.Equal("13:06", sut.ToString()); | |
} | |
[Fact] | |
public void Divides_leaves_only_minutes() | |
{ | |
var sut = new Clock(8, 0) / 9; | |
Assert.Equal("00:53", sut.ToString()); | |
sut = new Clock(8, 0) / 10; | |
Assert.Equal("00:48", sut.ToString()); | |
sut = new Clock(8, 0) / 11; | |
Assert.Equal("00:43", sut.ToString()); | |
sut = new Clock(8, 0) / 12; | |
Assert.Equal("00:40", sut.ToString()); | |
sut = new Clock(8, 0) / 13; | |
Assert.Equal("00:36", sut.ToString()); | |
sut = new Clock(8, 0) / 14; | |
Assert.Equal("00:34", sut.ToString()); | |
sut = new Clock(8, 0) / 15; | |
Assert.Equal("00:32", sut.ToString()); | |
sut = new Clock(8, 0) / 16; | |
Assert.Equal("00:30", sut.ToString()); | |
sut = new Clock(8, 0) / 17; | |
Assert.Equal("00:28", sut.ToString()); | |
sut = new Clock(8, 0) / 18; | |
Assert.Equal("00:26", sut.ToString()); | |
sut = new Clock(8, 0) / 19; | |
Assert.Equal("00:25", sut.ToString()); | |
sut = new Clock(8, 0) / 20; | |
Assert.Equal("00:24", sut.ToString()); | |
sut = new Clock(8, 0) / 21; | |
Assert.Equal("00:22", sut.ToString()); | |
sut = new Clock(8, 0) / 22; | |
Assert.Equal("00:21", sut.ToString()); | |
sut = new Clock(8, 0) / 23; | |
Assert.Equal("00:20", sut.ToString()); | |
sut = new Clock(8, 0) / 32; | |
Assert.Equal("00:15", sut.ToString()); | |
sut = new Clock(8, 0) / 44; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 45; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 46; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 47; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 48; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 45; | |
Assert.Equal("00:10", sut.ToString()); | |
sut = new Clock(8, 0) / 80; | |
Assert.Equal("00:06", sut.ToString()); | |
sut = new Clock(8, 0) / 81; | |
Assert.Equal("00:05", sut.ToString()); | |
sut = new Clock(8, 0) / 85; | |
Assert.Equal("00:05", sut.ToString()); | |
sut = new Clock(8, 0) / 90; | |
Assert.Equal("00:05", sut.ToString()); | |
sut = new Clock(8, 0) / 95; | |
Assert.Equal("00:05", sut.ToString()); | |
sut = new Clock(8, 0) / 96; | |
Assert.Equal("00:05", sut.ToString()); | |
sut = new Clock(8, 0) / 97; | |
Assert.Equal("00:04", sut.ToString()); | |
sut = new Clock(8, 0) / 100; | |
Assert.Equal("00:04", sut.ToString()); | |
sut = new Clock(8, 0) / 110; | |
Assert.Equal("00:04", sut.ToString()); | |
sut = new Clock(8, 0) / 120; | |
Assert.Equal("00:04", sut.ToString()); | |
sut = new Clock(8, 0) / 160; | |
Assert.Equal("00:03", sut.ToString()); | |
sut = new Clock(8, 0) / 240; | |
Assert.Equal("00:02", sut.ToString()); | |
sut = new Clock(8, 0) / 320; | |
Assert.Equal("00:01", sut.ToString()); | |
sut = new Clock(8, 0) / 480; | |
Assert.Equal("00:01", sut.ToString()); | |
sut = new Clock(8, 0) / 500; | |
Assert.Equal("00:00", sut.ToString()); | |
} | |
[Fact] | |
public void Divides_does_not_change_day() | |
{ | |
var sut = new Clock(8, 0) / 4; | |
Assert.Equal("02:00", sut.ToString()); | |
sut = new Clock(8, 0) / 3; | |
Assert.Equal("02:40", sut.ToString()); | |
sut = new Clock(8, 0) / 5; | |
Assert.Equal("01:36", sut.ToString()); | |
sut = new Clock(8, 0) / 6; | |
Assert.Equal("01:20", sut.ToString()); | |
sut = new Clock(8, 0) / 7; | |
Assert.Equal("01:08", sut.ToString()); | |
sut = new Clock(8, 0) / 8; | |
Assert.Equal("01:00", sut.ToString()); | |
} | |
[Fact] | |
public void Equality() | |
{ | |
Assert.True(new Clock(3, 0) == new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) == new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) == new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) == new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) == new Clock(75, 0)); | |
Assert.True(new Clock(3, 0) == new Clock(51, 0)); | |
Assert.True(new Clock(3, 0) == new Clock(27, 0)); | |
Assert.True(new Clock(3, 0) == new Clock(26, 60)); | |
Assert.True(new Clock(3, 0) == new Clock(25, 120)); | |
Assert.True(new Clock(3, 0) == new Clock(24, 180)); | |
Assert.True(new Clock(3, 0) == -new Clock(21, 0)); | |
Assert.True(new Clock(3, 0) == -new Clock(20, 60)); | |
Assert.True(new Clock(3, 0) == -new Clock(19, 120)); | |
Assert.True(new Clock(3, 0) == -new Clock(18, 180)); | |
Assert.True(new Clock(3, 0) == -new Clock(0, 1_260)); | |
Assert.True(new Clock(3, 0) == new Clock(-21, 0)); | |
Assert.True(new Clock(3, 0) == new Clock(-20, -60)); | |
Assert.True(new Clock(3, 0) == new Clock(-19, -120)); | |
Assert.True(new Clock(3, 0) == new Clock(-18, -180)); | |
Assert.True(new Clock(3, 0) == new Clock(0, -1_260)); | |
} | |
[Fact] | |
public void Inequality() | |
{ | |
Assert.True(new Clock(3, 0) != -new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) != -new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) != -new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) != -new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) != new Clock(-3, 0)); | |
Assert.True(new Clock(3, 0) != new Clock(-2, -60)); | |
Assert.True(new Clock(3, 0) != new Clock(-1, -120)); | |
Assert.True(new Clock(3, 0) != new Clock(0, -180)); | |
} | |
[Fact] | |
public void GreaterThan() | |
{ | |
Assert.True(new Clock(3, 0) > -new Clock(22, 0)); | |
Assert.True(new Clock(3, 0) > -new Clock(22, 59)); | |
Assert.True(new Clock(3, 0) > -new Clock(22, 01)); | |
Assert.True(new Clock(3, 0) > -new Clock(23, 0)); | |
Assert.True(new Clock(3, 0) > -new Clock(23, 59)); | |
Assert.True(new Clock(3, 0) > -new Clock(23, 01)); | |
Assert.True(new Clock(3, 0) > -new Clock(24, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(-22, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(-22, -59)); | |
Assert.True(new Clock(3, 0) > new Clock(-22, -01)); | |
Assert.True(new Clock(3, 0) > new Clock(-23, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(-23, -59)); | |
Assert.True(new Clock(3, 0) > new Clock(-23, -01)); | |
Assert.True(new Clock(3, 0) > new Clock(-24, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_261)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_380)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_320)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_410)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_437)); | |
Assert.True(new Clock(3, 0) > new Clock(0, -1_429)); | |
Assert.True(new Clock(3, 0) > new Clock(24, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(48, 0)); | |
Assert.True(new Clock(3, 0) > new Clock(72, 0)); | |
} | |
[Fact] | |
public void LesserThan() | |
{ | |
Assert.True(new Clock(3, 0) < -new Clock(24, 59)); | |
Assert.True(new Clock(3, 0) < -new Clock(24, 01)); | |
Assert.True(new Clock(3, 0) < -new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) < -new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) < -new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) < -new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) < new Clock(-24, -59)); | |
Assert.True(new Clock(3, 0) < new Clock(-24, -01)); | |
Assert.True(new Clock(3, 0) < new Clock(-3, 0)); | |
Assert.True(new Clock(3, 0) < new Clock(-2, -60)); | |
Assert.True(new Clock(3, 0) < new Clock(-1, -120)); | |
Assert.True(new Clock(3, 0) < new Clock(0, -180)); | |
} | |
[Fact] | |
public void GreaterThanOrEquals() | |
{ | |
Assert.True(new Clock(3, 0) >= -new Clock(22, 0)); | |
Assert.True(new Clock(3, 0) >= -new Clock(22, 59)); | |
Assert.True(new Clock(3, 0) >= -new Clock(22, 01)); | |
Assert.True(new Clock(3, 0) >= -new Clock(23, 0)); | |
Assert.True(new Clock(3, 0) >= -new Clock(23, 59)); | |
Assert.True(new Clock(3, 0) >= -new Clock(23, 01)); | |
Assert.True(new Clock(3, 0) >= -new Clock(24, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(-22, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(-22, -59)); | |
Assert.True(new Clock(3, 0) >= new Clock(-22, -01)); | |
Assert.True(new Clock(3, 0) >= new Clock(-23, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(-23, -59)); | |
Assert.True(new Clock(3, 0) >= new Clock(-23, -01)); | |
Assert.True(new Clock(3, 0) >= new Clock(-24, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_261)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_380)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_320)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_410)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_437)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_429)); | |
Assert.True(new Clock(3, 0) >= new Clock(24, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(48, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(72, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) >= new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) >= new Clock(75, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(51, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(27, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(26, 60)); | |
Assert.True(new Clock(3, 0) >= new Clock(25, 120)); | |
Assert.True(new Clock(3, 0) >= new Clock(24, 180)); | |
Assert.True(new Clock(3, 0) >= -new Clock(21, 0)); | |
Assert.True(new Clock(3, 0) >= -new Clock(20, 60)); | |
Assert.True(new Clock(3, 0) >= -new Clock(19, 120)); | |
Assert.True(new Clock(3, 0) >= -new Clock(18, 180)); | |
Assert.True(new Clock(3, 0) >= -new Clock(0, 1_260)); | |
Assert.True(new Clock(3, 0) >= new Clock(-21, 0)); | |
Assert.True(new Clock(3, 0) >= new Clock(-20, -60)); | |
Assert.True(new Clock(3, 0) >= new Clock(-19, -120)); | |
Assert.True(new Clock(3, 0) >= new Clock(-18, -180)); | |
Assert.True(new Clock(3, 0) >= new Clock(0, -1_260)); | |
} | |
[Fact] | |
public void LesserThanOrEquals() | |
{ | |
Assert.True(new Clock(3, 0) <= -new Clock(24, 59)); | |
Assert.True(new Clock(3, 0) <= -new Clock(24, 01)); | |
Assert.True(new Clock(3, 0) <= -new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) <= -new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) <= -new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) <= -new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) <= new Clock(-24, -59)); | |
Assert.True(new Clock(3, 0) <= new Clock(-24, -01)); | |
Assert.True(new Clock(3, 0) <= new Clock(-3, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(-2, -60)); | |
Assert.True(new Clock(3, 0) <= new Clock(-1, -120)); | |
Assert.True(new Clock(3, 0) <= new Clock(0, -180)); | |
Assert.True(new Clock(3, 0) <= new Clock(3, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(2, 60)); | |
Assert.True(new Clock(3, 0) <= new Clock(1, 120)); | |
Assert.True(new Clock(3, 0) <= new Clock(0, 180)); | |
Assert.True(new Clock(3, 0) <= new Clock(75, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(51, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(27, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(26, 60)); | |
Assert.True(new Clock(3, 0) <= new Clock(25, 120)); | |
Assert.True(new Clock(3, 0) <= new Clock(24, 180)); | |
Assert.True(new Clock(3, 0) <= -new Clock(21, 0)); | |
Assert.True(new Clock(3, 0) <= -new Clock(20, 60)); | |
Assert.True(new Clock(3, 0) <= -new Clock(19, 120)); | |
Assert.True(new Clock(3, 0) <= -new Clock(18, 180)); | |
Assert.True(new Clock(3, 0) <= -new Clock(0, 1_260)); | |
Assert.True(new Clock(3, 0) <= new Clock(-21, 0)); | |
Assert.True(new Clock(3, 0) <= new Clock(-20, -60)); | |
Assert.True(new Clock(3, 0) <= new Clock(-19, -120)); | |
Assert.True(new Clock(3, 0) <= new Clock(-18, -180)); | |
Assert.True(new Clock(3, 0) <= new Clock(0, -1_260)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment