Skip to content

Commit

Permalink
merge bitcoin#27036: Remove last uses of snprintf and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jan 14, 2025
1 parent d80bbe9 commit f961903
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
43 changes: 14 additions & 29 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <dbwrapper.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/string.h>

#include <memory>

Expand Down Expand Up @@ -324,12 +325,6 @@ struct StringContentsSerializer {
StringContentsSerializer() {}
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
{
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion test/lint/lint-locale-dependence.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
KNOWN_VIOLATIONS = [
"src/bitcoin-tx.cpp.*stoul",
"src/dbwrapper.cpp:.*vsnprintf",
"src/test/dbwrapper_tests.cpp:.*snprintf",
"src/test/fuzz/locale.cpp",
"src/test/fuzz/string.cpp",
"src/util/strencodings.cpp:.*strtoll",
Expand Down

0 comments on commit f961903

Please sign in to comment.