diff --git a/src/test/prevector_tests.cpp b/src/test/prevector_tests.cpp index 93898a0d0b..32c36e7070 100644 --- a/src/test/prevector_tests.cpp +++ b/src/test/prevector_tests.cpp @@ -1,264 +1,264 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(prevector_tests, TestingSetup) template class prevector_tester { typedef std::vector realtype; realtype real_vector; realtype real_vector_alt; typedef prevector pretype; pretype pre_vector; pretype pre_vector_alt; typedef typename pretype::size_type Size; bool passed = true; FastRandomContext rand_cache; uint256 rand_seed; template void local_check_equal(A a, B b) { local_check(a == b); } void local_check(bool b) { passed &= b; } void test() { const pretype &const_pre_vector = pre_vector; local_check_equal(real_vector.size(), pre_vector.size()); local_check_equal(real_vector.empty(), pre_vector.empty()); for (Size s = 0; s < real_vector.size(); s++) { local_check(real_vector[s] == pre_vector[s]); local_check(&(pre_vector[s]) == &(pre_vector.begin()[s])); local_check(&(pre_vector[s]) == &*(pre_vector.begin() + s)); local_check(&(pre_vector[s]) == &*((pre_vector.end() + s) - real_vector.size())); } // local_check(realtype(pre_vector) == real_vector); local_check(pretype(real_vector.begin(), real_vector.end()) == pre_vector); local_check(pretype(pre_vector.begin(), pre_vector.end()) == pre_vector); size_t pos = 0; for (const T &v : pre_vector) { local_check(v == real_vector[pos++]); } for (const T &v : reverse_iterate(pre_vector)) { local_check(v == real_vector[--pos]); } for (const T &v : const_pre_vector) { local_check(v == real_vector[pos++]); } for (const T &v : reverse_iterate(const_pre_vector)) { local_check(v == real_vector[--pos]); } CDataStream ss1(SER_DISK, 0); CDataStream ss2(SER_DISK, 0); ss1 << real_vector; ss2 << pre_vector; local_check_equal(ss1.size(), ss2.size()); for (Size s = 0; s < ss1.size(); s++) { local_check_equal(ss1[s], ss2[s]); } } public: void resize(Size s) { real_vector.resize(s); local_check_equal(real_vector.size(), s); pre_vector.resize(s); local_check_equal(pre_vector.size(), s); test(); } void reserve(Size s) { real_vector.reserve(s); local_check(real_vector.capacity() >= s); pre_vector.reserve(s); local_check(pre_vector.capacity() >= s); test(); } void insert(Size position, const T &value) { real_vector.insert(real_vector.begin() + position, value); pre_vector.insert(pre_vector.begin() + position, value); test(); } void insert(Size position, Size count, const T &value) { real_vector.insert(real_vector.begin() + position, count, value); pre_vector.insert(pre_vector.begin() + position, count, value); test(); } template void insert_range(Size position, I first, I last) { real_vector.insert(real_vector.begin() + position, first, last); pre_vector.insert(pre_vector.begin() + position, first, last); test(); } void erase(Size position) { real_vector.erase(real_vector.begin() + position); pre_vector.erase(pre_vector.begin() + position); test(); } void erase(Size first, Size last) { real_vector.erase(real_vector.begin() + first, real_vector.begin() + last); pre_vector.erase(pre_vector.begin() + first, pre_vector.begin() + last); test(); } void update(Size pos, const T &value) { real_vector[pos] = value; pre_vector[pos] = value; test(); } void push_back(const T &value) { real_vector.push_back(value); pre_vector.push_back(value); test(); } void pop_back() { real_vector.pop_back(); pre_vector.pop_back(); test(); } void clear() { real_vector.clear(); pre_vector.clear(); } void assign(Size n, const T &value) { real_vector.assign(n, value); pre_vector.assign(n, value); } Size size() const { return real_vector.size(); } Size capacity() const { return pre_vector.capacity(); } void shrink_to_fit() { pre_vector.shrink_to_fit(); test(); } void swap() { real_vector.swap(real_vector_alt); pre_vector.swap(pre_vector_alt); test(); } void move() { real_vector = std::move(real_vector_alt); real_vector_alt.clear(); pre_vector = std::move(pre_vector_alt); pre_vector_alt.clear(); } void copy() { real_vector = real_vector_alt; pre_vector = pre_vector_alt; } ~prevector_tester() { BOOST_CHECK_MESSAGE(passed, "insecure_rand: " + rand_seed.ToString()); } prevector_tester() { SeedInsecureRand(); rand_seed = insecure_rand_seed; rand_cache = insecure_rand_ctx; } }; BOOST_AUTO_TEST_CASE(PrevectorTestInt) { for (int j = 0; j < 64; j++) { prevector_tester<8, int> test; for (int i = 0; i < 2048; i++) { if (InsecureRandBits(2) == 0) { test.insert(InsecureRandRange(test.size() + 1), insecure_rand()); } if (test.size() > 0 && InsecureRandBits(2) == 1) { test.erase(InsecureRandRange(test.size())); } if (InsecureRandBits(3) == 2) { - int new_size = std::max( - 0, std::min(30, - test.size() + (InsecureRandRange(5)) - 2)); + int new_size = std::max( + 0, std::min(30, int(test.size()) + + int(InsecureRandRange(5)) - 2)); test.resize(new_size); } if (InsecureRandBits(3) == 3) { test.insert(InsecureRandRange(test.size() + 1), 1 + InsecureRandBool(), insecure_rand()); } if (InsecureRandBits(3) == 4) { int del = std::min(test.size(), 1 + (InsecureRandBool())); int beg = InsecureRandRange(test.size() + 1 - del); test.erase(beg, beg + del); } if (InsecureRandBits(4) == 5) { test.push_back(insecure_rand()); } if (test.size() > 0 && InsecureRandBits(4) == 6) { test.pop_back(); } if (InsecureRandBits(5) == 7) { int values[4]; int num = 1 + (InsecureRandBits(2)); for (int k = 0; k < num; k++) { values[k] = insecure_rand(); } test.insert_range(InsecureRandRange(test.size() + 1), values, values + num); } if (InsecureRandBits(5) == 8) { int del = std::min(test.size(), 1 + (InsecureRandBits(2))); int beg = InsecureRandRange(test.size() + 1 - del); test.erase(beg, beg + del); } if (InsecureRandBits(5) == 9) { test.reserve(InsecureRandBits(5)); } if (InsecureRandBits(6) == 10) { test.shrink_to_fit(); } if (test.size() > 0) { test.update(InsecureRandRange(test.size()), insecure_rand()); } if (InsecureRandBits(10) == 11) { test.clear(); } if (InsecureRandBits(9) == 12) { test.assign(InsecureRandBits(5), insecure_rand()); } if (InsecureRandBits(3) == 3) { test.swap(); } if (InsecureRandBits(4) == 8) { test.copy(); } if (InsecureRandBits(5) == 18) { test.move(); } } } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp index cf40ec54a1..01c44ae567 100644 --- a/src/test/scheduler_tests.cpp +++ b/src/test/scheduler_tests.cpp @@ -1,231 +1,231 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include BOOST_AUTO_TEST_SUITE(scheduler_tests) static void microTask(CScheduler &s, boost::mutex &mutex, int &counter, int delta, boost::chrono::system_clock::time_point rescheduleTime) { { boost::unique_lock lock(mutex); counter += delta; } boost::chrono::system_clock::time_point noTime = boost::chrono::system_clock::time_point::min(); if (rescheduleTime != noTime) { CScheduler::Function f = boost::bind(µTask, std::ref(s), std::ref(mutex), std::ref(counter), -delta + 1, noTime); s.schedule(f, rescheduleTime); } } static void MicroSleep(uint64_t n) { boost::this_thread::sleep_for(boost::chrono::microseconds(n)); } BOOST_AUTO_TEST_CASE(manythreads) { // Stress test: hundreds of microsecond-scheduled tasks, // serviced by 10 threads. // // So... ten shared counters, which if all the tasks execute // properly will sum to the number of tasks done. // Each task adds or subtracts a random amount from one of the // counters, and then schedules another task 0-1000 // microseconds in the future to subtract or add from // the counter -random_amount+1, so in the end the shared // counters should sum to the number of initial tasks performed. CScheduler microTasks; boost::mutex counterMutex[10]; int counter[10] = {0}; FastRandomContext rng{/* fDeterministic */ true}; // [0, 9] auto zeroToNine = [](FastRandomContext &rc) -> int { return rc.randrange(10); }; // [-11, 1000] auto randomMsec = [](FastRandomContext &rc) -> int { - return -11 + rc.randrange(1012); + return -11 + int(rc.randrange(1012)); }; // [-1000, 1000] auto randomDelta = [](FastRandomContext &rc) -> int { - return -1000 + rc.randrange(2001); + return -1000 + int(rc.randrange(2001)); }; boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now(); boost::chrono::system_clock::time_point now = start; boost::chrono::system_clock::time_point first, last; size_t nTasks = microTasks.getQueueInfo(first, last); BOOST_CHECK(nTasks == 0); for (int i = 0; i < 100; ++i) { boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng)); boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng)); int whichCounter = zeroToNine(rng); CScheduler::Function f = boost::bind( µTask, std::ref(microTasks), std::ref(counterMutex[whichCounter]), std::ref(counter[whichCounter]), randomDelta(rng), tReschedule); microTasks.schedule(f, t); } nTasks = microTasks.getQueueInfo(first, last); BOOST_CHECK(nTasks == 100); BOOST_CHECK(first < last); BOOST_CHECK(last > now); // As soon as these are created they will start running and servicing the // queue boost::thread_group microThreads; for (int i = 0; i < 5; i++) { microThreads.create_thread( boost::bind(&CScheduler::serviceQueue, µTasks)); } MicroSleep(600); now = boost::chrono::system_clock::now(); // More threads and more tasks: for (int i = 0; i < 5; i++) { microThreads.create_thread( boost::bind(&CScheduler::serviceQueue, µTasks)); } for (int i = 0; i < 100; i++) { boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng)); boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng)); int whichCounter = zeroToNine(rng); CScheduler::Function f = boost::bind( µTask, std::ref(microTasks), std::ref(counterMutex[whichCounter]), std::ref(counter[whichCounter]), randomDelta(rng), tReschedule); microTasks.schedule(f, t); } // Drain the task queue then exit threads microTasks.stop(true); // ... wait until all the threads are done microThreads.join_all(); int counterSum = 0; for (int i = 0; i < 10; i++) { BOOST_CHECK(counter[i] != 0); counterSum += counter[i]; } BOOST_CHECK_EQUAL(counterSum, 200); } BOOST_AUTO_TEST_CASE(schedule_every) { CScheduler scheduler; boost::condition_variable cvar; std::atomic counter{15}; std::atomic keepRunning{true}; scheduler.scheduleEvery( [&keepRunning, &cvar, &counter, &scheduler]() { BOOST_CHECK(counter > 0); cvar.notify_all(); if (--counter > 0) { return true; } // We reached the end of our test, make sure nothing run again for // 100ms. scheduler.scheduleFromNow( [&keepRunning, &cvar]() { keepRunning = false; cvar.notify_all(); }, 100); // We set the counter to some magic value to check the scheduler // empty its queue properly after 120ms. scheduler.scheduleFromNow([&counter]() { counter = 42; }, 120); return false; }, 5); // Start the scheduler thread. std::thread schedulerThread( std::bind(&CScheduler::serviceQueue, &scheduler)); boost::mutex mutex; boost::unique_lock lock(mutex); while (keepRunning) { cvar.wait(lock); BOOST_CHECK(counter >= 0); } BOOST_CHECK_EQUAL(counter, 0); scheduler.stop(true); schedulerThread.join(); BOOST_CHECK_EQUAL(counter, 42); } BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered) { CScheduler scheduler; // each queue should be well ordered with respect to itself but not other // queues SingleThreadedSchedulerClient queue1(&scheduler); SingleThreadedSchedulerClient queue2(&scheduler); // create more threads than queues // if the queues only permit execution of one task at once then // the extra threads should effectively be doing nothing // if they don't we'll get out of order behaviour boost::thread_group threads; for (int i = 0; i < 5; ++i) { threads.create_thread( boost::bind(&CScheduler::serviceQueue, &scheduler)); } // these are not atomic, if SinglethreadedSchedulerClient prevents // parallel execution at the queue level no synchronization should be // required here int counter1 = 0; int counter2 = 0; // just simply count up on each queue - if execution is properly ordered // then the callbacks should run in exactly the order in which they were // enqueued for (int i = 0; i < 100; ++i) { queue1.AddToProcessQueue([i, &counter1]() { bool expectation = i == counter1++; assert(expectation); }); queue2.AddToProcessQueue([i, &counter2]() { bool expectation = i == counter2++; assert(expectation); }); } // finish up scheduler.stop(true); threads.join_all(); BOOST_CHECK_EQUAL(counter1, 100); BOOST_CHECK_EQUAL(counter2, 100); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/wallet/test/accounting_tests.cpp b/src/wallet/test/accounting_tests.cpp index 9819dc4ec1..19fd460475 100644 --- a/src/wallet/test/accounting_tests.cpp +++ b/src/wallet/test/accounting_tests.cpp @@ -1,132 +1,132 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include BOOST_FIXTURE_TEST_SUITE(accounting_tests, WalletTestingSetup) static void GetResults(CWallet &wallet, std::map &results) { std::list aes; results.clear(); BOOST_CHECK(wallet.ReorderTransactions() == DBErrors::LOAD_OK); wallet.ListAccountCreditDebit("", aes); for (CAccountingEntry &ae : aes) { results[ae.nOrderPos * SATOSHI] = ae; } } BOOST_AUTO_TEST_CASE(acc_orderupgrade) { std::vector vpwtx; CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef()); CAccountingEntry ae; std::map results; LOCK(m_wallet.cs_wallet); ae.strAccount = ""; ae.nCreditDebit = SATOSHI; ae.nTime = 1333333333; ae.strOtherAccount = "b"; ae.strComment = ""; m_wallet.AddAccountingEntry(ae); wtx.mapValue["comment"] = "z"; m_wallet.AddToWallet(wtx); vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetId())); vpwtx[0]->nTimeReceived = (unsigned int)1333333335; vpwtx[0]->nOrderPos = -1; ae.nTime = 1333333336; ae.strOtherAccount = "c"; m_wallet.AddAccountingEntry(ae); GetResults(m_wallet, results); BOOST_CHECK(m_wallet.nOrderPosNext == 3); BOOST_CHECK(2 == results.size()); BOOST_CHECK(results[Amount::zero()].nTime == 1333333333); BOOST_CHECK(results[Amount::zero()].strComment.empty()); BOOST_CHECK(1 == vpwtx[0]->nOrderPos); BOOST_CHECK(results[2 * SATOSHI].nTime == 1333333336); BOOST_CHECK(results[2 * SATOSHI].strOtherAccount == "c"); ae.nTime = 1333333330; ae.strOtherAccount = "d"; ae.nOrderPos = m_wallet.IncOrderPosNext(); m_wallet.AddAccountingEntry(ae); GetResults(m_wallet, results); BOOST_CHECK(results.size() == 3); BOOST_CHECK(m_wallet.nOrderPosNext == 4); BOOST_CHECK(results[Amount::zero()].nTime == 1333333333); BOOST_CHECK(1 == vpwtx[0]->nOrderPos); BOOST_CHECK(results[2 * SATOSHI].nTime == 1333333336); BOOST_CHECK(results[3 * SATOSHI].nTime == 1333333330); BOOST_CHECK(results[3 * SATOSHI].strComment.empty()); wtx.mapValue["comment"] = "y"; { CMutableTransaction tx(*wtx.tx); // Just to change the hash :) - --tx.nLockTime; + ++tx.nLockTime; wtx.SetTx(MakeTransactionRef(std::move(tx))); } m_wallet.AddToWallet(wtx); vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetId())); vpwtx[1]->nTimeReceived = (unsigned int)1333333336; wtx.mapValue["comment"] = "x"; { CMutableTransaction tx(*wtx.tx); // Just to change the hash :) - --tx.nLockTime; + ++tx.nLockTime; wtx.SetTx(MakeTransactionRef(std::move(tx))); } m_wallet.AddToWallet(wtx); vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetId())); vpwtx[2]->nTimeReceived = (unsigned int)1333333329; vpwtx[2]->nOrderPos = -1; GetResults(m_wallet, results); BOOST_CHECK(results.size() == 3); BOOST_CHECK(m_wallet.nOrderPosNext == 6); BOOST_CHECK(0 == vpwtx[2]->nOrderPos); BOOST_CHECK(results[SATOSHI].nTime == 1333333333); BOOST_CHECK(2 == vpwtx[0]->nOrderPos); BOOST_CHECK(results[3 * SATOSHI].nTime == 1333333336); BOOST_CHECK(results[4 * SATOSHI].nTime == 1333333330); BOOST_CHECK(results[4 * SATOSHI].strComment.empty()); BOOST_CHECK(5 == vpwtx[1]->nOrderPos); ae.nTime = 1333333334; ae.strOtherAccount = "e"; ae.nOrderPos = -1; m_wallet.AddAccountingEntry(ae); GetResults(m_wallet, results); BOOST_CHECK(results.size() == 4); BOOST_CHECK(m_wallet.nOrderPosNext == 7); BOOST_CHECK(0 == vpwtx[2]->nOrderPos); BOOST_CHECK(results[SATOSHI].nTime == 1333333333); BOOST_CHECK(2 == vpwtx[0]->nOrderPos); BOOST_CHECK(results[3 * SATOSHI].nTime == 1333333336); BOOST_CHECK(results[3 * SATOSHI].strComment.empty()); BOOST_CHECK(results[4 * SATOSHI].nTime == 1333333330); BOOST_CHECK(results[4 * SATOSHI].strComment.empty()); BOOST_CHECK(results[5 * SATOSHI].nTime == 1333333334); BOOST_CHECK(6 == vpwtx[1]->nOrderPos); } BOOST_AUTO_TEST_SUITE_END()