private static void AssertRoundtripInvariant(MetricRange a, MetricRange b, MetricUnit metricUnit)
        {
            var c = HostEnvironmentInfo.MainCultureInfo;

            a.GetMinMaxString(metricUnit, out var aMin, out var aMax);
            b.GetMinMaxString(metricUnit, out var bMin, out var bMax);

            var a2 = MetricValueHelpers.CreateMetricRange(
                double.Parse(aMin, c),
                double.Parse(aMax, c),
                metricUnit);

            var b2 = MetricValueHelpers.CreateMetricRange(
                double.Parse(bMin, c),
                double.Parse(bMax, c),
                metricUnit);

            IsTrue(a.ContainsWithRounding(a2, metricUnit));
            IsTrue(a2.ContainsWithRounding(a, metricUnit));
            IsTrue(b.ContainsWithRounding(b2, metricUnit));
            IsTrue(b2.ContainsWithRounding(b, metricUnit));

            if (a.ContainsWithRounding(b, metricUnit))
            {
                IsTrue(a2.ContainsWithRounding(b2, metricUnit));
            }
        }
示例#2
0
        /// <summary>Initializes a new instance of the <see cref="CompetitionMetricValue"/> class.</summary>
        /// <param name="metric">The metric information.</param>
        /// <param name="valuesRange">The metric values range.</param>
        /// <param name="displayMetricUnit">The preferred metric unit for the values range.</param>
        public CompetitionMetricValue(
            [NotNull] MetricInfo metric,
            MetricRange valuesRange,
            [NotNull] MetricUnit displayMetricUnit)
        {
            Code.NotNull(metric, nameof(metric));
            Code.NotNull(displayMetricUnit, nameof(displayMetricUnit));

            Metric            = metric;
            ValuesRange       = valuesRange;
            DisplayMetricUnit = displayMetricUnit;
        }
        private static MetricRange AssertAssertRoundtripInvariant(
            long i,
            MetricRange lastRange,
            MetricUnitScale unitScale)
        {
            // best applicable unit
            var unit = unitScale[i];

            // new ranges
            var newRange    = MetricValueHelpers.CreateMetricRange(i, i);
            var minMaxRange = lastRange.Union(newRange);

            AssertRoundtripInvariant(newRange, lastRange, unit);
            AssertRoundtripInvariant(newRange, minMaxRange, unit);
            AssertRoundtripInvariant(lastRange, minMaxRange, unit);

            return(newRange.ContainsWithRounding(lastRange, unit) ? lastRange : newRange);
        }
示例#4
0
        public static void TestInfiniteMetricRange()
        {
            var r  = new MetricRange(FromNegativeInfinity, ToPositiveInfinity);
            var r2 = new MetricRange(1, 2);

            AreEqual(r.Min, FromNegativeInfinity);
            AreEqual(r.Max, ToPositiveInfinity);
            IsFalse(r.IsEmpty);
            IsFalse(r.Equals(Empty));
            IsFalse(r.Equals(r2));
            IsTrue(r == Infinite);
            IsTrue(r != r2);
            IsTrue(r.Contains(r2));
            IsFalse(r.Contains(Empty));
            IsTrue(r.Union(r2) == r);
            IsTrue(r.Union(r2).IsNotEmpty);
            IsTrue(r.Union(r2) != r2);
            IsTrue(r.Union(Empty) == r);
            AreEqual(r.ToString("F2"), "(-∞..+∞)");
        }
示例#5
0
        public static void TestEmptyMetricRange()
        {
            var r  = new MetricRange(EmptyMetricValue, EmptyMetricValue);
            var r2 = new MetricRange(1, 2);

            AreEqual(r.Min, EmptyMetricValue);
            AreEqual(r.Max, EmptyMetricValue);
            IsTrue(r.IsEmpty);
            IsTrue(r.Equals(Empty));
            IsFalse(r.Equals(r2));
            IsTrue(r == Empty);
            IsTrue(r != r2);
            IsFalse(r.Contains(r2));
            IsTrue(r.Contains(Empty));
            IsTrue(r.Union(r2) == r2);
            IsTrue(r.Union(r2).IsNotEmpty);
            IsTrue(r.Union(r2) != r);
            IsTrue(r.Union(Empty) == r);
            AreEqual(r.ToString("F2"), "∅");
        }
