faiss/tests/test_heap.cpp
Michael Norris eff0898a13 Enable linting: lint config changes plus arc lint command (#3966)
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/3966

This actually enables the linting.

Manual changes:
- tools/arcanist/lint/fbsource-licenselint-config.toml
- tools/arcanist/lint/fbsource-lint-engine.toml

Automated changes:
`arc lint --apply-patches --take LICENSELINT --paths-cmd 'hg files faiss'`

Reviewed By: asadoughi

Differential Revision: D64484165

fbshipit-source-id: 4f2f6e953c94ef6ebfea8a5ae035ccfbea65ed04
2024-10-22 09:46:48 -07:00

55 lines
1.7 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <faiss/utils/Heap.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <numeric>
using namespace faiss;
TEST(Heap, addn_with_ids) {
size_t n = 1000;
size_t k = 1;
std::vector<int64_t> heap_labels(n, -1);
std::vector<float> heap_distances(n, 0);
float_minheap_array_t heaps = {
n, k, heap_labels.data(), heap_distances.data()};
heaps.heapify();
std::vector<int64_t> labels(n, 1);
std::vector<float> distances(n, 0.0f);
std::vector<int64_t> subset(n);
std::iota(subset.begin(), subset.end(), 0);
heaps.addn_with_ids(1, distances.data(), labels.data(), 1);
heaps.reorder();
EXPECT_TRUE(
std::all_of(heap_labels.begin(), heap_labels.end(), [](int64_t i) {
return i == 1;
}));
}
TEST(Heap, addn_query_subset_with_ids) {
size_t n = 20000000; // more than 2^24
size_t k = 1;
std::vector<int64_t> heap_labels(n, -1);
std::vector<float> heap_distances(n, 0);
float_minheap_array_t heaps = {
n, k, heap_labels.data(), heap_distances.data()};
heaps.heapify();
std::vector<int64_t> labels(n, 1);
std::vector<float> distances(n, 0.0f);
std::vector<int64_t> subset(n);
std::iota(subset.begin(), subset.end(), 0);
heaps.addn_query_subset_with_ids(
n, subset.data(), 1, distances.data(), labels.data(), 1);
heaps.reorder();
EXPECT_TRUE(
std::all_of(heap_labels.begin(), heap_labels.end(), [](int64_t i) {
return i == 1;
}));
}