Skip to content

Commit 2cb4e8b

Browse files
committed
[test] move string helper functions into test library
1 parent 239d199 commit 2cb4e8b

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

src/test/settings_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
#include <util/settings.h>
66

7-
#include <test/util.h>
87
#include <test/util/setup_common.h>
8+
#include <test/util/str.h>
9+
910

1011
#include <boost/test/unit_test.hpp>
1112
#include <univalue.h>

src/test/util.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,4 @@ std::string getnewaddress(CWallet& w);
3434
/** Returns the generated coin */
3535
CTxIn generatetoaddress(const std::string& address);
3636

37-
/**
38-
* Increment a string. Useful to enumerate all fixed length strings with
39-
* characters in [min_char, max_char].
40-
*/
41-
template <typename CharType, size_t StringLength>
42-
bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
43-
{
44-
for (CharType& elem : string) {
45-
bool has_next = elem != max_char;
46-
elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
47-
if (has_next) return true;
48-
}
49-
return false;
50-
}
51-
52-
/**
53-
* Iterate over string values and call function for each string without
54-
* successive duplicate characters.
55-
*/
56-
template <typename CharType, size_t StringLength, typename Fn>
57-
void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
58-
for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
59-
int prev = -1;
60-
bool skip_string = false;
61-
for (CharType c : string) {
62-
if (c == prev) skip_string = true;
63-
if (skip_string || c < min_char || c > max_char) break;
64-
prev = c;
65-
}
66-
if (!skip_string) fn();
67-
}
68-
}
69-
7037
#endif // BITCOIN_TEST_UTIL_H

src/test/util/str.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,37 @@
99

1010
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2);
1111

12+
/**
13+
* Increment a string. Useful to enumerate all fixed length strings with
14+
* characters in [min_char, max_char].
15+
*/
16+
template <typename CharType, size_t StringLength>
17+
bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
18+
{
19+
for (CharType& elem : string) {
20+
bool has_next = elem != max_char;
21+
elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
22+
if (has_next) return true;
23+
}
24+
return false;
25+
}
26+
27+
/**
28+
* Iterate over string values and call function for each string without
29+
* successive duplicate characters.
30+
*/
31+
template <typename CharType, size_t StringLength, typename Fn>
32+
void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
33+
for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
34+
int prev = -1;
35+
bool skip_string = false;
36+
for (CharType c : string) {
37+
if (c == prev) skip_string = true;
38+
if (skip_string || c < min_char || c > max_char) break;
39+
prev = c;
40+
}
41+
if (!skip_string) fn();
42+
}
43+
}
44+
1245
#endif // BITCOIN_TEST_UTIL_STR_H

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <clientversion.h>
88
#include <sync.h>
99
#include <test/util/setup_common.h>
10-
#include <test/util.h>
10+
#include <test/util/str.h>
1111
#include <util/moneystr.h>
1212
#include <util/strencodings.h>
1313
#include <util/string.h>

0 commit comments

Comments
 (0)