From 930d3b67a49f0f7d9b4a335dae210e50e70cd3a3 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 4 Dec 2023 20:15:15 +0100 Subject: [PATCH] chore: Fix some -Wconversion warnings --- src/Config.cpp | 8 +- src/ProgressBar.cpp | 3 +- src/argprocessing.cpp | 2 +- src/core/CacheEntry.cpp | 7 +- src/core/Manifest.cpp | 29 ++++--- src/core/Manifest.hpp | 2 +- src/core/Result.cpp | 4 +- src/core/Statistics.cpp | 24 +++--- src/core/mainoptions.cpp | 12 +-- src/storage/local/LocalStorage.cpp | 40 ++++++---- src/storage/remote/RedisStorage.cpp | 16 ++-- src/util/Duration.hpp | 8 +- src/util/LockFile.cpp | 13 ++- src/util/TemporaryFile.cpp | 4 +- src/util/TimePoint.hpp | 2 +- src/util/conversion.hpp | 2 +- src/util/string.cpp | 20 +++-- src/util/zstd.cpp | 3 +- unittest/test_util_Tokenizer.cpp | 2 +- unittest/test_util_file.cpp | 2 +- unittest/test_util_string.cpp | 120 +++++++++++++--------------- 21 files changed, 177 insertions(+), 146 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 042e856fb..71bdc354c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1013,8 +1013,8 @@ Config::set_item(const std::string& key, break; case ConfigItem::compression_level: - m_compression_level = util::value_or_throw( - util::parse_signed(value, INT8_MIN, INT8_MAX, "compression_level")); + m_compression_level = static_cast(util::value_or_throw( + util::parse_signed(value, INT8_MIN, INT8_MAX, "compression_level"))); break; case ConfigItem::cpp_extension: @@ -1030,8 +1030,8 @@ Config::set_item(const std::string& key, break; case ConfigItem::debug_level: - m_debug_level = util::value_or_throw( - util::parse_unsigned(value, std::nullopt, std::nullopt, "debug level")); + m_debug_level = static_cast(util::value_or_throw( + util::parse_unsigned(value, 0, UINT8_MAX, "debug level"))); break; case ConfigItem::depend_mode: diff --git a/src/ProgressBar.cpp b/src/ProgressBar.cpp index c616d6d94..dc55dd4e0 100644 --- a/src/ProgressBar.cpp +++ b/src/ProgressBar.cpp @@ -90,7 +90,8 @@ ProgressBar::update(double value) PRINT(stdout, "\r{} {:5.1f}%", m_header, new_value_percent); } else { size_t total_bar_width = m_width - first_part_width; - size_t filled_bar_width = (new_value_percent / 100) * total_bar_width; + size_t filled_bar_width = static_cast( + (new_value_percent / 100) * static_cast(total_bar_width)); size_t unfilled_bar_width = total_bar_width - filled_bar_width; PRINT(stdout, "\r{} {:5.1f}% [{:=<{}}{: <{}}]", diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 874d41050..216cdc3e5 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -333,12 +333,12 @@ process_option_arg(const Context& ctx, return std::nullopt; } - // Ignore clang -ivfsoverlay to not detect multiple input files. if (arg == "-ivfsoverlay" && !(config.sloppiness().contains(core::Sloppy::ivfsoverlay))) { LOG_RAW( "You have to specify \"ivfsoverlay\" sloppiness when using" " -ivfsoverlay to get hits"); + ++i; return Statistic::unsupported_compiler_option; } diff --git a/src/core/CacheEntry.cpp b/src/core/CacheEntry.cpp index d1ab2b0f0..9ae0de305 100644 --- a/src/core/CacheEntry.cpp +++ b/src/core/CacheEntry.cpp @@ -172,9 +172,9 @@ CacheEntry::Header::serialize(util::Bytes& output) const writer.write_int(compression_level); writer.write_int(self_contained); writer.write_int(creation_time); - writer.write_int(ccache_version.length()); + writer.write_int(static_cast(ccache_version.length())); writer.write_str(ccache_version); - writer.write_int(namespace_.length()); + writer.write_int(static_cast(namespace_.length())); writer.write_str(namespace_); writer.write_int(entry_size); } @@ -182,7 +182,8 @@ CacheEntry::Header::serialize(util::Bytes& output) const uint32_t CacheEntry::Header::uncompressed_payload_size() const { - return entry_size - serialized_size() - k_epilogue_fields_size; + return static_cast(entry_size - serialized_size() + - k_epilogue_fields_size); } CacheEntry::CacheEntry(nonstd::span data) : m_header(data) diff --git a/src/core/Manifest.cpp b/src/core/Manifest.cpp index 70c1f4746..d54223930 100644 --- a/src/core/Manifest.cpp +++ b/src/core/Manifest.cpp @@ -212,8 +212,13 @@ Manifest::add_result( file_info_indexes.reserve(included_files.size()); for (const auto& [path, digest] : included_files) { - file_info_indexes.push_back(get_file_info_index( - path, digest, mf_files, mf_file_infos, stat_file_function)); + auto index = get_file_info_index( + path, digest, mf_files, mf_file_infos, stat_file_function); + if (!index) { + LOG_RAW("Index overflow in manifest"); + return false; + } + file_info_indexes.push_back(*index); } ResultEntry entry{std::move(file_info_indexes), result_key}; @@ -252,7 +257,7 @@ Manifest::serialized_size() const throw core::Error( FMT("Serialized manifest too large ({} > {})", size, max)); } - return size; + return static_cast(size); } void @@ -261,13 +266,13 @@ Manifest::serialize(util::Bytes& output) core::CacheEntryDataWriter writer(output); writer.write_int(k_format_version); - writer.write_int(m_files.size()); + writer.write_int(static_cast(m_files.size())); for (const auto& file : m_files) { - writer.write_int(file.length()); + writer.write_int(static_cast(file.length())); writer.write_str(file); } - writer.write_int(m_file_infos.size()); + writer.write_int(static_cast(m_file_infos.size())); for (const auto& file_info : m_file_infos) { writer.write_int(file_info.index); writer.write_bytes(file_info.digest); @@ -276,9 +281,9 @@ Manifest::serialize(util::Bytes& output) writer.write_int(file_info.ctime.nsec()); } - writer.write_int(m_results.size()); + writer.write_int(static_cast(m_results.size())); for (const auto& result : m_results) { - writer.write_int(result.file_info_indexes.size()); + writer.write_int(static_cast(result.file_info_indexes.size())); for (auto index : result.file_info_indexes) { writer.write_int(index); } @@ -307,7 +312,7 @@ Manifest::clear() m_results.clear(); } -uint32_t +std::optional Manifest::get_file_info_index( const std::string& path, const Hash::Digest& digest, @@ -320,9 +325,11 @@ Manifest::get_file_info_index( const auto f_it = mf_files.find(path); if (f_it != mf_files.end()) { fi.index = f_it->second; + } else if (m_files.size() > UINT32_MAX) { + return std::nullopt; } else { m_files.push_back(path); - fi.index = m_files.size() - 1; + fi.index = static_cast(m_files.size() - 1); } fi.digest = digest; @@ -335,6 +342,8 @@ Manifest::get_file_info_index( const auto fi_it = mf_file_infos.find(fi); if (fi_it != mf_file_infos.end()) { return fi_it->second; + } else if (m_file_infos.size() > UINT32_MAX) { + return std::nullopt; } else { m_file_infos.push_back(fi); return m_file_infos.size() - 1; diff --git a/src/core/Manifest.hpp b/src/core/Manifest.hpp index c4b1d3b3b..9ffa65ea3 100644 --- a/src/core/Manifest.hpp +++ b/src/core/Manifest.hpp @@ -94,7 +94,7 @@ private: void clear(); - uint32_t get_file_info_index( + std::optional get_file_info_index( const std::string& path, const Hash::Digest& digest, const std::unordered_map& mf_files, diff --git a/src/core/Result.cpp b/src/core/Result.cpp index 3f008f8a2..5694fdcef 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -268,7 +268,7 @@ Serializer::serialized_size() const throw Error( FMT("Serialized result too large ({} > {})", m_serialized_size, max)); } - return m_serialized_size; + return static_cast(m_serialized_size); } void @@ -277,7 +277,7 @@ Serializer::serialize(util::Bytes& output) CacheEntryDataWriter writer(output); writer.write_int(k_format_version); - writer.write_int(m_file_entries.size()); + writer.write_int(static_cast(m_file_entries.size())); uint8_t file_number = 0; for (const auto& entry : m_file_entries) { diff --git a/src/core/Statistics.cpp b/src/core/Statistics.cpp index cecb7ca6f..aa01b18c4 100644 --- a/src/core/Statistics.cpp +++ b/src/core/Statistics.cpp @@ -280,11 +280,15 @@ percent(const uint64_t nominator, const uint64_t denominator) return ""; } - std::string result = FMT("({:5.2f}%)", (100.0 * nominator) / denominator); + std::string result = FMT("({:5.2f}%)", + (100.0 * static_cast(nominator)) + / static_cast(denominator)); if (result.length() <= 8) { return result; } else { - return FMT("({:5.1f}%)", (100.0 * nominator) / denominator); + return FMT("({:5.1f}%)", + (100.0 * static_cast(nominator)) + / static_cast(denominator)); } } @@ -453,15 +457,17 @@ Statistics::format_human_readable(const Config& config, table.add_heading("Local storage:"); } if (!from_log) { - std::vector size_cells{ - FMT(" Cache size ({}):", size_unit), - C(FMT("{:.1f}", static_cast(local_size) / size_divider)) - .right_align()}; + std::vector size_cells{FMT(" Cache size ({}):", size_unit), + C(FMT("{:.1f}", + static_cast(local_size) + / static_cast(size_divider))) + .right_align()}; if (config.max_size() != 0) { size_cells.emplace_back("/"); - size_cells.emplace_back( - C(FMT("{:.1f}", static_cast(config.max_size()) / size_divider)) - .right_align()); + size_cells.emplace_back(C(FMT("{:.1f}", + static_cast(config.max_size()) + / static_cast(size_divider))) + .right_align()); size_cells.emplace_back(percent(local_size, config.max_size())); } table.add_row(size_cells); diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index f5ed6cebd..826ce5235 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -235,7 +235,8 @@ print_compression_statistics(const Config& config, const storage::local::CompressionStatistics& cs) { const double ratio = cs.actual_size > 0 - ? static_cast(cs.content_size) / cs.actual_size + ? static_cast(cs.content_size) + / static_cast(cs.actual_size) : 0.0; const double savings = ratio > 0.0 ? 100.0 - (100.0 / ratio) : 0.0; @@ -505,8 +506,9 @@ process_main_options(int argc, const char* const* argv) break; case RECOMPRESS_THREADS: - recompress_threads = util::value_or_throw(util::parse_unsigned( - arg, 1, std::numeric_limits::max(), "threads")); + recompress_threads = + static_cast(util::value_or_throw(util::parse_unsigned( + arg, 1, std::numeric_limits::max(), "threads"))); break; case TRIM_MAX_SIZE: { @@ -527,8 +529,8 @@ process_main_options(int argc, const char* const* argv) case TRIM_RECOMPRESS_THREADS: trim_recompress_threads = - util::value_or_throw(util::parse_unsigned( - arg, 1, std::numeric_limits::max(), "threads")); + static_cast(util::value_or_throw(util::parse_unsigned( + arg, 1, std::numeric_limits::max(), "threads"))); break; case 'v': // --verbose diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index 142820747..e0f6947f1 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -300,6 +300,17 @@ struct CleanDirResult Level2Counters after; }; +template +static double +ratio(T numerator, T denominator) +{ + if (denominator == 0) { + return 0.0; + } else { + return static_cast(numerator) / static_cast(denominator); + } +} + static CleanDirResult clean_dir( const std::string& l2_dir, @@ -322,7 +333,7 @@ clean_dir( raw_files_map; for (size_t i = 0; i < files.size(); - ++i, progress_receiver(1.0 / 3 + 1.0 * i / files.size() / 3)) { + ++i, progress_receiver(1.0 / 3 + 1.0 * ratio(i, files.size()) / 3)) { const auto& file = files[i]; if (!file.is_regular_file()) { @@ -360,7 +371,7 @@ clean_dir( bool cleaned = false; for (size_t i = 0; i < files.size(); - ++i, progress_receiver(2.0 / 3 + 1.0 * i / files.size() / 3)) { + ++i, progress_receiver(2.0 / 3 + 1.0 * ratio(i, files.size()) / 3)) { const auto& file = files[i]; if (!file || file.is_directory()) { @@ -440,8 +451,8 @@ LocalStorage::finalize() // Pseudo-randomly choose one of the stats files in the 256 level 2 // directories. const auto bucket = getpid() % 256; - const uint8_t l1_index = bucket / 16; - const uint8_t l2_index = bucket % 16; + const uint8_t l1_index = static_cast(bucket / 16); + const uint8_t l2_index = static_cast(bucket % 16); const auto l2_stats_file = get_stats_file(l1_index, l2_index); uint64_t l2_files_in_cache = 0; @@ -812,7 +823,7 @@ LocalStorage::wipe_all(const ProgressReceiver& progress_receiver) for (size_t i = 0; i < files.size(); ++i) { util::remove_nfs_safe(files[i].path().string()); - l2_progress_receiver(0.5 + 0.5 * i / files.size()); + l2_progress_receiver(0.5 + 0.5 * ratio(i, files.size())); } if (!files.empty()) { @@ -849,7 +860,7 @@ LocalStorage::get_compression_statistics( } catch (core::Error&) { cs.incompressible_size += cache_file.size_on_disk(); } - l2_progress_receiver(0.2 + 0.8 * i / files.size()); + l2_progress_receiver(0.2 + 0.8 * ratio(i, files.size())); } }); }); @@ -920,7 +931,7 @@ LocalStorage::recompress(const std::optional level, incompressible_size += file.size_on_disk(); } - l2_progress_receiver(0.1 + 0.9 * i / files.size()); + l2_progress_receiver(0.1 + 0.9 * ratio(i, files.size())); } if (util::ends_with(l2_dir, "f/f")) { @@ -938,16 +949,12 @@ LocalStorage::recompress(const std::optional level, PRINT_RAW(stdout, "\n\n"); } - const double old_ratio = recompressor.old_size() > 0 - ? static_cast(recompressor.content_size()) - / recompressor.old_size() - : 0.0; + const double old_ratio = + ratio(recompressor.content_size(), recompressor.old_size()); const double old_savings = old_ratio > 0.0 ? 100.0 - (100.0 / old_ratio) : 0.0; - const double new_ratio = recompressor.new_size() > 0 - ? static_cast(recompressor.content_size()) - / recompressor.new_size() - : 0.0; + const double new_ratio = + ratio(recompressor.content_size(), recompressor.new_size()); const double new_savings = new_ratio > 0.0 ? 100.0 - (100.0 / new_ratio) : 0.0; const int64_t size_diff = static_cast(recompressor.new_size()) @@ -1241,7 +1248,8 @@ LocalStorage::perform_automatic_cleanup() // practice removing much newer entries than the oldest in other // subdirectories. By doing cleanup based on the number of files, both example // scenarios are improved. - const uint64_t target_files = 0.9 * evaluation->total_files / 256; + const uint64_t target_files = static_cast( + 0.9 * static_cast(evaluation->total_files) / 256); auto clean_dir_result = clean_dir( get_subdir(evaluation->l1_index, largest_level_2_index), 0, target_files); diff --git a/src/storage/remote/RedisStorage.cpp b/src/storage/remote/RedisStorage.cpp index 3b74c22f3..2b3401e69 100644 --- a/src/storage/remote/RedisStorage.cpp +++ b/src/storage/remote/RedisStorage.cpp @@ -140,7 +140,9 @@ RedisStorageBackend::RedisStorageBackend(const Params& params) } } - connect(url, connect_timeout.count(), operation_timeout.count()); + connect(url, + static_cast(connect_timeout.count()), + static_cast(operation_timeout.count())); authenticate(url); select_database(url); } @@ -244,9 +246,10 @@ RedisStorageBackend::connect(const Url& url, } else { const std::string host = url.host().empty() ? "localhost" : url.host(); const uint32_t port = - url.port().empty() ? DEFAULT_PORT - : util::value_or_throw( - util::parse_unsigned(url.port(), 1, 65535, "port")); + url.port().empty() + ? DEFAULT_PORT + : static_cast(util::value_or_throw( + util::parse_unsigned(url.port(), 1, 65535, "port"))); ASSERT(url.path().empty() || url.path()[0] == '/'); LOG("Redis connecting to {}:{} (connect timeout {} ms)", @@ -293,8 +296,9 @@ RedisStorageBackend::select_database(const Url& url) } const uint32_t db_number = !db ? 0 - : util::value_or_throw(util::parse_unsigned( - *db, 0, std::numeric_limits::max(), "db number")); + : static_cast( + util::value_or_throw(util::parse_unsigned( + *db, 0, std::numeric_limits::max(), "db number"))); if (db_number != 0) { LOG("Redis SELECT {}", db_number); diff --git a/src/util/Duration.hpp b/src/util/Duration.hpp index 5ff4bef6f..b7ab08225 100644 --- a/src/util/Duration.hpp +++ b/src/util/Duration.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Joel Rosdahl and other contributors +// Copyright (C) 2022-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -105,13 +105,13 @@ Duration::operator-(const Duration& other) const inline Duration Duration::operator*(double factor) const { - return Duration(0, factor * m_ns); + return Duration(0, static_cast(factor * static_cast(m_ns))); } inline Duration Duration::operator/(double factor) const { - return Duration(0, m_ns / factor); + return Duration(0, static_cast(static_cast(m_ns) / factor)); } inline int64_t @@ -129,7 +129,7 @@ Duration::nsec() const inline int32_t Duration::nsec_decimal_part() const { - return m_ns % 1'000'000'000; + return static_cast(m_ns % 1'000'000'000); } } // namespace util diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index c57f265be..b6c0b9f42 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -38,9 +38,8 @@ #include #include -// Seconds. -const double k_min_sleep_time = 0.010; -const double k_max_sleep_time = 0.050; +const uint32_t k_min_sleep_time_ms = 100; +const uint32_t k_max_sleep_time_ms = 500; #ifndef _WIN32 const util::Duration k_staleness_limit(2); #endif @@ -228,8 +227,8 @@ LockFile::do_acquire(const bool blocking) }(); std::string initial_content; - RandomNumberGenerator sleep_ms_generator(k_min_sleep_time * 1000, - k_max_sleep_time * 1000); + RandomNumberGenerator sleep_ms_generator(k_min_sleep_time_ms, + k_max_sleep_time_ms); while (true) { const auto now = TimePoint::now(); @@ -356,8 +355,8 @@ void* LockFile::do_acquire(const bool blocking) { void* handle; - RandomNumberGenerator sleep_ms_generator(k_min_sleep_time * 1000, - k_max_sleep_time * 1000); + RandomNumberGenerator sleep_ms_generator(k_min_sleep_time_ms, + k_max_sleep_time_ms); while (true) { DWORD flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE; diff --git a/src/util/TemporaryFile.cpp b/src/util/TemporaryFile.cpp index 2c946a834..cc1785fa4 100644 --- a/src/util/TemporaryFile.cpp +++ b/src/util/TemporaryFile.cpp @@ -60,9 +60,9 @@ TemporaryFile::create(const fs::path& path_prefix, std::string_view suffix) // [1]: - Fd fd(bsd_mkstemps(&path_template[0], suffix.length())); + Fd fd(bsd_mkstemps(&path_template[0], static_cast(suffix.length()))); #else - Fd fd(mkstemps(&path_template[0], suffix.length())); + Fd fd(mkstemps(&path_template[0], static_cast(suffix.length()))); #endif if (!fd) { return tl::unexpected(FMT("failed to create temporary file for {}: {}", diff --git a/src/util/TimePoint.hpp b/src/util/TimePoint.hpp index c35908b41..98a3cc800 100644 --- a/src/util/TimePoint.hpp +++ b/src/util/TimePoint.hpp @@ -103,7 +103,7 @@ TimePoint::nsec() const inline int32_t TimePoint::nsec_decimal_part() const { - return m_ns % 1'000'000'000; + return static_cast(m_ns % 1'000'000'000); } inline void diff --git a/src/util/conversion.hpp b/src/util/conversion.hpp index 9eaed5ce6..9f9eb2553 100644 --- a/src/util/conversion.hpp +++ b/src/util/conversion.hpp @@ -89,7 +89,7 @@ void int_to_big_endian(T value, uint8_t* buffer) { for (size_t i = 0; i < sizeof(T); ++i) { - buffer[sizeof(T) - i - 1] = value & 0xFF; + buffer[sizeof(T) - i - 1] = static_cast(value & 0xFF); value >>= 8; } } diff --git a/src/util/string.cpp b/src/util/string.cpp index 45f80dc02..8f4c805d4 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -169,14 +169,15 @@ std::string format_human_readable_size(uint64_t size, SizeUnitPrefixType prefix_type) { const double factor = prefix_type == SizeUnitPrefixType::binary ? 1024 : 1000; + const double dsize = static_cast(size); const char* infix = prefix_type == SizeUnitPrefixType::binary ? "i" : ""; - if (size >= factor * factor * factor) { - return FMT("{:.1f} G{}B", size / (factor * factor * factor), infix); - } else if (size >= factor * factor) { - return FMT("{:.1f} M{}B", size / (factor * factor), infix); - } else if (size >= factor) { + if (dsize >= factor * factor * factor) { + return FMT("{:.1f} G{}B", dsize / (factor * factor * factor), infix); + } else if (dsize >= factor * factor) { + return FMT("{:.1f} M{}B", dsize / (factor * factor), infix); + } else if (dsize >= factor) { const char* k = prefix_type == SizeUnitPrefixType::binary ? "K" : "k"; - return FMT("{:.1f} {}{}B", size / factor, k, infix); + return FMT("{:.1f} {}{}B", dsize / factor, k, infix); } else if (size == 1) { return "1 byte"; } else { @@ -308,7 +309,12 @@ parse_size(const std::string& value) tl::expected parse_umask(std::string_view value) { - return parse_unsigned(value, 0, 0777, "umask", 8); + auto result = parse_unsigned(value, 0, 0777, "umask", 8); + if (result) { + return static_cast(*result); + } else { + return tl::unexpected(result.error()); + } } tl::expected diff --git a/src/util/zstd.cpp b/src/util/zstd.cpp index c47139e6b..81fe6f1ee 100644 --- a/src/util/zstd.cpp +++ b/src/util/zstd.cpp @@ -79,7 +79,8 @@ zstd_supported_compression_level(int8_t wanted_level) return {1, "minimum level supported by libzstd"}; } - const int8_t level = std::min(wanted_level, ZSTD_maxCLevel()); + const int8_t level = + static_cast(std::min(wanted_level, ZSTD_maxCLevel())); if (level != wanted_level) { return {level, "max libzstd level"}; } diff --git a/unittest/test_util_Tokenizer.cpp b/unittest/test_util_Tokenizer.cpp index f52fc4c6d..256217797 100644 --- a/unittest/test_util_Tokenizer.cpp +++ b/unittest/test_util_Tokenizer.cpp @@ -47,7 +47,7 @@ TEST_CASE("util::Tokenizer") const auto res = util::split_into_views(input, separators, m_mode, m_include_delimiter); REQUIRE(res.size() == expected.size()); - for (int i = 0, total = expected.size(); i < total; ++i) { + for (size_t i = 0, total = expected.size(); i < total; ++i) { CHECK(res[i] == expected[i]); } } diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index 67982d809..46f4ce8c3 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -122,7 +122,7 @@ TEST_CASE("util::read_file and util::write_file, binary data") std::vector expected; for (size_t i = 0; i < 512; ++i) { - expected.push_back((32 + i) % 256); + expected.push_back(static_cast((32 + i) % 256)); } CHECK(util::write_file("test", expected)); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 1c49d70d5..876295ba6 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -159,38 +159,36 @@ TEST_CASE("util::format_human_readable_diff") CHECK(util::format_human_readable_diff(0, SUPT::binary) == "0 bytes"); CHECK(util::format_human_readable_diff(1, SUPT::binary) == "+1 byte"); CHECK(util::format_human_readable_diff(42, SUPT::binary) == "+42 bytes"); - CHECK(util::format_human_readable_diff(1949, SUPT::binary) == "+1.9 KiB"); - CHECK(util::format_human_readable_diff(1951, SUPT::binary) == "+1.9 KiB"); - CHECK(util::format_human_readable_diff(499.7 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(1'949, SUPT::binary) == "+1.9 KiB"); + CHECK(util::format_human_readable_diff(1'951, SUPT::binary) == "+1.9 KiB"); + CHECK(util::format_human_readable_diff(499'700, SUPT::binary) == "+488.0 KiB"); - CHECK(util::format_human_readable_diff(1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(1'000'000, SUPT::binary) == "+976.6 KiB"); - CHECK(util::format_human_readable_diff(1234 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(1'234'000, SUPT::binary) == "+1.2 MiB"); - CHECK(util::format_human_readable_diff(438.5 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(438'500'000, SUPT::binary) == "+418.2 MiB"); - CHECK(util::format_human_readable_diff(1000 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(1'000'000'000, SUPT::binary) == "+953.7 MiB"); - CHECK( - util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000, SUPT::binary) - == "+15.9 GiB"); + CHECK(util::format_human_readable_diff(17'110'000'000, SUPT::binary) + == "+15.9 GiB"); CHECK(util::format_human_readable_diff(-1, SUPT::binary) == "-1 byte"); CHECK(util::format_human_readable_diff(-42, SUPT::binary) == "-42 bytes"); - CHECK(util::format_human_readable_diff(-1949, SUPT::binary) == "-1.9 KiB"); - CHECK(util::format_human_readable_diff(-1951, SUPT::binary) == "-1.9 KiB"); - CHECK(util::format_human_readable_diff(-499.7 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(-1'949, SUPT::binary) == "-1.9 KiB"); + CHECK(util::format_human_readable_diff(-1'951, SUPT::binary) == "-1.9 KiB"); + CHECK(util::format_human_readable_diff(-499'700, SUPT::binary) == "-488.0 KiB"); - CHECK(util::format_human_readable_diff(-1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(-1'000'000, SUPT::binary) == "-976.6 KiB"); - CHECK(util::format_human_readable_diff(-1234 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(-1'234'000, SUPT::binary) == "-1.2 MiB"); - CHECK(util::format_human_readable_diff(-438.5 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(-438'500'000, SUPT::binary) == "-418.2 MiB"); - CHECK(util::format_human_readable_diff(-1000 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_diff(-1'000'000'000, SUPT::binary) == "-953.7 MiB"); - CHECK(util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000, - SUPT::binary) + CHECK(util::format_human_readable_diff(-17'110'000'000, SUPT::binary) == "-15.9 GiB"); } @@ -199,38 +197,36 @@ TEST_CASE("util::format_human_readable_diff") CHECK(util::format_human_readable_diff(0, SUPT::decimal) == "0 bytes"); CHECK(util::format_human_readable_diff(1, SUPT::decimal) == "+1 byte"); CHECK(util::format_human_readable_diff(42, SUPT::decimal) == "+42 bytes"); - CHECK(util::format_human_readable_diff(1949, SUPT::decimal) == "+1.9 kB"); - CHECK(util::format_human_readable_diff(1951, SUPT::decimal) == "+2.0 kB"); - CHECK(util::format_human_readable_diff(499.7 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(1'949, SUPT::decimal) == "+1.9 kB"); + CHECK(util::format_human_readable_diff(1'951, SUPT::decimal) == "+2.0 kB"); + CHECK(util::format_human_readable_diff(499'700, SUPT::decimal) == "+499.7 kB"); - CHECK(util::format_human_readable_diff(1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(1'000'000, SUPT::decimal) == "+1.0 MB"); - CHECK(util::format_human_readable_diff(1234 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(1'234'000, SUPT::decimal) == "+1.2 MB"); - CHECK(util::format_human_readable_diff(438.5 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(438'500'000, SUPT::decimal) == "+438.5 MB"); - CHECK(util::format_human_readable_diff(1000 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(1'000'000'000, SUPT::decimal) == "+1.0 GB"); - CHECK(util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000, - SUPT::decimal) + CHECK(util::format_human_readable_diff(17'110'000'000, SUPT::decimal) == "+17.1 GB"); CHECK(util::format_human_readable_diff(-1, SUPT::decimal) == "-1 byte"); CHECK(util::format_human_readable_diff(-42, SUPT::decimal) == "-42 bytes"); - CHECK(util::format_human_readable_diff(-1949, SUPT::decimal) == "-1.9 kB"); - CHECK(util::format_human_readable_diff(-1951, SUPT::decimal) == "-2.0 kB"); - CHECK(util::format_human_readable_diff(-499.7 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(-1'949, SUPT::decimal) == "-1.9 kB"); + CHECK(util::format_human_readable_diff(-1'951, SUPT::decimal) == "-2.0 kB"); + CHECK(util::format_human_readable_diff(-499'700, SUPT::decimal) == "-499.7 kB"); - CHECK(util::format_human_readable_diff(-1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(-1'000'000, SUPT::decimal) == "-1.0 MB"); - CHECK(util::format_human_readable_diff(-1234 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(-1'234'000, SUPT::decimal) == "-1.2 MB"); - CHECK(util::format_human_readable_diff(-438.5 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(-438'500'000, SUPT::decimal) == "-438.5 MB"); - CHECK(util::format_human_readable_diff(-1000 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_diff(-1'000'000'000, SUPT::decimal) == "-1.0 GB"); - CHECK(util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000, - SUPT::decimal) + CHECK(util::format_human_readable_diff(-17'110'000'000, SUPT::decimal) == "-17.1 GB"); } } @@ -244,21 +240,20 @@ TEST_CASE("util::format_human_readable_size") CHECK(util::format_human_readable_size(0, SUPT::binary) == "0 bytes"); CHECK(util::format_human_readable_size(1, SUPT::binary) == "1 byte"); CHECK(util::format_human_readable_size(42, SUPT::binary) == "42 bytes"); - CHECK(util::format_human_readable_size(1949, SUPT::binary) == "1.9 KiB"); - CHECK(util::format_human_readable_size(1951, SUPT::binary) == "1.9 KiB"); - CHECK(util::format_human_readable_size(499.7 * 1000, SUPT::binary) + CHECK(util::format_human_readable_size(1'949, SUPT::binary) == "1.9 KiB"); + CHECK(util::format_human_readable_size(1'951, SUPT::binary) == "1.9 KiB"); + CHECK(util::format_human_readable_size(499'700, SUPT::binary) == "488.0 KiB"); - CHECK(util::format_human_readable_size(1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_size(1'000'000, SUPT::binary) == "976.6 KiB"); - CHECK(util::format_human_readable_size(1234 * 1000, SUPT::binary) + CHECK(util::format_human_readable_size(1'234'000, SUPT::binary) == "1.2 MiB"); - CHECK(util::format_human_readable_size(438.5 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_size(438'500'000, SUPT::binary) == "418.2 MiB"); - CHECK(util::format_human_readable_size(1000 * 1000 * 1000, SUPT::binary) + CHECK(util::format_human_readable_size(1'000'000'000, SUPT::binary) == "953.7 MiB"); - CHECK( - util::format_human_readable_size(17.11 * 1000 * 1000 * 1000, SUPT::binary) - == "15.9 GiB"); + CHECK(util::format_human_readable_size(17'110'000'000, SUPT::binary) + == "15.9 GiB"); } SUBCASE("decimal") @@ -266,20 +261,19 @@ TEST_CASE("util::format_human_readable_size") CHECK(util::format_human_readable_size(0, SUPT::decimal) == "0 bytes"); CHECK(util::format_human_readable_size(1, SUPT::decimal) == "1 byte"); CHECK(util::format_human_readable_size(42, SUPT::decimal) == "42 bytes"); - CHECK(util::format_human_readable_size(1949, SUPT::decimal) == "1.9 kB"); - CHECK(util::format_human_readable_size(1951, SUPT::decimal) == "2.0 kB"); - CHECK(util::format_human_readable_size(499.7 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_size(1'949, SUPT::decimal) == "1.9 kB"); + CHECK(util::format_human_readable_size(1'951, SUPT::decimal) == "2.0 kB"); + CHECK(util::format_human_readable_size(499'700, SUPT::decimal) == "499.7 kB"); - CHECK(util::format_human_readable_size(1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_size(1'000'000, SUPT::decimal) == "1.0 MB"); - CHECK(util::format_human_readable_size(1234 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_size(1'234'000, SUPT::decimal) == "1.2 MB"); - CHECK(util::format_human_readable_size(438.5 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_size(438'500'000, SUPT::decimal) == "438.5 MB"); - CHECK(util::format_human_readable_size(1000 * 1000 * 1000, SUPT::decimal) + CHECK(util::format_human_readable_size(1'000'000'000, SUPT::decimal) == "1.0 GB"); - CHECK(util::format_human_readable_size(17.11 * 1000 * 1000 * 1000, - SUPT::decimal) + CHECK(util::format_human_readable_size(17'110'000'000, SUPT::decimal) == "17.1 GB"); } } @@ -387,14 +381,14 @@ TEST_CASE("util::parse_size") == h(u64(42) * 1024 * 1024 * 1024, SUPT::binary)); // Decimal suffixes - CHECK(*util::parse_size("78k") == h(78 * 1000, SUPT::decimal)); - CHECK(*util::parse_size("78K") == h(78 * 1000, SUPT::decimal)); - CHECK(*util::parse_size("1.1 M") == h(u64(1.1 * 1000 * 1000), SUPT::decimal)); + CHECK(*util::parse_size("78k") == h(78'000, SUPT::decimal)); + CHECK(*util::parse_size("78K") == h(78'000, SUPT::decimal)); + CHECK(*util::parse_size("1.1 M") == h(u64(1.1 * 1'000'000), SUPT::decimal)); CHECK(*util::parse_size("438.55M") - == h(u64(438.55 * 1000 * 1000), SUPT::decimal)); - CHECK(*util::parse_size("1 G") == h(1 * 1000 * 1000 * 1000, SUPT::decimal)); + == h(u64(438.55 * 1'000'000), SUPT::decimal)); + CHECK(*util::parse_size("1 G") == h(1 * 1'000'000'000, SUPT::decimal)); CHECK(*util::parse_size("2T") - == h(u64(2) * 1000 * 1000 * 1000 * 1000, SUPT::decimal)); + == h(u64(2) * 1'000'000 * 1'000'000, SUPT::decimal)); // Binary suffixes CHECK(*util::parse_size("78 Ki") == h(78 * 1024, SUPT::binary)); @@ -406,7 +400,7 @@ TEST_CASE("util::parse_size") == h(u64(2) * 1024 * 1024 * 1024 * 1024, SUPT::binary)); // With B suffix - CHECK(*util::parse_size("9MB") == h(9 * 1000 * 1000, SUPT::decimal)); + CHECK(*util::parse_size("9MB") == h(9 * 1'000'000, SUPT::decimal)); CHECK(*util::parse_size("9MiB") == h(9 * 1024 * 1024, SUPT::binary)); // Errors -- 2.47.2