Added the /BARCHART option to CROSSTABS
[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 "libpspp/compiler.h"
25
26 #include "math/decimal.h"
27 #include "math/chart-geometry.h"
28 #include <limits.h>
29 #include <float.h>
30 #include <math.h>
31
32 #if 0
33 static void
34 dump_scale (const struct decimal *low, const struct decimal *interval, int n_ticks)
35 {
36   int i;
37   struct decimal tick = *low;
38   for (i = 0; i <= n_ticks; ++i)
39     {
40       printf ("Tick %d: %s (%g)\n", i, decimal_to_string (&tick), decimal_to_double (&tick));
41       decimal_add (&tick, interval);
42     }
43 }
44 #endif
45
46
47 static void
48 test_range (double low, double high)
49 {
50   int n_ticks = 0;
51   struct decimal interval;
52   struct decimal lower;
53
54   chart_get_scale (high, low,
55                    &lower, &interval, &n_ticks);
56
57   assert (n_ticks > 0);
58   assert (n_ticks < 12);
59
60   //  dump_scale (&lower, &interval, n_ticks);
61
62   assert (decimal_to_double (&lower) <= low);
63   
64   {
65     struct decimal  l = lower;
66     decimal_add (&l, &interval);
67     assert (decimal_to_double (&l) > low);
68   }
69
70   {
71     struct decimal  i = interval;
72
73     decimal_int_multiply (&i, n_ticks - 1);
74
75     decimal_add (&i, &lower);
76
77     /* i now contains the upper bound minus one tick */
78     assert (decimal_to_double (&i) < high);
79
80     decimal_add (&i, &interval);
81
82     assert (decimal_to_double (&i) >= high);
83   }
84
85 }
86
87
88 int 
89 main (int argc UNUSED, char **argv UNUSED)
90 {
91   test_range (0.2, 11);
92   test_range (-0.2, 11);
93   test_range (-10, 0.2);
94   test_range (-10, -0.2);
95
96   test_range (102, 50030); 
97   test_range (0.00102, 0.0050030); 
98
99   return 0;
100 }