Skip to content

Commit 4b33115

Browse files
committed
Add unit test NextString, ForEachNoDup functions
Remove testcase generating code from util_SettingsMerge so it can be reused in new tests. The hash value expected in util_SettingsMerge changes as a result of this, but only because the testcases are generated in a different order, not because any cases are added or removed. It is possible to verify this with: SETTINGS_MERGE_TEST_OUT=new.txt test/test_bitcoin --run_test=util_tests/util_SettingsMerge git checkout HEAD~1 make test/test_bitcoin SETTINGS_MERGE_TEST_OUT=old.txt test/test_bitcoin --run_test=util_tests/util_SettingsMerge diff -u <(sort old.txt) <(sort new.txt) The new output is a little more readable, with simpler testcases sorted first.
1 parent 05bfee3 commit 4b33115

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

src/test/util.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,37 @@ 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+
}
3769

3870
#endif // BITCOIN_TEST_UTIL_H

src/test/util_tests.cpp

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clientversion.h>
88
#include <primitives/transaction.h>
99
#include <sync.h>
10+
#include <test/util.h>
1011
#include <util/strencodings.h>
1112
#include <util/moneystr.h>
1213
#include <test/setup_common.h>
@@ -605,15 +606,17 @@ struct SettingsMergeTestingSetup : public BasicTestingSetup {
605606
//! debugging to make test results easier to understand.
606607
static constexpr int MAX_ACTIONS = 3;
607608

608-
enum Action { SET = 0, NEGATE, SECTION_SET, SECTION_NEGATE, END };
609+
enum Action { NONE, SET, NEGATE, SECTION_SET, SECTION_NEGATE };
609610
using ActionList = Action[MAX_ACTIONS];
610611

611612
//! Enumerate all possible test configurations.
612613
template <typename Fn>
613614
void ForEachMergeSetup(Fn&& fn)
614615
{
615-
ForEachActionList([&](const ActionList& arg_actions) {
616-
ForEachActionList([&](const ActionList& conf_actions) {
616+
ActionList arg_actions = {};
617+
ForEachNoDup(arg_actions, SET, SECTION_NEGATE, [&] {
618+
ActionList conf_actions = {};
619+
ForEachNoDup(conf_actions, SET, SECTION_NEGATE, [&] {
617620
for (bool soft_set : {false, true}) {
618621
for (bool force_set : {false, true}) {
619622
for (const std::string& section : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET}) {
@@ -629,36 +632,6 @@ struct SettingsMergeTestingSetup : public BasicTestingSetup {
629632
});
630633
}
631634

632-
//! Enumerate interesting combinations of actions.
633-
template <typename Fn>
634-
void ForEachActionList(Fn&& fn)
635-
{
636-
ActionList actions = {SET};
637-
for (bool done = false; !done;) {
638-
int prev_action = -1;
639-
bool skip_actions = false;
640-
for (Action action : actions) {
641-
if ((prev_action == END && action != END) || (prev_action != END && action == prev_action)) {
642-
// To cut down list of enumerated settings, skip enumerating
643-
// settings with ignored actions after an END, and settings that
644-
// repeat the same action twice in a row.
645-
skip_actions = true;
646-
break;
647-
}
648-
prev_action = action;
649-
}
650-
if (!skip_actions) fn(actions);
651-
done = true;
652-
for (Action& action : actions) {
653-
action = Action(action < END ? action + 1 : 0);
654-
if (action) {
655-
done = false;
656-
break;
657-
}
658-
}
659-
}
660-
}
661-
662635
//! Translate actions into a list of <key>=<value> setting strings.
663636
std::vector<std::string> GetValues(const ActionList& actions,
664637
const std::string& section,
@@ -668,7 +641,7 @@ struct SettingsMergeTestingSetup : public BasicTestingSetup {
668641
std::vector<std::string> values;
669642
int suffix = 0;
670643
for (Action action : actions) {
671-
if (action == END) break;
644+
if (action == NONE) break;
672645
std::string prefix;
673646
if (action == SECTION_SET || action == SECTION_NEGATE) prefix = section + ".";
674647
if (action == SET || action == SECTION_SET) {
@@ -801,7 +774,7 @@ BOOST_FIXTURE_TEST_CASE(util_SettingsMerge, SettingsMergeTestingSetup)
801774
// Results file is formatted like:
802775
//
803776
// <input> || <IsArgSet/IsArgNegated/GetArg output> | <GetArgs output> | <GetUnsuitable output>
804-
BOOST_CHECK_EQUAL(out_sha_hex, "f4281d9bff4c0b398ff118386e3a40caa6bac7645e17b296d6a10b95bff632ae");
777+
BOOST_CHECK_EQUAL(out_sha_hex, "b835eef5977d69114eb039a976201f8c7121f34fe2b7ea2b73cafb516e5c9dc8");
805778
}
806779

807780
BOOST_AUTO_TEST_CASE(util_FormatMoney)

0 commit comments

Comments
 (0)