diff options
Diffstat (limited to 'src/util/test_amount.c')
-rw-r--r-- | src/util/test_amount.c | 129 |
1 files changed, 65 insertions, 64 deletions
diff --git a/src/util/test_amount.c b/src/util/test_amount.c index 3854297dd..a8b37823c 100644 --- a/src/util/test_amount.c +++ b/src/util/test_amount.c @@ -25,8 +25,8 @@ int -main(int argc, - const char *const argv[]) +main (int argc, + const char *const argv[]) { struct TALER_Amount a1; struct TALER_Amount a2; @@ -34,51 +34,52 @@ main(int argc, char *c; GNUNET_log_setup ("test-amout", - "WARNING", - NULL); + "WARNING", + NULL); /* test invalid conversions */ GNUNET_log_skip (6, GNUNET_NO); /* non-numeric */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount ("EUR:4a", - &a1)); + TALER_string_to_amount ("EUR:4a", + &a1)); /* non-numeric */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount ("EUR:4.4a", - &a1)); + TALER_string_to_amount ("EUR:4.4a", + &a1)); /* non-numeric */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount ("EUR:4.a4", - &a1)); + TALER_string_to_amount ("EUR:4.a4", + &a1)); /* no currency */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount (":4.a4", - &a1)); + TALER_string_to_amount (":4.a4", + &a1)); /* precision too high */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount ("EUR:4.123456789", - &a1)); + TALER_string_to_amount ("EUR:4.123456789", + &a1)); /* value too big */ GNUNET_assert (GNUNET_SYSERR == - TALER_string_to_amount ("EUR:1234567890123456789012345678901234567890123456789012345678901234567890", - &a1)); + TALER_string_to_amount ( + "EUR:1234567890123456789012345678901234567890123456789012345678901234567890", + &a1)); GNUNET_log_skip (0, GNUNET_YES); /* test conversion without fraction */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:4", - &a1)); + TALER_string_to_amount ("EUR:4", + &a1)); GNUNET_assert (0 == strcasecmp ("EUR", - a1.currency)); + a1.currency)); GNUNET_assert (4 == a1.value); GNUNET_assert (0 == a1.fraction); /* test conversion with leading zero in fraction */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("eur:0.02", - &a2)); + TALER_string_to_amount ("eur:0.02", + &a2)); GNUNET_assert (0 == strcasecmp ("eur", - a2.currency)); + a2.currency)); GNUNET_assert (0 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 2 == a2.fraction); c = TALER_amount_to_string (&a2); @@ -88,95 +89,95 @@ main(int argc, /* test conversion with leading space and with fraction */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount (" eur:4.12", - &a2)); + TALER_string_to_amount (" eur:4.12", + &a2)); GNUNET_assert (0 == strcasecmp ("eur", - a2.currency)); + a2.currency)); GNUNET_assert (4 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 12 == a2.fraction); /* test use of local currency */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount (" *LOCAL:4444.1000", - &a3)); + TALER_string_to_amount (" *LOCAL:4444.1000", + &a3)); GNUNET_assert (0 == strcasecmp ("*LOCAL", - a3.currency)); + a3.currency)); GNUNET_assert (4444 == a3.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 10 == a3.fraction); /* test CMP with equal and unequal currencies */ GNUNET_assert (GNUNET_NO == - TALER_amount_cmp_currency (&a1, - &a3)); + TALER_amount_cmp_currency (&a1, + &a3)); GNUNET_assert (GNUNET_YES == - TALER_amount_cmp_currency (&a1, - &a2)); + TALER_amount_cmp_currency (&a1, + &a2)); /* test subtraction failure (currency missmatch) */ GNUNET_assert (GNUNET_SYSERR == - TALER_amount_subtract (&a3, - &a3, - &a2)); + TALER_amount_subtract (&a3, + &a3, + &a2)); GNUNET_assert (GNUNET_SYSERR == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); /* test subtraction failure (negative result) */ GNUNET_assert (GNUNET_SYSERR == - TALER_amount_subtract (&a3, - &a1, - &a2)); + TALER_amount_subtract (&a3, + &a1, + &a2)); GNUNET_assert (GNUNET_SYSERR == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); /* test subtraction success cases */ GNUNET_assert (GNUNET_YES == - TALER_amount_subtract (&a3, - &a2, - &a1)); + TALER_amount_subtract (&a3, + &a2, + &a1)); GNUNET_assert (GNUNET_NO == - TALER_amount_subtract (&a3, - &a1, - &a1)); + TALER_amount_subtract (&a3, + &a1, + &a1)); GNUNET_assert (0 == a3.value); GNUNET_assert (0 == a3.fraction); GNUNET_assert (GNUNET_NO == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); /* test addition success */ GNUNET_assert (GNUNET_OK == - TALER_amount_add (&a3, - &a3, - &a2)); + TALER_amount_add (&a3, + &a3, + &a2)); GNUNET_assert (GNUNET_NO == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); /* test normalization */ a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE; a3.value = 4; GNUNET_assert (GNUNET_YES == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); /* test conversion to string */ c = TALER_amount_to_string (&a3); GNUNET_assert (0 == strcmp ("EUR:6", - c)); + c)); GNUNET_free (c); /* test normalization with fraction overflow */ a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE + 1; a3.value = 4; GNUNET_assert (GNUNET_YES == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); c = TALER_amount_to_string (&a3); GNUNET_assert (0 == strcmp ("EUR:6.00000001", - c)); + c)); GNUNET_free (c); /* test normalization with overflow */ a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE + 1; a3.value = UINT64_MAX - 1; GNUNET_assert (GNUNET_SYSERR == - TALER_amount_normalize (&a3)); + TALER_amount_normalize (&a3)); c = TALER_amount_to_string (&a3); GNUNET_assert (NULL == c); @@ -186,7 +187,7 @@ main(int argc, a2.fraction = 2; a2.value = 5; GNUNET_assert (GNUNET_SYSERR == - TALER_amount_add (&a3, &a1, &a2)); + TALER_amount_add (&a3, &a1, &a2)); /* test addition with underflow on fraction */ a1.fraction = 1; @@ -194,19 +195,19 @@ main(int argc, a2.fraction = 2; a2.value = 0; GNUNET_assert (GNUNET_OK == - TALER_amount_subtract (&a3, &a1, &a2)); + TALER_amount_subtract (&a3, &a1, &a2)); GNUNET_assert (UINT64_MAX - 1 == a3.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE - 1 == a3.fraction); /* test division */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:3.33", - &a1)); + TALER_string_to_amount ("EUR:3.33", + &a1)); TALER_amount_divide (&a2, &a1, 1); GNUNET_assert (0 == strcasecmp ("EUR", - a2.currency)); + a2.currency)); GNUNET_assert (3 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 33 == a2.fraction); @@ -214,7 +215,7 @@ main(int argc, &a1, 3); GNUNET_assert (0 == strcasecmp ("EUR", - a2.currency)); + a2.currency)); GNUNET_assert (1 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 11 == a2.fraction); @@ -222,14 +223,14 @@ main(int argc, &a1, 2); GNUNET_assert (0 == strcasecmp ("EUR", - a2.currency)); + a2.currency)); GNUNET_assert (1 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 1000 * 665 == a2.fraction); TALER_amount_divide (&a2, &a1, TALER_AMOUNT_FRAC_BASE * 2); GNUNET_assert (0 == strcasecmp ("EUR", - a2.currency)); + a2.currency)); GNUNET_assert (0 == a2.value); GNUNET_assert (1 == a2.fraction); |