value_store_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, Int) {
  17. SharedValueStores value_stores;
  18. IntId id1 = value_stores.ints().Add(llvm::APInt(64, 1));
  19. IntId id2 = value_stores.ints().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.ints().Get(id1), Eq(1));
  24. EXPECT_THAT(value_stores.ints().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_literal_values().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_literal_values().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_literal_values().Add(a).index,
  62. Eq(a_id.index));
  63. EXPECT_THAT(value_stores.identifiers().Add(b).index, Eq(b_id.index));
  64. }
  65. auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> ints,
  66. testing::Matcher<Yaml::MappingValue> reals,
  67. testing::Matcher<Yaml::MappingValue> strings) -> auto {
  68. return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
  69. "shared_values",
  70. Yaml::Mapping(ElementsAre(Pair("ints", Yaml::Mapping(ints)),
  71. Pair("reals", Yaml::Mapping(reals)),
  72. Pair("strings", Yaml::Mapping(strings))))))))));
  73. }
  74. TEST(ValueStore, PrintEmpty) {
  75. SharedValueStores value_stores;
  76. TestRawOstream out;
  77. value_stores.Print(out);
  78. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  79. MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty()));
  80. }
  81. TEST(ValueStore, PrintVals) {
  82. SharedValueStores value_stores;
  83. llvm::APInt apint(64, 8, /*isSigned=*/true);
  84. value_stores.ints().Add(apint);
  85. value_stores.reals().Add(
  86. Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
  87. value_stores.string_literal_values().Add("foo'\"baz");
  88. TestRawOstream out;
  89. value_stores.Print(out);
  90. EXPECT_THAT(
  91. Yaml::Value::FromText(out.TakeStr()),
  92. MatchSharedValues(ElementsAre(Pair("int0", Yaml::Scalar("8"))),
  93. ElementsAre(Pair("real0", Yaml::Scalar("8*10^8"))),
  94. ElementsAre(Pair("str0", Yaml::Scalar("foo'\"baz")))));
  95. }
  96. } // namespace
  97. } // namespace Carbon::Testing