Files
carbon-lang/toolchain/base/value_store_test.cpp
T
Jon Ross-Perkins 3af7eb2672 Refactor YAML handling to use the llvm::yaml API. (#3337)
Provides an adapter for the llvm::yaml API because it otherwise needs a
bunch of const/non-const definitions, and the traits are difficult to
diagnose issues with. The current approach is pretty simple to use, even
if it's not super efficient (which, yaml output is more of a debugging
thing so I'm not really expecting it to be an issue).

Changes the format of yaml output to provide more index information,
just as reminders when seeing something like `node+0`. Note this would
create more churn in deltas if we were reliant on the output yaml in
tests, but we aren't so it should be okay.
2023-10-26 18:50:30 +00:00

118 lines
3.9 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/base/value_store.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "testing/base/test_raw_ostream.h"
#include "toolchain/testing/yaml_test_helpers.h"
namespace Carbon::Testing {
namespace {
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::Not;
using ::testing::Pair;
TEST(ValueStore, Integer) {
SharedValueStores value_stores;
IntegerId id1 = value_stores.integers().Add(llvm::APInt(64, 1));
IntegerId id2 = value_stores.integers().Add(llvm::APInt(64, 2));
ASSERT_TRUE(id1.is_valid());
ASSERT_TRUE(id2.is_valid());
EXPECT_THAT(id1, Not(Eq(id2)));
EXPECT_THAT(value_stores.integers().Get(id1), Eq(1));
EXPECT_THAT(value_stores.integers().Get(id2), Eq(2));
}
TEST(ValueStore, Real) {
Real real1{.mantissa = llvm::APInt(64, 1),
.exponent = llvm::APInt(64, 11),
.is_decimal = true};
Real real2{.mantissa = llvm::APInt(64, 2),
.exponent = llvm::APInt(64, 22),
.is_decimal = false};
SharedValueStores value_stores;
RealId id1 = value_stores.reals().Add(real1);
RealId id2 = value_stores.reals().Add(real2);
ASSERT_TRUE(id1.is_valid());
ASSERT_TRUE(id2.is_valid());
EXPECT_THAT(id1, Not(Eq(id2)));
const auto& real1_copy = value_stores.reals().Get(id1);
EXPECT_THAT(real1.mantissa, Eq(real1_copy.mantissa));
EXPECT_THAT(real1.exponent, Eq(real1_copy.exponent));
EXPECT_THAT(real1.is_decimal, Eq(real1_copy.is_decimal));
const auto& real2_copy = value_stores.reals().Get(id2);
EXPECT_THAT(real2.mantissa, Eq(real2_copy.mantissa));
EXPECT_THAT(real2.exponent, Eq(real2_copy.exponent));
EXPECT_THAT(real2.is_decimal, Eq(real2_copy.is_decimal));
}
TEST(ValueStore, String) {
std::string a = "a";
std::string b = "b";
SharedValueStores value_stores;
StringId a_id = value_stores.strings().Add(a);
StringId b_id = value_stores.strings().Add(b);
ASSERT_TRUE(a_id.is_valid());
ASSERT_TRUE(b_id.is_valid());
EXPECT_THAT(a_id, Not(Eq(b_id)));
EXPECT_THAT(value_stores.strings().Get(a_id), Eq(a));
EXPECT_THAT(value_stores.strings().Get(b_id), Eq(b));
// Adding the same string again should return the same id.
EXPECT_THAT(value_stores.strings().Add(a), Eq(a_id));
EXPECT_THAT(value_stores.strings().Add(b), Eq(b_id));
}
auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> integers,
testing::Matcher<Yaml::MappingValue> reals,
testing::Matcher<Yaml::MappingValue> strings) -> auto {
return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
"shared_values",
Yaml::Mapping(ElementsAre(Pair("integers", Yaml::Mapping(integers)),
Pair("reals", Yaml::Mapping(reals)),
Pair("strings", Yaml::Mapping(strings))))))))));
}
TEST(ValueStore, PrintEmpty) {
SharedValueStores value_stores;
TestRawOstream out;
value_stores.Print(out);
EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty()));
}
TEST(ValueStore, PrintVals) {
SharedValueStores value_stores;
llvm::APInt apint(64, 8, /*isSigned=*/true);
value_stores.integers().Add(apint);
value_stores.reals().Add(
Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
value_stores.strings().Add("foo'\"baz");
TestRawOstream out;
value_stores.Print(out);
EXPECT_THAT(
Yaml::Value::FromText(out.TakeStr()),
MatchSharedValues(ElementsAre(Pair("int0", Yaml::Scalar("8"))),
ElementsAre(Pair("real0", Yaml::Scalar("8*10^8"))),
ElementsAre(Pair("str0", Yaml::Scalar("foo'\"baz")))));
}
} // namespace
} // namespace Carbon::Testing