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));
            }
        }
        public static void TestCriticalProofContainsWithRoundingBruteForce()
        {
            var unitScale = MetricUnitScale.FromEnumValues(typeof(Distance));

            var lastRange = MetricValueHelpers.CreateMetricRange(0, 0);
            int repeats   = 0;
            int increment = 3;

            for (long i = 0; i < int.MaxValue; i += increment)
            {
                // exponential growth as the scale is exponential
                if (++repeats >= 1000 && increment < int.MaxValue / 10)
                {
                    repeats    = 0;
                    increment *= 2;
                }

                lastRange = AssertAssertRoundtripInvariant(i, lastRange, unitScale);
            }

            lastRange = MetricValueHelpers.CreateMetricRange(int.MaxValue, int.MaxValue);
            repeats   = 0;
            increment = int.MaxValue / 200;
            for (long i = int.MaxValue; i > 0; i -= increment)             // reverse order
            {
                // logarithmic decrease as the scale is enumerated in reverse order
                if (++repeats >= 1000 && increment > 10)
                {
                    repeats    = 0;
                    increment /= 2;
                }

                lastRange = AssertAssertRoundtripInvariant(i, lastRange, unitScale);
            }
        }
        public static void TestCreateMetricRange(double min, double max, string unit, double expectedMin, double expectedMax)
        {
            var unitScale  = MetricUnitScale.FromEnumValues(typeof(Distance));
            var metricUnit = unitScale[unit];
            var range      = MetricValueHelpers.CreateMetricRange(min, max, metricUnit);

            AreEqual(range.Min, expectedMin);
            AreEqual(range.Max, expectedMax);
        }
        public static void TestAutoformat(string values, string expected)
        {
            var valuesParsed = values.Split(';').Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();

            foreach (var value in valuesParsed)
            {
                AreEqual(MetricValueHelpers.GetAutoscaledFormat(value), expected);
            }
        }
Exemplo n.º 5
0
        /// <summary>Converts <see cref="IStoredMetricValue"/> to the competition metric value.</summary>
        /// <param name="storedMetric">The stored metric.</param>
        /// <param name="metric">The metric information.</param>
        /// <returns>The competition metric value.</returns>
        public static CompetitionMetricValue ToMetricValue(
            this IStoredMetricValue storedMetric,
            MetricInfo metric)
        {
            Code.BugIf(
                storedMetric.MetricAttributeType != metric.AttributeType,
                "storedMetric.MetricAttributeType != metric.AttributeType");

            var metricUnit = metric.MetricUnits[storedMetric.UnitOfMeasurement];

            var metricValues = MetricValueHelpers.CreateMetricRange(storedMetric.Min, storedMetric.Max, metricUnit);

            return(new CompetitionMetricValue(
                       metric,
                       metricValues,
                       metricUnit));
        }
        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);
        }