testCTestResourceGroups.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include "cmCTestTestHandler.h"
  7. struct ExpectedParseResult
  8. {
  9. std::string String;
  10. bool ExpectedReturnValue;
  11. std::vector<std::vector<cmCTestTestHandler::cmCTestTestResourceRequirement>>
  12. ExpectedValue;
  13. };
  14. static const std::vector<ExpectedParseResult> expectedResults{
  15. /* clang-format off */
  16. { "threads:2", true, {
  17. { { "threads", 2, 1 } },
  18. } },
  19. { "3,threads:2", true, {
  20. { { "threads", 2, 1 } },
  21. { { "threads", 2, 1 } },
  22. { { "threads", 2, 1 } },
  23. } },
  24. { "3,threads:2,gpus:4", true, {
  25. { { "threads", 2, 1 }, { "gpus", 4, 1 } },
  26. { { "threads", 2, 1 }, { "gpus", 4, 1 } },
  27. { { "threads", 2, 1 }, { "gpus", 4, 1 } },
  28. } },
  29. { "2,threads:2;gpus:4", true, {
  30. { { "threads", 2, 1 } },
  31. { { "threads", 2, 1 } },
  32. { { "gpus", 4, 1 } },
  33. } },
  34. { "threads:2;2,gpus:4", true, {
  35. { { "threads", 2, 1 } },
  36. { { "gpus", 4, 1 } },
  37. { { "gpus", 4, 1 } },
  38. } },
  39. { "threads:2;gpus:4", true, {
  40. { { "threads", 2, 1 } },
  41. { { "gpus", 4, 1 } },
  42. } },
  43. { "1,threads:2;0,gpus:4", true, {
  44. { { "threads", 2, 1 } },
  45. } },
  46. { "1,_:1", true, {
  47. { { "_", 1, 1 } },
  48. } },
  49. { "1,a:1", true, {
  50. { { "a", 1, 1 } },
  51. } },
  52. { "2", true, {
  53. {},
  54. {},
  55. } },
  56. { "1;2,threads:1", true, {
  57. {},
  58. { { "threads", 1, 1 } },
  59. { { "threads", 1, 1 } },
  60. } },
  61. { "1,,threads:1", true, {
  62. { { "threads", 1, 1 } },
  63. } },
  64. { ";1,threads:1", true, {
  65. { { "threads", 1, 1 } },
  66. } },
  67. { "1,threads:1;", true, {
  68. { { "threads", 1, 1 } },
  69. } },
  70. { "1,threads:1,", true, {
  71. { { "threads", 1, 1 } },
  72. } },
  73. { "threads:1;;threads:2", true, {
  74. { { "threads", 1, 1 } },
  75. { { "threads", 2, 1 } },
  76. } },
  77. { "1,", true, {
  78. {},
  79. } },
  80. { ";", true, {} },
  81. { "", true, {} },
  82. { ",", false, {} },
  83. { "1,0:1", false, {} },
  84. { "1,A:1", false, {} },
  85. { "1,a-b:1", false, {} },
  86. { "invalid", false, {} },
  87. { ",1,invalid:1", false, {} },
  88. { "1,1", false, {} },
  89. { "-1,invalid:1", false, {} },
  90. { "1,invalid:*", false, {} },
  91. { "1,invalid:-1", false, {} },
  92. { "1,invalid:-", false, {} },
  93. { "1,invalid:ab2", false, {} },
  94. { "1,invalid :2", false, {} },
  95. { "1, invalid:2", false, {} },
  96. { "1,invalid:ab", false, {} },
  97. /* clang-format on */
  98. };
  99. bool TestExpectedParseResult(const ExpectedParseResult& expected)
  100. {
  101. std::vector<std::vector<cmCTestTestHandler::cmCTestTestResourceRequirement>>
  102. result;
  103. bool retval;
  104. if ((retval = cmCTestTestHandler::ParseResourceGroupsProperty(
  105. expected.String, result)) != expected.ExpectedReturnValue) {
  106. std::cout << "ParseResourceGroupsProperty(\"" << expected.String
  107. << "\") returned " << retval << ", should be "
  108. << expected.ExpectedReturnValue << std::endl;
  109. return false;
  110. }
  111. if (result != expected.ExpectedValue) {
  112. std::cout << "ParseResourceGroupsProperty(\"" << expected.String
  113. << "\") did not yield expected set of resource groups"
  114. << std::endl;
  115. return false;
  116. }
  117. return true;
  118. }
  119. int testCTestResourceGroups(int /*unused*/, char* /*unused*/ [])
  120. {
  121. int retval = 0;
  122. for (auto const& expected : expectedResults) {
  123. if (!TestExpectedParseResult(expected)) {
  124. retval = 1;
  125. }
  126. }
  127. return retval;
  128. }