You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.1 KiB

2 years ago
  1. /*****************************************************************************
  2. *
  3. * CLIPP - command line interfaces for modern C++
  4. *
  5. * released under MIT license
  6. *
  7. * (c) 2017-2018 André Müller; foss@andremueller-online.de
  8. *
  9. *****************************************************************************/
  10. #include "testing.h"
  11. #include <cmath>
  12. //-------------------------------------------------------------------
  13. struct active {
  14. active() = default;
  15. explicit
  16. active(std::initializer_list<double> il): xs{il} {}
  17. std::vector<double> xs;
  18. friend bool operator == (const active& a, const active& b) noexcept {
  19. return std::equal(begin(a.xs), end(a.xs), begin(b.xs),
  20. [](double x, double y) {
  21. return std::abs(x - y) < 1e-5;
  22. });
  23. }
  24. };
  25. //-------------------------------------------------------------------
  26. void test(int lineNo,
  27. const std::initializer_list<const char*> args,
  28. const active& matches)
  29. {
  30. using namespace clipp;
  31. active m;
  32. auto cli = joinable( repeatable(
  33. option(",") , opt_number("number", m.xs)
  34. ) );
  35. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  36. [&]{ m = active{}; },
  37. [&]{ return m == matches; });
  38. }
  39. //-------------------------------------------------------------------
  40. int main()
  41. {
  42. try {
  43. test(__LINE__, {""}, active{});
  44. test(__LINE__, {"1" }, active{1});
  45. test(__LINE__, {"1", "2" }, active{1,2});
  46. test(__LINE__, {"1", "2", "3"}, active{1,2,3});
  47. test(__LINE__, {"1.1" }, active{1.1});
  48. test(__LINE__, {"1.1", "2.2" }, active{1.1,2.2});
  49. test(__LINE__, {"1.1", "2.2", "3.3"}, active{1.1,2.2,3.3});
  50. test(__LINE__, {"1" }, active{1});
  51. test(__LINE__, {"1", ",", "2" }, active{1,2});
  52. test(__LINE__, {"1", ",", "2", ",", "3"}, active{1,2,3});
  53. test(__LINE__, {"1.1" }, active{1.1});
  54. test(__LINE__, {"1.1", ",", "2.2" }, active{1.1,2.2});
  55. test(__LINE__, {"1.1", ",", "2.2", ",", "3.3"}, active{1.1,2.2,3.3});
  56. test(__LINE__, {"1," }, active{1});
  57. test(__LINE__, {"1,", "2" }, active{1,2});
  58. test(__LINE__, {"1,", "2", "3"}, active{1,2,3});
  59. test(__LINE__, {"1.1," }, active{1.1});
  60. test(__LINE__, {"1.1,", "2.2" }, active{1.1,2.2});
  61. test(__LINE__, {"1.1,", "2.2", "3.3"}, active{1.1,2.2,3.3});
  62. test(__LINE__, {"1," }, active{1});
  63. test(__LINE__, {"1,", "2," }, active{1,2});
  64. test(__LINE__, {"1,", "2,", "3"}, active{1,2,3});
  65. test(__LINE__, {"1.1," }, active{1.1});
  66. test(__LINE__, {"1.1,", "2.2," }, active{1.1,2.2});
  67. test(__LINE__, {"1.1,", "2.2,", "3.3"}, active{1.1,2.2,3.3});
  68. test(__LINE__, {"1," }, active{1});
  69. test(__LINE__, {"1,", "2," }, active{1,2});
  70. test(__LINE__, {"1,", "2,", "3,"}, active{1,2,3});
  71. test(__LINE__, {"1.1," }, active{1.1});
  72. test(__LINE__, {"1.1,", "2.2," }, active{1.1,2.2});
  73. test(__LINE__, {"1.1,", "2.2,", "3.3,"}, active{1.1,2.2,3.3});
  74. test(__LINE__, {"1" }, active{1});
  75. test(__LINE__, {"1", ",2" }, active{1,2});
  76. test(__LINE__, {"1", ",2", "3"}, active{1,2,3});
  77. test(__LINE__, {"1.1" }, active{1.1});
  78. test(__LINE__, {"1.1", ",2.2" }, active{1.1,2.2});
  79. test(__LINE__, {"1.1", ",2.2", "3.3"}, active{1.1,2.2,3.3});
  80. test(__LINE__, {"1" }, active{1});
  81. test(__LINE__, {"1", ",2" }, active{1,2});
  82. test(__LINE__, {"1", ",2", ",3"}, active{1,2,3});
  83. test(__LINE__, {"1.1" }, active{1.1});
  84. test(__LINE__, {"1.1", ",2.2" }, active{1.1,2.2});
  85. test(__LINE__, {"1.1", ",2.2", ",3.3"}, active{1.1,2.2,3.3});
  86. test(__LINE__, {"1" }, active{1});
  87. test(__LINE__, {"1,2", "3"}, active{1,2,3});
  88. test(__LINE__, {"1.1,2.2" }, active{1.1,2.2});
  89. test(__LINE__, {"1.1", "2.2", "3.3"}, active{1.1,2.2,3.3});
  90. test(__LINE__, {"1", "2,3"}, active{1,2,3});
  91. test(__LINE__, {"1.1" }, active{1.1});
  92. test(__LINE__, {"1.1", "2.2" }, active{1.1,2.2});
  93. test(__LINE__, {"1.1", "2.2,3.3"}, active{1.1,2.2,3.3});
  94. test(__LINE__, {"1" }, active{1});
  95. test(__LINE__, {"1,2" }, active{1,2});
  96. test(__LINE__, {"1,2,3"}, active{1,2,3});
  97. test(__LINE__, {"1.1" }, active{1.1});
  98. test(__LINE__, {"1.1,2.2" }, active{1.1,2.2});
  99. test(__LINE__, {"1.1,2.2,3.3"}, active{1.1,2.2,3.3});
  100. test(__LINE__, {"1", "2,3", "4", ",", "5", "6,7,8"}, active{1,2,3,4,5,6,7,8});
  101. test(__LINE__, {"1", "2.2,3", "4.4", ",", "5.5", "6,7,8"}, active{1,2.2,3,4.4,5.5,6,7,8});
  102. test(__LINE__, {","}, active{});
  103. test(__LINE__, {"1", ","}, active{1});
  104. test(__LINE__, {"1", ",", "2", ","}, active{1,2});
  105. }
  106. catch(std::exception& e) {
  107. std::cerr << e.what() << std::endl;
  108. return 1;
  109. }
  110. }