You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compress_tests.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2012-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "compressor.h"
  5. #include "util.h"
  6. #include "test/test_bitcoin.h"
  7. #include <stdint.h>
  8. #include <boost/test/unit_test.hpp>
  9. // amounts 0.00000001 .. 0.00100000
  10. #define NUM_MULTIPLES_UNIT 100000
  11. // amounts 0.01 .. 100.00
  12. #define NUM_MULTIPLES_CENT 10000
  13. // amounts 1 .. 10000
  14. #define NUM_MULTIPLES_1BTC 10000
  15. // amounts 50 .. 21000000
  16. #define NUM_MULTIPLES_50BTC 420000
  17. BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup)
  18. bool static TestEncode(uint64_t in) {
  19. return in == CTxOutCompressor::DecompressAmount(CTxOutCompressor::CompressAmount(in));
  20. }
  21. bool static TestDecode(uint64_t in) {
  22. return in == CTxOutCompressor::CompressAmount(CTxOutCompressor::DecompressAmount(in));
  23. }
  24. bool static TestPair(uint64_t dec, uint64_t enc) {
  25. return CTxOutCompressor::CompressAmount(dec) == enc &&
  26. CTxOutCompressor::DecompressAmount(enc) == dec;
  27. }
  28. BOOST_AUTO_TEST_CASE(compress_amounts)
  29. {
  30. BOOST_CHECK(TestPair( 0, 0x0));
  31. BOOST_CHECK(TestPair( 1, 0x1));
  32. BOOST_CHECK(TestPair( CENT, 0x7));
  33. BOOST_CHECK(TestPair( COIN, 0x9));
  34. BOOST_CHECK(TestPair( 50*COIN, 0x32));
  35. BOOST_CHECK(TestPair(21000000*COIN, 0x1406f40));
  36. for (uint64_t i = 1; i <= NUM_MULTIPLES_UNIT; i++)
  37. BOOST_CHECK(TestEncode(i));
  38. for (uint64_t i = 1; i <= NUM_MULTIPLES_CENT; i++)
  39. BOOST_CHECK(TestEncode(i * CENT));
  40. for (uint64_t i = 1; i <= NUM_MULTIPLES_1BTC; i++)
  41. BOOST_CHECK(TestEncode(i * COIN));
  42. for (uint64_t i = 1; i <= NUM_MULTIPLES_50BTC; i++)
  43. BOOST_CHECK(TestEncode(i * 50 * COIN));
  44. for (uint64_t i = 0; i < 100000; i++)
  45. BOOST_CHECK(TestDecode(i));
  46. }
  47. BOOST_AUTO_TEST_SUITE_END()