Permalink
Browse files

Miscellaneous extra tests

(CachedDateTimeZone.CacheType has been removed as unncessary.)
  • Loading branch information...
jskeet committed Aug 25, 2017
1 parent 0913621 commit 16aacad5b4c11e244726d9e0f6c7fae9c4eeb942
@@ -10,21 +10,8 @@ namespace NodaTime.Test.TimeZones
// Note that this tests CachingZoneIntervalMap as much as CachedDateTimeZone...
public class CachedDateTimeZoneTest
{
#region Setup/Teardown
[SetUp]
public void Setup()
{
timeZone = DateTimeZoneProviders.Tzdb["America/Los_Angeles"] as CachedDateTimeZone;
if (timeZone == null)
{
Assert.Fail("The America/Los_Angeles time zone does not contain a CachedDateTimeZone.");
}
summer = Instant.FromUtc(2010, 6, 1, 0, 0);
}
#endregion
private CachedDateTimeZone timeZone;
private Instant summer;
private static readonly CachedDateTimeZone timeZone = (CachedDateTimeZone) DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
private static readonly Instant summer = Instant.FromUtc(2010, 6, 1, 0, 0);
[Test]
public void GetZoneIntervalInstant_NotNull()
@@ -62,5 +49,18 @@ public void MinMaxOffsets()
Assert.AreEqual(timeZone.Uncached().MinOffset, timeZone.MinOffset);
Assert.AreEqual(timeZone.Uncached().MaxOffset, timeZone.MaxOffset);
}
[Test]
public void ForZone_Fixed()
{
var zone = DateTimeZone.ForOffset(Offset.FromHours(1));
Assert.AreSame(zone, CachedDateTimeZone.ForZone(zone));
}
[Test]
public void ForZone_AlreadyCached()
{
Assert.AreSame(timeZone, CachedDateTimeZone.ForZone(timeZone));
}
}
}
@@ -0,0 +1,30 @@
// Copyright 2017 The Noda Time Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.
using NodaTime.TimeZones.IO;
using NodaTime.Utility;
using NUnit.Framework;
using System.IO;
namespace NodaTime.Test.TimeZones.IO
{
public class TzdbStreamFieldTest
{
// Only tests for situations which aren't covered elsewhere
[Test]
public void InsufficientData()
{
var stream = new MemoryStream();
var writer = new DateTimeZoneWriter(stream, null);
writer.WriteByte(1);
writer.WriteCount(10);
stream.Position = 0;
var iterator = TzdbStreamField.ReadFields(stream).GetEnumerator();
Assert.Throws<InvalidNodaDataException>(() => iterator.MoveNext());
}
}
}
@@ -8,6 +8,7 @@
using NUnit.Framework;
using System.IO;
using NodaTime.TimeZones.IO;
using System;
namespace NodaTime.Test.TimeZones
{
@@ -278,6 +279,21 @@ public void Extremes()
Assert.AreEqual(lastWinter, zone.GetZoneInterval(Instant.MaxValue));
}
[Test]
public void InvalidMap_SimultaneousTransition()
{
// Two recurrences with different savings, but which occur at the same instant in time every year.
ZoneRecurrence r1 = new ZoneRecurrence("Recurrence1", Offset.Zero,
new ZoneYearOffset(TransitionMode.Utc, 10, 5, 0, false, new LocalTime(2, 0)), int.MinValue, int.MaxValue);
ZoneRecurrence r2 = new ZoneRecurrence("Recurrence2", Offset.FromHours(1),
new ZoneYearOffset(TransitionMode.Utc, 10, 5, 0, false, new LocalTime(2, 0)), int.MinValue, int.MaxValue);
var map = new StandardDaylightAlternatingMap(Offset.Zero, r1, r2);
Assert.Throws<InvalidOperationException>(() => map.GetZoneInterval(Instant.FromUtc(2017, 8, 25, 0, 0, 0)));
}
private void CheckMapping(ZoneLocalMapping mapping, string earlyIntervalName, string lateIntervalName, int count)
{
Assert.AreEqual(earlyIntervalName, mapping.EarlyInterval.Name);
@@ -21,5 +21,12 @@ public void Equality()
TestHelper.TestOperatorEquality(equal1, equal2, unequal1);
TestHelper.TestOperatorEquality(equal1, equal2, unequal2);
}
[Test]
public void TransitionToString()
{
var transition = new Transition(Instant.FromUtc(2017, 8, 25, 15, 26, 30), Offset.FromHours(1));
Assert.AreEqual("Transition to +01 at 2017-08-25T15:26:30Z", transition.ToString());
}
}
}
@@ -7,6 +7,7 @@
using NodaTime.Testing.TimeZones;
using NodaTime.TimeZones;
using NUnit.Framework;
using System;
namespace NodaTime.Test.TimeZones
{
@@ -141,6 +142,30 @@ public void ElidedTransitions_Degenerate()
AssertNotEqual(zone1, zone2, Instant.MinValue, Instant.MaxValue, ZoneEqualityComparer.Options.MatchAllTransitions);
}
[Test]
public void ReferenceComparison()
{
var comparer = ZoneEqualityComparer.ForInterval(new Interval(Instants[0], Instants[2]));
var zone = DateTimeZoneProviders.Tzdb["Europe/London"];
Assert.IsTrue(comparer.Equals(zone, zone));
}
[Test]
public void NullComparison()
{
var comparer = ZoneEqualityComparer.ForInterval(new Interval(Instants[0], Instants[2]));
var zone = DateTimeZoneProviders.Tzdb["Europe/London"];
Assert.IsFalse(comparer.Equals(zone, null));
Assert.IsFalse(comparer.Equals(null, zone));
}
[Test]
public void InvalidOptions()
{
var comparer = ZoneEqualityComparer.ForInterval(new Interval(Instants[0], Instants[2]));
Assert.Throws<ArgumentOutOfRangeException>(() => comparer.WithOptions((ZoneEqualityComparer.Options) 9999));
}
private void AssertEqual(DateTimeZone first, DateTimeZone second,
Instant start, Instant end, ZoneEqualityComparer.Options options)
{
@@ -9,6 +9,21 @@ namespace NodaTime.Test.Utility
{
public class PreconditionsTest
{
// This overload is only called from one place, and only when it's going to fail...
[Test]
public void CheckArgument2_Success()
{
Preconditions.CheckArgument(true, "param", "{0} is {1}", 1, 10);
}
[Test]
public void CheckArgument2_Failure()
{
var exception = Assert.Throws<ArgumentException>(() => Preconditions.CheckArgument(false, "param", "{0} is {1}", 1, 10));
Assert.AreEqual("param", exception.ParamName);
Assert.IsTrue(exception.Message.Contains("1 is 10"));
}
// It's really not worth inventing parameters for these...
#pragma warning disable InvokerParameterName
#pragma warning disable NotNullCheckWithoutParameter
@@ -62,6 +62,14 @@ public void Comparisons(string smallerText, string greaterText)
var greater = YearMonthDay.Parse(greaterText);
TestHelper.TestCompareToStruct(smaller, smaller, greater);
TestHelper.TestOperatorComparisonEquality(smaller, smaller, greater);
TestHelper.TestEqualsStruct(smaller, smaller, greater);
}
[Test]
public void YearMonthDayToString()
{
var ymd = new YearMonthDay(2017, 8, 25);
Assert.AreEqual("2017-08-25", ymd.ToString());
}
}
}
@@ -540,11 +540,15 @@ public void LocalComparer()
.WithCalendar(CalendarSystem.PersianSimple)
.InZoneStrictly(london);
TestHelper.TestComparerStruct(ZonedDateTime.Comparer.Local, losAngelesAfternoon, londonAfternoon, londonEvening);
Assert.Throws<ArgumentException>(() => ZonedDateTime.Comparer.Local.Compare(londonPersian, londonEvening));
Assert.IsFalse(ZonedDateTime.Comparer.Local.Equals(londonPersian, londonEvening));
Assert.IsFalse(ZonedDateTime.Comparer.Local.Equals(londonAfternoon, londonEvening));
Assert.IsTrue(ZonedDateTime.Comparer.Local.Equals(londonAfternoon, losAngelesAfternoon));
var comparer = ZonedDateTime.Comparer.Local;
TestHelper.TestComparerStruct(comparer, losAngelesAfternoon, londonAfternoon, londonEvening);
Assert.Throws<ArgumentException>(() => comparer.Compare(londonPersian, londonEvening));
Assert.IsFalse(comparer.Equals(londonPersian, londonEvening));
Assert.AreNotEqual(comparer.GetHashCode(londonPersian), comparer.GetHashCode(londonEvening));
Assert.IsFalse(comparer.Equals(londonAfternoon, londonEvening));
Assert.AreNotEqual(comparer.GetHashCode(londonAfternoon), comparer.GetHashCode(londonEvening));
Assert.IsTrue(comparer.Equals(londonAfternoon, losAngelesAfternoon));
Assert.AreEqual(comparer.GetHashCode(londonAfternoon), comparer.GetHashCode(losAngelesAfternoon));
}
[Test]
@@ -564,11 +568,15 @@ public void InstantComparer()
.WithCalendar(CalendarSystem.PersianSimple)
.InZoneStrictly(london);
TestHelper.TestComparerStruct(ZonedDateTime.Comparer.Instant, londonEvening, losAngelesLunchtime, losAngelesAfternoon);
Assert.AreEqual(0, ZonedDateTime.Comparer.Instant.Compare(londonPersian, londonEvening));
Assert.IsTrue(ZonedDateTime.Comparer.Instant.Equals(londonPersian, londonEvening));
Assert.IsTrue(ZonedDateTime.Comparer.Instant.Equals(losAngelesLunchtime, londonEvening));
Assert.IsFalse(ZonedDateTime.Comparer.Instant.Equals(losAngelesAfternoon, londonEvening));
var comparer = ZonedDateTime.Comparer.Instant;
TestHelper.TestComparerStruct(comparer, londonEvening, losAngelesLunchtime, losAngelesAfternoon);
Assert.AreEqual(0, comparer.Compare(londonPersian, londonEvening));
Assert.IsTrue(comparer.Equals(londonPersian, londonEvening));
Assert.AreEqual(comparer.GetHashCode(londonPersian), comparer.GetHashCode(londonEvening));
Assert.IsTrue(comparer.Equals(losAngelesLunchtime, londonEvening));
Assert.AreEqual(comparer.GetHashCode(losAngelesLunchtime), comparer.GetHashCode(londonEvening));
Assert.IsFalse(comparer.Equals(losAngelesAfternoon, londonEvening));
Assert.AreNotEqual(comparer.GetHashCode(losAngelesAfternoon), comparer.GetHashCode(londonEvening));
}
}
}
@@ -10,6 +10,7 @@
using NodaTime.Annotations;
using NodaTime.Calendars;
using NodaTime.Utility;
using System.Diagnostics.CodeAnalysis;
namespace NodaTime
{
@@ -602,6 +603,9 @@ internal Era GetEra([Trusted] int absoluteYear)
/// </summary>
/// <param name="yearMonthDay">The value to validate.</param>
[Conditional("DEBUG")]
#if NET45
[ExcludeFromCodeCoverage]
#endif
internal void DebugValidateYearMonthDay(YearMonthDay yearMonthDay)
{
// Avoid the line even being compiled in a release build...
@@ -89,7 +89,7 @@ public override ZoneInterval GetZoneInterval(Instant instant)
Offset maxRuleOffset = convertedRules.Aggregate(Offset.MinValue, (min, rule) => Offset.Max(min, rule.Savings + rule.StandardOffset));
IZoneIntervalMap uncachedMap = BuildMap(convertedRules, standardOffset, bclZone.StandardName);
IZoneIntervalMap cachedMap = CachingZoneIntervalMap.CacheMap(uncachedMap, CachingZoneIntervalMap.CacheType.Hashtable);
IZoneIntervalMap cachedMap = CachingZoneIntervalMap.CacheMap(uncachedMap);
return new BclDateTimeZone(bclZone, Offset.Min(standardOffset, minRuleOffset), Offset.Max(standardOffset, maxRuleOffset), cachedMap);
}
@@ -61,7 +61,7 @@ internal static DateTimeZone ForZone([NotNull] DateTimeZone timeZone)
{
return timeZone;
}
return new CachedDateTimeZone(timeZone, CachingZoneIntervalMap.CacheMap(timeZone, CachingZoneIntervalMap.CacheType.Hashtable));
return new CachedDateTimeZone(timeZone, CachingZoneIntervalMap.CacheMap(timeZone));
}
/// <summary>
@@ -13,26 +13,15 @@ namespace NodaTime.TimeZones
/// </summary>
internal static class CachingZoneIntervalMap
{
/// <summary>
/// The type of cache to build.
/// </summary>
internal enum CacheType
{
Hashtable
}
// Currently the only implementation is HashArrayCache. This container class is mostly for historical
// reasons; it's not really necessary but it does no harm.
/// <summary>
/// Returns a caching map for the given input map.
/// </summary>
internal static IZoneIntervalMap CacheMap([NotNull] IZoneIntervalMap map, CacheType type)
internal static IZoneIntervalMap CacheMap([NotNull] IZoneIntervalMap map)
{
switch (type)
{
case CacheType.Hashtable:
return new HashArrayCache(map);
default:
throw new ArgumentException("The type parameter is invalid", "type");
}
return new HashArrayCache(map);
}
#region Nested type: HashArrayCache

0 comments on commit 16aacad

Please sign in to comment.