ids_test.cpp 4.7 KB

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