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