docs
[pspp] / tests / data / test-time-input.py
1 #! /usr/bin/env python
2 # Copyright (C) 2020  Free Software Foundation
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 import re
18 import sys
19
20 fmt_name = sys.argv[1]
21 templates = sys.argv[2:]
22
23 times = (#  D  HH  MM     SS
24          (  0,  0,  0,  0.00),
25          (  1,  4, 50, 38.68),
26          (  5, 12, 31, 35.82),
27          (  0, 12, 47, 53.41),
28          (  3,  1, 26,  0.69),
29          (  1, 20, 58, 11.19),
30          ( 12,  7, 36,  5.98),
31          ( 52, 15, 43, 49.27),
32          (  7,  4, 25,  9.24),
33          (  0,  6, 49, 27.89),
34          ( 20,  2, 57, 52.56),
35          (555, 16, 45, 44.12),
36          (120, 21, 30, 57.27),
37          (  0,  4, 25,  9.98),
38          (  3,  6, 49, 27.24),
39          (  5,  2, 57, 52.13),
40          (  0, 16, 45, 44.35),
41          (  1, 21, 30, 57.32),
42          ( 10, 22, 30,  4.27),
43          ( 22,  1, 56, 51.18))
44
45 syntax_file = open('%s.sps' % fmt_name, 'w')
46 syntax_file.write('''\
47 DATA LIST NOTABLE FILE='%(fmt_name)s.input'/%(fmt_name)s 1-40 (%(fmt_name)s).
48 PRINT OUTFILE='%(fmt_name)s.output'/%(fmt_name)s (F16.2).
49 EXECUTE.
50 ''' % {'fmt_name': fmt_name})
51 syntax_file.close()
52
53 expout_file = open('expout', 'w')
54 input_file = open('%s.input' % fmt_name, 'w')
55
56 def print_all_formats(d, h, m, s, template, formatted, expected, sign):
57     if template != '':
58         c = template[0]
59         template = template[1:]
60         if c == '+':
61             assert sign == ''
62             for new_sign in ('', '-', '+'):
63                 print_all_formats(d, h, m, s, template,
64                                   formatted + new_sign, expected, new_sign)
65         elif c == 'D':
66             for f in ('%d', '%02d'):
67                 print_all_formats(0, h, m, s, template,
68                                   formatted + (f % d), expected + d * 86400,
69                                   sign)
70         elif c == 'H':
71             for f in ('%d', '%02d'):
72                 print_all_formats(0, 0, m, s, template,
73                                   formatted + (f % (h + d * 24)),
74                                   expected + h * 3600 + d * 86400,
75                                   sign)
76         elif c == 'M':
77             for f in ('%d', '%02d'):
78                 print_all_formats(0, 0, 0, s, template,
79                                   formatted + (f % (m + h * 60 + d * 1440)),
80                                   expected + m * 60 + h * 3600 + d * 86400,
81                                   sign)
82         elif c == 'S':
83             for f in ('%.0f', '%02.0f', '%.1f', '%.2f'):
84                 ns = s + m * 60 + h * 3600 + d * 86400
85                 formatted_s = f % ns
86                 print_all_formats(0, 0, 0, 0, template,
87                                   formatted + formatted_s,
88                                   expected + float(formatted_s),
89                                   sign)
90         elif c == ':':
91             for f in (' ', ':'):
92                 print_all_formats(d, h, m, s, template, formatted + f,
93                                   expected, sign)
94         elif c == ' ':
95             print_all_formats(d, h, m, s, template, formatted + ' ',
96                               expected, sign)
97         else:
98             assert False
99     else:
100         # Write the formatted value to fmt_name.input.
101         input_file.write('%s\n' % formatted)
102
103         # Write the expected value to 'expout'.
104         if sign == '-' and expected > 0:
105             expected = -expected
106         expected_s = '%17.2f\n' % expected
107         expected_s = expected_s.replace(' 0.', '  .')
108         expout_file.write(expected_s)
109
110
111 for template in templates:
112     for time in times:
113         d, h, m, s = time
114         print_all_formats(d, h, m, s, template, '', 0, '')
115