shared_value_stores_test.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/shared_value_stores.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/raw_string_ostream.h"
  8. #include "toolchain/testing/yaml_test_helpers.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using ::testing::ElementsAre;
  12. using ::testing::IsEmpty;
  13. using ::testing::Pair;
  14. auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> ints,
  15. testing::Matcher<Yaml::MappingValue> reals,
  16. testing::Matcher<Yaml::MappingValue> floats,
  17. testing::Matcher<Yaml::MappingValue> identifiers,
  18. testing::Matcher<Yaml::MappingValue> strings) -> auto {
  19. return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
  20. "shared_values",
  21. Yaml::Mapping(ElementsAre(Pair("ints", Yaml::Mapping(ints)),
  22. Pair("reals", Yaml::Mapping(reals)),
  23. Pair("floats", Yaml::Mapping(floats)),
  24. Pair("identifiers", Yaml::Mapping(identifiers)),
  25. Pair("strings", Yaml::Mapping(strings))))))))));
  26. }
  27. TEST(SharedValueStores, PrintEmpty) {
  28. SharedValueStores value_stores;
  29. RawStringOstream out;
  30. value_stores.Print(out);
  31. EXPECT_THAT(
  32. Yaml::Value::FromText(out.TakeStr()),
  33. MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty(), IsEmpty(), IsEmpty()));
  34. }
  35. TEST(SharedValueStores, PrintVals) {
  36. SharedValueStores value_stores;
  37. llvm::APInt apint(64, 8, /*isSigned=*/true);
  38. value_stores.ints().AddSigned(apint);
  39. value_stores.ints().AddSigned(llvm::APInt(64, 999'999'999'999));
  40. value_stores.reals().Add(
  41. Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
  42. value_stores.identifiers().Add("a");
  43. value_stores.string_literal_values().Add("foo'\"baz");
  44. RawStringOstream out;
  45. value_stores.Print(out);
  46. EXPECT_THAT(
  47. Yaml::Value::FromText(out.TakeStr()),
  48. MatchSharedValues(
  49. ElementsAre(Pair("ap_int00000000", Yaml::Scalar("999999999999"))),
  50. ElementsAre(Pair("real00000000", Yaml::Scalar("8*10^8"))), IsEmpty(),
  51. ElementsAre(Pair("identifier00000000", Yaml::Scalar("a"))),
  52. ElementsAre(Pair("string00000000", Yaml::Scalar("foo'\"baz")))));
  53. }
  54. } // namespace
  55. } // namespace Carbon::Testing