|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 European Spallation Source ERIC. |
| 3 | + */ |
| 4 | + |
| 5 | +package org.csstudio.javafx.rtplot.data; |
| 6 | + |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.locks.Lock; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | + |
| 16 | +public class PlotDataSearchTest { |
| 17 | + |
| 18 | + private PlotDataProvider<Double> simpleData = new SimpleDataProvider(); |
| 19 | + private PlotDataSearch<Double> plotDataSearch = new PlotDataSearch<>(); |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testFindClosestSample() { |
| 23 | + |
| 24 | + int index = plotDataSearch.findClosestSample(simpleData, 0.0); |
| 25 | + assertEquals(50, index); |
| 26 | + |
| 27 | + index = plotDataSearch.findClosestSample(simpleData, -50.0); |
| 28 | + assertEquals(0, index); |
| 29 | + |
| 30 | + index = plotDataSearch.findClosestSample(simpleData, 0.1); |
| 31 | + assertEquals(50, index); |
| 32 | + |
| 33 | + index = plotDataSearch.findClosestSample(simpleData, 0.9); |
| 34 | + assertEquals(51, index); |
| 35 | + |
| 36 | + index = plotDataSearch.findClosestSample(simpleData, 100.0); |
| 37 | + assertEquals(99, index); |
| 38 | + |
| 39 | + index = plotDataSearch.findClosestSample(simpleData, -100.0); |
| 40 | + assertEquals(0, index); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testWrongType() { |
| 46 | + PlotDataSearch plotDataSearch1 = new PlotDataSearch(); |
| 47 | + |
| 48 | + int index = plotDataSearch1.findClosestSample(simpleData, new UnsupportedType()); |
| 49 | + assertEquals(-1, index); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testFindSampleGreaterOrEqual() { |
| 54 | + |
| 55 | + int index = plotDataSearch.findSampleGreaterOrEqual(simpleData, 0.0); |
| 56 | + assertEquals(50, index); |
| 57 | + |
| 58 | + index = plotDataSearch.findSampleGreaterOrEqual(simpleData, 0.1); |
| 59 | + assertEquals(51, index); |
| 60 | + |
| 61 | + index = plotDataSearch.findSampleGreaterOrEqual(simpleData, 1.0); |
| 62 | + assertEquals(51, index); |
| 63 | + |
| 64 | + index = plotDataSearch.findSampleGreaterOrEqual(simpleData, 100.0); |
| 65 | + assertEquals(-1, index); |
| 66 | + |
| 67 | + index = plotDataSearch.findSampleGreaterOrEqual(simpleData, 48.5); |
| 68 | + assertEquals(99, index); |
| 69 | + |
| 70 | + index = plotDataSearch.findSampleGreaterOrEqual(simpleData, -100.0); |
| 71 | + assertEquals(0, index); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testFindSampleLessOrEqual() { |
| 77 | + |
| 78 | + int index = plotDataSearch.findSampleLessOrEqual(simpleData, 0.0); |
| 79 | + assertEquals(50, index); |
| 80 | + |
| 81 | + index = plotDataSearch.findSampleLessOrEqual(simpleData, 0.1); |
| 82 | + assertEquals(50, index); |
| 83 | + |
| 84 | + index = plotDataSearch.findSampleLessOrEqual(simpleData, -0.1); |
| 85 | + assertEquals(49, index); |
| 86 | + |
| 87 | + index = plotDataSearch.findSampleLessOrEqual(simpleData, 0.9); |
| 88 | + assertEquals(50, index); |
| 89 | + |
| 90 | + index = plotDataSearch.findSampleLessOrEqual(simpleData, 100.0); |
| 91 | + assertEquals(99, index); |
| 92 | + |
| 93 | + index = plotDataSearch.findSampleLessOrEqual(simpleData, -100.0); |
| 94 | + assertEquals(-1, index); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testFindSampleLessThan() { |
| 100 | + int index = plotDataSearch.findSampleLessThan(simpleData, 0.0); |
| 101 | + assertEquals(49, index); |
| 102 | + |
| 103 | + index = plotDataSearch.findSampleLessThan(simpleData, 0.0001); |
| 104 | + assertEquals(50, index); |
| 105 | + |
| 106 | + index = plotDataSearch.findSampleLessThan(simpleData, 100.0); |
| 107 | + assertEquals(99, index); |
| 108 | + |
| 109 | + index = plotDataSearch.findSampleLessThan(simpleData, -100.0); |
| 110 | + assertEquals(-1, index); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testFindSampleGreaterThan() { |
| 115 | + int index = plotDataSearch.findSampleGreaterThan(simpleData, 0.0); |
| 116 | + assertEquals(51, index); |
| 117 | + |
| 118 | + index = plotDataSearch.findSampleGreaterThan(simpleData, 0.9999); |
| 119 | + assertEquals(51, index); |
| 120 | + |
| 121 | + index = plotDataSearch.findSampleGreaterThan(simpleData, 100.0); |
| 122 | + assertEquals(-1, index); |
| 123 | + |
| 124 | + index = plotDataSearch.findSampleGreaterThan(simpleData, -100.0); |
| 125 | + assertEquals(0, index); |
| 126 | + } |
| 127 | + |
| 128 | + private static class SimpleDataProvider implements PlotDataProvider<Double> { |
| 129 | + |
| 130 | + private List<PlotDataItem<Double>> data = new ArrayList<>(); |
| 131 | + int elementCount = 100; |
| 132 | + |
| 133 | + public SimpleDataProvider() { |
| 134 | + for (int i = 0; i < elementCount; i++) { |
| 135 | + PlotDataItem<Double> item = new SimpleDataItem<>(1.0 * (i - 50), 1.0 * (i - 50)); |
| 136 | + data.add(i, item); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Lock getLock() { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int size() { |
| 147 | + return elementCount; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public PlotDataItem<Double> get(int index) { |
| 152 | + return data.get(index); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static class UnsupportedType implements Comparable { |
| 157 | + |
| 158 | + @Override |
| 159 | + public int compareTo(Object o) { |
| 160 | + return 0; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments