|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.algorithm.search; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import org.apache.arrow.algorithm.sort.DefaultVectorComparators; |
| 23 | +import org.apache.arrow.algorithm.sort.VectorValueComparator; |
| 24 | +import org.apache.arrow.memory.BufferAllocator; |
| 25 | +import org.apache.arrow.memory.RootAllocator; |
| 26 | +import org.apache.arrow.vector.IntVector; |
| 27 | + |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test cases for {@link VectorRangeSearcher}. |
| 34 | + */ |
| 35 | +public class TestVectorRangeSearcher { |
| 36 | + |
| 37 | + private BufferAllocator allocator; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void prepare() { |
| 41 | + allocator = new RootAllocator(1024 * 1024); |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + public void shutdown() { |
| 46 | + allocator.close(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testGetLowerBounds() { |
| 51 | + final int maxValue = 100; |
| 52 | + final int repeat = 5; |
| 53 | + try (IntVector intVector = new IntVector("int vec", allocator)) { |
| 54 | + // allocate vector |
| 55 | + intVector.allocateNew(maxValue * repeat); |
| 56 | + intVector.setValueCount(maxValue * repeat); |
| 57 | + |
| 58 | + // prepare data in sorted order |
| 59 | + // each value is repeated some times |
| 60 | + for (int i = 0; i < maxValue; i++) { |
| 61 | + for (int j = 0; j < repeat; j++) { |
| 62 | + if (i == 0) { |
| 63 | + intVector.setNull(i * repeat + j); |
| 64 | + } else { |
| 65 | + intVector.set(i * repeat + j, i); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // do search |
| 71 | + VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(intVector); |
| 72 | + for (int i = 0; i < maxValue; i++) { |
| 73 | + int result = VectorRangeSearcher.getFirstMatch(intVector, comparator, intVector, i * repeat); |
| 74 | + assertEquals(i * repeat, result); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testGetLowerBoundsNegative() { |
| 81 | + final int maxValue = 100; |
| 82 | + final int repeat = 5; |
| 83 | + try (IntVector intVector = new IntVector("int vec", allocator); |
| 84 | + IntVector negVector = new IntVector("neg vec", allocator)) { |
| 85 | + // allocate vector |
| 86 | + intVector.allocateNew(maxValue * repeat); |
| 87 | + intVector.setValueCount(maxValue * repeat); |
| 88 | + |
| 89 | + negVector.allocateNew(maxValue); |
| 90 | + negVector.setValueCount(maxValue); |
| 91 | + |
| 92 | + // prepare data in sorted order |
| 93 | + // each value is repeated some times |
| 94 | + for (int i = 0; i < maxValue; i++) { |
| 95 | + for (int j = 0; j < repeat; j++) { |
| 96 | + if (i == 0) { |
| 97 | + intVector.setNull(i * repeat + j); |
| 98 | + } else { |
| 99 | + intVector.set(i * repeat + j, i); |
| 100 | + } |
| 101 | + } |
| 102 | + negVector.set(i, maxValue + i); |
| 103 | + } |
| 104 | + |
| 105 | + // do search |
| 106 | + VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(intVector); |
| 107 | + for (int i = 0; i < maxValue; i++) { |
| 108 | + int result = VectorRangeSearcher.getFirstMatch(intVector, comparator, negVector, i); |
| 109 | + assertEquals(-1, result); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetUpperBounds() { |
| 116 | + final int maxValue = 100; |
| 117 | + final int repeat = 5; |
| 118 | + try (IntVector intVector = new IntVector("int vec", allocator)) { |
| 119 | + // allocate vector |
| 120 | + intVector.allocateNew(maxValue * repeat); |
| 121 | + intVector.setValueCount(maxValue * repeat); |
| 122 | + |
| 123 | + // prepare data in sorted order |
| 124 | + // each value is repeated some times |
| 125 | + for (int i = 0; i < maxValue; i++) { |
| 126 | + for (int j = 0; j < repeat; j++) { |
| 127 | + if (i == 0) { |
| 128 | + intVector.setNull(i * repeat + j); |
| 129 | + } else { |
| 130 | + intVector.set(i * repeat + j, i); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // do search |
| 136 | + VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(intVector); |
| 137 | + for (int i = 0; i < maxValue; i++) { |
| 138 | + int result = VectorRangeSearcher.getLastMatch(intVector, comparator, intVector, i * repeat); |
| 139 | + assertEquals((i + 1) * repeat - 1, result); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testGetUpperBoundsNegative() { |
| 146 | + final int maxValue = 100; |
| 147 | + final int repeat = 5; |
| 148 | + try (IntVector intVector = new IntVector("int vec", allocator); |
| 149 | + IntVector negVector = new IntVector("neg vec", allocator)) { |
| 150 | + // allocate vector |
| 151 | + intVector.allocateNew(maxValue * repeat); |
| 152 | + intVector.setValueCount(maxValue * repeat); |
| 153 | + |
| 154 | + negVector.allocateNew(maxValue); |
| 155 | + negVector.setValueCount(maxValue); |
| 156 | + |
| 157 | + // prepare data in sorted order |
| 158 | + // each value is repeated some times |
| 159 | + for (int i = 0; i < maxValue; i++) { |
| 160 | + for (int j = 0; j < repeat; j++) { |
| 161 | + if (i == 0) { |
| 162 | + intVector.setNull(i * repeat + j); |
| 163 | + } else { |
| 164 | + intVector.set(i * repeat + j, i); |
| 165 | + } |
| 166 | + } |
| 167 | + negVector.set(i, maxValue + i); |
| 168 | + } |
| 169 | + |
| 170 | + // do search |
| 171 | + VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(intVector); |
| 172 | + for (int i = 0; i < maxValue; i++) { |
| 173 | + int result = VectorRangeSearcher.getLastMatch(intVector, comparator, negVector, i); |
| 174 | + assertEquals(-1, result); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments