From 9a312def288ea420104a4557130c1bbd0535c200 Mon Sep 17 00:00:00 2001 From: Hyunjae Woo Date: Mon, 14 Aug 2023 16:35:13 -0700 Subject: [PATCH] Add IsNonNegativeFloat util function --- src/c++/perf_analyzer/command_line_parser.cc | 4 +-- src/c++/perf_analyzer/perf_utils.cc | 15 +++++++-- src/c++/perf_analyzer/perf_utils.h | 3 ++ src/c++/perf_analyzer/test_perf_utils.cc | 33 ++++++++++++++++---- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/c++/perf_analyzer/command_line_parser.cc b/src/c++/perf_analyzer/command_line_parser.cc index 6877deaa4..35f8ecb98 100644 --- a/src/c++/perf_analyzer/command_line_parser.cc +++ b/src/c++/perf_analyzer/command_line_parser.cc @@ -940,9 +940,8 @@ CLParser::ParseCommandLine(int argc, char** argv) } case 9: case 's': { - // TODO std::string stability_threshold{optarg}; - if (std::stof(stability_threshold) >= 0.0) { + if (IsNonNegativeFloat(stability_threshold)) { params_->stability_threshold = std::stof(optarg) / 100; } else { Usage( @@ -1361,7 +1360,6 @@ CLParser::ParseCommandLine(int argc, char** argv) break; } case 47: { - // TODO std::string trace_count{optarg}; if (std::stoi(trace_count) >= -1) { params_->trace_options["trace_count"] = {trace_count}; diff --git a/src/c++/perf_analyzer/perf_utils.cc b/src/c++/perf_analyzer/perf_utils.cc index 03234be9e..aac862a06 100644 --- a/src/c++/perf_analyzer/perf_utils.cc +++ b/src/c++/perf_analyzer/perf_utils.cc @@ -395,8 +395,6 @@ IsPositiveInteger(const std::string& str) return false; } else if (str.size() == 1 && str[0] == '0') { return false; - } else if (str.size() > 1 && str[0] == '0') { - return false; } return std::all_of( str.begin(), str.end(), @@ -418,4 +416,17 @@ IsNonNegativeInteger(const std::string& str) return IsPositiveInteger(str); } +bool +IsNonNegativeFloat(const std::string& str) +{ + size_t pos{0}; + size_t dot_pos = str.find(".", pos); + if (dot_pos == std::string::npos) { + return IsNonNegativeInteger(str); + } else { + return IsNonNegativeInteger(str.substr(0, dot_pos)) && + IsNonNegativeInteger(str.substr(dot_pos + 1)); + } +} + }} // namespace triton::perfanalyzer diff --git a/src/c++/perf_analyzer/perf_utils.h b/src/c++/perf_analyzer/perf_utils.h index e21b64286..e18a0c672 100644 --- a/src/c++/perf_analyzer/perf_utils.h +++ b/src/c++/perf_analyzer/perf_utils.h @@ -146,4 +146,7 @@ bool IsPositiveInteger(const std::string& str); // Check if the given string is a valid non-negative integer. bool IsNonNegativeInteger(const std::string& str); +// Check if the given string is a valid non-negative floating point number. +bool IsNonNegativeFloat(const std::string& str); + }} // namespace triton::perfanalyzer diff --git a/src/c++/perf_analyzer/test_perf_utils.cc b/src/c++/perf_analyzer/test_perf_utils.cc index a3e319d91..87764ff28 100644 --- a/src/c++/perf_analyzer/test_perf_utils.cc +++ b/src/c++/perf_analyzer/test_perf_utils.cc @@ -375,8 +375,8 @@ TEST_CASE("perf_utils: Test string input numbers") std::string pos{"123"}; std::string neg{"-123"}; std::string zero{"0"}; - std::string not_number{"helloworld"}; - std::string almost_number{"123hello"}; + std::string not_integer{"helloworld"}; + std::string almost_integer{"123hello"}; std::string empty{""}; SUBCASE("IsPositiveInteger") @@ -384,8 +384,8 @@ TEST_CASE("perf_utils: Test string input numbers") CHECK(IsPositiveInteger(pos)); CHECK(!IsPositiveInteger(neg)); CHECK(!IsPositiveInteger(zero)); - CHECK(!IsPositiveInteger(not_number)); - CHECK(!IsPositiveInteger(almost_number)); + CHECK(!IsPositiveInteger(not_integer)); + CHECK(!IsPositiveInteger(almost_integer)); CHECK(!IsPositiveInteger(empty)); } @@ -394,10 +394,31 @@ TEST_CASE("perf_utils: Test string input numbers") CHECK(IsNonNegativeInteger(pos)); CHECK(!IsNonNegativeInteger(neg)); CHECK(IsNonNegativeInteger(zero)); - CHECK(!IsNonNegativeInteger(not_number)); - CHECK(!IsNonNegativeInteger(almost_number)); + CHECK(!IsNonNegativeInteger(not_integer)); + CHECK(!IsNonNegativeInteger(almost_integer)); CHECK(!IsNonNegativeInteger(empty)); } + + SUBCASE("IsNonNegativeFloat") + { + std::string pos_float{"123.456"}; + std::string neg_float{"-123.456"}; + std::string zero_float{"0.0"}; + std::string fraction{"0.01"}; + std::string almost_float{"123.01hello"}; + + CHECK(IsNonNegativeFloat(pos_float)); + CHECK(!IsNonNegativeFloat(neg_float)); + CHECK(IsNonNegativeFloat(zero_float)); + CHECK(IsNonNegativeFloat(fraction)); + CHECK(IsNonNegativeFloat(pos)); + CHECK(!IsNonNegativeFloat(neg)); + CHECK(IsNonNegativeFloat(zero)); + CHECK(!IsNonNegativeFloat(not_integer)); + CHECK(!IsNonNegativeFloat(almost_integer)); + CHECK(!IsNonNegativeFloat(almost_float)); + CHECK(!IsNonNegativeFloat(empty)); + } }