aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_amount.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-07-10 14:52:59 +0200
committerChristian Grothoff <christian@grothoff.org>2021-07-10 14:55:35 +0200
commit249ba03c36d6bc61b78bdd5a7f1ca55701f1c287 (patch)
tree4bb49f6defd74b77b9eed0622c74f41749301717 /src/util/test_amount.c
parent883b1fc70bc84bb053d09b5eab921f24134194b9 (diff)
expose 2^52 amount value limit in header, check that limit in test cases, add TALER_amount_multiply and TALER_amount_divide2 operations
Diffstat (limited to 'src/util/test_amount.c')
-rw-r--r--src/util/test_amount.c67
1 files changed, 60 insertions, 7 deletions
diff --git a/src/util/test_amount.c b/src/util/test_amount.c
index 8a83e4cf6..1af383dcc 100644
--- a/src/util/test_amount.c
+++ b/src/util/test_amount.c
@@ -1,6 +1,6 @@
/*
This file is part of TALER
- (C) 2015 Taler Systems SA
+ (C) 2015, 2021 Taler Systems SA
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
@@ -186,21 +186,27 @@ main (int argc,
/* test addition with overflow */
a1.fraction = TALER_AMOUNT_FRAC_BASE - 1;
- a1.value = UINT64_MAX - 5;
+ a1.value = TALER_AMOUNT_MAX_VALUE - 5;
a2.fraction = 2;
a2.value = 5;
GNUNET_assert (TALER_AAR_INVALID_RESULT_OVERFLOW ==
- TALER_amount_add (&a3, &a1, &a2));
+ TALER_amount_add (&a3,
+ &a1,
+ &a2));
/* test addition with underflow on fraction */
a1.fraction = 1;
- a1.value = UINT64_MAX;
+ a1.value = TALER_AMOUNT_MAX_VALUE;
a2.fraction = 2;
a2.value = 0;
GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
- TALER_amount_subtract (&a3, &a1, &a2));
- GNUNET_assert (UINT64_MAX - 1 == a3.value);
- GNUNET_assert (TALER_AMOUNT_FRAC_BASE - 1 == a3.fraction);
+ TALER_amount_subtract (&a3,
+ &a1,
+ &a2));
+ GNUNET_assert (TALER_AMOUNT_MAX_VALUE - 1 ==
+ a3.value);
+ GNUNET_assert (TALER_AMOUNT_FRAC_BASE - 1 ==
+ a3.fraction);
/* test division */
GNUNET_assert (GNUNET_OK ==
@@ -288,6 +294,53 @@ main (int argc,
&r));
GNUNET_assert (0 == TALER_amount_cmp (&a1,
&a2));
+
+ /* test multiplication */
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:0",
+ &a1));
+ GNUNET_assert (TALER_AAR_RESULT_ZERO ==
+ TALER_amount_multiply (&a2,
+ &a1,
+ 42));
+ GNUNET_assert (0 == TALER_amount_cmp (&a1,
+ &a2));
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:5.001",
+ &a1));
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:5001",
+ &r));
+ GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
+ TALER_amount_multiply (&a2,
+ &a1,
+ 1000));
+ GNUNET_assert (0 == TALER_amount_cmp (&r,
+ &a2));
+ GNUNET_assert (1000 ==
+ TALER_amount_divide2 (&a2,
+ &a1));
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:5006.00099999",
+ &r));
+ GNUNET_assert (1000 ==
+ TALER_amount_divide2 (&r,
+ &a1));
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:5000.99999999",
+ &r));
+ GNUNET_assert (999 ==
+ TALER_amount_divide2 (&r,
+ &a1));
+ GNUNET_assert (GNUNET_OK ==
+ TALER_string_to_amount ("BTC:0",
+ &a1));
+ GNUNET_assert (INT_MAX ==
+ TALER_amount_divide2 (&a2,
+ &a1));
+ GNUNET_assert (0 ==
+ TALER_amount_divide2 (&a1,
+ &a2));
return 0;
}