From: Radoslaw Zarzynski Date: Thu, 30 Jul 2020 14:41:48 +0000 (+0200) Subject: tests: modernize all occurrences of rand_choose(). X-Git-Tag: v16.1.0~431^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a44b937b2cf7b11b950dd8b492dcd178d47a39e0;p=ceph.git tests: modernize all occurrences of rand_choose(). We should consider deduplication as well. Signed-off-by: Radoslaw Zarzynski --- diff --git a/src/test/ObjectMap/test_object_map.cc b/src/test/ObjectMap/test_object_map.cc index ad2c56e5a03..db19c17e8cd 100644 --- a/src/test/ObjectMap/test_object_map.cc +++ b/src/test/ObjectMap/test_object_map.cc @@ -1,4 +1,5 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +#include #include #include #include @@ -20,14 +21,10 @@ using namespace std; template typename T::iterator rand_choose(T &cont) { - if (cont.size() == 0) { - return cont.end(); + if (std::empty(cont)) { + return std::end(cont); } - int index = rand() % cont.size(); - typename T::iterator retval = cont.begin(); - - for (; index > 0; --index) ++retval; - return retval; + return std::next(std::begin(cont), rand() % cont.size()); } string num_str(unsigned i) { diff --git a/src/test/objectstore/test_idempotent.cc b/src/test/objectstore/test_idempotent.cc index c89ea9b70fe..0889375ed17 100644 --- a/src/test/objectstore/test_idempotent.cc +++ b/src/test/objectstore/test_idempotent.cc @@ -13,6 +13,7 @@ */ #include +#include #include #include #include "os/filestore/FileStore.h" @@ -31,14 +32,10 @@ void usage(const string &name) { template typename T::iterator rand_choose(T &cont) { - if (cont.size() == 0) { - return cont.end(); + if (std::empty(cont)) { + return std::end(cont); } - int index = rand() % cont.size(); - typename T::iterator retval = cont.begin(); - - for (; index > 0; --index) ++retval; - return retval; + return std::next(std::begin(cont), rand() % cont.size()); } int main(int argc, char **argv) { diff --git a/src/test/osd/RadosModel.h b/src/test/osd/RadosModel.h index b2e2496fd21..d5689f6067a 100644 --- a/src/test/osd/RadosModel.h +++ b/src/test/osd/RadosModel.h @@ -6,6 +6,7 @@ #include "include/rados/librados.hpp" #include +#include #include #include #include @@ -32,14 +33,10 @@ class TestOpStat; template typename T::iterator rand_choose(T &cont) { - if (cont.size() == 0) { - return cont.end(); + if (std::empty(cont)) { + return std::end(cont); } - int index = rand() % cont.size(); - typename T::iterator retval = cont.begin(); - - for (; index > 0; --index) ++retval; - return retval; + return std::next(std::begin(cont), rand() % cont.size()); } enum TestOpType { diff --git a/src/test/test_snap_mapper.cc b/src/test/test_snap_mapper.cc index 9b6dbdd2b0c..50730080d8d 100644 --- a/src/test/test_snap_mapper.cc +++ b/src/test/test_snap_mapper.cc @@ -1,4 +1,5 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +#include #include #include #include @@ -16,14 +17,10 @@ using namespace std; template typename T::iterator rand_choose(T &cont) { - if (cont.size() == 0) { - return cont.end(); + if (std::empty(cont)) { + return std::end(cont); } - int index = rand() % cont.size(); - typename T::iterator retval = cont.begin(); - - for (; index > 0; --index) ++retval; - return retval; + return std::next(std::begin(cont), rand() % cont.size()); } string random_string(size_t size) diff --git a/src/test/xattr_bench.cc b/src/test/xattr_bench.cc index edbfae25bef..7fac235be49 100644 --- a/src/test/xattr_bench.cc +++ b/src/test/xattr_bench.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "os/filestore/FileStore.h" #include "include/Context.h" @@ -40,14 +41,10 @@ const int THREADS = 5; template typename T::iterator rand_choose(T &cont) { - if (cont.size() == 0) { - return cont.end(); + if (std::empty(cont) == 0) { + return std::end(cont); } - int index = rand() % cont.size(); - typename T::iterator retval = cont.begin(); - - for (; index > 0; --index) ++retval; - return retval; + return std::next(std::begin(cont), rand() % cont.size()); } class OnApplied : public Context {