|
| 1 | +/* |
| 2 | + * @notice |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | + * contributor license agreements. See the NOTICE file distributed with |
| 5 | + * this work for additional information regarding copyright ownership. |
| 6 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | + * (the "License"); you may not use this file except in compliance with |
| 8 | + * the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * Modifications copyright (C) 2025 Elasticsearch B.V. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.codec.vectors; |
| 21 | + |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * copied and modified from Lucene |
| 26 | + */ |
| 27 | +public class NeighborQueueTests extends ESTestCase { |
| 28 | + public void testNeighborsProduct() { |
| 29 | + // make sure we have the sign correct |
| 30 | + NeighborQueue nn = new NeighborQueue(2, false); |
| 31 | + assertTrue(nn.insertWithOverflow(2, 0.5f)); |
| 32 | + assertTrue(nn.insertWithOverflow(1, 0.2f)); |
| 33 | + assertTrue(nn.insertWithOverflow(3, 1f)); |
| 34 | + assertEquals(0.5f, nn.topScore(), 0); |
| 35 | + nn.pop(); |
| 36 | + assertEquals(1f, nn.topScore(), 0); |
| 37 | + nn.pop(); |
| 38 | + } |
| 39 | + |
| 40 | + public void testNeighborsMaxHeap() { |
| 41 | + NeighborQueue nn = new NeighborQueue(2, true); |
| 42 | + assertTrue(nn.insertWithOverflow(2, 2)); |
| 43 | + assertTrue(nn.insertWithOverflow(1, 1)); |
| 44 | + assertFalse(nn.insertWithOverflow(3, 3)); |
| 45 | + assertEquals(2f, nn.topScore(), 0); |
| 46 | + nn.pop(); |
| 47 | + assertEquals(1f, nn.topScore(), 0); |
| 48 | + } |
| 49 | + |
| 50 | + public void testTopMaxHeap() { |
| 51 | + NeighborQueue nn = new NeighborQueue(2, true); |
| 52 | + nn.add(1, 2); |
| 53 | + nn.add(2, 1); |
| 54 | + // lower scores are better; highest score on top |
| 55 | + assertEquals(2, nn.topScore(), 0); |
| 56 | + assertEquals(1, nn.topNode()); |
| 57 | + } |
| 58 | + |
| 59 | + public void testTopMinHeap() { |
| 60 | + NeighborQueue nn = new NeighborQueue(2, false); |
| 61 | + nn.add(1, 0.5f); |
| 62 | + nn.add(2, -0.5f); |
| 63 | + // higher scores are better; lowest score on top |
| 64 | + assertEquals(-0.5f, nn.topScore(), 0); |
| 65 | + assertEquals(2, nn.topNode()); |
| 66 | + } |
| 67 | + |
| 68 | + public void testClear() { |
| 69 | + NeighborQueue nn = new NeighborQueue(2, false); |
| 70 | + nn.add(1, 1.1f); |
| 71 | + nn.add(2, -2.2f); |
| 72 | + nn.clear(); |
| 73 | + |
| 74 | + assertEquals(0, nn.size()); |
| 75 | + } |
| 76 | + |
| 77 | + public void testMaxSizeQueue() { |
| 78 | + NeighborQueue nn = new NeighborQueue(2, false); |
| 79 | + nn.add(1, 1); |
| 80 | + nn.add(2, 2); |
| 81 | + assertEquals(2, nn.size()); |
| 82 | + assertEquals(1, nn.topNode()); |
| 83 | + |
| 84 | + // insertWithOverflow does not extend the queue |
| 85 | + nn.insertWithOverflow(3, 3); |
| 86 | + assertEquals(2, nn.size()); |
| 87 | + assertEquals(2, nn.topNode()); |
| 88 | + |
| 89 | + // add does extend the queue beyond maxSize |
| 90 | + nn.add(4, 1); |
| 91 | + assertEquals(3, nn.size()); |
| 92 | + } |
| 93 | + |
| 94 | + public void testUnboundedQueue() { |
| 95 | + NeighborQueue nn = new NeighborQueue(1, true); |
| 96 | + float maxScore = -2; |
| 97 | + int maxNode = -1; |
| 98 | + for (int i = 0; i < 256; i++) { |
| 99 | + // initial size is 32 |
| 100 | + float score = random().nextFloat(); |
| 101 | + if (score > maxScore) { |
| 102 | + maxScore = score; |
| 103 | + maxNode = i; |
| 104 | + } |
| 105 | + nn.add(i, score); |
| 106 | + } |
| 107 | + assertEquals(maxScore, nn.topScore(), 0); |
| 108 | + assertEquals(maxNode, nn.topNode()); |
| 109 | + } |
| 110 | + |
| 111 | + public void testInvalidArguments() { |
| 112 | + expectThrows(IllegalArgumentException.class, () -> new NeighborQueue(0, false)); |
| 113 | + } |
| 114 | + |
| 115 | + public void testToString() { |
| 116 | + assertEquals("Neighbors[0]", new NeighborQueue(2, false).toString()); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments