Пример #1
0
        public void TestswithJsonCompareVersions()
        {
            var actual   = Request.RestRequest("https://api.exchangeratesapi.io/", "2010-01-14", "GET");
            var expected = Request.RestRequest("https://api.exchangeratesapi.io/", "2010-01-15", "GET");

            Assert.True(CompareUtilities.CompareObjects(actual, expected.Content, "CompareVersions"));
        }
Пример #2
0
        public async Task AddAsync(TValue value)
        {
            await semaphoreSlim.WaitAsync().ConfigureAwait(false);

            try
            {
                var(anomaly, trigger) = await AddValue(value).ConfigureAwait(false);

                if (anomaly != Anomaly.None)
                {
                    OnAnomalyEvent(value, anomaly);
                }

                if (trigger != Trigger.None)
                {
                    var referenceDate     = DateTimeOffset.Now;
                    var calculationResult = Calculator.Calculate(Values.Values, Anomalies);

                    if (!CompareUtilities.Equals(calculationResult.Normal, Range.Normal))
                    {
                        OnValueChangedEvent(calculationResult.Normal, trigger);
                    }

                    Range = calculationResult;
                    SetCalculationDate(referenceDate);
                    HistorySettings.ReduceValues(Values, Anomalies, referenceDate);
                }
            }
            finally
            {
                semaphoreSlim.Release();
            }

            return;
        }
Пример #3
0
        public void TryInvert_Should_ReturnTrue_When_Double()
        {
            const double value = 3.2;

            Assert.True(CalculateUtilities.TryInvert(value, out var _result));
            Assert.True(CompareUtilities.EqualsWithinTolerance(value * -1, _result, 6));
        }
 private void ApplyUpToOrder(T value, TOrder order)
 {
     Items
     .Where(x =>
            (x is IOrderedEvent <TKey, TOrder> orderedEvent) &&
            CompareUtilities.SmallerOrEqualTo(orderedEvent.Order, order))
     .Apply(value);
 }
