summaryrefslogtreecommitdiff
path: root/src/util/test_amount.c
blob: 8a83e4cf64f1da0eaa0082d9ab178c0f89d0ba8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
  This file is part of TALER
  (C) 2015 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
  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
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/

/**
 * @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
main (int argc,
      const char *const argv[])
{
  struct TALER_Amount a1;
  struct TALER_Amount a2;
  struct TALER_Amount a3;
  struct TALER_Amount r;
  char *c;

  (void) argc;
  (void) argv;
  GNUNET_log_setup ("test-amout",
                    "WARNING",
                    NULL);
  /* test invalid conversions */
  GNUNET_log_skip (6, GNUNET_NO);
  /* non-numeric */
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_string_to_amount ("EUR:4a",
                                         &a1));
  /* non-numeric */
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_string_to_amount ("EUR:4.4a",
                                         &a1));
  /* non-numeric */
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_string_to_amount ("EUR:4.a4",
                                         &a1));
  /* no currency */
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_string_to_amount (":4.a4",
                                         &a1));
  /* precision too high */
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_string_to_amount ("EUR:4.123456789",
                                         &a1));
  /* value too big */
  GNUNET_assert (GNUNET_SYSERR ==
                 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));
  GNUNET_assert (0 == strcasecmp ("EUR",
                                  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));
  GNUNET_assert (0 == strcasecmp ("eur",
                                  a2.currency));
  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);

  /* test conversion with leading space and with fraction */
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (" eur:4.12",
                                         &a2));
  GNUNET_assert (0 == strcasecmp ("eur",
                                  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));
  GNUNET_assert (0 == strcasecmp ("*LOCAL",
                                  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));
  GNUNET_assert (GNUNET_YES ==
                 TALER_amount_cmp_currency (&a1,
                                            &a2));

  /* test subtraction failure (currency mismatch) */
  GNUNET_assert (TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE ==
                 TALER_amount_subtract (&a3,
                                        &a3,
                                        &a2));
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_amount_normalize (&a3));

  /* test subtraction failure (negative result) */
  GNUNET_assert (TALER_AAR_INVALID_NEGATIVE_RESULT ==
                 TALER_amount_subtract (&a3,
                                        &a1,
                                        &a2));
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_amount_normalize (&a3));

  /* test subtraction success cases */
  GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
                 TALER_amount_subtract (&a3,
                                        &a2,
                                        &a1));
  GNUNET_assert (TALER_AAR_RESULT_ZERO ==
                 TALER_amount_subtract (&a3,
                                        &a1,
                                        &a1));
  GNUNET_assert (0 == a3.value);
  GNUNET_assert (0 == a3.fraction);
  GNUNET_assert (GNUNET_NO ==
                 TALER_amount_normalize (&a3));

  /* test addition success */
  GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
                 TALER_amount_add (&a3,
                                   &a3,
                                   &a2));
  GNUNET_assert (GNUNET_NO ==
                 TALER_amount_normalize (&a3));

  /* test normalization */
  a3.fraction = 2 * TALER_AMOUNT_FRAC_BASE;
  a3.value = 4;
  GNUNET_assert (GNUNET_YES ==
                 TALER_amount_normalize (&a3));

  /* test conversion to string */
  c = TALER_amount_to_string (&a3);
  GNUNET_assert (0 == strcmp ("EUR:6",
                              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));
  c = TALER_amount_to_string (&a3);
  GNUNET_assert (0 == strcmp ("EUR:6.00000001",
                              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));
  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;
  GNUNET_assert (TALER_AAR_INVALID_RESULT_OVERFLOW ==
                 TALER_amount_add (&a3, &a1, &a2));

  /* test addition with underflow on fraction */
  a1.fraction = 1;
  a1.value = UINT64_MAX;
  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);

  /* test division */
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:3.33",
                                         &a1));
  TALER_amount_divide (&a2,
                       &a1,
                       1);
  GNUNET_assert (0 == strcasecmp ("EUR",
                                  a2.currency));
  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",
                                  a2.currency));
  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",
                                  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));
  GNUNET_assert (0 == a2.value);
  GNUNET_assert (1 == a2.fraction);

  /* test rounding #1 */
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:0.01",
                                         &r));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:4.001",
                                         &a1));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:4",
                                         &a2));
  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));

  /* test rounding #2 */
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:0.001",
                                         &r));

  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:4.001",
                                         &a1));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:4.001",
                                         &a2));
  GNUNET_assert (GNUNET_NO ==
                 TALER_amount_round_down (&a1,
                                          &r));
  GNUNET_assert (0 == TALER_amount_cmp (&a1,
                                        &a2));

  /* 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));
  return 0;
}


/* end of test_amount.c */