ids_test.cpp 4.7 KB

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