Пример #5
0
        public void TestwithLotsofFailures()
        {
            var           response      = Request.RestRequest("https://api.exchangeratesapi.io/", "2010-01-14", "GET");
            List <string> ignoredFields = new List <string> {
                "date"
            };

            Assert.True(CompareUtilities.CompareObjects(response, CompareUtilities.ReadCachedJson("NewObjCurrencyByDate2010-01-15"), "Date2010-01-15-failures", ignoredFields));
        }
        /// <summary>
        /// Adds <see cref="item"/> to list.
        /// Applies <see cref="item"/> if <see cref="IMutationEvent{T}"/>
        /// or  <see cref="item"/> if <see cref="IOrderedEvent{TKey, TOrder}.Order"/> <see cref="CompareUtilities.GreaterOrEqualTo"/> <see cref="MaxOrder"/>.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="item"></param>
        /// <returns>Wether <see cref="item"/> is applied.</returns>
        public override bool AddAndApply(IEvent <T, TKey> item, T value)
        {
            Add(item);

            if (!(item is IOrderedEvent <TKey, TOrder>) ||
                item is IMutationEvent <T, TKey> ||
                (item is IOrderedEvent <TKey, TOrder> orderedEvent && CompareUtilities.GreaterOrEqualTo(orderedEvent.Order, MaxOrder)))
            {
                item.Apply(value);
                return(true);
            }

            return(false);
        }
        public void ReduceValues_Should_ReduceToValue_When_PreserveCount(long preserve)
        {
            var settings        = new HistorySettings(preserve);
            var values          = GetValues();
            var anomalies       = GetAnomalies();
            var count           = values.Count;
            var reductionResult = settings.ReduceValues(values, anomalies, new DateTimeOffset(2018, 1, 2, 0, 0, 1, TimeSpan.Zero));

            var expected = CompareUtilities.Min(count, CompareUtilities.Max(0, count - preserve + anomalies.Count));

            reductionResult.Should().Be(expected);
            values.Count.Should().Be(count - (int)expected);
            anomalies.Count.Should().Be(0);
        }
        public void GetStandardDeviationTest()
        {
            var values = new List <double> {
                1000, 1100, 1130, 1140, 1145, 1165, 1200
            };
            const double ExpectedStandardDeviation = 58.7019451751553;
            var          expectedAverage           = values.Average();

            var(average, standardDeviation) = StandardDeviationUtilities.GetStandardDeviationAverage(values);
            var actualAverage           = average;
            var actualStandardDeviation = standardDeviation;

            Assert.True(CompareUtilities.EqualsWithinTolerance(ExpectedStandardDeviation, actualStandardDeviation, 6));
            Assert.True(CompareUtilities.EqualsWithinTolerance(expectedAverage, actualAverage, 6));
        }
        internal static Anomaly Validate <TValue>(this IDetectionRange <TValue> range, TValue value)
            where TValue : IComparable <TValue>
        {
            if (CompareUtilities.GreaterThan(value, range.MaxDetectionHigh))
            {
                return(Anomaly.High);
            }

            if (CompareUtilities.SmallerThan(value, range.MaxDetectionLow))
            {
                return(Anomaly.Low);
            }

            return(Anomaly.None);
        }
        public void ReduceValues_Should_ReduceToValue_When_PreserveTimeSpan(int preserve)
        {
            var settings        = new HistorySettings(TimeSpan.FromSeconds(1));
            var values          = GetValues();
            var anomalies       = GetAnomalies();
            var count           = values.Count;
            var referenceDate   = new DateTimeOffset(2018, 1, 5, 0, 0, 1, TimeSpan.Zero).AddDays(-preserve);
            var reductionResult = settings.ReduceValues(values, anomalies, referenceDate);

            var expected = CompareUtilities.Min(count, CompareUtilities.Max(0, (2 * (4 - preserve)) + anomalies.Count));

            reductionResult.Should().Be(expected);
            values.Count.Should().Be(count - expected);
            anomalies.Count.Should().Be(0);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DetectionRange"/> class.
        /// </summary>
        /// <param name="maxDetectionLow"></param>
        /// <param name="low"></param>
        /// <param name="normal"></param>
        /// <param name="high"></param>
        /// <param name="maxDetectionHigh"></param>
        /// <exception cref="ArgumentException">If <see cref="MaxDetectionLow"/> greater than <see cref="low"/>.</exception>
        /// <exception cref="ArgumentException">If <see cref="MaxDetectionHigh"/> smaller than <see cref="high"/>.</exception>
        public DetectionRange(TValue maxDetectionLow, TValue low, TValue normal, TValue high, TValue maxDetectionHigh)
            : base(low, normal, high)
        {
            if (!CompareUtilities.SmallerOrEqualTo(maxDetectionLow, low))
            {
                throw new ArgumentException($"{nameof(maxDetectionLow)} must be smaller or equal to {low}.", nameof(maxDetectionLow));
            }

            if (!CompareUtilities.GreaterOrEqualTo(maxDetectionHigh, high))
            {
                throw new ArgumentException($"{nameof(maxDetectionHigh)} must be smaller or equal to {high}.", nameof(maxDetectionHigh));
            }

            MaxDetectionLow  = maxDetectionLow;
            MaxDetectionHigh = maxDetectionHigh;
        }
Пример #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DetectionRange"/> class.
        /// </summary>
        /// <param name="low"></param>
        /// <param name="normal"></param>
        /// <param name="high"></param>
        /// <exception cref="ArgumentException">If <see cref=low"/> greater than <see cref="normal"/>.</exception>
        /// <exception cref="ArgumentException">If <see cref="high"/> smaller than <see cref="normal"/>.</exception>
        public ValueRange(TValue low, TValue normal, TValue high)
        {
            if (!CompareUtilities.SmallerOrEqualTo(low, normal))
            {
                throw new ArgumentException($"{nameof(low)} must be smaller or equal to {normal}.", nameof(low));
            }

            if (!CompareUtilities.GreaterOrEqualTo(high, normal))
            {
                throw new ArgumentException($"{nameof(high)} must be smaller or equal to {normal}.", nameof(high));
            }

            Low    = low;
            Normal = normal;
            High   = high;
        }
        protected string BuildResult(bool key)
        {
            var result = new StringBuilder();
            var prefix = "?";

            if (SubBuilder)
            {
                result.Append("$expand=");
                prefix = "&";
            }

            result.Append(base.BuildResult());

            var options = BuildOptionsResult(prefix, key);

            if (!string.IsNullOrWhiteSpace(options))
            {
                if (!CompareUtilities.IsDefault(Key))
                {
                    throw new ArgumentException($"{Filter} may only be used on collections. {nameof(Key)} is already applied.");
                }

                if (SubBuilder)
                {
                    result.Append("(");
                }

                result.Append(options);

                if (SubBuilder)
                {
                    result.Append(")");
                }

                prefix = "&";
            }

            if (SubEntity != null)
            {
                result.Append(prefix);
                result.Append(SubEntity.Build());
            }

            return(result.ToString());
        }
        /// <summary>
        /// Reduce number of entries in <see cref="settings"/> based on <see cref="IHistorySettings"/>.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="settings"></param>
        /// <param name="values"></param>
        /// <param name="anomalies"></param>
        /// <param name="referenceDate"></param>
        /// <returns></returns>
        internal static long ReduceValues <TValue>(
            this IHistorySettings settings,
            ConcurrentDictionary <DateTimeOffset, TValue> values,
            ConcurrentBag <TValue> anomalies,
            DateTimeOffset referenceDate)
        {
            long result      = 0;
            var  oldestValue = values.Keys.OrderBy(x => x).FirstOrDefault();

            while (!anomalies.IsEmpty)
            {
                if (!anomalies.TryTake(out var _))
                {
                    return(result++);
                }
            }

            if (settings.PreserveTimeSpan > TimeSpan.Zero)
            {
                var preserveDate = referenceDate - settings.PreserveTimeSpan;

                while (!CompareUtilities.IsDefault(oldestValue) && oldestValue < preserveDate)
                {
                    values.TryRemove(oldestValue, out _);
                    oldestValue = values.Keys.OrderBy(x => x).FirstOrDefault();
                    result++;
                }

                return(result);
            }

            while (!CompareUtilities.IsDefault(oldestValue) && values.Keys.Count > settings.PreserveCount)
            {
                if (!values.TryRemove(oldestValue, out _))
                {
                    return(result);
                }

                oldestValue = values.Keys.OrderBy(x => x).FirstOrDefault();
                result++;
            }

            return(result);
        }
        protected override string BuildResult()
        {
            var result = new StringBuilder();

            result.Append(Entity.ToLowerInvariant());

            if (!CompareUtilities.IsDefault(Key))
            {
                if (SubBuilder)
                {
                    throw new ArgumentException($"Expand can not be used with {nameof(Key)}.");
                }

                result
                .Append("(")
                .Append(StringKey)
                .Append(")");
            }

            return(result.ToString());
        }
Пример #16
0
 public void Equals_When_Int(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.Equals(value, compare));
 }
