// OpenTween - Client of Twitter // Copyright (c) 2018 kim_upsilon (@kim_upsilon) // All rights reserved. // // This file is part of OpenTween. // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 3 of the License, or (at your option) // any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License along // with this program. If not, see , or write to // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, // Boston, MA 02110-1301, USA. using System; using Xunit; namespace OpenTween { public class DateTimeUtcTest { [Fact] public void Constructor_DateTest() { var utc = new DateTimeUtc(2018, 5, 6); Assert.Equal(new DateTime(2018, 5, 6, 0, 0, 0, 0, DateTimeKind.Utc), utc.ToDateTimeUnsafe()); } [Fact] public void Constructor_DateAndTimeTest() { var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33); Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 0, DateTimeKind.Utc), utc.ToDateTimeUnsafe()); } [Fact] public void Constructor_DateAndTimeMillisecondsTest() { var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 456); Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 456, DateTimeKind.Utc), utc.ToDateTimeUnsafe()); } [Fact] public void Constructor_DateTimeOffsetTest() { var datetimeOffset = new DateTimeOffset(2018, 5, 6, 11, 22, 33, 456, TimeSpan.FromHours(9)); var utc = new DateTimeUtc(datetimeOffset); Assert.Equal(new DateTime(2018, 5, 6, 2, 22, 33, 456, DateTimeKind.Utc), utc.ToDateTimeUnsafe()); } [Fact] public void Constructor_DateTimeTest() { var datetime = new DateTime(2018, 5, 6, 11, 22, 33, DateTimeKind.Utc); var utc = new DateTimeUtc(datetime); Assert.Equal(datetime, utc.ToDateTimeUnsafe()); } [Fact] public void Constructor_LocalDateTimeTest() { Assert.Throws( () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Local))); Assert.Throws( () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Unspecified))); } [Fact] public void ToUnixTime_Test() { var utc = new DateTimeUtc(2009, 2, 13, 23, 31, 30, 0); Assert.Equal(1234567890, utc.ToUnixTime()); } [Fact] public void ToDateTimeOffset() { var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); Assert.Equal(new DateTimeOffset(2018, 5, 6, 11, 22, 33, 111, TimeSpan.Zero), utc.ToDateTimeOffset()); } [Fact] public void CompareTo_Test() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222); Assert.Equal(0, utc1.CompareTo(utc2)); Assert.True(utc1.CompareTo(utc3) < 0); Assert.True(utc3.CompareTo(utc1) > 0); } [Fact] public void Equals_Test() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222); Assert.True(utc1.Equals(utc2)); Assert.True(utc1.Equals((object)utc2)); Assert.False(utc1.Equals(utc3)); Assert.False(utc1.Equals((object)utc3)); } [Fact] public void GetHashCode_Test() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222); Assert.Equal(utc1.GetHashCode(), utc2.GetHashCode()); Assert.NotEqual(utc1.GetHashCode(), utc3.GetHashCode()); } [Fact] public void OperatorPlus_Test() { var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var diff = TimeSpan.FromDays(1); Assert.Equal(new DateTime(2018, 5, 7, 11, 22, 33, 111, DateTimeKind.Utc), (utc + diff).ToDateTimeUnsafe()); } [Fact] public void OperatorMinus_TimeSpanTest() { var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var diff = TimeSpan.FromDays(1); Assert.Equal(new DateTime(2018, 5, 5, 11, 22, 33, 111, DateTimeKind.Utc), (utc - diff).ToDateTimeUnsafe()); } [Fact] public void OperatorMinus_DateTimeTest() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 7, 11, 22, 33, 111); Assert.Equal(TimeSpan.Zero, utc1 - utc1); Assert.Equal(TimeSpan.FromDays(-1), utc1 - utc2); Assert.Equal(TimeSpan.FromDays(1), utc2 - utc1); } [Fact] public void OperatorEqual_Test() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222); #pragma warning disable CS1718 Assert.True(utc1 == utc1); Assert.False(utc1 == utc2); Assert.False(utc1 != utc1); Assert.True(utc1 != utc2); #pragma warning restore CS1718 } [Fact] public void OperatorCompare_Test() { var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111); var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222); #pragma warning disable CS1718 Assert.False(utc1 < utc1); Assert.True(utc1 < utc2); Assert.False(utc2 < utc1); Assert.True(utc1 <= utc1); Assert.True(utc1 <= utc2); Assert.False(utc2 <= utc1); Assert.False(utc1 > utc1); Assert.False(utc1 > utc2); Assert.True(utc2 > utc1); Assert.True(utc1 >= utc1); Assert.False(utc1 >= utc2); Assert.True(utc2 >= utc1); #pragma warning restore CS1718 } [Fact] public void MinValue_Test() => Assert.Equal(DateTime.MinValue.Ticks, DateTimeUtc.MinValue.ToDateTimeUnsafe().Ticks); [Fact] public void MaxValue_Test() => Assert.Equal(DateTime.MaxValue.Ticks, DateTimeUtc.MaxValue.ToDateTimeUnsafe().Ticks); [Fact] public void FromUnixTime_Test() { var utc = DateTimeUtc.FromUnixTime(1234567890); Assert.Equal(new DateTime(2009, 2, 13, 23, 31, 30, 0, DateTimeKind.Utc), utc.ToDateTimeUnsafe()); } } }