6f2ca711d0bea538fabf2f920db08a63215fc9fb
[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/chart-geometry.h"
27 #include <limits.h>
28 #include <float.h>
29 #include <math.h>
30
31 #if 0
32 static void
33 dump_scale (const double low, const double interval, int n_ticks)
34 {
35   int i;
36   double tick = low;
37   for (i = 0; i <= n_ticks; ++i)
38     {
39       printf ("Tick %d: %g\n", i, tick);
40       tick += interval;
41     }
42 }
43 #endif
44
45
46 static void
47 test_range (double low, double high)
48 {
49   int n_ticks = 0;
50   double interval;
51   double lower;
52
53   chart_get_scale (high, low,
54                    &lower, &interval, &n_ticks);
55
56   if ((high - low) < 10 * DBL_MIN){
57     assert (n_ticks == 0);
58     assert (lower == low);
59     assert (interval <= 10 * DBL_MIN);
60   }
61   else
62     assert (n_ticks > 4);
63
64   assert (n_ticks <= 10);
65
66 #if 0
67   printf("%s: high: %lg, low %lg, interval: %lg, nticks: %d\n", 
68          __FUNCTION__, high, low, interval, n_ticks);
69   dump_scale (lower, interval, n_ticks);
70 #endif
71
72   if ((high - low) > 10 * DBL_MIN) {
73     assert (lower <= low);
74     assert (lower + interval > low);
75     assert (lower + n_ticks * interval < high);
76     assert (lower + (n_ticks + 1) * interval >= high);
77   }
78 }
79
80
81 int 
82 main (int argc UNUSED, char **argv UNUSED)
83 {
84   test_range (0, 0);
85   test_range (5, 5);
86   test_range (-5, -5);
87   test_range (0, 7);
88   test_range (0.2, 11);
89   test_range (-0.2, 11);
90   test_range (-10, 0.2);
91   test_range (-10, -0.2);
92   test_range (-10000, 10003);
93   test_range (50042,50053);
94   test_range (-50010, -49999);
95   test_range (0.000100002, 0.000100010);
96
97   test_range (102, 50030); 
98   test_range (0.00102, 0.0050030); 
99
100   return 0;
101 }