Пример #17
0
 public void Min_Should_ReturnLowestValue(int high, int low)
 {
     Assert.Equal(low, CompareUtilities.Min(low, high));
 }
Пример #18
0
 public void GreaterOrEqualTo_When_Double(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.GreaterOrEqualTo(RangeFactory.ToTestDouble(value), RangeFactory.ToTestDouble(compare)));
 }
Пример #19
0
 public void SmallerThan_When_Int(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.SmallerThan(value, compare));
 }
Пример #20
0
 public void GreaterOrEqualTo_When_Int(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.GreaterOrEqualTo(value, compare));
 }
Пример #21
0
 public void EqualsWithinTolerance(double value, double compare, int tolerance, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.EqualsWithinTolerance(value, compare, tolerance));
 }
 /// <summary>
 /// Is <see cref="value"/> greater than <see cref="IRange{TValue}.From"/> and smaller than <see cref="IRange{TValue}.Till"/>
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="range"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public static bool InBetween <TValue>(this IRange <TValue> range, TValue value)
     where TValue : IComparable <TValue>
 {
     return(CompareUtilities.SmallerThan(range.From, value) && CompareUtilities.GreaterThan(range.Till, value));
 }
 /// <summary>
 /// Do the ranges overlap.
 /// Is <see cref="range.From"/> smaller than <see cref="compare.Till"/>
 /// And <see cref="range.Till"/> greater than <see cref="compare.From"/>
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="range"></param>
 /// <param name="compare"></param>
 /// <returns></returns>
 public static bool OverLaps <TValue>(this IRange <TValue> range, IRange <TValue> compare)
     where TValue : IComparable <TValue>
 {
     return(CompareUtilities.SmallerThan(range.From, compare.Till) && CompareUtilities.GreaterThan(range.Till, compare.From));
 }
 /// <summary>
 /// Does the <see cref="range"/> equal or exceed the limits of <see cref="compare"/>
 /// Is <see cref="range.From"/> smaller or equal to <see cref="compare.From"/>
 /// Is <see cref="range.Till"/> greater or equal to <see cref="compare.Till"/>
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="range"></param>
 /// <param name="compare"></param>
 /// <returns></returns>
 public static bool Ecapsulates <TValue>(this IRange <TValue> range, IRange <TValue> compare)
     where TValue : IComparable <TValue>
 {
     return(CompareUtilities.SmallerOrEqualTo(range.From, compare.From) && CompareUtilities.GreaterOrEqualTo(range.Till, compare.Till));
 }
Пример #25
0
 public void Max_Should_ReturnHighestValue(int low, int high)
 {
     Assert.Equal(high, CompareUtilities.Max(low, high));
 }
Пример #26
0
 public void Equals_When_DateTimeOffset(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.Equals(RangeFactory.ToTestDateTimeOffset(value), RangeFactory.ToTestDateTimeOffset(compare)));
 }
Пример #27
0
 public void SmallerThan_When_DateTime(int value, int compare, bool expected)
 {
     Assert.Equal(expected, CompareUtilities.SmallerThan(RangeFactory.ToTestDateTime(value), RangeFactory.ToTestDateTime(compare)));
 }
Пример #28
0
        public void TestwithJsonCompareShouldPass()
        {
            var response = Request.RestRequest("https://api.exchangeratesapi.io/", "2010-01-14", "GET");

            Assert.True(CompareUtilities.CompareObjects(response, CompareUtilities.ReadCachedJson("CurrencyByDate2010-01-14"), "Date2010-01-14"));
        }