Skip to content

Commit

Permalink
Reinitialize state in between individual unit tests.
Browse files Browse the repository at this point in the history
This changes the TestingSetup fixture to be per-unit-test rather than global.
Most tests don't need it, so it's only invoked in a few.
  • Loading branch information
sipa authored and Warrows committed May 16, 2019
1 parent a373fee commit 962a1bc
Show file tree
Hide file tree
Showing 30 changed files with 511 additions and 433 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ RAW_TEST_FILES = test/data/alertTests.raw
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)

BITCOIN_TEST_SUITE = \
test/test_pivx.h \
test/test_pivx.cpp
# test_pivx binary #
BITCOIN_TESTS =\
Expand Down
23 changes: 22 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5294,10 +5294,31 @@ bool CVerifyDB::VerifyDB(CCoinsView* coinsview, int nCheckLevel, int nCheckDepth

void UnloadBlockIndex()
{
mapBlockIndex.clear();
LOCK(cs_main);
setBlockIndexCandidates.clear();
chainActive.SetTip(NULL);
pindexBestInvalid = NULL;
pindexBestHeader = NULL;
mempool.clear();
mapOrphanTransactions.clear();
mapOrphanTransactionsByPrev.clear();
nSyncStarted = 0;
mapBlocksUnlinked.clear();
vinfoBlockFile.clear();
nLastBlockFile = 0;
nBlockSequenceId = 1;
mapBlockSource.clear();
mapBlocksInFlight.clear();
nQueuedValidatedHeaders = 0;
nPreferredDownload = 0;
setDirtyBlockIndex.clear();
setDirtyFileInfo.clear();
mapNodeState.clear();

for (BlockMap::value_type& entry : mapBlockIndex) {
delete entry.second;
}
mapBlockIndex.clear();
}

bool LoadBlockIndex(string& strError)
Expand Down
3 changes: 2 additions & 1 deletion src/test/Checkpoints_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#include "checkpoints.h"

#include "uint256.h"
#include "test_pivx.h"

#include <boost/test/unit_test.hpp>

using namespace std;

BOOST_AUTO_TEST_SUITE(Checkpoints_tests)
BOOST_FIXTURE_TEST_SUITE(Checkpoints_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(sanity)
{
Expand Down
4 changes: 3 additions & 1 deletion src/test/DoS_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "serialize.h"
#include "util.h"

#include "test/test_pivx.h"

#include <stdint.h>

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
Expand All @@ -40,7 +42,7 @@ CService ip(uint32_t i)
return CService(CNetAddr(s), Params().GetDefaultPort());
}

BOOST_AUTO_TEST_SUITE(DoS_tests)
BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(DoS_banning)
{
Expand Down
4 changes: 3 additions & 1 deletion src/test/accounting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include "wallet/wallet.h"
#include "wallet/walletdb.h"

#include "test/test_pivx.h"

#include <stdint.h>

#include <boost/test/unit_test.hpp>

extern CWallet* pwalletMain;

BOOST_AUTO_TEST_SUITE(accounting_tests)
BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)

static void
GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)
Expand Down
4 changes: 3 additions & 1 deletion src/test/alert_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "util.h"
#include "utilstrencodings.h"

#include "test/test_bitcoin.h"

#include <fstream>

#include <boost/filesystem/operations.hpp>
Expand Down Expand Up @@ -77,7 +79,7 @@
}
#endif

struct ReadAlerts
struct ReadAlerts : public TestingSetup
{
ReadAlerts()
{
Expand Down
4 changes: 3 additions & 1 deletion src/test/benchmark_zerocoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "libzerocoin/Coin.h"
#include "libzerocoin/CoinSpend.h"
#include "libzerocoin/Accumulator.h"
#include "test_pivx.h"

using namespace std;
using namespace libzerocoin;
Expand Down Expand Up @@ -402,7 +403,8 @@ Testb_RunAllTests()
cout << ggSuccessfulTests << " out of " << ggNumTests << " tests passed." << endl << endl;
delete gg_Params;
}
BOOST_AUTO_TEST_SUITE(benchmark_zerocoin)

BOOST_FIXTURE_TEST_SUITE(benchmark_zerocoin, TestingSetup)

BOOST_AUTO_TEST_CASE(benchmark_test)
{
Expand Down
3 changes: 2 additions & 1 deletion src/test/budget_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#include "masternode-budget.h"
#include "tinyformat.h"
#include "utilmoneystr.h"
#include "test_pivx.h"

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(budget_tests)
BOOST_FIXTURE_TEST_SUITE(budget_tests, TestingSetup)

void CheckBudgetValue(int nHeight, std::string strNetwork, CAmount nExpectedValue)
{
Expand Down
3 changes: 2 additions & 1 deletion src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "uint256.h"
#include "util.h"
#include "utilstrencodings.h"
#include "test_pivx.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -59,7 +60,7 @@ void dumpKeyInfo(uint256 privkey)
#endif


BOOST_AUTO_TEST_SUITE(key_tests)
BOOST_FIXTURE_TEST_SUITE(key_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(key_test1)
{
Expand Down
Loading

0 comments on commit 962a1bc

Please sign in to comment.