canonical_value_store_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/base/canonical_value_store.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <string>
  8. #include "llvm/ADT/APFloat.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "toolchain/base/value_ids.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::Eq;
  14. using ::testing::Not;
  15. TEST(CanonicalValueStore, Float) {
  16. llvm::APFloat float1(1.0);
  17. llvm::APFloat float2(2.0);
  18. CanonicalValueStore<FloatId, llvm::APFloat> floats;
  19. FloatId id1 = floats.Add(float1);
  20. FloatId id2 = floats.Add(float2);
  21. ASSERT_TRUE(id1.has_value());
  22. ASSERT_TRUE(id2.has_value());
  23. EXPECT_THAT(id1, Not(Eq(id2)));
  24. EXPECT_THAT(floats.Get(id1).compare(float1), Eq(llvm::APFloatBase::cmpEqual));
  25. EXPECT_THAT(floats.Get(id2).compare(float2), Eq(llvm::APFloatBase::cmpEqual));
  26. }
  27. TEST(CanonicalValueStore, Identifiers) {
  28. std::string a = "a";
  29. std::string b = "b";
  30. CanonicalValueStore<IdentifierId, llvm::StringRef> identifiers;
  31. // Make sure reserve works, we use it with identifiers.
  32. identifiers.Reserve(100);
  33. auto a_id = identifiers.Add(a);
  34. auto b_id = identifiers.Add(b);
  35. ASSERT_TRUE(a_id.has_value());
  36. ASSERT_TRUE(b_id.has_value());
  37. EXPECT_THAT(a_id, Not(Eq(b_id)));
  38. EXPECT_THAT(identifiers.Get(a_id), Eq(a));
  39. EXPECT_THAT(identifiers.Get(b_id), Eq(b));
  40. EXPECT_THAT(identifiers.Lookup(a), Eq(a_id));
  41. EXPECT_THAT(identifiers.Lookup("c"), Eq(IdentifierId::None));
  42. }
  43. TEST(CanonicalValueStore, StringLiterals) {
  44. std::string a = "a";
  45. std::string b = "b";
  46. CanonicalValueStore<StringLiteralValueId, llvm::StringRef> string_literals;
  47. auto a_id = string_literals.Add(a);
  48. auto b_id = string_literals.Add(b);
  49. ASSERT_TRUE(a_id.has_value());
  50. ASSERT_TRUE(b_id.has_value());
  51. EXPECT_THAT(a_id, Not(Eq(b_id)));
  52. EXPECT_THAT(string_literals.Get(a_id), Eq(a));
  53. EXPECT_THAT(string_literals.Get(b_id), Eq(b));
  54. EXPECT_THAT(string_literals.Lookup(a), Eq(a_id));
  55. EXPECT_THAT(string_literals.Lookup("c"), Eq(StringLiteralValueId::None));
  56. }
  57. } // namespace
  58. } // namespace Carbon::Testing