test-opts.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. TEST(opts) {
  2. suppressEnsureNoUnusedOpts();
  3. {
  4. const char* args[] = {"test"};
  5. prepareOpts(sizeof(args) / sizeof(const char*), (char **)args);
  6. ensure(!has_opt("a"));
  7. ensure_exit(3, [](){ opt<int>(1);});
  8. }
  9. {
  10. const char* args[] = {"test", "-a", "-2147483648", "-f1", "0", "-f2", "1", "-f3", "true", "-f4", "false", "--test=me=me", "--d=-17.125", "--ex=3e6"};
  11. prepareOpts(sizeof(args) / sizeof(const char*), (char **)args);
  12. ensure(has_opt("a"));
  13. ensure(has_opt("f1"));
  14. ensure(has_opt("f2"));
  15. ensure(has_opt("f3"));
  16. ensure(has_opt("f4"));
  17. ensure(has_opt("test"));
  18. ensure(has_opt("d"));
  19. ensure(has_opt("ex"));
  20. ensure(!has_opt("f"));
  21. ensure(!has_opt("f5"));
  22. ensure(!has_opt("true"));
  23. ensure(!has_opt("0"));
  24. ensure(!has_opt("1"));
  25. ensure(!has_opt("-a"));
  26. ensure(!has_opt("--test"));
  27. ensure(opt<int>("a") == INT_MIN);
  28. ensure(!opt<bool>("f1"));
  29. ensure(opt<bool>("f2"));
  30. ensure(opt<bool>("f3"));
  31. ensure(!opt<bool>("f4"));
  32. ensure(opt<string>("test") == "me=me");
  33. ensure(opt<double>("d") == -17.125);
  34. ensure(opt<int>("ex") == 3000000);
  35. ensure(opt<long long>("ex") == 3000000);
  36. ensure_exit(3, [](){opt<short>("ex");});
  37. ensure(opt<int>(2) == INT_MIN);
  38. ensure(opt<string>(13) == "--ex=3e6");
  39. ensure_exit(3, [](){ opt<string>(-1);});
  40. ensure("test" == opt<string>(0));
  41. ensure_exit(3, [](){ opt<bool>(2);});
  42. ensure_exit(3, [](){ opt<int>(3);});
  43. ensure_exit(3, [](){ opt<int>(14);});
  44. ensure_exit(3, [](){ opt<unsigned long long>("a");});
  45. }
  46. {
  47. // Testing opt with default value and unused opt.
  48. // The following function are opts of the generators/gen-array-with-opt.cpp
  49. // Test is conducted by throwing arguments to this function.
  50. struct GenArrayOpts {
  51. int test_count;
  52. int sum_n, min_n;
  53. int min_value, max_value, value_bias;
  54. };
  55. auto gen_array_with_opt_parse_opt = [](const string& opts) {
  56. {
  57. auto parts = split(opts, ' ');
  58. vector<const char*> opts(parts.size() + 1);
  59. opts[0] = "./gen-array-with-opt";
  60. for (size_t i = 0; i < parts.size(); ++i) {
  61. opts[i + 1] = parts[i].c_str();
  62. }
  63. prepareOpts(int(opts.size()), (char**)opts.data());
  64. }
  65. {
  66. GenArrayOpts res;
  67. res.test_count = opt<int>("test-count");
  68. res.sum_n = opt<int>("sum-n");
  69. res.min_n = opt<int>("min-n", 1);
  70. res.min_value = opt<int>("min-value", 1);
  71. res.max_value = opt<int>("max-value", 1000 * 1000 * 1000);
  72. res.value_bias = opt<int>("value-bias", 0);
  73. ensureNoUnusedOpts();
  74. return res;
  75. }
  76. };
  77. // missing test-count
  78. ensure_exit(3, [&] { gen_array_with_opt_parse_opt(""); });
  79. // missing sum-n
  80. ensure_exit(3, [&]{ gen_array_with_opt_parse_opt("-test-count 3"); });
  81. // min-value, not min-val
  82. ensure_exit(3, [&]{ gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10 -min-val 1"); } );
  83. // max-value, not max-val
  84. ensure_exit(3, [&]{ gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10 -min-value 1 -max-val 20"); } );
  85. // min-n, not min-length
  86. ensure_exit(3, [&]{ gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10 -min-length 3 -min-value 10 -max-value 20"); });
  87. // value-bias, not bias-value
  88. ensure_exit(3, [&]{ gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10 -min-n 3 -min-value 10 -max-value 20 -bias-value 3"); });
  89. // OK cases
  90. {
  91. auto t = gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10");
  92. ensure(t.test_count == 3);
  93. ensure(t.sum_n == 10);
  94. ensure(t.min_n == 1);
  95. ensure(t.min_value == 1);
  96. ensure(t.max_value == 1000 * 1000 * 1000);
  97. ensure(t.value_bias == 0);
  98. }
  99. {
  100. auto t = gen_array_with_opt_parse_opt("-test-count 3 -sum-n 10 -min-value 10");
  101. ensure(t.test_count == 3);
  102. ensure(t.sum_n == 10);
  103. ensure(t.min_n == 1);
  104. ensure(t.min_value == 10);
  105. ensure(t.max_value == 1000 * 1000 * 1000);
  106. ensure(t.value_bias == 0);
  107. }
  108. {
  109. auto t = gen_array_with_opt_parse_opt("-test-count 5 -sum-n 100 -min-n 3 -min-value 10 -max-value 20 -value-bias 3 ");
  110. ensure(t.test_count == 5);
  111. ensure(t.sum_n == 100);
  112. ensure(t.min_n == 3);
  113. ensure(t.min_value == 10);
  114. ensure(t.max_value == 20);
  115. ensure(t.value_bias == 3);
  116. }
  117. }
  118. }