value_store_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/value_store.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "testing/base/test_raw_ostream.h"
  8. #include "toolchain/testing/yaml_test_helpers.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using ::testing::ElementsAre;
  12. using ::testing::Eq;
  13. using ::testing::IsEmpty;
  14. using ::testing::Not;
  15. using ::testing::Pair;
  16. TEST(ValueStore, Integer) {
  17. SharedValueStores value_stores;
  18. IntegerId id1 = value_stores.integers().Add(llvm::APInt(64, 1));
  19. IntegerId id2 = value_stores.integers().Add(llvm::APInt(64, 2));
  20. ASSERT_TRUE(id1.is_valid());
  21. ASSERT_TRUE(id2.is_valid());
  22. EXPECT_THAT(id1, Not(Eq(id2)));
  23. EXPECT_THAT(value_stores.integers().Get(id1), Eq(1));
  24. EXPECT_THAT(value_stores.integers().Get(id2), Eq(2));
  25. }
  26. TEST(ValueStore, Real) {
  27. Real real1{.mantissa = llvm::APInt(64, 1),
  28. .exponent = llvm::APInt(64, 11),
  29. .is_decimal = true};
  30. Real real2{.mantissa = llvm::APInt(64, 2),
  31. .exponent = llvm::APInt(64, 22),
  32. .is_decimal = false};
  33. SharedValueStores value_stores;
  34. RealId id1 = value_stores.reals().Add(real1);
  35. RealId id2 = value_stores.reals().Add(real2);
  36. ASSERT_TRUE(id1.is_valid());
  37. ASSERT_TRUE(id2.is_valid());
  38. EXPECT_THAT(id1, Not(Eq(id2)));
  39. const auto& real1_copy = value_stores.reals().Get(id1);
  40. EXPECT_THAT(real1.mantissa, Eq(real1_copy.mantissa));
  41. EXPECT_THAT(real1.exponent, Eq(real1_copy.exponent));
  42. EXPECT_THAT(real1.is_decimal, Eq(real1_copy.is_decimal));
  43. const auto& real2_copy = value_stores.reals().Get(id2);
  44. EXPECT_THAT(real2.mantissa, Eq(real2_copy.mantissa));
  45. EXPECT_THAT(real2.exponent, Eq(real2_copy.exponent));
  46. EXPECT_THAT(real2.is_decimal, Eq(real2_copy.is_decimal));
  47. }
  48. TEST(ValueStore, String) {
  49. std::string a = "a";
  50. std::string b = "b";
  51. SharedValueStores value_stores;
  52. auto a_id = value_stores.identifiers().Add(a);
  53. auto b_id = value_stores.string_literals().Add(b);
  54. ASSERT_TRUE(a_id.is_valid());
  55. ASSERT_TRUE(b_id.is_valid());
  56. EXPECT_THAT(a_id.index, Not(Eq(b_id.index)));
  57. EXPECT_THAT(value_stores.identifiers().Get(a_id), Eq(a));
  58. EXPECT_THAT(value_stores.string_literals().Get(b_id), Eq(b));
  59. // Adding the same string again, even with a different Id type, should return
  60. // the same id.
  61. EXPECT_THAT(value_stores.string_literals().Add(a).index, Eq(a_id.index));
  62. EXPECT_THAT(value_stores.identifiers().Add(b).index, Eq(b_id.index));
  63. }
  64. auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> integers,
  65. testing::Matcher<Yaml::MappingValue> reals,
  66. testing::Matcher<Yaml::MappingValue> strings) -> auto {
  67. return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
  68. "shared_values",
  69. Yaml::Mapping(ElementsAre(Pair("integers", Yaml::Mapping(integers)),
  70. Pair("reals", Yaml::Mapping(reals)),
  71. Pair("strings", Yaml::Mapping(strings))))))))));
  72. }
  73. TEST(ValueStore, PrintEmpty) {
  74. SharedValueStores value_stores;
  75. TestRawOstream out;
  76. value_stores.Print(out);
  77. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  78. MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty()));
  79. }
  80. TEST(ValueStore, PrintVals) {
  81. SharedValueStores value_stores;
  82. llvm::APInt apint(64, 8, /*isSigned=*/true);
  83. value_stores.integers().Add(apint);
  84. value_stores.reals().Add(
  85. Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
  86. value_stores.string_literals().Add("foo'\"baz");
  87. TestRawOstream out;
  88. value_stores.Print(out);
  89. EXPECT_THAT(
  90. Yaml::Value::FromText(out.TakeStr()),
  91. MatchSharedValues(ElementsAre(Pair("int0", Yaml::Scalar("8"))),
  92. ElementsAre(Pair("real0", Yaml::Scalar("8*10^8"))),
  93. ElementsAre(Pair("str0", Yaml::Scalar("foo'\"baz")))));
  94. }
  95. } // namespace
  96. } // namespace Carbon::Testing