Files
carbon-lang/toolchain/base/value_store_test.cpp
T
Chandler Carruth 14e8bffe24 Refactor ValueStore to use custom iterators and ranges (#7506)
Replace the llvm::map_range based anonymous ranges returned by values()
and enumerate() with custom range and iterator types. This avoids
exposing complex template return types and improves encapsulation. But
most importantly, it is *much* cheaper to compile.

This change:
- Defines ValueStoreIterator, ValueStoreEnumerateIterator, and
ValueStoreRange templates in the Internal namespace.
- Exposes them via aliases in ValueStore: Iterator, ConstIterator,
Range, MutableRange, and EnumerateRange.
- Supports safe implicit conversion from Iterator to ConstIterator.
- Supports operator-> even when the value type is returned by value
(e.g., llvm::StringRef) by conditionally const-qualifying the pointer
type.
- Ensures C++20 comparison consistency with custom operator<=> and
operator== definitions.
- Restricts construction of these iterators and ranges to ValueStore
methods by using private constructors and friend declarations.

Assisted-by: Antigravity with Gemini
2026-07-15 17:06:52 +00:00

72 lines
2.0 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 <string>
#include "llvm/ADT/StringRef.h"
#include "toolchain/base/value_ids.h"
namespace Carbon::Testing {
namespace {
using ::testing::Eq;
using ::testing::Not;
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};
ValueStore<RealId, Real> reals;
RealId id1 = reals.Add(real1);
RealId id2 = reals.Add(real2);
ASSERT_TRUE(id1.has_value());
ASSERT_TRUE(id2.has_value());
EXPECT_THAT(id1, Not(Eq(id2)));
const auto& real1_copy = 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 = 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, StringRefIteratorArrow) {
ValueStore<IdentifierId, llvm::StringRef> store;
store.Add("hello");
// Non-const values() returns MutableRange which uses Iterator
auto mutable_range = store.values();
auto it = mutable_range.begin();
// This calls Iterator::operator->
EXPECT_EQ(it->size(), 5);
}
TEST(ValueStore, IteratorConversion) {
ValueStore<IdentifierId, llvm::StringRef> store;
store.Add("hello");
auto mutable_range = store.values();
ValueStore<IdentifierId, llvm::StringRef>::Iterator it =
mutable_range.begin();
ValueStore<IdentifierId, llvm::StringRef>::ConstIterator cit = it;
EXPECT_EQ(*cit, "hello");
}
} // namespace
} // namespace Carbon::Testing