value_store_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, Float) {
  49. llvm::APFloat float1(1.0);
  50. llvm::APFloat float2(2.0);
  51. SharedValueStores value_stores;
  52. FloatId id1 = value_stores.floats().Add(float1);
  53. FloatId id2 = value_stores.floats().Add(float2);
  54. ASSERT_TRUE(id1.is_valid());
  55. ASSERT_TRUE(id2.is_valid());
  56. EXPECT_THAT(id1, Not(Eq(id2)));
  57. EXPECT_THAT(value_stores.floats().Get(id1).compare(float1),
  58. Eq(llvm::APFloatBase::cmpEqual));
  59. EXPECT_THAT(value_stores.floats().Get(id2).compare(float2),
  60. Eq(llvm::APFloatBase::cmpEqual));
  61. }
  62. TEST(ValueStore, Identifiers) {
  63. std::string a = "a";
  64. std::string b = "b";
  65. SharedValueStores value_stores;
  66. auto a_id = value_stores.identifiers().Add(a);
  67. auto b_id = value_stores.identifiers().Add(b);
  68. ASSERT_TRUE(a_id.is_valid());
  69. ASSERT_TRUE(b_id.is_valid());
  70. EXPECT_THAT(a_id, Not(Eq(b_id)));
  71. EXPECT_THAT(value_stores.identifiers().Get(a_id), Eq(a));
  72. EXPECT_THAT(value_stores.identifiers().Get(b_id), Eq(b));
  73. EXPECT_THAT(value_stores.identifiers().Lookup(a), Eq(a_id));
  74. EXPECT_THAT(value_stores.identifiers().Lookup("c"),
  75. Eq(IdentifierId::Invalid));
  76. }
  77. TEST(ValueStore, StringLiterals) {
  78. std::string a = "a";
  79. std::string b = "b";
  80. SharedValueStores value_stores;
  81. auto a_id = value_stores.string_literal_values().Add(a);
  82. auto b_id = value_stores.string_literal_values().Add(b);
  83. ASSERT_TRUE(a_id.is_valid());
  84. ASSERT_TRUE(b_id.is_valid());
  85. EXPECT_THAT(a_id, Not(Eq(b_id)));
  86. EXPECT_THAT(value_stores.string_literal_values().Get(a_id), Eq(a));
  87. EXPECT_THAT(value_stores.string_literal_values().Get(b_id), Eq(b));
  88. EXPECT_THAT(value_stores.string_literal_values().Lookup(a), Eq(a_id));
  89. EXPECT_THAT(value_stores.string_literal_values().Lookup("c"),
  90. Eq(StringLiteralValueId::Invalid));
  91. }
  92. auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> ints,
  93. testing::Matcher<Yaml::MappingValue> reals,
  94. testing::Matcher<Yaml::MappingValue> identifiers,
  95. testing::Matcher<Yaml::MappingValue> strings) -> auto {
  96. return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
  97. "shared_values",
  98. Yaml::Mapping(ElementsAre(Pair("ints", Yaml::Mapping(ints)),
  99. Pair("reals", Yaml::Mapping(reals)),
  100. Pair("identifiers", Yaml::Mapping(identifiers)),
  101. Pair("strings", Yaml::Mapping(strings))))))))));
  102. }
  103. TEST(ValueStore, PrintEmpty) {
  104. SharedValueStores value_stores;
  105. TestRawOstream out;
  106. value_stores.Print(out);
  107. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  108. MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty(), IsEmpty()));
  109. }
  110. TEST(ValueStore, PrintVals) {
  111. SharedValueStores value_stores;
  112. llvm::APInt apint(64, 8, /*isSigned=*/true);
  113. value_stores.ints().Add(apint);
  114. value_stores.reals().Add(
  115. Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
  116. value_stores.identifiers().Add("a");
  117. value_stores.string_literal_values().Add("foo'\"baz");
  118. TestRawOstream out;
  119. value_stores.Print(out);
  120. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  121. MatchSharedValues(
  122. ElementsAre(Pair("int0", Yaml::Scalar("8"))),
  123. ElementsAre(Pair("real0", Yaml::Scalar("8*10^8"))),
  124. ElementsAre(Pair("identifier0", Yaml::Scalar("a"))),
  125. ElementsAre(Pair("string0", Yaml::Scalar("foo'\"baz")))));
  126. }
  127. } // namespace
  128. } // namespace Carbon::Testing