Files
carbon-lang/explorer/common/decompose_test.cpp
T
c93a0e5e42 Implement canonicalization of Value and Element (#3024)
See arena.h for discussion of what canonicalization means in this
context. This is primarily intended to support implementing a memo table
of template instantiations to resolve #2951, but could be useful for
other purposes as well.

Additional changes:
- Pass `VTable` constructor parameters by pointer, to avoid the need to
define `operator==` and `hash_value` for it.
- Clean up the recurring pattern of allocating identical `NamedElement`s
on the stack and heap. Instead we always allocate it on the heap and
pass it by pointer.
- Add `Print()` and `Dump()` to `Bindings` as a debugging convenience.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-08-03 16:05:59 +00:00

40 lines
931 B
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 "explorer/common/decompose.h"
#include <gtest/gtest.h>
namespace Carbon {
namespace {
struct Decomposeable : public HashFromDecompose<Decomposeable> {
template <typename F>
auto Decompose(F f) const {
return f(i, s);
}
int i = 0;
std::string s;
};
TEST(HashFromDecomposeTest, EqualValues) {
Decomposeable d1 = {.i = 42, .s = "foo"};
Decomposeable d2 = {.i = 42, .s = "foo"};
EXPECT_TRUE(d1 == d2);
EXPECT_TRUE(hash_value(d1) == hash_value(d2));
}
TEST(HashFromDecomposeTest, NonEqualValues) {
Decomposeable d1 = {.i = 42, .s = "foo"};
Decomposeable d2 = {.i = 42, .s = "bar"};
EXPECT_FALSE(d1 == d2);
EXPECT_FALSE(hash_value(d1) == hash_value(d2));
}
} // namespace
} // namespace Carbon