78 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/CAS/OnDiskKeyValueDB.h"10#include "CASTestConfig.h"11#include "OnDiskCommonUtils.h"12#include "llvm/Testing/Support/Error.h"13#include "llvm/Testing/Support/SupportHelpers.h"14#include "gtest/gtest.h"15 16using namespace llvm;17using namespace llvm::cas;18using namespace llvm::cas::ondisk;19using namespace llvm::unittest::cas;20 21TEST_F(OnDiskCASTest, OnDiskKeyValueDBTest) {22 unittest::TempDir Temp("ondiskkv", /*Unique=*/true);23 std::unique_ptr<OnDiskKeyValueDB> DB;24 ASSERT_THAT_ERROR(OnDiskKeyValueDB::open(Temp.path(), "blake3",25 sizeof(HashType), "test",26 sizeof(ValueType))27 .moveInto(DB),28 Succeeded());29 {30 std::optional<ArrayRef<char>> Val;31 ASSERT_THAT_ERROR(DB->get(digest("hello")).moveInto(Val), Succeeded());32 EXPECT_FALSE(Val.has_value());33 }34 35 ValueType ValW = valueFromString("world");36 std::optional<ArrayRef<char>> Val;37 ASSERT_THAT_ERROR(DB->put(digest("hello"), ValW).moveInto(Val), Succeeded());38 EXPECT_EQ(*Val, ArrayRef(ValW));39 ASSERT_THAT_ERROR(40 DB->put(digest("hello"), valueFromString("other")).moveInto(Val),41 Succeeded());42 EXPECT_EQ(*Val, ArrayRef(ValW));43 44 {45 std::optional<ArrayRef<char>> Val;46 ASSERT_THAT_ERROR(DB->get(digest("hello")).moveInto(Val), Succeeded());47 EXPECT_TRUE(Val.has_value());48 EXPECT_EQ(*Val, ArrayRef(ValW));49 }50 51 // Validate52 {53 auto ValidateFunc = [](FileOffset Offset, ArrayRef<char> Data) -> Error {54 EXPECT_EQ(Data.size(), sizeof(ValueType));55 return Error::success();56 };57 ASSERT_THAT_ERROR(DB->validate(ValidateFunc), Succeeded());58 }59 60 // Size61 {62 size_t InitSize = DB->getStorageSize();63 unsigned InitPrecent = DB->getHardStorageLimitUtilization();64 65 // Insert a lot of entries.66 for (unsigned I = 0; I < 1024 * 100; ++I) {67 std::string Index = Twine(I).str();68 std::optional<ArrayRef<char>> Val;69 ASSERT_THAT_ERROR(70 DB->put(digest(Index), valueFromString(Index)).moveInto(Val),71 Succeeded());72 }73 74 EXPECT_GT(DB->getStorageSize(), InitSize);75 EXPECT_GT(DB->getHardStorageLimitUtilization(), InitPrecent);76 }77}78