ids_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/sem_ir/ids.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <limits>
  8. #include <tuple>
  9. namespace Carbon::SemIR {
  10. namespace {
  11. using ::testing::Eq;
  12. TEST(IdsTest, LocIdValues) {
  13. // This testing should match the ranges documented on LocId.
  14. EXPECT_THAT(static_cast<LocId>(Parse::NodeId::None).index, Eq(-1));
  15. EXPECT_THAT(static_cast<LocId>(InstId(0)).index, Eq(0));
  16. EXPECT_THAT(
  17. static_cast<LocId>(InstId(std::numeric_limits<int32_t>::max())).index,
  18. Eq(std::numeric_limits<int32_t>::max()));
  19. auto min_node_id = static_cast<LocId>(Parse::NodeId(0));
  20. EXPECT_THAT(min_node_id.index, Eq(-2));
  21. EXPECT_THAT(min_node_id.AsDesugared().index, Eq(-2 - (1 << 24)));
  22. auto max_node_id = static_cast<LocId>(Parse::NodeId(Parse::NodeId::Max - 1));
  23. EXPECT_THAT(max_node_id.index, Eq(-2 - (1 << 24) + 1));
  24. EXPECT_THAT(max_node_id.AsDesugared().index, Eq(-2 - (1 << 25) + 1));
  25. EXPECT_THAT(static_cast<LocId>(ImportIRInstId(0)).index, Eq(-2 - (1 << 25)));
  26. EXPECT_THAT(static_cast<LocId>(ImportIRInstId(ImportIRInstId::Max - 1)).index,
  27. Eq(std::numeric_limits<int32_t>::min()));
  28. }
  29. // A standard parameterized test for (is_desugared, index).
  30. class IdsTestWithParam
  31. : public testing::TestWithParam<std::tuple<bool, int32_t>> {
  32. public:
  33. explicit IdsTestWithParam() {
  34. llvm::errs() << "is_desugared=" << is_desugared() << ", index=" << index()
  35. << "\n";
  36. }
  37. // Returns IdT with its matching LocId form. Sets flags based on test
  38. // parameters.
  39. template <typename IdT>
  40. auto BuildIdAndLocId() -> std::pair<IdT, LocId> {
  41. IdT id(index());
  42. if (is_desugared()) {
  43. return {id, LocId(id).AsDesugared()};
  44. } else {
  45. return {id, LocId(id)};
  46. }
  47. }
  48. auto is_desugared() -> bool { return std::get<0>(GetParam()); }
  49. auto index() -> int32_t { return std::get<1>(GetParam()); }
  50. };
  51. // Returns a test case generator for edge-case values.
  52. static auto GetValueRange(int32_t max) -> auto {
  53. return testing::Values(0, 1, max - 2, max - 1);
  54. }
  55. // Returns a test case generator for `IdsTestWithParam` uses.
  56. static auto CombineWithFlags(auto value_range) -> auto {
  57. return testing::Combine(testing::Bool(), value_range);
  58. }
  59. class LocIdAsNoneTestWithParam : public IdsTestWithParam {};
  60. INSTANTIATE_TEST_SUITE_P(
  61. LocIdAsNoneTest, LocIdAsNoneTestWithParam,
  62. CombineWithFlags(testing::Values(Parse::NodeId::NoneIndex)));
  63. TEST_P(LocIdAsNoneTestWithParam, Test) {
  64. auto [_, loc_id] = BuildIdAndLocId<Parse::NodeId>();
  65. EXPECT_FALSE(loc_id.has_value());
  66. EXPECT_THAT(loc_id.kind(), Eq(LocId::Kind::None));
  67. EXPECT_FALSE(loc_id.is_desugared());
  68. EXPECT_THAT(loc_id.import_ir_inst_id(), Eq(ImportIRInstId::None));
  69. EXPECT_THAT(loc_id.inst_id(), Eq(InstId::None));
  70. EXPECT_THAT(loc_id.node_id(),
  71. // The actual type is NoneNodeId, so cast to NodeId.
  72. Eq<Parse::NodeId>(Parse::NodeId::None));
  73. }
  74. class LocIdAsImportIRInstIdTest : public IdsTestWithParam {};
  75. INSTANTIATE_TEST_SUITE_P(Test, LocIdAsImportIRInstIdTest,
  76. CombineWithFlags(GetValueRange(ImportIRInstId::Max)));
  77. TEST_P(LocIdAsImportIRInstIdTest, Test) {
  78. auto [import_ir_inst_id, loc_id] = BuildIdAndLocId<ImportIRInstId>();
  79. EXPECT_TRUE(loc_id.has_value());
  80. ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::ImportIRInstId));
  81. EXPECT_THAT(loc_id.import_ir_inst_id(), import_ir_inst_id);
  82. EXPECT_FALSE(loc_id.is_desugared());
  83. }
  84. class LocIdAsInstIdTest : public IdsTestWithParam {};
  85. INSTANTIATE_TEST_SUITE_P(
  86. Test, LocIdAsInstIdTest,
  87. testing::Combine(testing::Values(false),
  88. GetValueRange(std::numeric_limits<int32_t>::max())));
  89. TEST_P(LocIdAsInstIdTest, Test) {
  90. auto [inst_id, loc_id] = BuildIdAndLocId<InstId>();
  91. EXPECT_TRUE(loc_id.has_value());
  92. ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::InstId));
  93. EXPECT_THAT(loc_id.inst_id(), inst_id);
  94. // Note that `is_desugared` is invalid to use with `InstId`.
  95. }
  96. class LocIdAsNodeIdTest : public IdsTestWithParam {};
  97. INSTANTIATE_TEST_SUITE_P(Test, LocIdAsNodeIdTest,
  98. CombineWithFlags(GetValueRange(Parse::NodeId::Max)));
  99. TEST_P(LocIdAsNodeIdTest, Test) {
  100. auto [node_id, loc_id] = BuildIdAndLocId<Parse::NodeId>();
  101. EXPECT_TRUE(loc_id.has_value());
  102. ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::NodeId));
  103. EXPECT_THAT(loc_id.node_id(), node_id);
  104. EXPECT_THAT(loc_id.is_desugared(), Eq(is_desugared()));
  105. }
  106. } // namespace
  107. } // namespace Carbon::SemIR