diff options
author | John Moffett <john.moff@gmail.com> | 2023-02-03 10:49:12 -0500 |
---|---|---|
committer | John Moffett <john.moff@gmail.com> | 2023-02-03 12:35:54 -0500 |
commit | b8032293e67a3b61ecad531412be5330f7cb39e3 (patch) | |
tree | 967a04d675832561f84829fed760a511a8a8196d /src/test/dbwrapper_tests.cpp | |
parent | aaa55971f6af3f19b22c28103630b856df266ebb (diff) | |
download | bitcoin-b8032293e67a3b61ecad531412be5330f7cb39e3.tar.xz |
Remove use of snprintf and simplify
One test case uses snprintf to convert an
int to a string. Change it to use ToString
(which uses a locale-independent version of
std::to_string). Also remove unnecessary
parts of StringContentsSerializer.
Diffstat (limited to 'src/test/dbwrapper_tests.cpp')
-rw-r--r-- | src/test/dbwrapper_tests.cpp | 43 |
1 files changed, 14 insertions, 29 deletions
diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index 2447c882ae..7ad123754b 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -5,6 +5,7 @@ #include <dbwrapper.h> #include <test/util/setup_common.h> #include <uint256.h> +#include <util/string.h> #include <memory> @@ -324,12 +325,6 @@ struct StringContentsSerializer { StringContentsSerializer() = default; explicit StringContentsSerializer(const std::string& inp) : str(inp) {} - StringContentsSerializer& operator+=(const std::string& s) { - str += s; - return *this; - } - StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; } - template<typename Stream> void Serialize(Stream& s) const { @@ -343,44 +338,34 @@ struct StringContentsSerializer { { str.clear(); uint8_t c{0}; - while (true) { - try { - s >> c; - str.push_back(c); - } catch (const std::ios_base::failure&) { - break; - } + while (!s.eof()) { + s >> c; + str.push_back(c); } } }; BOOST_AUTO_TEST_CASE(iterator_string_ordering) { - char buf[10]; - fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering"; CDBWrapper dbw(ph, (1 << 20), true, false, false); - for (int x=0x00; x<10; ++x) { - for (int y = 0; y < 10; y++) { - snprintf(buf, sizeof(buf), "%d", x); - StringContentsSerializer key(buf); - for (int z = 0; z < y; z++) + for (int x = 0; x < 10; ++x) { + for (int y = 0; y < 10; ++y) { + std::string key{ToString(x)}; + for (int z = 0; z < y; ++z) key += key; uint32_t value = x*x; - BOOST_CHECK(dbw.Write(key, value)); + BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value)); } } std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator()); for (const int seek_start : {0, 5}) { - snprintf(buf, sizeof(buf), "%d", seek_start); - StringContentsSerializer seek_key(buf); - it->Seek(seek_key); - for (unsigned int x=seek_start; x<10; ++x) { - for (int y = 0; y < 10; y++) { - snprintf(buf, sizeof(buf), "%d", x); - std::string exp_key(buf); - for (int z = 0; z < y; z++) + it->Seek(StringContentsSerializer{ToString(seek_start)}); + for (unsigned int x = seek_start; x < 10; ++x) { + for (int y = 0; y < 10; ++y) { + std::string exp_key{ToString(x)}; + for (int z = 0; z < y; ++z) exp_key += exp_key; StringContentsSerializer key; uint32_t value; |