File: math.t

package info (click to toggle)
libtime-clock-perl 1.03-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 108 kB
  • sloc: perl: 680; makefile: 2
file content (226 lines) | stat: -rw-r--r-- 6,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl -w

use strict;

use Test::More tests => 49;

use Time::Clock;

my $t = Time::Clock->new;

#
# Add
#

$t->add(seconds => 1);
is($t->as_string, '00:00:01', 'add 1 second');

$t->parse('00:00:00');
$t->add(nanoseconds => 1);
is($t->as_string, '00:00:00.000000001', 'add 1 nanosecond');

$t->parse('00:00:00');
$t->add(minutes => 1);
is($t->as_string, '00:01:00', 'add 1 minute');

$t->parse('00:00:00');
$t->add(hours => 1);
is($t->as_string, '01:00:00', 'add 1 hour');

# Unit wrap

$t->parse('00:00:00.999999999');
$t->add(nanoseconds => 1);
is($t->as_string, '00:00:01', 'add 1 nanosecond - unit wrap');

$t->parse('00:00:59');
$t->add(seconds => 1);
is($t->as_string, '00:01:00', 'add 1 second - unit wrap');

$t->parse('00:59:00');
$t->add(minutes => 1);
is($t->as_string, '01:00:00', 'add 1 minute - unit wrap');

$t->parse('23:00:00');
$t->add(hours => 1);
is($t->as_string, '00:00:00', 'add 1 hour - unit wrap');

$t->parse('23:59:59.999999999');
$t->add(nanoseconds => 1);
is($t->as_string, '00:00:00', 'add 1 nanosecond - unit wrap 2');

$t->parse('23:59:59');
$t->add(seconds => 1);
is($t->as_string, '00:00:00', 'add 1 second - unit wrap 2');

$t->parse('23:59:00');
$t->add(minutes => 1);
is($t->as_string, '00:00:00', 'add 1 minute - unit wrap 2');

$t->parse('23:00:00');
$t->add(hours => 1);
is($t->as_string, '00:00:00', 'add 1 hour - unit wrap 2');

# Bulk units

$t->parse('12:34:56.789');
$t->add(nanoseconds => 2_000_000_123);
is($t->as_string, '12:34:58.789000123', 'add 2,000,000,123 nanoseconds');

$t->parse('01:02:03');
$t->add(seconds => 3800);
is($t->as_string, '02:05:23', 'add 3,800 seconds');

$t->parse('01:02:03');
$t->add(minutes => 62);
is($t->as_string, '02:04:03', 'add 62 minutes');

$t->parse('01:02:03');
$t->add(hours => 24);
is($t->as_string, '01:02:03', 'add 24 hours');

$t->parse('01:02:03');
$t->add(hours => 25);
is($t->as_string, '02:02:03', 'add 25 hours');

# Mixed units

$t->parse('01:02:03');
$t->add(hours => 3, minutes => 2, seconds => 1, nanoseconds => 54321);
is($t->as_string, '04:04:04.000054321', 'add 03:02:01.000054321');

$t->parse('01:02:03');
$t->add(hours => 125, minutes => 161, seconds => 161, nanoseconds => 1_234_567_890);
is($t->as_string, '08:45:45.23456789', 'add 125:161:162.234567890');

# Strings

$t->parse('01:02:03');
$t->add('03:02:01.000054321');
is($t->as_string, '04:04:04.000054321', 'add 03:02:01.000054321 string');

$t->parse('01:02:03');
$t->add('125:161:162.234567890');
is($t->as_string, '08:45:45.23456789', 'add 125:161:162.234567890 string');

$t->parse('01:02:03');
$t->add('1');
is($t->as_string, '02:02:03', 'add 1 string');

$t->parse('01:02:03');
$t->add('1:2');
is($t->as_string, '02:04:03', 'add 1:2 string');

$t->parse('01:02:03');
$t->add('1:2:3');
is($t->as_string, '02:04:06', 'add 1:2:3 string');

$t->parse('01:02:03');
$t->add('1:2:3.456');
is($t->as_string, '02:04:06.456', 'add 1:2:3.456 string');

