New module to perform decimal floating point arithmetic for charts.
[pspp] / tests / math / chart-get-scale-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2015 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <assert.h>
22 #include <string.h>
23
24 #include "math/decimal.h"
25 #include <limits.h>
26 #include <float.h>
27 #include <math.h>
28
29 void
30 dump_scale (const struct decimal *low, const struct decimal *interval, int n_ticks)
31 {
32   int i;
33   struct decimal tick = *low;
34   for (i = 0; i <= n_ticks; ++i)
35     {
36       printf ("Tick %d: %s (%g)\n", i, decimal_to_string (&tick), decimal_to_double (&tick));
37       decimal_add (&tick, interval);
38     }
39 }
40
41
42 void
43 test_range (double low, double high)
44 {
45   int n_ticks = 0;
46   struct decimal interval;
47   struct decimal lower;
48
49   
50   chart_get_scale (high, low,
51                    &lower, &interval, &n_ticks);
52
53   assert (n_ticks > 0);
54   assert (n_ticks < 12);
55
56   //  dump_scale (&lower, &interval, n_ticks);
57
58   assert (decimal_to_double (&lower) <= low);
59   
60   {
61     struct decimal  l = lower;
62     decimal_add (&l, &interval);
63     assert (decimal_to_double (&l) > low);
64   }
65
66   {
67     struct decimal  i = interval;
68
69     decimal_int_multiply (&i, n_ticks - 1);
70
71     decimal_add (&i, &lower);
72
73     /* i now contains the upper bound minus one tick */
74     assert (decimal_to_double (&i) < high);
75
76     decimal_add (&i, &interval);
77
78     assert (decimal_to_double (&i) >= high);
79   }
80
81 }
82
83
84 int 
85 main (int argc, char **argv)
86 {
87   test_range (0.2, 11);
88   test_range (-0.2, 11);
89   test_range (-10, 0.2);
90   test_range (-10, -0.2);
91
92   test_range (102, 50030); 
93   test_range (0.00102, 0.0050030); 
94
95   return 0;
96 }