From 303384f6f2df676ae3802d6fbde8085c71b572c6 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Mon, 20 Mar 2023 11:16:59 +0200 Subject: [PATCH] Fix clang-tidy warnings (#1334) --- .clang-tidy | 2 +- src/main.cc | 4 +- src/stats/disk_stats.cc | 6 - src/stats/stats.cc | 1 - src/storage/compact_filter.cc | 10 +- src/storage/compact_filter.h | 2 + src/storage/compaction_checker.cc | 4 +- src/storage/redis_db.cc | 22 ++-- src/storage/redis_metadata.cc | 5 +- src/storage/redis_pubsub.cc | 2 + src/storage/scripting.cc | 1 + src/storage/table_properties_collector.cc | 3 +- src/types/geohash.cc | 3 +- src/types/redis_bitmap.cc | 21 ++-- src/types/redis_bitmap_string.cc | 25 ++-- src/types/redis_geo.cc | 4 +- src/types/redis_hash.cc | 3 +- src/types/redis_list.cc | 9 +- src/types/redis_set.cc | 3 +- src/types/redis_sortedint.cc | 14 +-- src/types/redis_string.cc | 8 +- src/types/redis_zset.cc | 22 ++-- tests/cppunit/types/bitmap_test.cc | 11 +- tests/cppunit/types/encoding_test.cc | 2 +- tests/cppunit/types/geo_test.cc | 21 ++-- tests/cppunit/types/hash_test.cc | 6 +- tests/cppunit/types/list_test.cc | 146 +++++++++++----------- tests/cppunit/types/metadata_test.cc | 16 +-- tests/cppunit/types/set_test.cc | 40 +++--- tests/cppunit/types/sortedint_test.cc | 7 +- tests/cppunit/types/string_test.cc | 67 +++++----- tests/cppunit/types/zset_test.cc | 64 +++++----- 32 files changed, 279 insertions(+), 275 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index e1bb8f055d9..a6870e827b6 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,7 +1,7 @@ # refer to https://clang.llvm.org/extra/clang-tidy/checks/list.html Checks: -*, clang-analyzer-core.*, clang-analyzer-cplusplus.*, clang-analyzer-deadcode.*, clang-analyzer-nullability.*, clang-analyzer-security.*, clang-analyzer-unix.*, clang-analyzer-valist.*, cppcoreguidelines-init-variables, cppcoreguidelines-macro-usage, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-no-malloc, cppcoreguidelines-prefer-member-initializer, cppcoreguidelines-special-member-functions, cppcoreguidelines-slicing, google-build-explicit-make-pair, google-default-arguments, google-explicit-constructor, modernize-avoid-bind, modernize-loop-convert, modernize-macro-to-enum, modernize-make-shared, modernize-make-unique, modernize-pass-by-value, modernize-redundant-void-arg, modernize-return-braced-init-list, modernize-use-auto, modernize-use-bool-literals, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, modernize-use-nullptr, modernize-use-override, modernize-use-using, performance-faster-string-find, performance-for-range-copy, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-inefficient-vector-operation, performance-move-const-arg, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, performance-unnecessary-value-param -WarningsAsErrors: clang-analyzer-*, -clang-analyzer-security.insecureAPI.rand, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-no-malloc, cppcoreguidelines-slicing, google-*, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, modernize-use-bool-literals, performance-unnecessary-value-param, modernize-make-unique, performance-for-range-copy, performance-faster-string-find, modernize-redundant-void-arg, modernize-avoid-bind, modernize-use-auto, modernize-use-using, performance-inefficient-vector-operation, cppcoreguidelines-special-member-functions, modernize-loop-convert, cppcoreguidelines-init-variables, modernize-use-nullptr, cppcoreguidelines-macro-usage, cppcoreguidelines-narrowing-conversions +WarningsAsErrors: clang-analyzer-*, -clang-analyzer-security.insecureAPI.rand, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-no-malloc, cppcoreguidelines-slicing, google-*, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, modernize-use-bool-literals, performance-unnecessary-value-param, modernize-make-unique, performance-for-range-copy, performance-faster-string-find, modernize-redundant-void-arg, modernize-avoid-bind, modernize-use-auto, modernize-use-using, performance-inefficient-vector-operation, cppcoreguidelines-special-member-functions, modernize-loop-convert, cppcoreguidelines-init-variables, modernize-use-nullptr, cppcoreguidelines-macro-usage, cppcoreguidelines-narrowing-conversions, modernize-return-braced-init-list CheckOptions: - key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor diff --git a/src/main.cc b/src/main.cc index 7c51b348c4d..280105efe5f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -275,13 +275,15 @@ bool isSupervisedMode(int mode) { static Status createPidFile(const std::string &path) { auto fd = UniqueFD(open(path.data(), O_RDWR | O_CREAT, 0660)); if (!fd) { - return Status(Status::NotOK, strerror(errno)); + return Status::FromErrno(); } + std::string pid_str = std::to_string(getpid()); auto s = Util::Write(*fd, pid_str); if (!s.IsOK()) { return s.Prefixed("failed to write to PID-file"); } + return Status::OK(); } diff --git a/src/stats/disk_stats.cc b/src/stats/disk_stats.cc index 4c3ac181d3d..d17b842a142 100644 --- a/src/stats/disk_stats.cc +++ b/src/stats/disk_stats.cc @@ -20,18 +20,12 @@ #include "disk_stats.h" -#include #include #include -#include -#include #include "db_util.h" #include "rocksdb/status.h" -#include "status.h" #include "storage/redis_metadata.h" -#include "types/redis_bitmap.h" -#include "types/redis_sortedint.h" #include "types/redis_zset.h" namespace Redis { diff --git a/src/stats/stats.cc b/src/stats/stats.cc index d4cfba76205..6ba7a6d3f5f 100644 --- a/src/stats/stats.cc +++ b/src/stats/stats.cc @@ -53,7 +53,6 @@ int64_t Stats::GetMemoryRSS() { #else #include -#include #include #include diff --git a/src/storage/compact_filter.cc b/src/storage/compact_filter.cc index 474c1c1928a..f44bfec9adf 100644 --- a/src/storage/compact_filter.cc +++ b/src/storage/compact_filter.cc @@ -28,6 +28,7 @@ #include "types/redis_bitmap.h" namespace Engine { + using rocksdb::Slice; bool MetadataFilter::Filter(int level, const Slice &key, const Slice &value, std::string *new_value, @@ -66,21 +67,20 @@ Status SubKeyFilter::GetMetadata(const InternalKey &ikey, Metadata *metadata) co // metadata was deleted(perhaps compaction or manual) // clear the metadata cached_metadata_.clear(); - return Status(Status::NotFound, "metadata is not found"); + return {Status::NotFound, "metadata is not found"}; } else { cached_key_.clear(); cached_metadata_.clear(); - return Status(Status::NotOK, "fetch error: " + s.ToString()); + return {Status::NotOK, "fetch error: " + s.ToString()}; } } // the metadata was not found - if (cached_metadata_.empty()) return Status(Status::NotFound, "metadata is not found"); + if (cached_metadata_.empty()) return {Status::NotFound, "metadata is not found"}; // the metadata is cached rocksdb::Status s = metadata->Decode(cached_metadata_); if (!s.ok()) { cached_key_.clear(); - return Status(Status::NotOK, "decode error: " + s.ToString()); - ; + return {Status::NotOK, "decode error: " + s.ToString()}; } return Status::OK(); } diff --git a/src/storage/compact_filter.h b/src/storage/compact_filter.h index 316ba883e9a..b7d180ebf11 100644 --- a/src/storage/compact_filter.h +++ b/src/storage/compact_filter.h @@ -31,6 +31,7 @@ #include "storage.h" namespace Engine { + class MetadataFilter : public rocksdb::CompactionFilter { public: explicit MetadataFilter(Storage *storage) : stor_(storage) {} @@ -122,4 +123,5 @@ class PubSubFilterFactory : public rocksdb::CompactionFilterFactory { return std::unique_ptr(new PubSubFilter()); } }; + } // namespace Engine diff --git a/src/storage/compaction_checker.cc b/src/storage/compaction_checker.cc index a8c6f0be5f0..b440e82e1c4 100644 --- a/src/storage/compaction_checker.cc +++ b/src/storage/compaction_checker.cc @@ -68,7 +68,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) { if (file_creation_time == 0) { // Fallback to the file Modification time to prevent repeatedly compacting the same file, // file_creation_time is 0 which means the unknown condition in rocksdb - auto s = rocksdb::Env::Default()->GetFileModificationTime(iter.first, &file_creation_time); + s = rocksdb::Env::Default()->GetFileModificationTime(iter.first, &file_creation_time); if (!s.ok()) { LOG(INFO) << "[compaction checker] Failed to get the file creation time: " << iter.first << ", err: " << s.ToString(); @@ -107,7 +107,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) { // pick the file which was created more than 2 days if (file_creation_time < static_cast(now - forceCompactSeconds)) { LOG(INFO) << "[compaction checker] Going to compact the key in file(created more than 2 days): " << iter.first; - auto s = storage_->Compact(&start_key, &stop_key); + s = storage_->Compact(&start_key, &stop_key); LOG(INFO) << "[compaction checker] Compact the key in file(created more than 2 days): " << iter.first << " finished, result: " << s.ToString(); maxFilesToCompact--; diff --git a/src/storage/redis_db.cc b/src/storage/redis_db.cc index be3901a19ea..82f4174b52f 100644 --- a/src/storage/redis_db.cc +++ b/src/storage/redis_db.cc @@ -227,7 +227,7 @@ rocksdb::Status Database::Scan(const std::string &cursor, uint64_t limit, const std::vector *keys, std::string *end_cursor) { end_cursor->clear(); uint64_t cnt = 0; - uint16_t slot_id = 0, slot_start = 0; + uint16_t slot_start = 0; std::string ns_prefix, ns_cursor, ns, user_key, value, index_key; LatestSnapShot ss(storage_); @@ -259,7 +259,7 @@ rocksdb::Status Database::Scan(const std::string &cursor, uint64_t limit, const iter->Seek(ns_prefix); } - slot_id = slot_start; + uint16_t slot_id = slot_start; while (true) { for (; iter->Valid() && cnt < limit; iter->Next()) { if (!ns_prefix.empty() && !iter->key().starts_with(ns_prefix)) { @@ -325,8 +325,8 @@ rocksdb::Status Database::RandomKey(const std::string &cursor, std::string *key) return s; } if (keys.empty() && !cursor.empty()) { - // if reach the end, restart from begining - auto s = Scan("", 60, "", &keys, &end_cursor); + // if reach the end, restart from beginning + s = Scan("", 60, "", &keys, &end_cursor); if (!s.ok()) { return s; } @@ -414,13 +414,13 @@ rocksdb::Status Database::Dump(const Slice &user_key, std::vector * infos->emplace_back(created_at_str + "." + std::to_string(created_at.tv_usec)); if (metadata.Type() == kRedisList) { - ListMetadata metadata(false); - GetMetadata(kRedisList, ns_key, &metadata); + ListMetadata list_metadata(false); + GetMetadata(kRedisList, ns_key, &list_metadata); if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s; infos->emplace_back("head"); - infos->emplace_back(std::to_string(metadata.head)); + infos->emplace_back(std::to_string(list_metadata.head)); infos->emplace_back("tail"); - infos->emplace_back(std::to_string(metadata.tail)); + infos->emplace_back(std::to_string(list_metadata.tail)); } return rocksdb::Status::OK(); @@ -518,11 +518,11 @@ rocksdb::Status Database::ClearKeysOfSlot(const rocksdb::Slice &ns, int slot) { rocksdb::Status Database::GetSlotKeysInfo(int slot, std::map *slotskeys, std::vector *keys, int count) { - const rocksdb::Snapshot *snapshot = nullptr; - snapshot = storage_->GetDB()->GetSnapshot(); + LatestSnapShot ss(storage_); rocksdb::ReadOptions read_options; - read_options.snapshot = snapshot; + read_options.snapshot = ss.GetSnapShot(); storage_->SetReadOptions(read_options); + auto iter = DBUtil::UniqueIterator(storage_, read_options, metadata_cf_handle_); bool end = false; for (int i = 0; i < HASH_SLOTS_SIZE; i++) { diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index 717fe3c86fc..fa717c5393c 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -26,7 +26,6 @@ #include #include #include -#include #include "cluster/redis_slot.h" #include "time_util.h" @@ -184,9 +183,9 @@ void Metadata::InitVersionCounter() { } uint64_t Metadata::generateVersion() { - uint64_t version = Util::GetTimeStampUS(); + uint64_t timestamp = Util::GetTimeStampUS(); uint64_t counter = version_counter_.fetch_add(1); - return (version << VersionCounterBits) + (counter % (1 << VersionCounterBits)); + return (timestamp << VersionCounterBits) + (counter % (1 << VersionCounterBits)); } bool Metadata::operator==(const Metadata &that) const { diff --git a/src/storage/redis_pubsub.cc b/src/storage/redis_pubsub.cc index 4c49b7445f3..e07d675b50a 100644 --- a/src/storage/redis_pubsub.cc +++ b/src/storage/redis_pubsub.cc @@ -21,9 +21,11 @@ #include "redis_pubsub.h" namespace Redis { + rocksdb::Status PubSub::Publish(const Slice &channel, const Slice &value) { auto batch = storage_->GetWriteBatchBase(); batch->Put(pubsub_cf_handle_, channel, value); return storage_->Write(storage_->DefaultWriteOptions(), batch->GetWriteBatch()); } + } // namespace Redis diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc index e8eb6b140bf..108982cc3e6 100644 --- a/src/storage/scripting.cc +++ b/src/storage/scripting.cc @@ -49,6 +49,7 @@ enum { }; namespace Lua { + lua_State *CreateState(bool read_only) { lua_State *lua = lua_open(); loadLibraries(lua); diff --git a/src/storage/table_properties_collector.cc b/src/storage/table_properties_collector.cc index 99418be1158..4bb998164b1 100644 --- a/src/storage/table_properties_collector.cc +++ b/src/storage/table_properties_collector.cc @@ -30,7 +30,6 @@ rocksdb::Status CompactOnExpiredCollector::AddUserKey(const rocksdb::Slice &key, const rocksdb::Slice &value, rocksdb::EntryType entry_type, rocksdb::SequenceNumber, uint64_t) { - int now = 0; uint8_t type = 0; uint32_t expired = 0, subkeys = 0; uint64_t version = 0; @@ -62,7 +61,7 @@ rocksdb::Status CompactOnExpiredCollector::AddUserKey(const rocksdb::Slice &key, GetFixed32(&cv, &subkeys); } total_keys_ += subkeys; - now = Server::GetCachedUnixTime(); + int now = Server::GetCachedUnixTime(); if ((expired > 0 && expired < static_cast(now)) || (type != kRedisString && subkeys == 0)) { deleted_keys_ += subkeys + 1; } diff --git a/src/types/geohash.cc b/src/types/geohash.cc index 583c57c75a0..b5ac26d5e51 100644 --- a/src/types/geohash.cc +++ b/src/types/geohash.cc @@ -364,7 +364,6 @@ GeoHashRadius GeoHashHelper::GetAreasByRadius(double longitude, double latitude, GeoHashArea area; double min_lon = NAN, max_lon = NAN, min_lat = NAN, max_lat = NAN; double bounds[4]; - int steps = 0; BoundingBox(longitude, latitude, radius_meters, bounds); min_lon = bounds[0]; @@ -372,7 +371,7 @@ GeoHashRadius GeoHashHelper::GetAreasByRadius(double longitude, double latitude, max_lon = bounds[2]; max_lat = bounds[3]; - steps = EstimateStepsByRadius(radius_meters, latitude); + int steps = EstimateStepsByRadius(radius_meters, latitude); geohashGetCoordRange(&long_range, &lat_range); geohashEncode(&long_range, &lat_range, longitude, latitude, steps, &hash); diff --git a/src/types/redis_bitmap.cc b/src/types/redis_bitmap.cc index 59c1cd388ac..2f01164ee0d 100644 --- a/src/types/redis_bitmap.cc +++ b/src/types/redis_bitmap.cc @@ -122,7 +122,6 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos LatestSnapShot ss(storage_); read_options.snapshot = ss.GetSnapShot(); storage_->SetReadOptions(read_options); - uint32_t frag_index = 0, valid_size = 0; auto iter = DBUtil::UniqueIterator(storage_, read_options); for (iter->Seek(prefix_key); iter->Valid() && iter->key().starts_with(prefix_key); iter->Next()) { @@ -131,11 +130,11 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos if (!parse_result) { return rocksdb::Status::InvalidArgument(parse_result.Msg()); } - frag_index = *parse_result; + uint32_t frag_index = *parse_result; fragment = iter->value().ToString(); // To be compatible with data written before the commit d603b0e(#338) // and avoid returning extra null char after expansion. - valid_size = std::min( + uint32_t valid_size = std::min( {fragment.size(), static_cast(kBitmapSegmentBytes), static_cast(metadata.size - frag_index)}); /* @@ -166,7 +165,6 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos for (uint32_t i = 0; i < valid_size; i++) { if (!fragment[i]) continue; fragment[i] = static_cast(swap_table[static_cast(fragment[i])]); - ; } value->replace(frag_index, valid_size, fragment.data(), valid_size); } @@ -352,14 +350,13 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co AppendNamespacePrefix(user_key, &ns_key); LockGuard guard(storage_->GetLockManager(), ns_key); - rocksdb::Status s; std::vector> meta_pairs; uint64_t max_size = 0, num_keys = op_keys.size(); for (const auto &op_key : op_keys) { BitmapMetadata metadata(false); AppendNamespacePrefix(op_key, &ns_op_key); - s = GetMetadata(ns_op_key, &metadata, &raw_value); + auto s = GetMetadata(ns_op_key, &metadata, &raw_value); if (!s.ok()) { if (s.IsNotFound()) { num_keys--; @@ -388,7 +385,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co BitmapMetadata res_metadata; if (num_keys == op_keys.size() || op_flag != kBitOpAnd) { - uint64_t i = 0, frag_numkeys = num_keys, stop_index = (max_size - 1) / kBitmapSegmentBytes; + uint64_t frag_numkeys = num_keys, stop_index = (max_size - 1) / kBitmapSegmentBytes; std::unique_ptr frag_res(new unsigned char[kBitmapSegmentBytes]); uint16_t frag_maxlen = 0, frag_minlen = 0; std::string sub_key, fragment; @@ -432,14 +429,14 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co if (frag_minlen >= sizeof(uint64_t) * 4 && frag_numkeys <= 16) { auto *lres = reinterpret_cast(frag_res.get()); const uint64_t *lp[16]; - for (i = 0; i < frag_numkeys; i++) { + for (uint64_t i = 0; i < frag_numkeys; i++) { lp[i] = reinterpret_cast(fragments[i].data()); } memcpy(frag_res.get(), fragments[0].data(), frag_minlen); if (op_flag == kBitOpAnd) { while (frag_minlen >= sizeof(uint64_t) * 4) { - for (i = 1; i < frag_numkeys; i++) { + for (uint64_t i = 1; i < frag_numkeys; i++) { lres[0] &= lp[i][0]; lres[1] &= lp[i][1]; lres[2] &= lp[i][2]; @@ -452,7 +449,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co } } else if (op_flag == kBitOpOr) { while (frag_minlen >= sizeof(uint64_t) * 4) { - for (i = 1; i < frag_numkeys; i++) { + for (uint64_t i = 1; i < frag_numkeys; i++) { lres[0] |= lp[i][0]; lres[1] |= lp[i][1]; lres[2] |= lp[i][2]; @@ -465,7 +462,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co } } else if (op_flag == kBitOpXor) { while (frag_minlen >= sizeof(uint64_t) * 4) { - for (i = 1; i < frag_numkeys; i++) { + for (uint64_t i = 1; i < frag_numkeys; i++) { lres[0] ^= lp[i][0]; lres[1] ^= lp[i][1]; lres[2] ^= lp[i][2]; @@ -493,7 +490,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co for (; j < frag_maxlen; j++) { output = (fragments[0].size() <= j) ? 0 : fragments[0][j]; if (op_flag == kBitOpNot) output = ~output; - for (i = 1; i < frag_numkeys; i++) { + for (uint64_t i = 1; i < frag_numkeys; i++) { byte = (fragments[i].size() <= j) ? 0 : fragments[i][j]; switch (op_flag) { case kBitOpAnd: diff --git a/src/types/redis_bitmap_string.cc b/src/types/redis_bitmap_string.cc index 7c4311942e0..d68cd656658 100644 --- a/src/types/redis_bitmap_string.cc +++ b/src/types/redis_bitmap_string.cc @@ -31,12 +31,12 @@ extern const uint8_t kNum2Bits[256]; rocksdb::Status BitmapString::GetBit(const std::string &raw_value, uint32_t offset, bool *bit) { auto string_value = raw_value.substr(STRING_HDR_SIZE, raw_value.size() - STRING_HDR_SIZE); uint32_t byte_index = offset >> 3; - uint32_t bitval = 0; + uint32_t bit_val = 0; uint32_t bit_offset = 7 - (offset & 0x7); if (byte_index < string_value.size()) { - bitval = string_value[byte_index] & (1 << bit_offset); + bit_val = string_value[byte_index] & (1 << bit_offset); } - *bit = bitval == 0 ? false : true; + *bit = bit_val != 0; return rocksdb::Status::OK(); } @@ -130,7 +130,6 @@ rocksdb::Status BitmapString::BitPos(const std::string &raw_value, bool bit, int * */ size_t BitmapString::redisPopcount(unsigned char *p, int64_t count) { size_t bits = 0; - uint32_t *p4 = nullptr; /* Count initial bytes not aligned to 32 bit. */ while (reinterpret_cast(p) & 3 && count) { @@ -139,17 +138,15 @@ size_t BitmapString::redisPopcount(unsigned char *p, int64_t count) { } /* Count bits 28 bytes at a time */ - p4 = reinterpret_cast(p); + auto p4 = reinterpret_cast(p); while (count >= 28) { - uint32_t aux1 = 0, aux2 = 0, aux3 = 0, aux4 = 0, aux5 = 0, aux6 = 0, aux7 = 0; - - aux1 = *p4++; - aux2 = *p4++; - aux3 = *p4++; - aux4 = *p4++; - aux5 = *p4++; - aux6 = *p4++; - aux7 = *p4++; + uint32_t aux1 = *p4++; + uint32_t aux2 = *p4++; + uint32_t aux3 = *p4++; + uint32_t aux4 = *p4++; + uint32_t aux5 = *p4++; + uint32_t aux6 = *p4++; + uint32_t aux7 = *p4++; count -= 28; aux1 = aux1 - ((aux1 >> 1) & 0x55555555); diff --git a/src/types/redis_geo.cc b/src/types/redis_geo.cc index d4c2740923e..36f4c8ce413 100644 --- a/src/types/redis_geo.cc +++ b/src/types/redis_geo.cc @@ -211,7 +211,7 @@ int Geo::decodeGeoHash(double bits, double *xy) { int Geo::membersOfAllNeighbors(const Slice &user_key, GeoHashRadius n, double lon, double lat, double radius, std::vector *geo_points) { GeoHashBits neighbors[9]; - unsigned int i = 0, last_processed = 0; + unsigned int last_processed = 0; int count = 0; neighbors[0] = n.hash; @@ -226,7 +226,7 @@ int Geo::membersOfAllNeighbors(const Slice &user_key, GeoHashRadius n, double lo /* For each neighbor (*and* our own hashbox), get all the matching * members and add them to the potential result list. */ - for (i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) { + for (unsigned int i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) { if (HASHISZERO(neighbors[i])) { continue; } diff --git a/src/types/redis_hash.cc b/src/types/redis_hash.cc index 91c25507065..2a52fdb10b2 100644 --- a/src/types/redis_hash.cc +++ b/src/types/redis_hash.cc @@ -238,13 +238,12 @@ rocksdb::Status Hash::MSet(const Slice &user_key, const std::vector if (!s.ok() && !s.IsNotFound()) return s; int added = 0; - bool exists = false; auto batch = storage_->GetWriteBatchBase(); WriteBatchLogData log_data(kRedisHash); batch->PutLogData(log_data.Encode()); for (const auto &fv : field_values) { - exists = false; + bool exists = false; std::string sub_key; InternalKey(ns_key, fv.field, metadata.version, storage_->IsSlotIdEncoded()).Encode(&sub_key); diff --git a/src/types/redis_list.cc b/src/types/redis_list.cc index 3bdbf5ae6f8..6ad826c4fbe 100644 --- a/src/types/redis_list.cc +++ b/src/types/redis_list.cc @@ -226,16 +226,16 @@ rocksdb::Status List::Rem(const Slice &user_key, int count, const Slice &elem, i buf.clear(); PutFixed64(&buf, reversed ? max_to_delete_index : min_to_delete_index); InternalKey(ns_key, buf, metadata.version, storage_->IsSlotIdEncoded()).Encode(&start_key); - size_t count = 0; + size_t processed = 0; for (iter->Seek(start_key); iter->Valid() && iter->key().starts_with(prefix); !reversed ? iter->Next() : iter->Prev()) { - if (iter->value() != elem || count >= to_delete_indexes.size()) { + if (iter->value() != elem || processed >= to_delete_indexes.size()) { buf.clear(); PutFixed64(&buf, reversed ? max_to_delete_index-- : min_to_delete_index++); InternalKey(ns_key, buf, metadata.version, storage_->IsSlotIdEncoded()).Encode(&to_update_key); batch->Put(to_update_key, iter->value()); } else { - count++; + processed++; } } @@ -271,7 +271,7 @@ rocksdb::Status List::Insert(const Slice &user_key, const Slice &pivot, const Sl if (!s.ok()) return s; std::string buf, start_key, prefix, next_version_prefix; - uint64_t pivot_index = metadata.head - 1, new_elem_index = 0; + uint64_t pivot_index = metadata.head - 1; PutFixed64(&buf, metadata.head); InternalKey(ns_key, buf, metadata.version, storage_->IsSlotIdEncoded()).Encode(&start_key); InternalKey(ns_key, "", metadata.version, storage_->IsSlotIdEncoded()).Encode(&prefix); @@ -307,6 +307,7 @@ rocksdb::Status List::Insert(const Slice &user_key, const Slice &pivot, const Sl uint64_t left_part_len = pivot_index - metadata.head + (before ? 0 : 1); uint64_t right_part_len = metadata.tail - 1 - pivot_index + (before ? 1 : 0); bool reversed = left_part_len <= right_part_len; + uint64_t new_elem_index = 0; if ((reversed && !before) || (!reversed && before)) { new_elem_index = pivot_index; } else { diff --git a/src/types/redis_set.cc b/src/types/redis_set.cc index 4a9198bf755..e7ff397fb8c 100644 --- a/src/types/redis_set.cc +++ b/src/types/redis_set.cc @@ -20,7 +20,6 @@ #include "redis_set.h" -#include #include #include @@ -340,7 +339,7 @@ rocksdb::Status Set::Inter(const std::vector &keys, std::vector #include -#include #include "db_util.h" #include "parse_util.h" @@ -238,23 +237,21 @@ rocksdb::Status Sortedint::MExist(const Slice &user_key, const std::vector max"); + return {Status::NotOK, "min > max"}; } if (min == "-inf") { spec->min = std::numeric_limits::lowest(); } else { - sptr = min.data(); + const char *sptr = min.data(); if (!min.empty() && min[0] == '(') { spec->minex = true; sptr++; } auto parse_result = ParseInt(sptr, 10); if (!parse_result) { - return Status(Status::NotOK, "the min isn't integer"); + return {Status::NotOK, "the min isn't integer"}; } spec->min = *parse_result; } @@ -262,17 +259,18 @@ Status Sortedint::ParseRangeSpec(const std::string &min, const std::string &max, if (max == "+inf") { spec->max = std::numeric_limits::max(); } else { - sptr = max.data(); + const char *sptr = max.data(); if (!max.empty() && max[0] == '(') { spec->maxex = true; sptr++; } auto parse_result = ParseInt(sptr, 10); if (!parse_result) { - return Status(Status::NotOK, "the max isn't integer"); + return {Status::NotOK, "the max isn't integer"}; } spec->max = *parse_result; } return Status::OK(); } + } // namespace Redis diff --git a/src/types/redis_string.cc b/src/types/redis_string.cc index d6631ec92fa..3024dd0c4e3 100644 --- a/src/types/redis_string.cc +++ b/src/types/redis_string.cc @@ -241,7 +241,6 @@ rocksdb::Status String::SetXX(const std::string &user_key, const std::string &va } rocksdb::Status String::SetRange(const std::string &user_key, int offset, const std::string &value, int *ret) { - int size = 0; std::string ns_key; AppendNamespacePrefix(user_key, &ns_key); @@ -251,16 +250,17 @@ rocksdb::Status String::SetRange(const std::string &user_key, int offset, const if (!s.ok() && !s.IsNotFound()) return s; if (s.IsNotFound()) { - // Return 0 directly instead of storing an empty key - // when set nothing on a non-existing string. + // Return 0 directly instead of storing an empty key when set nothing on a non-existing string. if (value.empty()) { *ret = 0; return rocksdb::Status::OK(); } + Metadata metadata(kRedisString, false); metadata.Encode(&raw_value); } - size = static_cast(raw_value.size()); + + int size = static_cast(raw_value.size()); offset += STRING_HDR_SIZE; if (offset > size) { // padding the value with zero byte while offset is longer than value size diff --git a/src/types/redis_zset.cc b/src/types/redis_zset.cc index 4f492b6c7ea..5f03150fc51 100644 --- a/src/types/redis_zset.cc +++ b/src/types/redis_zset.cc @@ -20,8 +20,6 @@ #include "redis_zset.h" -#include - #include #include #include @@ -664,15 +662,18 @@ rocksdb::Status ZSet::InterStore(const Slice &dst, const std::vector ZRangeSpec spec; auto s = RangeByScore(keys_weights[0].key, spec, &target_mscores, &target_size); if (!s.ok() || target_mscores.empty()) return s; + for (const auto &ms : target_mscores) { double score = ms.score * keys_weights[0].weight; if (std::isnan(score)) score = 0; dst_zset[ms.member] = score; member_counters[ms.member] = 1; } + for (size_t i = 1; i < keys_weights.size(); i++) { - auto s = RangeByScore(keys_weights[i].key, spec, &target_mscores, &target_size); + s = RangeByScore(keys_weights[i].key, spec, &target_mscores, &target_size); if (!s.ok() || target_mscores.empty()) return s; + for (const auto &ms : target_mscores) { if (dst_zset.find(ms.member) == dst_zset.end()) continue; member_counters[ms.member]++; @@ -762,38 +763,37 @@ rocksdb::Status ZSet::UnionStore(const Slice &dst, const std::vector } Status ZSet::ParseRangeSpec(const std::string &min, const std::string &max, ZRangeSpec *spec) { - const char *sptr = nullptr; char *eptr = nullptr; if (min == "+inf" || max == "-inf") { - return Status(Status::NotOK, "min > max"); + return {Status::NotOK, "min > max"}; } if (min == "-inf") { spec->min = kMinScore; } else { - sptr = min.data(); + const char *sptr = min.data(); if (!min.empty() && min[0] == '(') { spec->minex = true; sptr++; } spec->min = strtod(sptr, &eptr); - if ((eptr && eptr[0] != '\0') || isnan(spec->min)) { - return Status(Status::NotOK, "the min isn't double"); + if ((eptr && eptr[0] != '\0') || std::isnan(spec->min)) { + return {Status::NotOK, "the min isn't double"}; } } if (max == "+inf") { spec->max = kMaxScore; } else { - sptr = max.data(); + const char *sptr = max.data(); if (!max.empty() && max[0] == '(') { spec->maxex = true; sptr++; } spec->max = strtod(sptr, &eptr); - if ((eptr && eptr[0] != '\0') || isnan(spec->max)) { - return Status(Status::NotOK, "the max isn't double"); + if ((eptr && eptr[0] != '\0') || std::isnan(spec->max)) { + return {Status::NotOK, "the max isn't double"}; } } return Status::OK(); diff --git a/tests/cppunit/types/bitmap_test.cc b/tests/cppunit/types/bitmap_test.cc index f17eae6f0a9..051a0fce2c7 100644 --- a/tests/cppunit/types/bitmap_test.cc +++ b/tests/cppunit/types/bitmap_test.cc @@ -28,7 +28,8 @@ class RedisBitmapTest : public TestBase { protected: explicit RedisBitmapTest() : TestBase() { bitmap = std::make_unique(storage_, "bitmap_ns"); } - ~RedisBitmapTest() = default; + ~RedisBitmapTest() override = default; + void SetUp() override { key_ = "test_bitmap_key"; } void TearDown() override {} @@ -55,7 +56,7 @@ TEST_F(RedisBitmapTest, BitCount) { bool bit = false; bitmap->SetBit(key_, offset, true, &bit); } - uint32_t cnt; + uint32_t cnt = 0; bitmap->BitCount(key_, 0, 4 * 1024, &cnt); EXPECT_EQ(cnt, 6); bitmap->BitCount(key_, 0, -1, &cnt); @@ -64,8 +65,8 @@ TEST_F(RedisBitmapTest, BitCount) { } TEST_F(RedisBitmapTest, BitPosClearBit) { - int64_t pos; - bool old_bit; + int64_t pos = 0; + bool old_bit = false; for (int i = 0; i < 1024 + 16; i++) { bitmap->BitPos(key_, false, 0, -1, true, &pos); EXPECT_EQ(pos, i); @@ -81,7 +82,7 @@ TEST_F(RedisBitmapTest, BitPosSetBit) { bool bit = false; bitmap->SetBit(key_, offset, true, &bit); } - int64_t pos; + int64_t pos = 0; int start_indexes[] = {0, 1, 124, 1025, 1027, 3 * 1024 + 1}; for (size_t i = 0; i < sizeof(start_indexes) / sizeof(start_indexes[0]); i++) { bitmap->BitPos(key_, true, start_indexes[i], -1, true, &pos); diff --git a/tests/cppunit/types/encoding_test.cc b/tests/cppunit/types/encoding_test.cc index 2658a9be159..82272abe206 100644 --- a/tests/cppunit/types/encoding_test.cc +++ b/tests/cppunit/types/encoding_test.cc @@ -50,7 +50,7 @@ TEST(Util, EncodeAndDecodeInt32AsVarint32) { std::string buf; PutVarint32(&buf, values[i]); EXPECT_EQ(buf.size(), encoded_sizes[i]); - uint32_t result; + uint32_t result = 0; rocksdb::Slice s(buf); GetVarint32(&s, &result); ASSERT_EQ(result, values[i]); diff --git a/tests/cppunit/types/geo_test.cc b/tests/cppunit/types/geo_test.cc index b42da22e48f..d08522c53b4 100644 --- a/tests/cppunit/types/geo_test.cc +++ b/tests/cppunit/types/geo_test.cc @@ -29,8 +29,9 @@ class RedisGeoTest : public TestBase { protected: RedisGeoTest() : TestBase() { geo = std::make_unique(storage_, "geo_ns"); } - ~RedisGeoTest() = default; - void SetUp() { + ~RedisGeoTest() override = default; + + void SetUp() override { key_ = "test_geo_key"; fields_ = {"geo_test_key-1", "geo_test_key-2", "geo_test_key-3", "geo_test_key-4", "geo_test_key-5", "geo_test_key-6", "geo_test_key-7"}; @@ -48,7 +49,7 @@ class RedisGeoTest : public TestBase { }; TEST_F(RedisGeoTest, Add) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); @@ -66,21 +67,21 @@ TEST_F(RedisGeoTest, Add) { } TEST_F(RedisGeoTest, Dist) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); } geo->Add(key_, &geo_points, &ret); EXPECT_EQ(fields_.size(), ret); - double dist; + double dist = 0.0; geo->Dist(key_, fields_[2], fields_[3], &dist); EXPECT_EQ(ceilf(dist), 194102); geo->Del(key_); } TEST_F(RedisGeoTest, Hash) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); @@ -96,7 +97,7 @@ TEST_F(RedisGeoTest, Hash) { } TEST_F(RedisGeoTest, Pos) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); @@ -114,7 +115,7 @@ TEST_F(RedisGeoTest, Pos) { } TEST_F(RedisGeoTest, Radius) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); @@ -132,7 +133,7 @@ TEST_F(RedisGeoTest, Radius) { } TEST_F(RedisGeoTest, RadiusByMember) { - int ret; + int ret = 0; std::vector geo_points; for (size_t i = 0; i < fields_.size(); i++) { geo_points.emplace_back(GeoPoint{longitudes_[i], latitudes_[i], fields_[i].ToString()}); @@ -147,4 +148,4 @@ TEST_F(RedisGeoTest, RadiusByMember) { EXPECT_EQ(geo->EncodeGeoHash(gps[i].longitude, gps[i].latitude), geoHashes_[i]); } geo->Del(key_); -} \ No newline at end of file +} diff --git a/tests/cppunit/types/hash_test.cc b/tests/cppunit/types/hash_test.cc index f51c9111ae1..2d64b7af3eb 100644 --- a/tests/cppunit/types/hash_test.cc +++ b/tests/cppunit/types/hash_test.cc @@ -66,7 +66,7 @@ TEST_F(RedisHashTest, MGetAndMSet) { int ret = 0; std::vector fvs; for (size_t i = 0; i < fields_.size(); i++) { - fvs.emplace_back(FieldValue{fields_[i].ToString(), values_[i].ToString()}); + fvs.emplace_back(fields_[i].ToString(), values_[i].ToString()); } auto s = hash->MSet(key_, fvs, false, &ret); EXPECT_TRUE(s.ok() && static_cast(fvs.size()) == ret); @@ -204,10 +204,10 @@ TEST_F(RedisHashTest, HRangeByLex) { int ret = 0; std::vector fvs; for (size_t i = 0; i < 4; i++) { - fvs.emplace_back(FieldValue{"key" + std::to_string(i), "value" + std::to_string(i)}); + fvs.emplace_back("key" + std::to_string(i), "value" + std::to_string(i)); } for (size_t i = 0; i < 26; i++) { - fvs.emplace_back(FieldValue{std::to_string(char(i + 'a')), std::to_string(char(i + 'a'))}); + fvs.emplace_back(std::to_string(char(i + 'a')), std::to_string(char(i + 'a'))); } std::random_device rd; diff --git a/tests/cppunit/types/list_test.cc b/tests/cppunit/types/list_test.cc index 3020a0d224a..242dab1c2a4 100644 --- a/tests/cppunit/types/list_test.cc +++ b/tests/cppunit/types/list_test.cc @@ -28,7 +28,8 @@ class RedisListTest : public TestBase { protected: explicit RedisListTest() : TestBase() { list = std::make_unique(storage_, "list_ns"); } - ~RedisListTest() = default; + ~RedisListTest() override = default; + void SetUp() override { key_ = "test-list-key"; fields_ = {"list-test-key-1", "list-test-key-2", "list-test-key-3", "list-test-key-4", "list-test-key-5", @@ -81,48 +82,49 @@ class RedisListLMoveTest : public RedisListTest { }; TEST_F(RedisListTest, PushAndPop) { - int ret; + int ret = 0; list->Push(key_, fields_, true, &ret); EXPECT_EQ(fields_.size(), ret); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(key_, false, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } list->Del(key_); } TEST_F(RedisListTest, Pushx) { - int ret; + int ret = 0; Slice pushx_key("test-pushx-key"); rocksdb::Status s = list->PushX(pushx_key, fields_, true, &ret); EXPECT_TRUE(s.ok()); list->Push(pushx_key, fields_, true, &ret); EXPECT_EQ(fields_.size(), ret); s = list->PushX(pushx_key, fields_, true, &ret); + EXPECT_TRUE(s.ok()); EXPECT_EQ(ret, fields_.size() * 2); list->Del(pushx_key); } TEST_F(RedisListTest, Index) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::string elem; for (size_t i = 0; i < fields_.size(); i++) { - list->Index(key_, i, &elem); + list->Index(key_, static_cast(i), &elem); EXPECT_EQ(fields_[i].ToString(), elem); } - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } rocksdb::Status s = list->Index(key_, -1, &elem); EXPECT_TRUE(s.IsNotFound()); @@ -130,7 +132,7 @@ TEST_F(RedisListTest, Index) { } TEST_F(RedisListTest, Set) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); Slice new_elem("new_elem"); @@ -145,7 +147,7 @@ TEST_F(RedisListTest, Set) { } TEST_F(RedisListTest, Range) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::vector elems; @@ -154,17 +156,17 @@ TEST_F(RedisListTest, Range) { for (size_t i = 0; i < elems.size(); i++) { EXPECT_EQ(fields_[i].ToString(), elems[i]); } - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } list->Del(key_); } TEST_F(RedisListTest, Rem) { - int ret; - uint32_t len; + int ret = 0; + uint32_t len = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); Slice del_elem("list-test-key-1"); @@ -185,11 +187,11 @@ TEST_F(RedisListTest, Rem) { EXPECT_EQ(4, ret); list->Size(key_, &len); EXPECT_EQ(fields_.size() - 4, len); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; - if (fields_[i] == del_elem) continue; + if (field == del_elem) continue; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } // lrem key_ 1 nosuchelement Slice no_elem("no_such_element"); @@ -199,10 +201,10 @@ TEST_F(RedisListTest, Rem) { EXPECT_EQ(0, ret); list->Size(key_, &len); EXPECT_EQ(fields_.size(), len); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } // lrem key_ -1 list-test-key-1 list->Push(key_, fields_, false, &ret); @@ -211,13 +213,13 @@ TEST_F(RedisListTest, Rem) { list->Size(key_, &len); EXPECT_EQ(fields_.size() - 1, len); int cnt = 0; - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; - if (fields_[i] == del_elem) { + if (field == del_elem) { if (++cnt > 3) continue; } list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } // lrem key_ -5 list-test-key-1 list->Push(key_, fields_, false, &ret); @@ -226,34 +228,34 @@ TEST_F(RedisListTest, Rem) { EXPECT_EQ(4, ret); list->Size(key_, &len); EXPECT_EQ(fields_.size() - 4, len); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; - if (fields_[i] == del_elem) continue; + if (field == del_elem) continue; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } list->Del(key_); } TEST_F(RedisListSpecificTest, Rem) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); Slice del_elem("9"); // lrem key_ 1 9 list->Rem(key_, 1, del_elem, &ret); EXPECT_EQ(1, ret); - uint32_t len; + uint32_t len = 0; list->Size(key_, &len); EXPECT_EQ(fields_.size() - 1, len); int cnt = 0; - for (size_t i = 0; i < fields_.size(); i++) { - if (fields_[i] == del_elem) { + for (auto &field : fields_) { + if (field == del_elem) { if (++cnt <= 1) continue; } std::string elem; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } // lrem key_ -2 9 list->Push(key_, fields_, false, &ret); @@ -274,11 +276,11 @@ TEST_F(RedisListSpecificTest, Rem) { } TEST_F(RedisListTest, Trim) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); list->Trim(key_, 1, 2000); - uint32_t len; + uint32_t len = 0; list->Size(key_, &len); EXPECT_EQ(fields_.size() - 1, len); for (size_t i = 1; i < fields_.size(); i++) { @@ -290,13 +292,13 @@ TEST_F(RedisListTest, Trim) { } TEST_F(RedisListSpecificTest, Trim) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); // ltrim key_ 3 -3 then linsert 2 3 and lrem key_ 5 3 Slice del_elem("3"); list->Trim(key_, 3, -3); - uint32_t len; + uint32_t len = 0; list->Size(key_, &len); EXPECT_EQ(fields_.size() - 5, len); Slice insert_elem("3"); @@ -314,19 +316,19 @@ TEST_F(RedisListSpecificTest, Trim) { } TEST_F(RedisListTest, RPopLPush) { - int ret; + int ret = 0; list->Push(key_, fields_, true, &ret); EXPECT_EQ(fields_.size(), ret); Slice dst("test-list-rpoplpush-key"); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->RPopLPush(key_, dst, &elem); - EXPECT_EQ(fields_[i].ToString(), elem); + EXPECT_EQ(field.ToString(), elem); } - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(dst, false, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } list->Del(key_); list->Del(dst); @@ -341,7 +343,7 @@ TEST_F(RedisListLMoveTest, LMoveSrcNotExist) { } TEST_F(RedisListLMoveTest, LMoveSrcAndDstAreTheSameSingleElem) { - int ret; + int ret = 0; Slice element = fields_[0]; list->Push(key_, {element}, false, &ret); EXPECT_EQ(1, ret); @@ -349,45 +351,47 @@ TEST_F(RedisListLMoveTest, LMoveSrcAndDstAreTheSameSingleElem) { auto s = list->LMove(key_, key_, true, true, &expected_elem); EXPECT_EQ(expected_elem, element); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[0]}); + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[0]}); } TEST_F(RedisListLMoveTest, LMoveSrcAndDstAreTheSameManyElemsLeftRight) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::string elem; auto s = list->LMove(key_, key_, true, false, &elem); EXPECT_TRUE(s.ok()); EXPECT_EQ(elem, fields_[0].ToString()); - listElementsAreEqualTo(key_, 0, fields_.size() + 1, {fields_[1], fields_[2], fields_[3], fields_[0]}); + listElementsAreEqualTo(key_, 0, static_cast(fields_.size() + 1), + {fields_[1], fields_[2], fields_[3], fields_[0]}); } TEST_F(RedisListLMoveTest, LMoveSrcAndDstAreTheSameManyElemsRightLeft) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::string elem; auto s = list->LMove(key_, key_, false, true, &elem); EXPECT_TRUE(s.ok()); EXPECT_EQ(elem, fields_[fields_.size() - 1].ToString()); - listElementsAreEqualTo(key_, 0, fields_.size() + 1, {fields_[3], fields_[0], fields_[1], fields_[2]}); + listElementsAreEqualTo(key_, 0, static_cast(fields_.size() + 1), + {fields_[3], fields_[0], fields_[1], fields_[2]}); } TEST_F(RedisListLMoveTest, LMoveDstNotExist) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::string elem; auto s = list->LMove(key_, dst_key_, true, false, &elem); EXPECT_EQ(elem, fields_[0].ToString()); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[1], fields_[2], fields_[3]}); - listElementsAreEqualTo(dst_key_, 0, dst_fields_.size(), {fields_[0]}); + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[1], fields_[2], fields_[3]}); + listElementsAreEqualTo(dst_key_, 0, static_cast(dst_fields_.size()), {fields_[0]}); } TEST_F(RedisListLMoveTest, LMoveSrcLeftDstLeft) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); list->Push(dst_key_, dst_fields_, false, &ret); @@ -396,13 +400,13 @@ TEST_F(RedisListLMoveTest, LMoveSrcLeftDstLeft) { auto s = list->LMove(key_, dst_key_, true, true, &elem); EXPECT_EQ(elem, fields_[0].ToString()); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[1], fields_[2], fields_[3]}); - listElementsAreEqualTo(dst_key_, 0, dst_fields_.size() + 1, + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[1], fields_[2], fields_[3]}); + listElementsAreEqualTo(dst_key_, 0, static_cast(dst_fields_.size() + 1), {fields_[0], dst_fields_[0], dst_fields_[1], dst_fields_[2], dst_fields_[3]}); } TEST_F(RedisListLMoveTest, LMoveSrcLeftDstRight) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); list->Push(dst_key_, dst_fields_, false, &ret); @@ -411,13 +415,13 @@ TEST_F(RedisListLMoveTest, LMoveSrcLeftDstRight) { auto s = list->LMove(key_, dst_key_, true, false, &elem); EXPECT_EQ(elem, fields_[0].ToString()); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[1], fields_[2], fields_[3]}); - listElementsAreEqualTo(dst_key_, 0, dst_fields_.size() + 1, + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[1], fields_[2], fields_[3]}); + listElementsAreEqualTo(dst_key_, 0, static_cast(dst_fields_.size() + 1), {dst_fields_[0], dst_fields_[1], dst_fields_[2], dst_fields_[3], fields_[0]}); } TEST_F(RedisListLMoveTest, LMoveSrcRightDstLeft) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); list->Push(dst_key_, dst_fields_, false, &ret); @@ -426,13 +430,13 @@ TEST_F(RedisListLMoveTest, LMoveSrcRightDstLeft) { auto s = list->LMove(key_, dst_key_, false, true, &elem); EXPECT_EQ(elem, fields_[3].ToString()); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[0], fields_[1], fields_[2]}); - listElementsAreEqualTo(dst_key_, 0, dst_fields_.size() + 1, + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[0], fields_[1], fields_[2]}); + listElementsAreEqualTo(dst_key_, 0, static_cast(dst_fields_.size() + 1), {fields_[3], dst_fields_[0], dst_fields_[1], dst_fields_[2], dst_fields_[3]}); } TEST_F(RedisListLMoveTest, LMoveSrcRightDstRight) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); list->Push(dst_key_, dst_fields_, false, &ret); @@ -441,8 +445,8 @@ TEST_F(RedisListLMoveTest, LMoveSrcRightDstRight) { auto s = list->LMove(key_, dst_key_, false, false, &elem); EXPECT_EQ(elem, fields_[3].ToString()); EXPECT_TRUE(s.ok()); - listElementsAreEqualTo(key_, 0, fields_.size(), {fields_[0], fields_[1], fields_[2]}); - listElementsAreEqualTo(dst_key_, 0, dst_fields_.size() + 1, + listElementsAreEqualTo(key_, 0, static_cast(fields_.size()), {fields_[0], fields_[1], fields_[2]}); + listElementsAreEqualTo(dst_key_, 0, static_cast(dst_fields_.size() + 1), {dst_fields_[0], dst_fields_[1], dst_fields_[2], dst_fields_[3], fields_[3]}); } @@ -458,13 +462,13 @@ TEST_F(RedisListTest, LPopEmptyList) { } TEST_F(RedisListTest, LPopOneElement) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); - for (size_t i = 0; i < fields_.size(); i++) { + for (auto &field : fields_) { std::string elem; list->Pop(key_, true, &elem); - EXPECT_EQ(elem, fields_[i].ToString()); + EXPECT_EQ(elem, field.ToString()); } std::string elem; auto s = list->Pop(key_, true, &elem); @@ -473,7 +477,7 @@ TEST_F(RedisListTest, LPopOneElement) { } TEST_F(RedisListTest, LPopMulti) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::vector elems; @@ -488,7 +492,7 @@ TEST_F(RedisListTest, LPopMulti) { } TEST_F(RedisListTest, LPopMultiCountGreaterThanListSize) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::vector elems; @@ -513,7 +517,7 @@ TEST_F(RedisListTest, RPopEmptyList) { } TEST_F(RedisListTest, RPopOneElement) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); for (size_t i = 0; i < fields_.size(); i++) { @@ -528,7 +532,7 @@ TEST_F(RedisListTest, RPopOneElement) { } TEST_F(RedisListTest, RPopMulti) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::vector elems; @@ -543,7 +547,7 @@ TEST_F(RedisListTest, RPopMulti) { } TEST_F(RedisListTest, RPopMultiCountGreaterThanListSize) { - int ret; + int ret = 0; list->Push(key_, fields_, false, &ret); EXPECT_EQ(fields_.size(), ret); std::vector elems; diff --git a/tests/cppunit/types/metadata_test.cc b/tests/cppunit/types/metadata_test.cc index 453038ddd59..fa1a7e4dc7a 100644 --- a/tests/cppunit/types/metadata_test.cc +++ b/tests/cppunit/types/metadata_test.cc @@ -75,7 +75,7 @@ class RedisTypeTest : public TestBase { fields_ = {"test-hash-key-1", "test-hash-key-2", "test-hash-key-3"}; values_ = {"hash-test-value-1", "hash-test-value-2", "hash-test-value-3"}; } - ~RedisTypeTest() = default; + ~RedisTypeTest() override = default; protected: std::unique_ptr redis; @@ -83,10 +83,10 @@ class RedisTypeTest : public TestBase { }; TEST_F(RedisTypeTest, GetMetadata) { - int ret; + int ret = 0; std::vector fvs; for (size_t i = 0; i < fields_.size(); i++) { - fvs.emplace_back(FieldValue{fields_[i].ToString(), values_[i].ToString()}); + fvs.emplace_back(fields_[i].ToString(), values_[i].ToString()); } rocksdb::Status s = hash->MSet(key_, fvs, false, &ret); EXPECT_TRUE(s.ok() && static_cast(fvs.size()) == ret); @@ -100,18 +100,18 @@ TEST_F(RedisTypeTest, GetMetadata) { } TEST_F(RedisTypeTest, Expire) { - int ret; + int ret = 0; std::vector fvs; for (size_t i = 0; i < fields_.size(); i++) { - fvs.emplace_back(FieldValue{fields_[i].ToString(), values_[i].ToString()}); + fvs.emplace_back(fields_[i].ToString(), values_[i].ToString()); } rocksdb::Status s = hash->MSet(key_, fvs, false, &ret); EXPECT_TRUE(s.ok() && static_cast(fvs.size()) == ret); - int64_t now; + int64_t now = 0; rocksdb::Env::Default()->GetCurrentTime(&now); redis->Expire(key_, int(now + 2)); - int ttl; + int ttl = 0; redis->TTL(key_, &ttl); ASSERT_TRUE(ttl >= 1 && ttl <= 2); sleep(2); -} \ No newline at end of file +} diff --git a/tests/cppunit/types/set_test.cc b/tests/cppunit/types/set_test.cc index b5090fdca41..d0551087965 100644 --- a/tests/cppunit/types/set_test.cc +++ b/tests/cppunit/types/set_test.cc @@ -28,7 +28,8 @@ class RedisSetTest : public TestBase { protected: explicit RedisSetTest() : TestBase() { set = std::make_unique(storage_, "set_ns"); } - ~RedisSetTest() = default; + ~RedisSetTest() override = default; + void SetUp() override { key_ = "test-set-key"; fields_ = {"set-key-1", "set-key-2", "set-key-3", "set-key-4"}; @@ -39,7 +40,7 @@ class RedisSetTest : public TestBase { }; TEST_F(RedisSetTest, AddAndRemove) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); s = set->Card(key_, &ret); @@ -52,7 +53,7 @@ TEST_F(RedisSetTest, AddAndRemove) { } TEST_F(RedisSetTest, Members) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); std::vector members; @@ -68,11 +69,11 @@ TEST_F(RedisSetTest, Members) { } TEST_F(RedisSetTest, IsMember) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); - for (size_t i = 0; i < fields_.size(); i++) { - s = set->IsMember(key_, fields_[i], &ret); + for (auto &field : fields_) { + s = set->IsMember(key_, field, &ret); EXPECT_TRUE(s.ok() && ret == 1); } set->IsMember(key_, "foo", &ret); @@ -83,7 +84,7 @@ TEST_F(RedisSetTest, IsMember) { } TEST_F(RedisSetTest, MIsMember) { - int ret; + int ret = 0; std::vector exists; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); @@ -103,12 +104,12 @@ TEST_F(RedisSetTest, MIsMember) { } TEST_F(RedisSetTest, Move) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); Slice dst("set-test-move-key"); - for (size_t i = 0; i < fields_.size(); i++) { - s = set->Move(key_, dst, fields_[i], &ret); + for (auto &field : fields_) { + s = set->Move(key_, dst, field, &ret); EXPECT_TRUE(s.ok() && ret == 1); } s = set->Move(key_, dst, "set-no-exists-key", &ret); @@ -124,21 +125,24 @@ TEST_F(RedisSetTest, Move) { } TEST_F(RedisSetTest, TakeWithPop) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); std::vector members; s = set->Take(key_, &members, 3, true); + EXPECT_TRUE(s.ok()); EXPECT_EQ(members.size(), 3); s = set->Take(key_, &members, 2, true); + EXPECT_TRUE(s.ok()); EXPECT_EQ(members.size(), 1); s = set->Take(key_, &members, 1, true); + EXPECT_TRUE(s.ok()); EXPECT_TRUE(s.ok() && members.size() == 0); set->Del(key_); } TEST_F(RedisSetTest, Diff) { - int ret; + int ret = 0; std::string k1 = "key1", k2 = "key2", k3 = "key3"; rocksdb::Status s = set->Add(k1, {"a", "b", "c", "d"}, &ret); EXPECT_EQ(ret, 4); @@ -155,7 +159,7 @@ TEST_F(RedisSetTest, Diff) { } TEST_F(RedisSetTest, Union) { - int ret; + int ret = 0; std::string k1 = "key1", k2 = "key2", k3 = "key3"; rocksdb::Status s = set->Add(k1, {"a", "b", "c", "d"}, &ret); EXPECT_EQ(ret, 4); @@ -172,7 +176,7 @@ TEST_F(RedisSetTest, Union) { } TEST_F(RedisSetTest, Inter) { - int ret; + int ret = 0; std::string k1 = "key1", k2 = "key2", k3 = "key3"; rocksdb::Status s = set->Add(k1, {"a", "b", "c", "d"}, &ret); EXPECT_EQ(ret, 4); @@ -189,24 +193,26 @@ TEST_F(RedisSetTest, Inter) { } TEST_F(RedisSetTest, Overwrite) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); set->Overwrite(key_, {"a"}); - int count; + int count = 0; set->Card(key_, &count); EXPECT_EQ(count, 1); set->Del(key_); } TEST_F(RedisSetTest, TakeWithoutPop) { - int ret; + int ret = 0; rocksdb::Status s = set->Add(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); std::vector members; s = set->Take(key_, &members, int(fields_.size() + 1), false); + EXPECT_TRUE(s.ok()); EXPECT_EQ(members.size(), fields_.size()); s = set->Take(key_, &members, int(fields_.size() - 1), false); + EXPECT_TRUE(s.ok()); EXPECT_EQ(members.size(), fields_.size() - 1); s = set->Remove(key_, fields_, &ret); EXPECT_TRUE(s.ok() && static_cast(fields_.size()) == ret); diff --git a/tests/cppunit/types/sortedint_test.cc b/tests/cppunit/types/sortedint_test.cc index 14c064a87b6..aab0f7133c9 100644 --- a/tests/cppunit/types/sortedint_test.cc +++ b/tests/cppunit/types/sortedint_test.cc @@ -30,7 +30,8 @@ class RedisSortedintTest : public TestBase { explicit RedisSortedintTest() : TestBase() { sortedint = std::make_unique(storage_, "sortedint_ns"); } - ~RedisSortedintTest() = default; + ~RedisSortedintTest() override = default; + void SetUp() override { key_ = "test-sortedint-key"; ids_ = {1, 2, 3, 4}; @@ -42,7 +43,7 @@ class RedisSortedintTest : public TestBase { }; TEST_F(RedisSortedintTest, AddAndRemove) { - int ret; + int ret = 0; rocksdb::Status s = sortedint->Add(key_, ids_, &ret); EXPECT_TRUE(s.ok() && static_cast(ids_.size()) == ret); s = sortedint->Card(key_, &ret); @@ -55,7 +56,7 @@ TEST_F(RedisSortedintTest, AddAndRemove) { } TEST_F(RedisSortedintTest, Range) { - int ret; + int ret = 0; rocksdb::Status s = sortedint->Add(key_, ids_, &ret); EXPECT_TRUE(s.ok() && static_cast(ids_.size()) == ret); std::vector ids; diff --git a/tests/cppunit/types/string_test.cc b/tests/cppunit/types/string_test.cc index 497cb0eb75f..d9d220ea37e 100644 --- a/tests/cppunit/types/string_test.cc +++ b/tests/cppunit/types/string_test.cc @@ -28,7 +28,8 @@ class RedisStringTest : public TestBase { protected: explicit RedisStringTest() : TestBase() { string = std::make_unique(storage_, "string_ns"); } - ~RedisStringTest() = default; + ~RedisStringTest() override = default; + void SetUp() override { key_ = "test-string-key"; pairs_ = { @@ -44,7 +45,7 @@ class RedisStringTest : public TestBase { }; TEST_F(RedisStringTest, Append) { - int ret; + int ret = 0; for (size_t i = 0; i < 32; i++) { rocksdb::Status s = string->Append(key_, "a", &ret); EXPECT_TRUE(s.ok()); @@ -54,16 +55,16 @@ TEST_F(RedisStringTest, Append) { } TEST_F(RedisStringTest, GetAndSet) { - for (size_t i = 0; i < pairs_.size(); i++) { - string->Set(pairs_[i].key.ToString(), pairs_[i].value.ToString()); + for (auto &pair : pairs_) { + string->Set(pair.key.ToString(), pair.value.ToString()); } - for (size_t i = 0; i < pairs_.size(); i++) { + for (auto &pair : pairs_) { std::string got_value; - string->Get(pairs_[i].key.ToString(), &got_value); - EXPECT_EQ(pairs_[i].value, got_value); + string->Get(pair.key.ToString(), &got_value); + EXPECT_EQ(pair.value, got_value); } - for (size_t i = 0; i < pairs_.size(); i++) { - string->Del(pairs_[i].key); + for (auto &pair : pairs_) { + string->Del(pair.key); } } @@ -78,13 +79,13 @@ TEST_F(RedisStringTest, MGetAndMSet) { for (size_t i = 0; i < pairs_.size(); i++) { EXPECT_EQ(pairs_[i].value, values[i]); } - for (size_t i = 0; i < pairs_.size(); i++) { - string->Del(pairs_[i].key); + for (auto &pair : pairs_) { + string->Del(pair.key); } } TEST_F(RedisStringTest, IncrByFloat) { - double f; + double f = 0.0; double max_float = std::numeric_limits::max(); string->IncrByFloat(key_, 1.0, &f); EXPECT_EQ(1.0, f); @@ -106,7 +107,7 @@ TEST_F(RedisStringTest, IncrByFloat) { } TEST_F(RedisStringTest, IncrBy) { - int64_t ret; + int64_t ret = 0; string->IncrBy(key_, 1, &ret); EXPECT_EQ(1, ret); string->IncrBy(key_, INT64_MAX - 1, &ret); @@ -136,8 +137,8 @@ TEST_F(RedisStringTest, GetEmptyValue) { } TEST_F(RedisStringTest, GetSet) { - int ttl; - int64_t now; + int ttl = 0; + int64_t now = 0; rocksdb::Env::Default()->GetCurrentTime(&now); std::vector values = {"a", "b", "c", "d"}; for (size_t i = 0; i < values.size(); i++) { @@ -155,35 +156,35 @@ TEST_F(RedisStringTest, GetSet) { string->Del(key_); } TEST_F(RedisStringTest, GetDel) { - for (size_t i = 0; i < pairs_.size(); i++) { - string->Set(pairs_[i].key.ToString(), pairs_[i].value.ToString()); + for (auto &pair : pairs_) { + string->Set(pair.key.ToString(), pair.value.ToString()); } - for (size_t i = 0; i < pairs_.size(); i++) { + for (auto &pair : pairs_) { std::string got_value; - string->GetDel(pairs_[i].key.ToString(), &got_value); - EXPECT_EQ(pairs_[i].value, got_value); + string->GetDel(pair.key.ToString(), &got_value); + EXPECT_EQ(pair.value, got_value); std::string second_got_value; - auto s = string->GetDel(pairs_[i].key.ToString(), &second_got_value); + auto s = string->GetDel(pair.key.ToString(), &second_got_value); EXPECT_TRUE(!s.ok() && s.IsNotFound()); } } TEST_F(RedisStringTest, MSetXX) { - int ret; + int ret = 0; string->SetXX(key_, "test-value", 3, &ret); EXPECT_EQ(ret, 0); string->Set(key_, "test-value"); string->SetXX(key_, "test-value", 3, &ret); EXPECT_EQ(ret, 1); - int ttl; + int ttl = 0; string->TTL(key_, &ttl); EXPECT_TRUE(ttl >= 2 && ttl <= 3); string->Del(key_); } TEST_F(RedisStringTest, MSetNX) { - int ret; + int ret = 0; string->MSetNX(pairs_, 0, &ret); EXPECT_EQ(1, ret); std::vector keys; @@ -202,15 +203,15 @@ TEST_F(RedisStringTest, MSetNX) { string->MSetNX(pairs_, 0, &ret); EXPECT_EQ(0, ret); - for (size_t i = 0; i < pairs_.size(); i++) { - string->Del(pairs_[i].key); + for (auto &pair : pairs_) { + string->Del(pair.key); } } TEST_F(RedisStringTest, MSetNXWithTTL) { - int ret; + int ret = 0; string->SetNX(key_, "test-value", 3, &ret); - int ttl; + int ttl = 0; string->TTL(key_, &ttl); EXPECT_TRUE(ttl >= 2 && ttl <= 3); string->Del(key_); @@ -218,14 +219,14 @@ TEST_F(RedisStringTest, MSetNXWithTTL) { TEST_F(RedisStringTest, SetEX) { string->SetEX(key_, "test-value", 3); - int ttl; + int ttl = 0; string->TTL(key_, &ttl); EXPECT_TRUE(ttl >= 2 && ttl <= 3); string->Del(key_); } TEST_F(RedisStringTest, SetRange) { - int ret; + int ret = 0; string->Set(key_, "hello,world"); string->SetRange(key_, 6, "redis", &ret); EXPECT_EQ(11, ret); @@ -250,7 +251,7 @@ TEST_F(RedisStringTest, SetRange) { } TEST_F(RedisStringTest, CAS) { - int ret; + int ret = 0; std::string key = "cas_key", value = "cas_value", new_value = "new_value"; auto status = string->Set(key, value); @@ -273,7 +274,7 @@ TEST_F(RedisStringTest, CAS) { ASSERT_TRUE(status.ok()); EXPECT_EQ(new_value, current_value); - int ttl; + int ttl = 0; string->TTL(key, &ttl); EXPECT_TRUE(ttl >= 9 && ttl <= 10); @@ -281,7 +282,7 @@ TEST_F(RedisStringTest, CAS) { } TEST_F(RedisStringTest, CAD) { - int ret; + int ret = 0; std::string key = "cas_key", value = "cas_value"; auto status = string->Set(key, value); diff --git a/tests/cppunit/types/zset_test.cc b/tests/cppunit/types/zset_test.cc index 8dc3917568e..9e2ee9c5073 100644 --- a/tests/cppunit/types/zset_test.cc +++ b/tests/cppunit/types/zset_test.cc @@ -28,8 +28,9 @@ class RedisZSetTest : public TestBase { protected: RedisZSetTest() : TestBase() { zset = std::make_unique(storage_, "zset_ns"); } - ~RedisZSetTest() = default; - void SetUp() { + ~RedisZSetTest() override = default; + + void SetUp() override { key_ = "test_zset_key"; fields_ = {"zset_test_key-1", "zset_test_key-2", "zset_test_key-3", "zset_test_key-4", "zset_test_key-5", "zset_test_key-6", "zset_test_key-7"}; @@ -42,7 +43,7 @@ class RedisZSetTest : public TestBase { }; TEST_F(RedisZSetTest, Add) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -50,7 +51,7 @@ TEST_F(RedisZSetTest, Add) { zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(static_cast(fields_.size()), ret); for (size_t i = 0; i < fields_.size(); i++) { - double got; + double got = 0.0; rocksdb::Status s = zset->Score(key_, fields_[i], &got); EXPECT_EQ(scores_[i], got); } @@ -60,7 +61,7 @@ TEST_F(RedisZSetTest, Add) { } TEST_F(RedisZSetTest, IncrBy) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -68,7 +69,8 @@ TEST_F(RedisZSetTest, IncrBy) { zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(fields_.size(), ret); for (size_t i = 0; i < fields_.size(); i++) { - double increment = 12.3, score; + double increment = 12.3; + double score = 0.0; zset->IncrBy(key_, fields_[i], increment, &score); EXPECT_EQ(scores_[i] + increment, score); } @@ -76,7 +78,7 @@ TEST_F(RedisZSetTest, IncrBy) { } TEST_F(RedisZSetTest, Remove) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -85,21 +87,21 @@ TEST_F(RedisZSetTest, Remove) { EXPECT_EQ(fields_.size(), ret); zset->Remove(key_, fields_, &ret); EXPECT_EQ(fields_.size(), ret); - for (size_t i = 0; i < fields_.size(); i++) { - double score; - rocksdb::Status s = zset->Score(key_, fields_[i], &score); + for (auto &field : fields_) { + double score = 0.0; + rocksdb::Status s = zset->Score(key_, field, &score); EXPECT_TRUE(s.IsNotFound()); } zset->Del(key_); } TEST_F(RedisZSetTest, Range) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } - int count = mscores.size() - 1; + int count = static_cast(mscores.size() - 1); zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(fields_.size(), ret); zset->Range(key_, 0, -2, 0, &mscores); @@ -112,12 +114,12 @@ TEST_F(RedisZSetTest, Range) { } TEST_F(RedisZSetTest, RevRange) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } - int count = mscores.size() - 1; + int count = static_cast(mscores.size() - 1); zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(static_cast(fields_.size()), ret); zset->Range(key_, 0, -2, kZSetReversed, &mscores); @@ -130,14 +132,14 @@ TEST_F(RedisZSetTest, RevRange) { } TEST_F(RedisZSetTest, PopMin) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(static_cast(fields_.size()), ret); - zset->Pop(key_, mscores.size() - 1, true, &mscores); + zset->Pop(key_, static_cast(mscores.size() - 1), true, &mscores); for (size_t i = 0; i < mscores.size(); i++) { EXPECT_EQ(mscores[i].member, fields_[i].ToString()); EXPECT_EQ(mscores[i].score, scores_[i]); @@ -148,15 +150,15 @@ TEST_F(RedisZSetTest, PopMin) { } TEST_F(RedisZSetTest, PopMax) { - int ret; + int ret = 0; std::vector mscores; - int count = fields_.size(); + int count = static_cast(fields_.size()); for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(static_cast(fields_.size()), ret); - zset->Pop(key_, mscores.size() - 1, false, &mscores); + zset->Pop(key_, static_cast(mscores.size() - 1), false, &mscores); for (size_t i = 0; i < mscores.size(); i++) { EXPECT_EQ(mscores[i].member, fields_[count - i - 1].ToString()); EXPECT_EQ(mscores[i].score, scores_[count - i - 1]); @@ -166,7 +168,7 @@ TEST_F(RedisZSetTest, PopMax) { } TEST_F(RedisZSetTest, RangeByLex) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -222,7 +224,7 @@ TEST_F(RedisZSetTest, RangeByLex) { } TEST_F(RedisZSetTest, RangeByScore) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -270,7 +272,7 @@ TEST_F(RedisZSetTest, RangeByScore) { } TEST_F(RedisZSetTest, RangeByScoreWithLimit) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -291,7 +293,7 @@ TEST_F(RedisZSetTest, RangeByScoreWithLimit) { } TEST_F(RedisZSetTest, RemRangeByScore) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -310,35 +312,35 @@ TEST_F(RedisZSetTest, RemRangeByScore) { } TEST_F(RedisZSetTest, RemoveRangeByRank) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(fields_.size(), ret); - zset->RemoveRangeByRank(key_, 0, fields_.size() - 2, &ret); + zset->RemoveRangeByRank(key_, 0, static_cast(fields_.size() - 2), &ret); EXPECT_EQ(fields_.size() - 1, ret); zset->RemoveRangeByRank(key_, 0, 2, &ret); EXPECT_EQ(1, ret); } TEST_F(RedisZSetTest, RemoveRevRangeByRank) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); } zset->Add(key_, ZAddFlags::Default(), &mscores, &ret); EXPECT_EQ(fields_.size(), ret); - zset->RemoveRangeByRank(key_, 0, fields_.size() - 2, &ret); + zset->RemoveRangeByRank(key_, 0, static_cast(fields_.size() - 2), &ret); EXPECT_EQ(static_cast(fields_.size() - 1), ret); zset->RemoveRangeByRank(key_, 0, 2, &ret); EXPECT_EQ(1, ret); } TEST_F(RedisZSetTest, Rank) { - int ret; + int ret = 0; std::vector mscores; for (size_t i = 0; i < fields_.size(); i++) { mscores.emplace_back(MemberScore{fields_[i].ToString(), scores_[i]}); @@ -347,18 +349,18 @@ TEST_F(RedisZSetTest, Rank) { EXPECT_EQ(static_cast(fields_.size()), ret); for (size_t i = 0; i < fields_.size(); i++) { - int rank; + int rank = 0; zset->Rank(key_, fields_[i], false, &rank); EXPECT_EQ(i, rank); } for (size_t i = 0; i < fields_.size(); i++) { - int rank; + int rank = 0; zset->Rank(key_, fields_[i], true, &rank); EXPECT_EQ(i, static_cast(fields_.size() - rank - 1)); } std::vector no_exist_members = {"a", "b"}; for (const auto &member : no_exist_members) { - int rank; + int rank = 0; zset->Rank(key_, member, true, &rank); EXPECT_EQ(-1, rank); }