aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
authorAntonio Juarez <antonio.maria.juarez@live.com>2014-04-02 17:00:17 +0100
committerAntonio Juarez <antonio.maria.juarez@live.com>2014-04-02 17:00:17 +0100
commit29c2859a3e8a935ef605534c6c36333894980d50 (patch)
tree1d31daa09741cc82ecc24c0c89f15b84e74cb12d /tests/unit_tests
parentsome fixes (diff)
downloadmonero-29c2859a3e8a935ef605534c6c36333894980d50.tar.xz
json rpc for wallet and bugfix
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/block_reward.cpp76
-rw-r--r--tests/unit_tests/get_xtype_from_string.cpp3
-rw-r--r--tests/unit_tests/parse_amount.cpp124
-rw-r--r--tests/unit_tests/test_format_utils.cpp18
4 files changed, 171 insertions, 50 deletions
diff --git a/tests/unit_tests/block_reward.cpp b/tests/unit_tests/block_reward.cpp
index cfa12a7c8..2381d3686 100644
--- a/tests/unit_tests/block_reward.cpp
+++ b/tests/unit_tests/block_reward.cpp
@@ -16,14 +16,13 @@ namespace
protected:
static const size_t current_block_size = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE / 2;
- std::vector<size_t> m_last_block_sizes;
- bool m_block_too_big;
+ bool m_block_not_too_big;
uint64_t m_block_reward;
};
- #define TEST_ALREADY_GENERATED_COINS(already_generated_coins, expected_reward) \
- m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins); \
- ASSERT_FALSE(m_block_too_big); \
+ #define TEST_ALREADY_GENERATED_COINS(already_generated_coins, expected_reward) \
+ m_block_not_too_big = get_block_reward(0, current_block_size, already_generated_coins, m_block_reward); \
+ ASSERT_TRUE(m_block_not_too_big); \
ASSERT_EQ(m_block_reward, UINT64_C(expected_reward));
TEST_F(block_reward_and_already_generated_coins, handles_first_values)
@@ -53,72 +52,70 @@ namespace
protected:
virtual void SetUp()
{
- m_standard_block_reward = get_block_reward(m_last_block_sizes, 0, m_block_too_big, already_generated_coins);
- ASSERT_FALSE(m_block_too_big);
+ m_block_not_too_big = get_block_reward(0, 0, already_generated_coins, m_standard_block_reward);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE, m_standard_block_reward);
}
- void do_test(size_t current_block_size)
+ void do_test(size_t median_block_size, size_t current_block_size)
{
- m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins);
+ m_block_not_too_big = get_block_reward(median_block_size, current_block_size, already_generated_coins, m_block_reward);
}
static const uint64_t already_generated_coins = 0;
- std::vector<size_t> m_last_block_sizes;
- bool m_block_too_big;
+ bool m_block_not_too_big;
uint64_t m_block_reward;
uint64_t m_standard_block_reward;
};
TEST_F(block_reward_and_current_block_size, handles_block_size_less_relevance_level)
{
- do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1);
- ASSERT_FALSE(m_block_too_big);
+ do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_current_block_size, handles_block_size_eq_relevance_level)
{
- do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE);
- ASSERT_FALSE(m_block_too_big);
+ do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_current_block_size, handles_block_size_gt_relevance_level)
{
- do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1);
- ASSERT_FALSE(m_block_too_big);
+ do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_current_block_size, handles_block_size_less_2_relevance_level)
{
- do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1);
- ASSERT_FALSE(m_block_too_big);
+ do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(m_block_reward, m_standard_block_reward);
ASSERT_LT(0, m_block_reward);
}
TEST_F(block_reward_and_current_block_size, handles_block_size_eq_2_relevance_level)
{
- do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE);
- ASSERT_FALSE(m_block_too_big);
+ do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(0, m_block_reward);
}
TEST_F(block_reward_and_current_block_size, handles_block_size_gt_2_relevance_level)
{
- do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1);
- ASSERT_TRUE(m_block_too_big);
+ do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1);
+ ASSERT_FALSE(m_block_not_too_big);
}
TEST_F(block_reward_and_current_block_size, fails_on_huge_median_size)
{
#if !defined(NDEBUG)
size_t huge_size = std::numeric_limits<uint32_t>::max() + UINT64_C(2);
- m_last_block_sizes.push_back(huge_size);
- ASSERT_DEATH(do_test(huge_size + 1), "");
+ ASSERT_DEATH(do_test(huge_size, huge_size + 1), "");
#endif
}
@@ -126,8 +123,7 @@ namespace
{
#if !defined(NDEBUG)
size_t huge_size = std::numeric_limits<uint32_t>::max() + UINT64_C(2);
- m_last_block_sizes.push_back(huge_size - 2);
- ASSERT_DEATH(do_test(huge_size), "");
+ ASSERT_DEATH(do_test(huge_size - 2, huge_size), "");
#endif
}
@@ -145,21 +141,21 @@ namespace
m_last_block_sizes_median = 7 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE;
- m_standard_block_reward = get_block_reward(m_last_block_sizes, 0, m_block_too_big, already_generated_coins);
- ASSERT_FALSE(m_block_too_big);
+ m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_sizes), 0, already_generated_coins, m_standard_block_reward);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE, m_standard_block_reward);
}
void do_test(size_t current_block_size)
{
- m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins);
+ m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_sizes), current_block_size, already_generated_coins, m_block_reward);
}
static const uint64_t already_generated_coins = 0;
std::vector<size_t> m_last_block_sizes;
uint64_t m_last_block_sizes_median;
- bool m_block_too_big;
+ bool m_block_not_too_big;
uint64_t m_block_reward;
uint64_t m_standard_block_reward;
};
@@ -167,28 +163,28 @@ namespace
TEST_F(block_reward_and_last_block_sizes, handles_block_size_less_median)
{
do_test(m_last_block_sizes_median - 1);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_last_block_sizes, handles_block_size_eq_median)
{
do_test(m_last_block_sizes_median);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_last_block_sizes, handles_block_size_gt_median)
{
do_test(m_last_block_sizes_median + 1);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(m_block_reward, m_standard_block_reward);
}
TEST_F(block_reward_and_last_block_sizes, handles_block_size_less_2_medians)
{
do_test(2 * m_last_block_sizes_median - 1);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_LT(m_block_reward, m_standard_block_reward);
ASSERT_LT(0, m_block_reward);
}
@@ -196,14 +192,14 @@ namespace
TEST_F(block_reward_and_last_block_sizes, handles_block_size_eq_2_medians)
{
do_test(2 * m_last_block_sizes_median);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(0, m_block_reward);
}
TEST_F(block_reward_and_last_block_sizes, handles_block_size_gt_2_medians)
{
do_test(2 * m_last_block_sizes_median + 1);
- ASSERT_TRUE(m_block_too_big);
+ ASSERT_FALSE(m_block_not_too_big);
}
TEST_F(block_reward_and_last_block_sizes, calculates_correctly)
@@ -211,16 +207,16 @@ namespace
ASSERT_EQ(0, m_last_block_sizes_median % 8);
do_test(m_last_block_sizes_median * 9 / 8);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward * 63 / 64);
// 3/2 = 12/8
do_test(m_last_block_sizes_median * 3 / 2);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward * 3 / 4);
do_test(m_last_block_sizes_median * 15 / 8);
- ASSERT_FALSE(m_block_too_big);
+ ASSERT_TRUE(m_block_not_too_big);
ASSERT_EQ(m_block_reward, m_standard_block_reward * 15 / 64);
}
}
diff --git a/tests/unit_tests/get_xtype_from_string.cpp b/tests/unit_tests/get_xtype_from_string.cpp
index 0ba80fb3d..dd4d2e419 100644
--- a/tests/unit_tests/get_xtype_from_string.cpp
+++ b/tests/unit_tests/get_xtype_from_string.cpp
@@ -45,6 +45,7 @@ TEST_pos(uint16_t, 0, "0");
TEST_pos(uint16_t, 1, "1");
TEST_pos(uint16_t, 65535, "65535");
+TEST_neg(uint16_t, "");
TEST_neg(uint16_t, "+0");
TEST_neg(uint16_t, "+1");
TEST_neg(uint16_t, "+65535");
@@ -77,6 +78,7 @@ TEST_pos(uint32_t, 0, "0");
TEST_pos(uint32_t, 1, "1");
TEST_pos(uint32_t, 4294967295, "4294967295");
+TEST_neg(uint32_t, "");
TEST_neg(uint32_t, "+0");
TEST_neg(uint32_t, "+1");
TEST_neg(uint32_t, "+4294967295");
@@ -107,6 +109,7 @@ TEST_pos(uint64_t, 0, "0");
TEST_pos(uint64_t, 1, "1");
TEST_pos(uint64_t, 18446744073709551615ULL, "18446744073709551615");
+TEST_neg(uint64_t, "");
TEST_neg(uint64_t, "+0");
TEST_neg(uint64_t, "+1");
TEST_neg(uint64_t, "+18446744073709551615");
diff --git a/tests/unit_tests/parse_amount.cpp b/tests/unit_tests/parse_amount.cpp
new file mode 100644
index 000000000..7d0b64088
--- /dev/null
+++ b/tests/unit_tests/parse_amount.cpp
@@ -0,0 +1,124 @@
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include "gtest/gtest.h"
+
+#include "cryptonote_core/cryptonote_format_utils.h"
+
+using namespace cryptonote;
+
+namespace
+{
+ void do_pos_test(uint64_t expected, const std::string& str)
+ {
+ uint64_t val;
+ std::string number_str = str;
+ std::replace(number_str.begin(), number_str.end(), '_', '.');
+ number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end());
+ ASSERT_TRUE(parse_amount(val, number_str));
+ ASSERT_EQ(expected, val);
+ }
+
+ void do_neg_test(const std::string& str)
+ {
+ uint64_t val;
+ std::string number_str = str;
+ std::replace(number_str.begin(), number_str.end(), '_', '.');
+ number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end());
+ ASSERT_FALSE(parse_amount(val, number_str));
+ }
+}
+
+#define TEST_pos(expected, str) \
+ TEST(parse_amount, handles_pos_ ## str) \
+ { \
+ do_pos_test(UINT64_C(expected), #str); \
+ }
+
+#define TEST_neg(str) \
+ TEST(parse_amount, handles_neg_ ## str) \
+ { \
+ do_neg_test(#str); \
+ }
+
+#define TEST_neg_n(str, name) \
+ TEST(parse_amount, handles_neg_ ## name) \
+ { \
+ do_neg_test(#str); \
+ }
+
+
+TEST_pos(0, 0);
+TEST_pos(0, 00);
+TEST_pos(0, 00000000);
+TEST_pos(0, 000000000);
+TEST_pos(0, 00000000000000000000000000000000);
+
+TEST_pos(0, _0);
+TEST_pos(0, _00);
+TEST_pos(0, _00000000);
+TEST_pos(0, _000000000);
+TEST_pos(0, _00000000000000000000000000000000);
+
+TEST_pos(0, 00000000_);
+TEST_pos(0, 000000000_);
+TEST_pos(0, 00000000000000000000000000000000_);
+
+TEST_pos(0, 0_);
+TEST_pos(0, 0_0);
+TEST_pos(0, 0_00);
+TEST_pos(0, 0_00000000);
+TEST_pos(0, 0_000000000);
+TEST_pos(0, 0_00000000000000000000000000000000);
+
+TEST_pos(0, 00_);
+TEST_pos(0, 00_0);
+TEST_pos(0, 00_00);
+TEST_pos(0, 00_00000000);
+TEST_pos(0, 00_000000000);
+TEST_pos(0, 00_00000000000000000000000000000000);
+
+TEST_pos(1, 0_00000001);
+TEST_pos(1, 0_000000010);
+TEST_pos(1, 0_000000010000000000000000000000000);
+TEST_pos(9, 0_00000009);
+TEST_pos(9, 0_000000090);
+TEST_pos(9, 0_000000090000000000000000000000000);
+
+TEST_pos( 100000000, 1);
+TEST_pos( 6553500000000, 65535);
+TEST_pos( 429496729500000000, 4294967295);
+TEST_pos(18446744073700000000, 184467440737_);
+TEST_pos(18446744073700000000, 184467440737_0);
+TEST_pos(18446744073700000000, 184467440737_00000000);
+TEST_pos(18446744073700000000, 184467440737_000000000);
+TEST_pos(18446744073700000000, 184467440737_0000000000000000000);
+TEST_pos(18446744073709551615, 184467440737_09551615);
+
+// Invalid numbers
+TEST_neg_n(~, empty_string);
+TEST_neg_n(-0, minus_0);
+TEST_neg_n(+0, plus_0);
+TEST_neg_n(-1, minus_1);
+TEST_neg_n(+1, plus_1);
+TEST_neg_n(_, only_point);
+
+// A lot of fraction digits
+TEST_neg(0_000000001);
+TEST_neg(0_000000009);
+TEST_neg(184467440737_000000001);
+
+// Overflow
+TEST_neg(184467440737_09551616);
+TEST_neg(184467440738);
+TEST_neg(18446744073709551616);
+
+// Two or more points
+TEST_neg(__);
+TEST_neg(0__);
+TEST_neg(__0);
+TEST_neg(0__0);
+TEST_neg(0_0_);
+TEST_neg(_0_0);
+TEST_neg(0_0_0);
diff --git a/tests/unit_tests/test_format_utils.cpp b/tests/unit_tests/test_format_utils.cpp
index 22f56628e..dfc05f55a 100644
--- a/tests/unit_tests/test_format_utils.cpp
+++ b/tests/unit_tests/test_format_utils.cpp
@@ -7,30 +7,28 @@
#include "common/util.h"
#include "cryptonote_core/cryptonote_format_utils.h"
-TEST(parse_and_validate_tx_extra, is_correct_parse_and_validate_tx_extra)
+TEST(parse_and_validate_tx_extra, is_correct_parse_and_validate_tx_extra)
{
cryptonote::transaction tx = AUTO_VAL_INIT(tx);
cryptonote::account_base acc;
acc.generate();
- std::vector<size_t> bss;
cryptonote::blobdata b = "dsdsdfsdfsf";
- bool r = cryptonote::construct_miner_tx(0, 10000000000000, acc.get_keys().m_account_address, tx, DEFAULT_FEE, bss, 1000, b, 1);
+ bool r = cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, DEFAULT_FEE, acc.get_keys().m_account_address, tx, b, 1);
ASSERT_TRUE(r);
crypto::public_key tx_pub_key;
r = cryptonote::parse_and_validate_tx_extra(tx, tx_pub_key);
ASSERT_TRUE(r);
}
-TEST(parse_and_validate_tx_extra, is_correct_extranonce_too_big)
+TEST(parse_and_validate_tx_extra, is_correct_extranonce_too_big)
{
cryptonote::transaction tx = AUTO_VAL_INIT(tx);
cryptonote::account_base acc;
acc.generate();
- std::vector<size_t> bss;
cryptonote::blobdata b(260, 0);
- bool r = cryptonote::construct_miner_tx(0, 10000000000000, acc.get_keys().m_account_address, tx, DEFAULT_FEE, bss, 1000, b, 1);
+ bool r = cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, DEFAULT_FEE, acc.get_keys().m_account_address, tx, b, 1);
ASSERT_FALSE(r);
}
-TEST(parse_and_validate_tx_extra, is_correct_wrong_extra_couner_too_big)
+TEST(parse_and_validate_tx_extra, is_correct_wrong_extra_couner_too_big)
{
cryptonote::transaction tx = AUTO_VAL_INIT(tx);
tx.extra.resize(20, 0);
@@ -75,7 +73,7 @@ TEST(validate_parse_amount_case, validate_parse_amount)
r = cryptonote::parse_amount(res, " 100.0001 ");
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000010000);
-
+
r = cryptonote::parse_amount(res, " 100.0000 ");
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000000000);
@@ -88,10 +86,10 @@ TEST(validate_parse_amount_case, validate_parse_amount)
r = cryptonote::parse_amount(res, "100 . 0000");
ASSERT_FALSE(r);
-
+
r = cryptonote::parse_amount(res, "100.00 00");
ASSERT_FALSE(r);
-
+
r = cryptonote::parse_amount(res, "1 00.00 00");
ASSERT_FALSE(r);
}