示例#6
0
        public static void TestMetricRangeWithValues()
        {
            var r  = new MetricRange(1, 5);
            var r2 = new MetricRange(1, 2);
            var r3 = new MetricRange(10, 20);

            AreEqual(r.Min, 1);
            AreEqual(r.Max, 5);
            IsFalse(r.IsEmpty);
            IsFalse(r.Equals(Empty));
            IsFalse(r.Equals(r2));
            IsFalse(r == Infinite);
            IsTrue(r != r2);
            IsTrue(r.Contains(r2));
            IsFalse(r.Contains(Empty));
            IsTrue(r.Union(r2) == r);
            IsTrue(r.Union(r2).IsNotEmpty);
            IsTrue(r.Union(r2) != r2);
            IsTrue(r.Union(Empty) == r);
            AreEqual(r.ToString("F2", CultureInfo.InvariantCulture), "[1.00..5.00]");
            AreEqual(r.Union(r3).ToString("F0", CultureInfo.InvariantCulture), "[1..20]");
        }
        public static void TestContainsWithRounding()
        {
            var unitScale = MetricUnitScale.FromEnumValues(typeof(Distance));

            var valueX = 1002;
            var valueY = 1005;
            var valueZ = 1008;

            var rangeX  = new MetricRange(valueX, valueX);
            var rangeY  = new MetricRange(valueY, valueY);
            var rangeZ  = new MetricRange(valueZ, valueZ);
            var rangeYz = new MetricRange(valueY, valueZ);

            var unit = unitScale[Distance.Meter];

            AssertRoundtripInvariant(rangeY, rangeX, unit);
            AssertRoundtripInvariant(rangeY, rangeZ, unit);
            AssertRoundtripInvariant(rangeY, rangeYz, unit);

            IsFalse(rangeY.ContainsWithRounding(rangeX, unit));
            IsTrue(rangeY.ContainsWithRounding(rangeZ, unit));
            IsTrue(rangeY.ContainsWithRounding(rangeYz, unit));
        }
        /// <summary>
        /// Takes a result list from an IntervalTree and merges adjacent intervals with the same name and adjust for priority levels
        /// </summary>
        /// <param name="source"></param>
        void ProcessIntervalTreeResults(List <MetricRange> source)
        {
            /*
             * A second level of processing is done to merge intervals that share the same name and return intervals with higher priority
             */
            source.Sort((left, right) => Comparer <int> .Default.Compare(left.priority, right.priority));
            // start from 2nd to last result hiding and splitting lower priority results and proceeding backwards in the list
            for (int i = source.Count - 2; i >= 0; --i)
            {
                MetricRange higherPriority = source[i];

                for (int j = i + 1; j < source.Count;)
                {
                    MetricRange lowerPriority = source[j];
                    if (lowerPriority.intervalStart >= higherPriority.intervalStart &&
                        lowerPriority.intervalEnd <= higherPriority.intervalEnd)
                    {
                        source.RemoveAt(j);
                        continue;
                    }

                    bool insertNew = false;

                    if (lowerPriority.intervalStart <= higherPriority.intervalStart)
                    {
                        if (lowerPriority.intervalEnd <= higherPriority.intervalStart)
                        {
                            ++j;
                            continue;
                        }

                        // merge with higher priority
                        if (lowerPriority.name.Equals(higherPriority.name))
                        {
                            higherPriority = new MetricRange
                            {
                                intervalStart = lowerPriority.intervalStart,
                                intervalEnd   = Mathf.Max(lowerPriority.intervalEnd, higherPriority.intervalEnd),
                                priority      = higherPriority.priority,
                                name          = lowerPriority.name
                            };

                            source[i] = higherPriority;
                            source.RemoveAt(j);
                            continue;
                        }

                        if (lowerPriority.intervalEnd > higherPriority.intervalEnd)
                        {
                            // we need to split the lowerPriority interval
                            insertNew = true;
                        }

                        // shrink
                        source[j] = new MetricRange
                        {
                            intervalStart = lowerPriority.intervalStart, intervalEnd = higherPriority.intervalStart, priority = lowerPriority.priority, name = lowerPriority.name
                        };
                    }

                    if (lowerPriority.intervalEnd >= higherPriority.intervalEnd)
                    {
                        if (lowerPriority.intervalStart < higherPriority.intervalEnd)
                        {
                            // merge with higher priority
                            if (lowerPriority.name.Equals(higherPriority.name))
                            {
                                higherPriority = new MetricRange
                                {
                                    intervalStart = higherPriority.intervalStart,
                                    intervalEnd   = lowerPriority.intervalEnd,
                                    priority      = higherPriority.priority,
                                    name          = lowerPriority.name
                                };

                                source[i] = higherPriority;
                                source.RemoveAt(j);
                                continue;
                            }

                            var newResult = new MetricRange
                            {
                                intervalStart = higherPriority.intervalEnd, intervalEnd = lowerPriority.intervalEnd, priority = lowerPriority.priority, name = lowerPriority.name
                            };

                            // if this lower priority interval surrounded the higher priority one we will need to add a second lower priority interval, otherwise we can just replace the original
                            if (insertNew)
                            {
                                source.Insert(j + 1, newResult);
                            }
                            else
                            {
                                source[j] = newResult;
                            }
                        }
                    }

                    ++j;
                }
            }
        }