2015-04-13 17:20:46 +02:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2020-01-19 19:21:58 +01:00
|
|
|
(C) 2015 Taler Systems SA
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2016-07-07 17:55:25 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2015-04-13 17:20:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file util/test_amount.c
|
|
|
|
* @brief Tests for amount logic
|
|
|
|
* @author Christian Grothoff <christian@grothoff.org>
|
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "taler_util.h"
|
|
|
|
#include "taler_amount_lib.h"
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-08-25 16:18:24 +02:00
|
|
|
main (int argc,
|
|
|
|
const char *const argv[])
|
2015-04-13 17:20:46 +02:00
|
|
|
{
|
|
|
|
struct TALER_Amount a1;
|
|
|
|
struct TALER_Amount a2;
|
|
|
|
struct TALER_Amount a3;
|
2020-01-17 18:59:15 +01:00
|
|
|
struct TALER_Amount r;
|
2015-04-13 17:20:46 +02:00
|
|
|
char *c;
|
|
|
|
|
2020-03-27 17:28:33 +01:00
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_log_setup ("test-amout",
|
2019-08-25 16:18:24 +02:00
|
|
|
"WARNING",
|
|
|
|
NULL);
|
2015-04-13 17:20:46 +02:00
|
|
|
/* test invalid conversions */
|
|
|
|
GNUNET_log_skip (6, GNUNET_NO);
|
|
|
|
/* non-numeric */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:4a",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
/* non-numeric */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:4.4a",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
/* non-numeric */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:4.a4",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
/* no currency */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount (":4.a4",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
/* precision too high */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:4.123456789",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
/* value too big */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount (
|
|
|
|
"EUR:1234567890123456789012345678901234567890123456789012345678901234567890",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_log_skip (0, GNUNET_YES);
|
|
|
|
|
|
|
|
/* test conversion without fraction */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:4",
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (0 == strcasecmp ("EUR",
|
2019-08-25 16:18:24 +02:00
|
|
|
a1.currency));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (4 == a1.value);
|
|
|
|
GNUNET_assert (0 == a1.fraction);
|
|
|
|
|
2016-04-10 02:52:46 +02:00
|
|
|
/* test conversion with leading zero in fraction */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("eur:0.02",
|
|
|
|
&a2));
|
2016-04-10 02:52:46 +02:00
|
|
|
GNUNET_assert (0 == strcasecmp ("eur",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2016-04-10 02:52:46 +02:00
|
|
|
GNUNET_assert (0 == a2.value);
|
|
|
|
GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 2 == a2.fraction);
|
|
|
|
c = TALER_amount_to_string (&a2);
|
|
|
|
GNUNET_assert (0 == strcmp ("eur:0.02",
|
|
|
|
c));
|
|
|
|
GNUNET_free (c);
|
|
|
|
|
2015-04-13 17:20:46 +02:00
|
|
|
/* test conversion with leading space and with fraction */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount (" eur:4.12",
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (0 == strcasecmp ("eur",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (4 == a2.value);
|
|
|
|
GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 12 == a2.fraction);
|
|
|
|
|
|
|
|
/* test use of local currency */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount (" *LOCAL:4444.1000",
|
|
|
|
&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (0 == strcasecmp ("*LOCAL",
|
2019-08-25 16:18:24 +02:00
|
|
|
a3.currency));
|
2015-04-13 17:20:46 +02:00
|
|
|
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 ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_cmp_currency (&a1,
|
|
|
|
&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_cmp_currency (&a1,
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
2020-01-20 02:11:03 +01:00
|
|
|
/* test subtraction failure (currency mismatch) */
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_subtract (&a3,
|
|
|
|
&a3,
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test subtraction failure (negative result) */
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_INVALID_NEGATIVE_RESULT ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_subtract (&a3,
|
|
|
|
&a1,
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test subtraction success cases */
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_subtract (&a3,
|
|
|
|
&a2,
|
|
|
|
&a1));
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_RESULT_ZERO ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_subtract (&a3,
|
|
|
|
&a1,
|
|
|
|
&a1));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (0 == a3.value);
|
|
|
|
GNUNET_assert (0 == a3.fraction);
|
|
|
|
GNUNET_assert (GNUNET_NO ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test addition success */
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_add (&a3,
|
|
|
|
&a3,
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_NO ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test normalization */
|
|
|
|
a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE;
|
|
|
|
a3.value = 4;
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test conversion to string */
|
|
|
|
c = TALER_amount_to_string (&a3);
|
|
|
|
GNUNET_assert (0 == strcmp ("EUR:6",
|
2019-08-25 16:18:24 +02:00
|
|
|
c));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_free (c);
|
|
|
|
|
|
|
|
/* test normalization with fraction overflow */
|
|
|
|
a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE + 1;
|
|
|
|
a3.value = 4;
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
c = TALER_amount_to_string (&a3);
|
2016-11-18 16:12:04 +01:00
|
|
|
GNUNET_assert (0 == strcmp ("EUR:6.00000001",
|
2019-08-25 16:18:24 +02:00
|
|
|
c));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_free (c);
|
|
|
|
|
|
|
|
/* test normalization with overflow */
|
|
|
|
a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE + 1;
|
|
|
|
a3.value = UINT64_MAX - 1;
|
|
|
|
GNUNET_assert (GNUNET_SYSERR ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&a3));
|
2015-04-13 17:20:46 +02:00
|
|
|
c = TALER_amount_to_string (&a3);
|
|
|
|
GNUNET_assert (NULL == c);
|
|
|
|
|
|
|
|
/* test addition with overflow */
|
|
|
|
a1.fraction = TALER_AMOUNT_FRAC_BASE - 1;
|
|
|
|
a1.value = UINT64_MAX - 5;
|
|
|
|
a2.fraction = 2;
|
|
|
|
a2.value = 5;
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_INVALID_RESULT_OVERFLOW ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_add (&a3, &a1, &a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
|
|
|
|
/* test addition with underflow on fraction */
|
|
|
|
a1.fraction = 1;
|
|
|
|
a1.value = UINT64_MAX;
|
|
|
|
a2.fraction = 2;
|
|
|
|
a2.value = 0;
|
2020-04-08 23:52:01 +02:00
|
|
|
GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_subtract (&a3, &a1, &a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (UINT64_MAX - 1 == a3.value);
|
|
|
|
GNUNET_assert (TALER_AMOUNT_FRAC_BASE - 1 == a3.fraction);
|
2017-03-06 16:35:30 +01:00
|
|
|
|
|
|
|
/* test division */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_string_to_amount ("EUR:3.33",
|
|
|
|
&a1));
|
2017-03-06 16:35:30 +01:00
|
|
|
TALER_amount_divide (&a2,
|
|
|
|
&a1,
|
|
|
|
1);
|
|
|
|
GNUNET_assert (0 == strcasecmp ("EUR",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2017-03-06 16:35:30 +01:00
|
|
|
GNUNET_assert (3 == a2.value);
|
|
|
|
GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 33 == a2.fraction);
|
|
|
|
|
|
|
|
TALER_amount_divide (&a2,
|
|
|
|
&a1,
|
|
|
|
3);
|
|
|
|
GNUNET_assert (0 == strcasecmp ("EUR",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2017-03-06 16:35:30 +01:00
|
|
|
GNUNET_assert (1 == a2.value);
|
|
|
|
GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 11 == a2.fraction);
|
|
|
|
|
|
|
|
TALER_amount_divide (&a2,
|
|
|
|
&a1,
|
|
|
|
2);
|
|
|
|
GNUNET_assert (0 == strcasecmp ("EUR",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2017-03-06 16:35:30 +01:00
|
|
|
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",
|
2019-08-25 16:18:24 +02:00
|
|
|
a2.currency));
|
2017-03-06 16:35:30 +01:00
|
|
|
GNUNET_assert (0 == a2.value);
|
|
|
|
GNUNET_assert (1 == a2.fraction);
|
|
|
|
|
2020-01-15 15:17:02 +01:00
|
|
|
/* test rounding #1 */
|
2020-01-17 18:59:15 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:0.01",
|
|
|
|
&r));
|
2020-01-15 15:17:02 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:4.001",
|
|
|
|
&a1));
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:4",
|
|
|
|
&a2));
|
2020-01-19 20:11:32 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_amount_round_down (&a1,
|
|
|
|
&r));
|
|
|
|
GNUNET_assert (GNUNET_NO ==
|
|
|
|
TALER_amount_round_down (&a1,
|
|
|
|
&r));
|
|
|
|
GNUNET_assert (0 == TALER_amount_cmp (&a1,
|
|
|
|
&a2));
|
2020-01-15 15:17:02 +01:00
|
|
|
|
|
|
|
/* test rounding #2 */
|
2020-01-17 18:59:15 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:0.001",
|
|
|
|
&r));
|
|
|
|
|
2020-01-15 15:17:02 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:4.001",
|
|
|
|
&a1));
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("EUR:4.001",
|
|
|
|
&a2));
|
2020-01-19 20:11:32 +01:00
|
|
|
GNUNET_assert (GNUNET_NO ==
|
|
|
|
TALER_amount_round_down (&a1,
|
|
|
|
&r));
|
|
|
|
GNUNET_assert (0 == TALER_amount_cmp (&a1,
|
|
|
|
&a2));
|
2017-03-06 16:35:30 +01:00
|
|
|
|
2020-01-19 20:11:32 +01:00
|
|
|
/* test rounding #3 */
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("BTC:5",
|
|
|
|
&r));
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("BTC:12.3",
|
|
|
|
&a1));
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_string_to_amount ("BTC:10",
|
|
|
|
&a2));
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
TALER_amount_round_down (&a1,
|
|
|
|
&r));
|
|
|
|
GNUNET_assert (0 == TALER_amount_cmp (&a1,
|
|
|
|
&a2));
|
2015-04-13 17:20:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-31 12:59:50 +01:00
|
|
|
|
2015-04-13 17:20:46 +02:00
|
|
|
/* end of test_amount.c */
|