Skip to content

Commit

Permalink
optimize the implementation logic of TestSerializeReaderSet (#332)
Browse files Browse the repository at this point in the history
Signed-off-by: jinjiabao.jjb <[email protected]>
  • Loading branch information
inabao authored Jan 16, 2025
1 parent 7106bd7 commit 09fd74e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
50 changes: 50 additions & 0 deletions tests/fixtures/test_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

// Copyright 2024-present the vsag project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <string.h>

#include "vsag/binaryset.h"
#include "vsag/readerset.h"

namespace fixtures {

class TestReader : public vsag::Reader {
public:
TestReader(vsag::Binary binary) : binary_(binary) {
}

void
Read(uint64_t offset, uint64_t len, void* dest) override {
memcpy((char*)dest, binary_.data.get() + offset, len);
}

void
AsyncRead(uint64_t offset, uint64_t len, void* dest, vsag::CallBack callback) override {
Read(offset, len, dest);
callback(vsag::IOErrorCode::IO_SUCCESS, "success");
}

uint64_t
Size() const override {
return binary_.size;
}

private:
vsag::Binary binary_;
};

} // namespace fixtures
16 changes: 7 additions & 9 deletions tests/test_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "fixtures/memory_record_allocator.h"
#include "fixtures/test_logger.h"
#include "fixtures/test_reader.h"
#include "fixtures/thread_pool.h"
#include "simd/fp32_simd.h"

Expand Down Expand Up @@ -432,16 +433,13 @@ TestIndex::TestSerializeReaderSet(const IndexPtr& index_from,
const std::string& search_param,
const std::string& index_name,
bool expected_success) {
auto dir = fixtures::TempDir("serialize");
auto path = dir.GenerateRandomFile();
std::ofstream outfile(path, std::ios::out | std::ios::binary);
auto serialize_index = index_from->Serialize(outfile);
REQUIRE(serialize_index.has_value() == expected_success);
outfile.close();

vsag::ReaderSet rs;
auto reader = vsag::Factory::CreateLocalFileReader(path, 0, 0);
rs.Set(index_name, reader);
auto serialize_binary = index_from->Serialize();
REQUIRE(serialize_binary.has_value() == expected_success);
auto binary_set = serialize_binary.value();
for (const auto& key : binary_set.GetKeys()) {
rs.Set(key, std::make_shared<TestReader>(binary_set.Get(key)));
}
auto deserialize_index = index_to->Deserialize(rs);
REQUIRE(deserialize_index.has_value() == expected_success);

Expand Down

0 comments on commit 09fd74e

Please sign in to comment.