|
| 1 | +// -------------------------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="XYAxisSeriesTests.cs" company="OxyPlot"> |
| 3 | +// Copyright (c) 2020 OxyPlot contributors |
| 4 | +// </copyright> |
| 5 | +// <summary> |
| 6 | +// Provides unit tests for the <see cref="XYAxisSeries" /> class. |
| 7 | +// </summary> |
| 8 | +// -------------------------------------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +namespace OxyPlot.Tests |
| 11 | +{ |
| 12 | + using System; |
| 13 | + |
| 14 | + using NUnit.Framework; |
| 15 | + |
| 16 | + using OxyPlot.Series; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Provides unit tests for the <see cref="XYAxisSeries" /> class. |
| 20 | + /// </summary> |
| 21 | + public class XYAxisSeriesTests |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// Test class just to allow methods in XYAxisSeries to be tested. |
| 25 | + /// </summary> |
| 26 | + private class TestAxisSeries : XYAxisSeries |
| 27 | + { |
| 28 | + public override void Render(IRenderContext rc) |
| 29 | + { |
| 30 | + throw new NotImplementedException(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Generate lots of random data, then checking the FindWindowStartIndex method |
| 36 | + /// against this data. |
| 37 | + /// </summary> |
| 38 | + [Test] |
| 39 | + public void RunFuzzTest() |
| 40 | + { |
| 41 | + for (int i = 0; i < 10000; ++i) |
| 42 | + { |
| 43 | + FuzzIteration(i); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Generate lots of random data, then checking the FindWindowStartIndex method |
| 49 | + /// against this data. This time with data containing NaN-values. |
| 50 | + /// </summary> |
| 51 | + [Test] |
| 52 | + public void RunFuzzWithNanTest() |
| 53 | + { |
| 54 | + for (int i = 0; i < 10000; ++i) |
| 55 | + { |
| 56 | + FuzzIterationWithNan(i); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static void FuzzIteration(int seed) |
| 61 | + { |
| 62 | + var xyAxiseries = new TestAxisSeries(); |
| 63 | + |
| 64 | + var N = 15; |
| 65 | + var random = new Random(seed); |
| 66 | + var testData = new System.Collections.Generic.List<double>(); |
| 67 | + var currentValue = random.NextDouble() - 0.5; |
| 68 | + for (int i = 0; i < N; ++i) |
| 69 | + { |
| 70 | + testData.Add(currentValue); |
| 71 | + currentValue += random.NextDouble(); |
| 72 | + } |
| 73 | + |
| 74 | + var targetX = random.NextDouble() * (N / 2.0) - 1.0; |
| 75 | + var guess = random.Next(0, testData.Count); |
| 76 | + |
| 77 | + int foundIndex = xyAxiseries.FindWindowStartIndex(testData, x => x, targetX, guess); |
| 78 | + |
| 79 | + if (foundIndex > 0) |
| 80 | + Assert.LessOrEqual(testData[foundIndex], targetX, "At " + seed); |
| 81 | + if (foundIndex < testData.Count - 1) |
| 82 | + Assert.GreaterOrEqual(testData[foundIndex + 1], targetX, "At " + seed); |
| 83 | + } |
| 84 | + |
| 85 | + private static void FuzzIterationWithNan(int seed) |
| 86 | + { |
| 87 | + var xyAxiseries = new TestAxisSeries(); |
| 88 | + |
| 89 | + var N = 15; |
| 90 | + var random = new Random(seed); |
| 91 | + var testData = new System.Collections.Generic.List<double>(); |
| 92 | + var currentValue = random.NextDouble() - 0.5; |
| 93 | + for (int i = 0; i < N; ++i) |
| 94 | + { |
| 95 | + if (random.Next(4) == 0) |
| 96 | + testData.Add(double.NaN); |
| 97 | + testData.Add(currentValue); |
| 98 | + currentValue += random.NextDouble(); |
| 99 | + } |
| 100 | + |
| 101 | + double? PrevNonNan(int index) |
| 102 | + { |
| 103 | + while (index >= 0) |
| 104 | + { |
| 105 | + if (double.IsNaN(testData[index]) == false) |
| 106 | + return testData[index]; |
| 107 | + index -= 1; |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + double? NextNonNan(int index) |
| 114 | + { |
| 115 | + while (index >= 0) |
| 116 | + { |
| 117 | + if (double.IsNaN(testData[index]) == false) |
| 118 | + return testData[index]; |
| 119 | + index += 1; |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + var targetX = random.NextDouble() * (N / 2.0) - 1.0; |
| 126 | + var guess = random.Next(0, testData.Count); |
| 127 | + |
| 128 | + int foundIndex = xyAxiseries.FindWindowStartIndex(testData, x => x, targetX, guess); |
| 129 | + |
| 130 | + if (foundIndex > 0) |
| 131 | + { |
| 132 | + if (double.IsNaN(testData[foundIndex])) |
| 133 | + { |
| 134 | + var prevNonNaN = PrevNonNan(foundIndex-1); |
| 135 | + if (prevNonNaN.HasValue) |
| 136 | + Assert.LessOrEqual(prevNonNaN, targetX, "At " + seed); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + Assert.LessOrEqual(testData[foundIndex], targetX, "At " + seed); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (foundIndex < testData.Count - 1) |
| 145 | + { |
| 146 | + if (double.IsNaN(testData[foundIndex + 1])) |
| 147 | + { |
| 148 | + var nextNonNaN = NextNonNan(foundIndex+1); |
| 149 | + if (nextNonNaN.HasValue) |
| 150 | + Assert.GreaterOrEqual(nextNonNaN, targetX, "At " + seed); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + Assert.GreaterOrEqual(testData[foundIndex + 1], targetX, "At " + seed); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments