Skip to content

Commit

Permalink
Added annoy index serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
FuexFollets committed Jan 31, 2024
1 parent b4b4e27 commit 5be6471
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions tests/annoy_serialize.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "cereal/archives/binary.hpp"
#include <iostream>

#include <annoy/annoylib.h>
#include <annoy/kissrandom.h>

#include <lexocraft/cereal_annoy_index.hpp>
#include <cereal/cereal.hpp>

#include <random>
#include <sstream>

constexpr int DIMENSIONS = 10;

Expand Down Expand Up @@ -40,18 +41,41 @@ int main() {

annoy_index.build(10);

auto bytes = annoy_index.serialize();
std::stringstream sstream;

cereal::BinaryOutputArchive oarchive {sstream};

oarchive(annoy_index);

cereal::BinaryInputArchive iarchive {sstream};

AnnoyIndex_t loaded_annoy_index;

iarchive(loaded_annoy_index);

AnnoyIndex_t annoy_index2(DIMENSIONS);
std::cout << "Loaded Annoy Index\n";

annoy_index2.deserialize(&bytes);
const std::size_t num_items = annoy_index.get_n_items();
const std::size_t num_items_loaded = loaded_annoy_index.get_n_items();

std::cout << "annoy_index.get_n_items() = " << annoy_index.get_n_items() << '\n';
std::cout << "annoy_index.get_n_trees() = " << annoy_index.get_n_trees() << '\n';
std::cout << "Number of items in original index: " << num_items << '\n';
std::cout << "Number of items in loaded index: " << num_items_loaded << '\n';

std::cout << "annoy_index2.get_n_items() = " << annoy_index2.get_n_items() << '\n';
std::cout << "annoy_index2.get_n_trees() = " << annoy_index2.get_n_trees() << '\n';
std::cout << "Original Annoy Index\n";
for (int index {}; index < 10; ++index) {
float* vector = new float [DIMENSIONS];
annoy_index.get_item(index, vector);
print_vector(vector);

delete [] vector;
}

std::cout << "(annoy_index.serialize() == annoy_index2.serialize()) = "
<< (annoy_index.serialize() == annoy_index2.serialize()) << '\n';
std::cout << "\n\nLoaded Annoy Index\n";
for (int index {}; index < 10; ++index) {
float* vector = new float [DIMENSIONS];
loaded_annoy_index.get_item(index, vector);
print_vector(vector);

delete [] vector;
}
}

0 comments on commit 5be6471

Please sign in to comment.