aboutsummaryrefslogtreecommitdiff
path: root/src/lib/test_stefan.c
blob: 838cca769bd82c73d608f5f7c551d505921222bd (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
/*
  This file is part of TALER
  Copyright (C) 2023 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 lib/test_stefan.c
 * @brief test calculations on the STEFAN curve
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler_json_lib.h"
#include <gnunet/gnunet_curl_lib.h>
#include "exchange_api_handle.h"


/**
 * Check if @a a and @a b are numerically close.
 *
 * @param a an amount
 * @param b an amount
 * @return true if both values are quite close
 */
static bool
amount_close (const struct TALER_Amount *a,
              const struct TALER_Amount *b)
{
  struct TALER_Amount delta;

  switch (TALER_amount_cmp (a,
                            b))
  {
  case -1: /* a < b */
    GNUNET_assert (0 <
                   TALER_amount_subtract (&delta,
                                          b,
                                          a));
    break;
  case 0:
    /* perfect */
    return true;
  case 1: /* a > b */
    GNUNET_assert (0 <
                   TALER_amount_subtract (&delta,
                                          a,
                                          b));
    break;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Rounding error is %s\n",
              TALER_amount2s (&delta));
  if (delta.value > 0)
  {
    GNUNET_break (0);
    return false;
  }
  if (delta.fraction > 5000)
  {
    GNUNET_break (0);
    return false;
  }
  return true; /* let's consider this a rounding error */
}


int
main (int argc,
      char **argv)
{
  struct TALER_EXCHANGE_DenomPublicKey dk;
  struct TALER_EXCHANGE_Keys keys = {
    .denom_keys = &dk,
    .num_denom_keys = 1
  };
  struct TALER_Amount brut;
  struct TALER_Amount net;

  (void) argc;
  (void) argv;
  GNUNET_log_setup ("test-stefan",
                    "INFO",
                    NULL);
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:0.13",
                                         &dk.value));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:1",
                                         &keys.stefan_abs));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:0.13",
                                         &keys.stefan_log));
  keys.stefan_lin = 1.15;
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:4",
                                         &brut));
  GNUNET_log_skip (1,
                   GNUNET_NO);
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_EXCHANGE_keys_stefan_b2n (&keys,
                                                 &brut,
                                                 &net));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:4",
                                         &net));
  GNUNET_log_skip (1,
                   GNUNET_NO);
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_EXCHANGE_keys_stefan_n2b (&keys,
                                                 &net,
                                                 &brut));
  keys.stefan_lin = 1.0;
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:4",
                                         &brut));
  GNUNET_log_skip (1,
                   GNUNET_NO);
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_EXCHANGE_keys_stefan_b2n (&keys,
                                                 &brut,
                                                 &net));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("MAGIC:4",
                                         &net));
  GNUNET_log_skip (1,
                   GNUNET_NO);
  GNUNET_assert (GNUNET_SYSERR ==
                 TALER_EXCHANGE_keys_stefan_n2b (&keys,
                                                 &net,
                                                 &brut));
  GNUNET_assert (0 == GNUNET_get_log_skip ());
  keys.stefan_lin = 0.1;

  /* try various values for lin and log STEFAN values */
  for (unsigned int li = 1; li < 13; li += 1)
  {
    keys.stefan_lin = 1.0 * li / 100.0;

    for (unsigned int lx = 1; lx < 100; lx += 1)
    {
      keys.stefan_log.fraction = lx * TALER_AMOUNT_FRAC_BASE / 100;

      /* Check brutto-to-netto is stable */
      for (unsigned int i = 0; i<10; i++)
      {
        struct TALER_Amount rval;

        brut.value = i;
        brut.fraction = i * TALER_AMOUNT_FRAC_BASE / 10;
        GNUNET_assert (GNUNET_SYSERR !=
                       TALER_EXCHANGE_keys_stefan_b2n (&keys,
                                                       &brut,
                                                       &net));
        GNUNET_assert (GNUNET_SYSERR !=
                       TALER_EXCHANGE_keys_stefan_n2b (&keys,
                                                       &net,
                                                       &rval));
        if (TALER_amount_is_zero (&net))
          GNUNET_assert (TALER_amount_is_zero (&rval));
        else
        {
          GNUNET_assert (amount_close (&brut,
                                       &rval));
          TALER_EXCHANGE_keys_stefan_round (&keys,
                                            &rval);
          GNUNET_assert (amount_close (&brut,
                                       &rval));
        }
      }

      /* Check netto-to-brutto is stable */
      for (unsigned int i = 0; i<10; i++)
      {
        struct TALER_Amount rval;

        net.value = i;
        net.fraction = i * TALER_AMOUNT_FRAC_BASE / 10;
        GNUNET_assert (GNUNET_SYSERR !=
                       TALER_EXCHANGE_keys_stefan_n2b (&keys,
                                                       &net,
                                                       &brut));
        GNUNET_assert (GNUNET_SYSERR !=
                       TALER_EXCHANGE_keys_stefan_b2n (&keys,
                                                       &brut,
                                                       &rval));
        GNUNET_assert (amount_close (&net,
                                     &rval));
        TALER_EXCHANGE_keys_stefan_round (&keys,
                                          &rval);
        GNUNET_assert (amount_close (&net,
                                     &rval));
      }
    }
  }
  return 0;
}