|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | +#include <faiss/utils/Heap.h> |
| 8 | +#include <gtest/gtest.h> |
| 9 | +#include <algorithm> |
| 10 | +#include <numeric> |
| 11 | + |
| 12 | +using namespace faiss; |
| 13 | + |
| 14 | +TEST(Heap, addn_with_ids) { |
| 15 | + size_t n = 1000; |
| 16 | + size_t k = 1; |
| 17 | + std::vector<int64_t> heap_labels(n, -1); |
| 18 | + std::vector<float> heap_distances(n, 0); |
| 19 | + float_minheap_array_t heaps = { |
| 20 | + n, k, heap_labels.data(), heap_distances.data()}; |
| 21 | + heaps.heapify(); |
| 22 | + std::vector<int64_t> labels(n, 1); |
| 23 | + std::vector<float> distances(n, 0.0f); |
| 24 | + std::vector<int64_t> subset(n); |
| 25 | + std::iota(subset.begin(), subset.end(), 0); |
| 26 | + heaps.addn_with_ids(1, distances.data(), labels.data(), 1); |
| 27 | + heaps.reorder(); |
| 28 | + EXPECT_TRUE( |
| 29 | + std::all_of(heap_labels.begin(), heap_labels.end(), [](int64_t i) { |
| 30 | + return i == 1; |
| 31 | + })); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(Heap, addn_query_subset_with_ids) { |
| 35 | + size_t n = 20000000; // more than 2^24 |
| 36 | + size_t k = 1; |
| 37 | + std::vector<int64_t> heap_labels(n, -1); |
| 38 | + std::vector<float> heap_distances(n, 0); |
| 39 | + float_minheap_array_t heaps = { |
| 40 | + n, k, heap_labels.data(), heap_distances.data()}; |
| 41 | + heaps.heapify(); |
| 42 | + std::vector<int64_t> labels(n, 1); |
| 43 | + std::vector<float> distances(n, 0.0f); |
| 44 | + std::vector<int64_t> subset(n); |
| 45 | + std::iota(subset.begin(), subset.end(), 0); |
| 46 | + heaps.addn_query_subset_with_ids( |
| 47 | + n, subset.data(), 1, distances.data(), labels.data(), 1); |
| 48 | + heaps.reorder(); |
| 49 | + EXPECT_TRUE( |
| 50 | + std::all_of(heap_labels.begin(), heap_labels.end(), [](int64_t i) { |
| 51 | + return i == 1; |
| 52 | + })); |
| 53 | +} |
0 commit comments