eval { $t->add('125:161:162.2345678901x') };
ok($@, 'bad delta string 125:161:162.2345678901');

eval { $t->add(':161:162.2345678901') };
ok($@, 'bad delta string :161:162.2345678901');

#
# Subtract
#

$t->parse('00:00:01');
$t->subtract(seconds => 1);
is($t->as_string, '00:00:00', 'subtract 1 second');

$t->parse('00:00:00.000000001');
$t->subtract(nanoseconds => 1);
is($t->as_string, '00:00:00', 'subtract 1 nanosecond');

$t->parse('00:01:00');
$t->subtract(minutes => 1);
is($t->as_string, '00:00:00', 'subtract 1 minute');

$t->parse('01:00:00');
$t->subtract(hours => 1);
is($t->as_string, '00:00:00', 'subtract 1 hour');

# Unit wrap

$t->parse('00:00:01');
$t->subtract(nanoseconds => 1);
is($t->as_string, '00:00:00.999999999', 'subtract 1 nanosecond - unit wrap');

$t->parse('00:01:00');
$t->subtract(seconds => 1);
is($t->as_string, '00:00:59', 'subtract 1 second - unit wrap');

$t->parse('01:00:00');
$t->subtract(minutes => 1);
is($t->as_string, '00:59:00', 'subtract 1 minute - unit wrap');

$t->parse('00:00:00');
$t->subtract(hours => 1);
is($t->as_string, '23:00:00', 'subtract 1 hour - unit wrap');

$t->parse('00:00:00');
$t->subtract(nanoseconds => 1);
is($t->as_string, '23:59:59.999999999', 'subtract 1 nanosecond - unit wrap 2');

$t->parse('00:00:00');
$t->subtract(seconds => 1);
is($t->as_string, '23:59:59', 'subtract 1 second - unit wrap 2');

$t->parse('00:00:00');
$t->subtract(minutes => 1);
is($t->as_string, '23:59:00', 'subtract 1 minute - unit wrap 2');

$t->parse('00:00:00');
$t->subtract(hours => 1);
is($t->as_string, '23:00:00', 'subtract 1 hour - unit wrap 2');

# Bulk units

$t->parse('12:34:58.789000123');
$t->subtract(nanoseconds => 2_000_000_123);
is($t->as_string, '12:34:56.789', 'subtract 2,000,000,123 nanoseconds');

$t->parse('02:05:23');
$t->subtract(seconds => 3800);
is($t->as_string, '01:02:03', 'subtract 3,800 seconds');

$t->parse('02:04:03');
$t->subtract(minutes => 62);
is($t->as_string, '01:02:03', 'subtract 62 minutes');

$t->parse('01:02:03');
$t->subtract(hours => 24);
is($t->as_string, '01:02:03', 'subtract 24 hours');

$t->parse('02:02:03');
$t->subtract(hours => 25);
is($t->as_string, '01:02:03', 'subtract 25 hours');

# Mixed units

$t->parse('04:04:04.000054321');
$t->subtract(hours => 3, minutes => 2, seconds => 1, nanoseconds => 54321);
is($t->as_string, '01:02:03', 'subtract 03:02:01.000054321');

$t->parse('08:45:45.234567890');
$t->subtract(hours => 125, minutes => 161, seconds => 161, nanoseconds => 1_234_567_890);
is($t->as_string, '01:02:03', 'subtract 125:161:162.234567890');

$t->parse('08:45:45.234567890');
for(1 .. 125) { $t->subtract(hours => 1) }
for(1 .. 161) { $t->subtract(minutes => 1) }
for(1 .. 161) { $t->subtract(seconds => 1) }
is($t->as_string, '01:02:04.23456789', 'subtract 125:161:161 by 1s');

$t->parse('08:45:45.234567890');
$t->subtract(nanoseconds => 1_234_567_890);
is($t->as_string, '08:45:44', 'subtract 0.234567890');

$t->parse('24:00');
$t->subtract(hours => 3, minutes => 2, seconds => 1, nanoseconds => 54321);
is($t->as_string, '20:57:58.999945679', 'subtract 03:02:01.000054321');