From f0677fb5a7b6fede7e2e39f8f3d9bbe1d78b3f5e Mon Sep 17 00:00:00 2001 From: Florin Iucha Date: Fri, 29 Nov 2019 20:58:35 -0500 Subject: [PATCH] Clean-up sign-conversion warnings in test code --- test/format-impl-test.cc | 16 ++++++++++------ test/format-test.cc | 20 ++++++++++---------- test/printf-test.cc | 14 ++++++++------ test/scan.h | 14 +++++++------- test/std-format-test.cc | 2 +- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 3c944382..89ba25c1 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -224,19 +224,23 @@ TEST(FPTest, Normalize) { } TEST(FPTest, ComputeFloatBoundaries) { + /* + * TODO: investigate why changing type of x to 'float' and removing the + * static casts causes the test to fail. + */ struct { double x, lower, upper; } tests[] = { // regular - {1.5f, 1.4999999403953552, 1.5000000596046448}, + {static_cast(1.5f), 1.4999999403953552, 1.5000000596046448}, // boundary - {1.0f, 0.9999999701976776, 1.0000000596046448}, + {static_cast(1.0f), 0.9999999701976776, 1.0000000596046448}, // min normal - {1.1754944e-38f, 1.1754942807573643e-38, 1.1754944208872107e-38}, + {static_cast(1.1754944e-38f), 1.1754942807573643e-38, 1.1754944208872107e-38}, // max subnormal - {1.1754942e-38f, 1.1754941406275179e-38, 1.1754942807573643e-38}, + {static_cast(1.1754942e-38f), 1.1754941406275179e-38, 1.1754942807573643e-38}, // min subnormal - {1e-45f, 7.006492321624085e-46, 2.1019476964872256e-45}, + {static_cast(1e-45f), 7.006492321624085e-46, 2.1019476964872256e-45}, }; for (auto test : tests) { fp vlower = normalize(fp(test.lower)); @@ -277,7 +281,7 @@ TEST(FPTest, GetRoundDirection) { EXPECT_EQ(fmt::internal::up, get_round_direction(100, 51, 0)); EXPECT_EQ(fmt::internal::down, get_round_direction(100, 40, 10)); EXPECT_EQ(fmt::internal::up, get_round_direction(100, 60, 10)); - for (int i = 41; i < 60; ++i) + for (size_t i = 41; i < 60; ++i) EXPECT_EQ(fmt::internal::unknown, get_round_direction(100, i, 10)); uint64_t max = max_value(); EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure); diff --git a/test/format-test.cc b/test/format-test.cc index 84c9d8a0..aebd6943 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1852,7 +1852,7 @@ TEST(FormatTest, Dynamic) { std::string result = fmt::vformat( "{} and {} and {}", fmt::basic_format_args( - args.data(), static_cast(args.size()))); + args.data(), static_cast(args.size()))); EXPECT_EQ("42 and abc1 and 1.5", result); } @@ -2166,12 +2166,12 @@ TEST(FormatTest, WideFormatToN) { struct test_arg_id_handler { enum result { NONE, EMPTY, INDEX, NAME, ERROR }; result res = NONE; - unsigned index = 0; + int index = 0; string_view name; FMT_CONSTEXPR void operator()() { res = EMPTY; } - FMT_CONSTEXPR void operator()(unsigned i) { + FMT_CONSTEXPR void operator()(int i) { res = INDEX; index = i; } @@ -2207,9 +2207,9 @@ struct test_format_specs_handler { fmt::align_t align = fmt::align::none; char fill = 0; - unsigned width = 0; + int width = 0; fmt::internal::arg_ref width_ref; - unsigned precision = 0; + int precision = 0; fmt::internal::arg_ref precision_ref; char type = 0; @@ -2235,14 +2235,14 @@ struct test_format_specs_handler { FMT_CONSTEXPR void on_hash() { res = HASH; } FMT_CONSTEXPR void on_zero() { res = ZERO; } - FMT_CONSTEXPR void on_width(unsigned w) { width = w; } + FMT_CONSTEXPR void on_width(int w) { width = w; } FMT_CONSTEXPR void on_dynamic_width(fmt::internal::auto_id) {} - FMT_CONSTEXPR void on_dynamic_width(unsigned index) { width_ref = index; } + FMT_CONSTEXPR void on_dynamic_width(int index) { width_ref = index; } FMT_CONSTEXPR void on_dynamic_width(string_view) {} - FMT_CONSTEXPR void on_precision(unsigned p) { precision = p; } + FMT_CONSTEXPR void on_precision(int p) { precision = p; } FMT_CONSTEXPR void on_dynamic_precision(fmt::internal::auto_id) {} - FMT_CONSTEXPR void on_dynamic_precision(unsigned index) { + FMT_CONSTEXPR void on_dynamic_precision(int index) { precision_ref = index; } FMT_CONSTEXPR void on_dynamic_precision(string_view) {} @@ -2279,7 +2279,7 @@ TEST(FormatTest, ConstexprParseFormatSpecs) { struct test_parse_context { typedef char char_type; - FMT_CONSTEXPR unsigned next_arg_id() { return 11; } + FMT_CONSTEXPR int next_arg_id() { return 11; } template FMT_CONSTEXPR void check_arg_id(Id) {} FMT_CONSTEXPR const char* begin() { return nullptr; } diff --git a/test/printf-test.cc b/test/printf-test.cc index 2d68fab4..f591ea61 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -5,12 +5,13 @@ // // For the license information refer to format.h. +#include "fmt/printf.h" + #include #include #include #include "fmt/core.h" -#include "fmt/printf.h" #include "gtest-extra.h" #include "util.h" @@ -300,15 +301,15 @@ void TestLength(const char* length_spec, U value) { using fmt::internal::const_check; if (const_check(max <= static_cast(max_value()))) { signed_value = static_cast(value); - unsigned_value = static_cast(value); + unsigned_value = static_cast(value); } else if (const_check(max <= max_value())) { signed_value = static_cast(value); - unsigned_value = static_cast(value); + unsigned_value = static_cast(value); } if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) { signed_value = static_cast(value); - unsigned_value = - static_cast::type>(value); + unsigned_value = static_cast( + static_cast::type>(value)); } else { signed_value = static_cast::type>(value); unsigned_value = static_cast::type>(value); @@ -589,7 +590,8 @@ class custom_printf_arg_formatter : public formatter_t { if (round(value * pow(10, specs()->precision)) == 0.0) value = 0; return formatter_t::operator()(value); } -}; +} +; typedef fmt::basic_format_args format_args_t; diff --git a/test/scan.h b/test/scan.h index 50ac0941..061c5449 100644 --- a/test/scan.h +++ b/test/scan.h @@ -6,6 +6,7 @@ // For the license information refer to format.h. #include +#include #include "fmt/format.h" @@ -135,21 +136,20 @@ struct scan_handler : error_handler { char c = *it++; if (c < '0' || c > '9') on_error("invalid input"); // TODO: check overflow - value = value * 10 + (c - '0'); + value = value * 10 + static_cast(c - '0'); } scan_ctx_.advance_to(it); return value; } template T read_int() { - T value = 0; auto it = scan_ctx_.begin(), end = scan_ctx_.end(); bool negative = it != end && *it == '-'; if (negative) ++it; scan_ctx_.advance_to(it); - value = read_uint::type>(); - if (negative) value = -value; - return value; + const auto value = read_uint::type>(); + if (negative) return -static_cast(value); + return static_cast(value); } public: @@ -159,7 +159,7 @@ struct scan_handler : error_handler { const char* pos() const { return scan_ctx_.begin(); } void on_text(const char* begin, const char* end) { - auto size = end - begin; + auto size = static_cast(end - begin); auto it = scan_ctx_.begin(); if (it + size > scan_ctx_.end() || !std::equal(begin, end, make_checked(it, size))) { @@ -197,7 +197,7 @@ struct scan_handler : error_handler { case scan_type::string_view_type: { auto s = it; while (it != end && *it != ' ') ++it; - *arg_.string_view = fmt::string_view(s, it - s); + *arg_.string_view = fmt::string_view(s, static_cast(it - s)); scan_ctx_.advance_to(it); break; } diff --git a/test/std-format-test.cc b/test/std-format-test.cc index a95f244e..b0c6abf7 100644 --- a/test/std-format-test.cc +++ b/test/std-format-test.cc @@ -110,7 +110,7 @@ template <> struct std::formatter { char c = get_char(); if (!isdigit(c) || (++iter, get_char()) != '}') throw format_error("invalid format"); - width_arg_id = c - '0'; + width_arg_id = static_cast(c - '0'); ctx.check_arg_id(width_arg_id); return ++iter; }