bfts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ class TestStruct < RubiconTestCase
5
+
6
+ # TODO: why is this a class variable, and why in setup?
7
+ def setup
8
+ @@struct ||= Struct.new 'TestStruct', :alpha, :bravo
9
+ end
10
+
11
+ def test_clone
12
+ for taint in [ false, true ]
13
+ for frozen in [ false, true ]
14
+ a = @@struct.new
15
+ a.alpha = 112
16
+ a.taint if taint
17
+ a.freeze if frozen
18
+ b = a.clone
19
+
20
+ assert_equal(a, b)
21
+ assert(a.__id__ != b.__id__)
22
+ assert_equal(a.frozen?, b.frozen?)
23
+ assert_equal(a.tainted?, b.tainted?)
24
+ assert_equal(a.alpha, b.alpha)
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_each_pair
30
+ # TODO: raise NotImplementedError, 'Need to write test_each_pair'
31
+ end
32
+
33
+ def test_each
34
+ assert_raises LocalJumpError do
35
+ @@struct.new.each
36
+ end
37
+
38
+ a = []
39
+ @@struct.new('a', 'b').each { |x| a << x }
40
+ assert_equal ['a', 'b'], a
41
+ end
42
+
43
+ def test_eql_eh
44
+ # TODO: raise NotImplementedError, 'Need to write test_eql_eh'
45
+ end
46
+
47
+ def test_equals2
48
+ ts1 = @@struct.new 64, 42
49
+ ts2 = @@struct.new 64, 42
50
+
51
+ assert_equal ts1, ts2
52
+
53
+ ts3 = @@struct.new 64
54
+
55
+ assert_not_equal ts1, ts3
56
+
57
+ os1 = Struct.new('OtherStruct', :alpha, :bravo).new 64, 42
58
+
59
+ assert_not_equal os1, ts1
60
+
61
+ os2 = Struct.new('OtherStruct2', :alpha, :bravo, :charlie).new 64, 42
62
+
63
+ assert_not_equal os2, ts1
64
+ end
65
+
66
+ def test_hash
67
+ # TODO: raise NotImplementedError, 'Need to write test_hash'
68
+ end
69
+
70
+ def test_index_equals
71
+ t = @@struct.new
72
+ assert_nothing_raised do
73
+ t[:alpha] = 64
74
+ assert_equal t.alpha, 64
75
+
76
+ t['bravo'] = 112
77
+ assert_equal t.bravo, 112
78
+
79
+ t[0] = 65
80
+ assert_equal t.alpha, 65
81
+
82
+ t[1] = 113
83
+ assert_equal t.bravo, 113
84
+
85
+ t[-2] = 66
86
+ assert_equal t.alpha, 66
87
+ end
88
+
89
+ assert_raises NameError do
90
+ t['gamma'] = 1
91
+ end
92
+ assert_raise IndexError do
93
+ t[2] = 1
94
+ end
95
+ end
96
+
97
+ def test_index
98
+ t = @@struct.new 64, 112
99
+
100
+ assert_equal 64, t['alpha']
101
+ assert_equal 64, t[:alpha]
102
+
103
+ assert_equal 64, t[0]
104
+ assert_equal 112, t[1]
105
+ assert_equal 112, t[-1]
106
+
107
+ assert_equal 112, t[1.5]
108
+
109
+ assert_raises NameError do
110
+ t['gamma']
111
+ end
112
+
113
+ assert_raises IndexError do
114
+ t[2]
115
+ end
116
+ end
117
+
118
+ def test_initialize
119
+ assert_instance_of Class, @@struct
120
+ assert_equal Struct::TestStruct, @@struct
121
+ assert_instance_of Struct::TestStruct, @@struct.new(5)
122
+
123
+ t1 = @@struct.new
124
+ assert_equal nil, t1.alpha
125
+ assert_equal nil, t1.bravo
126
+
127
+ t2 = @@struct.new 1
128
+ assert_equal 1, t2.alpha
129
+ assert_equal nil, t2.bravo
130
+
131
+ t3 = @@struct.new 2, 3
132
+ assert_equal 2, t3.alpha
133
+ assert_equal 3, t3.bravo
134
+
135
+ assert_raises ArgumentError do
136
+ @@struct.new 4, 5, 6
137
+ end
138
+ end
139
+
140
+ def test_to_s
141
+ expected = "#<struct Struct::TestStruct alpha=\"a\", bravo=\"b\">"
142
+ assert_equal expected, @@struct.new('a', 'b').to_s
143
+ end
144
+
145
+ def test_inspect
146
+ s = @@struct.new('a', 'b')
147
+ expected = "#<struct Struct::TestStruct alpha=\"a\", bravo=\"b\">"
148
+ assert_equal expected, s.inspect
149
+ end
150
+
151
+ def test_length
152
+ t = @@struct.new
153
+ assert_equal(2,t.length)
154
+ end
155
+
156
+ def test_members
157
+ assert_equal ["alpha", "bravo"], @@struct.members
158
+ end
159
+
160
+ def test_members
161
+ assert_equal ["alpha", "bravo"], @@struct.new.members
162
+ end
163
+
164
+ def test_select
165
+ struct = @@struct.new 'a', 'b'
166
+ assert_equal ['a'], struct.select { |item| item == 'a' }
167
+ end
168
+
169
+ # REFACTOR / length
170
+ def test_size
171
+ t = @@struct.new
172
+ assert_equal(2, t.size)
173
+ end
174
+
175
+ def test_to_a
176
+ t = @@struct.new 'a', 'b'
177
+ assert_equal ['a', 'b'], t.to_a
178
+ end
179
+
180
+ def test_values_at
181
+ struct = @@struct.new 'a', 'b'
182
+ assert_equal ['b'], struct.values_at(-1)
183
+ assert_equal ['a'], struct.values_at(0)
184
+ assert_equal ['b'], struct.values_at(1)
185
+ assert_equal ['a', 'b'], struct.values_at(0..1)
186
+ end
187
+
188
+ def test_values
189
+ t = @@struct.new 'a', 'b'
190
+ assert_equal ['a', 'b'], t.values
191
+ end
192
+
193
+ end
194
+
@@ -0,0 +1,700 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ #
5
+ # NOTICE: These tests assume that your local time zone is *not* GMT.
6
+ #
7
+
8
+ class T # ZenTest SKIP
9
+ attr :orig
10
+ attr :amt
11
+ attr :result
12
+ def initialize(a1, anAmt, a2)
13
+ @orig = a1
14
+ @amt = anAmt
15
+ @result = a2
16
+ end
17
+ def to_s
18
+ @orig.join("-")
19
+ end
20
+ end
21
+
22
+ class TestTime < RubiconTestCase
23
+
24
+ ONEDAYSEC = 60 * 60 * 24
25
+
26
+ #
27
+ # Test month name to month number
28
+ #
29
+ @@months = {
30
+ 'Jan' => 1,
31
+ 'Feb' => 2,
32
+ 'Mar' => 3,
33
+ 'Apr' => 4,
34
+ 'May' => 5,
35
+ 'Jun' => 6,
36
+ 'Jul' => 7,
37
+ 'Aug' => 8,
38
+ 'Sep' => 9,
39
+ 'Oct' => 10,
40
+ 'Nov' => 11,
41
+ 'Dec' => 12
42
+ }
43
+
44
+ #
45
+ # A random selection of interesting dates
46
+ #
47
+ @@dates = [
48
+ # Source + amt == dest
49
+ T.new([1999, 12, 31, 23,59,59], 1, [2000, 1, 1, 0,0,0]),
50
+ T.new([2036, 12, 31, 23,59,59], 1, [2037, 1, 1, 0,0,0]),
51
+ T.new([2000, 2, 28, 23,59,59], 1, [2000, 2, 29, 0,0,0]),
52
+ T.new([1970, 2, 1, 0, 0, 0], ONEDAYSEC, [1970, 2, 2, 0,0,0]),
53
+ T.new([2000, 7, 1, 0, 0, 0], 32 * ONEDAYSEC, [2000, 8, 2, 0,0,0]),
54
+ T.new([2000, 1, 1, 0, 0, 0], 366 * ONEDAYSEC, [2001, 1, 1, 0,0,0]),
55
+ T.new([2001, 1, 1, 0, 0, 0], 365 * ONEDAYSEC, [2002, 1, 1, 0,0,0]),
56
+
57
+ T.new([2000, 1, 1, 0, 0, 0], 0, [2000, 1, 1, 0,0,0]),
58
+ T.new([2000, 2, 1, 0, 0, 0], 0, [2000, 2, 1, 0,0,0]),
59
+ T.new([2000, 3, 1, 0, 0, 0], 0, [2000, 3, 1, 0,0,0]),
60
+ T.new([2000, 4, 1, 0, 0, 0], 0, [2000, 4, 1, 0,0,0]),
61
+ T.new([2000, 5, 1, 0, 0, 0], 0, [2000, 5, 1, 0,0,0]),
62
+ T.new([2000, 6, 1, 0, 0, 0], 0, [2000, 6, 1, 0,0,0]),
63
+ T.new([2000, 7, 1, 0, 0, 0], 0, [2000, 7, 1, 0,0,0]),
64
+ T.new([2000, 8, 1, 0, 0, 0], 0, [2000, 8, 1, 0,0,0]),
65
+ T.new([2000, 9, 1, 0, 0, 0], 0, [2000, 9, 1, 0,0,0]),
66
+ T.new([2000, 10, 1, 0, 0, 0], 0, [2000, 10, 1, 0,0,0]),
67
+ T.new([2000, 11, 1, 0, 0, 0], 0, [2000, 11, 1, 0,0,0]),
68
+ T.new([2000, 12, 1, 0, 0, 0], 0, [2000, 12, 1, 0,0,0]),
69
+
70
+ T.new([2001, 1, 1, 0, 0, 0], 0, [2001, 1, 1, 0,0,0]),
71
+ T.new([2001, 2, 1, 0, 0, 0], 0, [2001, 2, 1, 0,0,0]),
72
+ T.new([2001, 3, 1, 0, 0, 0], 0, [2001, 3, 1, 0,0,0]),
73
+ T.new([2001, 4, 1, 0, 0, 0], 0, [2001, 4, 1, 0,0,0]),
74
+ T.new([2001, 5, 1, 0, 0, 0], 0, [2001, 5, 1, 0,0,0]),
75
+ T.new([2001, 6, 1, 0, 0, 0], 0, [2001, 6, 1, 0,0,0]),
76
+ T.new([2001, 7, 1, 0, 0, 0], 0, [2001, 7, 1, 0,0,0]),
77
+ T.new([2001, 8, 1, 0, 0, 0], 0, [2001, 8, 1, 0,0,0]),
78
+ T.new([2001, 9, 1, 0, 0, 0], 0, [2001, 9, 1, 0,0,0]),
79
+ T.new([2001, 10, 1, 0, 0, 0], 0, [2001, 10, 1, 0,0,0]),
80
+ T.new([2001, 11, 1, 0, 0, 0], 0, [2001, 11, 1, 0,0,0]),
81
+ T.new([2001, 12, 1, 0, 0, 0], 0, [2001, 12, 1, 0,0,0]),
82
+ ]
83
+
84
+ def setup
85
+ @orig_zone = ENV['TZ']
86
+ ENV['TZ'] = 'PST8PDT'
87
+ @utc = Time.utc(2001, 2, 3, 4, 5, 6)
88
+ @loc = Time.local(2001, 2, 3, 4, 5, 6)
89
+ @zone = @loc.zone
90
+ end
91
+
92
+ def teardown
93
+ ENV['TZ'] = @orig_zone
94
+ end
95
+
96
+ ##
97
+ # Check a particular date component -- m is the method (day, month, etc)
98
+ # and i is the index in the date specifications above.
99
+
100
+ def util_check_component(m, i)
101
+ @@dates.each do |x|
102
+ assert_equal(x.orig[i], Time.local(*x.orig).send(m))
103
+ assert_equal(x.result[i], Time.local(*x.result).send(m))
104
+ assert_equal(x.orig[i], Time.gm(*x.orig).send(m))
105
+ assert_equal(x.result[i], Time.gm(*x.result).send(m))
106
+ end
107
+ end
108
+
109
+ def util_class_now(method)
110
+ min = 0.1
111
+ max = min * 3.0 # some ruby impls will be SLOOOW
112
+ t1 = Time.send(method)
113
+ sleep min
114
+ t2 = Time.send(method)
115
+ delta = t2.to_f - t1.to_f
116
+ assert(delta >= min, "time difference must be at least #{min}")
117
+ assert(max >= delta, "time difference should not be more than #{max}")
118
+ end
119
+
120
+ def util_os_specific_epoch
121
+ if $os == MsWin32 || $os == JRuby then
122
+ "Thu Jan 01 00:00:00 1970"
123
+ else
124
+ "Thu Jan 1 00:00:00 1970"
125
+ end
126
+ end
127
+
128
+ ##
129
+ # If this test is failing, you've got big problems. Start with Time::at,
130
+ # Time::utc and Time::local before looking at bugs in any of your other
131
+ # code.
132
+
133
+ def test_00sanity # ZenTest SKIP
134
+ assert_equal(Time.at(981173106), Time.utc(2001, 2, 3, 4, 5, 6),
135
+ "If this test fails, don't bother debugging anything else.")
136
+ assert_equal(Time.at(981201906), Time.local(2001, 2, 3, 4, 5, 6),
137
+ "If this test fails, don't bother debugging anything else.")
138
+ end
139
+
140
+ def test_asctime
141
+ expected = util_os_specific_epoch
142
+ assert_equal(expected, Time.at(0).gmtime.asctime)
143
+ end
144
+
145
+ def test_class__load
146
+ # TODO: raise NotImplementedError, 'Need to write test_class__load'
147
+ end
148
+
149
+ def test_class_at
150
+ sec = @loc.to_i
151
+ assert_equal(0, Time.at(0).to_i)
152
+ assert_equal(@loc, Time.at(@loc))
153
+ assert_in_delta(Time.at(sec,1_000_000).to_f, Time.at(sec).to_f, 1.0)
154
+
155
+ # no arguments ==> error
156
+ assert_raise(ArgumentError) do
157
+ Time.at
158
+ end
159
+
160
+ # one integer argument ==> seconds
161
+ t = Time.at(1_234_567)
162
+ assert_equal(1_234_567, t.tv_sec)
163
+ assert_equal( 0, t.tv_usec)
164
+
165
+ # two integer arguments ==> seconds & microseconds
166
+ t = Time.at(1_234_567, 888_999)
167
+ assert_equal(1_234_567, t.tv_sec)
168
+ assert_equal( 888_999, t.tv_usec)
169
+
170
+ # float argument ==> second & rounded microseconds
171
+ t = Time.at(1_234_567.5)
172
+ assert_equal(1_234_567, t.tv_sec)
173
+ assert_equal( 500_000, t.tv_usec)
174
+
175
+ # float + integer arguments ==> rounded seconds & microseconds
176
+ t = Time.at(1_234_567.5, 300_000)
177
+ assert_equal(1_234_567, t.tv_sec)
178
+ assert_equal( 300_000, t.tv_usec)
179
+
180
+ # Time argument
181
+ t1 = Time.at(1_234_567, 888_999)
182
+ t2 = Time.at(t1)
183
+ assert_equal(1_234_567, t2.tv_sec)
184
+ assert_equal( 888_999, t2.tv_usec)
185
+ end
186
+
187
+ def test_class_at_utc
188
+ utc1 = @utc
189
+ utc2 = Time.at(@utc)
190
+ assert(utc1.utc?)
191
+ assert(utc2.utc?)
192
+ assert_equal(utc1.to_i, utc2.to_i)
193
+ end
194
+
195
+ def test_class_gm
196
+ assert_raises(ArgumentError) { Time.gm }
197
+ assert_not_equal(Time.gm(2000), Time.local(2000))
198
+ assert_equal(Time.gm(2000), Time.gm(2000,1,1,0,0,0))
199
+ assert_equal(Time.gm(2000,nil,nil,nil,nil,nil), Time.gm(2000,1,1,0,0,0))
200
+ assert_raises(ArgumentError) { Time.gm(2000,0) }
201
+ assert_raises(ArgumentError) { Time.gm(2000,13) }
202
+ assert_raises(ArgumentError) { Time.gm(2000,1,1,24) }
203
+ Time.gm(2000,1,1,23)
204
+ @@months.each do |month, num|
205
+ assert_equal(Time.gm(2000,month), Time.gm(2000,num,1,0,0,0))
206
+ assert_equal(Time.gm(1970,month), Time.gm(1970,num,1,0,0,0))
207
+ assert_equal(Time.gm(2037,month), Time.gm(2037,num,1,0,0,0))
208
+ end
209
+ t = Time.gm(2000,1,1)
210
+ a = t.to_a
211
+ assert_equal(Time.gm(*a),t)
212
+ end
213
+
214
+ def test_class_local
215
+ assert_raises(ArgumentError) { Time.local }
216
+ assert_not_equal(Time.gm(2000), Time.local(2000))
217
+ assert_equal(Time.local(2000), Time.local(2000,1,1,0,0,0))
218
+ assert_equal(Time.local(2000,nil,nil,nil,nil,nil), Time.local(2000,1,1,0,0,0))
219
+ assert_raises(ArgumentError) { Time.local(2000,0) }
220
+ assert_raises(ArgumentError) { Time.local(2000,13) }
221
+ assert_raises(ArgumentError) { Time.local(2000,1,1,24) }
222
+ Time.local(2000,1,1,23)
223
+ @@months.each do |month, num|
224
+ assert_equal(Time.local(2000,month), Time.local(2000,num,1,0,0,0))
225
+ assert_equal(Time.local(1971,month), Time.local(1971,num,1,0,0,0))
226
+ assert_equal(Time.local(2037,month), Time.local(2037,num,1,0,0,0))
227
+ end
228
+ t = Time.local(2000,1,1)
229
+ a = t.to_a
230
+ assert_equal(Time.local(*a),t)
231
+ end
232
+
233
+ def test_class_mktime
234
+ #
235
+ # Test insufficient arguments
236
+ #
237
+ assert_raises(ArgumentError) { Time.mktime }
238
+ assert_not_equal(Time.gm(2000), Time.mktime(2000))
239
+ assert_equal(Time.mktime(2000), Time.mktime(2000,1,1,0,0,0))
240
+ assert_equal(Time.mktime(2000,nil,nil,nil,nil,nil), Time.mktime(2000,1,1,0,0,0))
241
+ assert_raises(ArgumentError) { Time.mktime(2000,0) }
242
+ assert_raises(ArgumentError) { Time.mktime(2000,13) }
243
+ assert_raises(ArgumentError) { Time.mktime(2000,1,1,24) }
244
+ Time.mktime(2000,1,1,23)
245
+
246
+ #
247
+ # Make sure spelled-out month names work
248
+ #
249
+ @@months.each do |month, num|
250
+ assert_equal(Time.mktime(2000,month), Time.mktime(2000,num,1,0,0,0))
251
+ assert_equal(Time.mktime(1971,month), Time.mktime(1971,num,1,0,0,0))
252
+ assert_equal(Time.mktime(2037,month), Time.mktime(2037,num,1,0,0,0))
253
+ end
254
+ t = Time.mktime(2000,1,1)
255
+ a = t.to_a
256
+ assert_equal(Time.mktime(*a),t)
257
+ end
258
+
259
+ def test_class_now
260
+ util_class_now(:now) # Time.now
261
+ end
262
+
263
+ def test_class_times
264
+ assert_instance_of(Struct::Tms, Process.times)
265
+ end
266
+
267
+ def test_class_utc
268
+ test_class_gm # TODO: refactor to ensure they really are synonyms
269
+ end
270
+
271
+ def test_clone
272
+ for taint in [ false, true ]
273
+ for frozen in [ false, true ]
274
+ a = @loc.dup
275
+ a.taint if taint
276
+ a.freeze if frozen
277
+ b = a.clone
278
+
279
+ assert_equal(a, b)
280
+ assert_not_equal(a.__id__, b.__id__)
281
+ assert_equal(a.frozen?, b.frozen?)
282
+ assert_equal(a.tainted?, b.tainted?)
283
+ end
284
+ end
285
+ end
286
+
287
+ def test_ctime
288
+ expected = util_os_specific_epoch
289
+ assert_equal(expected, Time.at(0).gmtime.ctime)
290
+ end
291
+
292
+ def test_day
293
+ util_check_component(:day, 2)
294
+ end
295
+
296
+ def test_dst_eh
297
+ test_isdst # TODO: refactor to test that they really are the same
298
+ end
299
+
300
+ def test_dump
301
+ # TODO: raise NotImplementedError, 'Need to write test__dump'
302
+ end
303
+
304
+ def test_eql_eh
305
+ t1 = @loc
306
+ t2 = Time.at(t1)
307
+ t3 = t1 + 2e-6
308
+ t4 = t1 + 1
309
+ assert(t1.eql?(t1))
310
+ assert(t1.eql?(t2))
311
+ assert(!t1.eql?(t3))
312
+ assert(!t1.eql?(t4))
313
+ assert(t1.eql?(t1.getutc))
314
+ end
315
+
316
+ def test_getgm
317
+ # TODO: this only tests local -> gm
318
+ t1 = @loc
319
+ loc = Time.at(t1)
320
+ assert(!t1.gmt?)
321
+ t2 = t1.getgm
322
+ assert(!t1.gmt?)
323
+ assert(t2.gmt?)
324
+ assert_equal(t1, loc)
325
+ assert_equal(t1.asctime, loc.asctime)
326
+ assert_not_equal(t2.asctime, loc.asctime)
327
+ assert_not_equal(t1.asctime, t2.asctime)
328
+ assert_equal(t1, t2)
329
+ end
330
+
331
+ def test_getlocal
332
+ # TODO: this only tests gm -> local
333
+ t1 = @utc
334
+ utc = Time.at(t1)
335
+ assert(t1.gmt?)
336
+ t2 = t1.getlocal
337
+ assert(t1.gmt?)
338
+ assert(!t2.gmt?)
339
+ assert_equal(t1, utc)
340
+ assert_equal(t1.asctime, utc.asctime)
341
+ assert_not_equal(t2.asctime, utc.asctime)
342
+ assert_not_equal(t1.asctime, t2.asctime)
343
+ assert_equal(t1, t2)
344
+ end
345
+
346
+ def test_getutc
347
+ test_getgm # REFACTOR to test both calls
348
+ end
349
+
350
+ def test_gmt_eh
351
+ assert(!@loc.gmt?)
352
+ assert(@utc.gmt?)
353
+ assert(!Time.local(2000).gmt?)
354
+ assert(Time.gm(2000).gmt?)
355
+ end
356
+
357
+ def test_gmt_offset
358
+ test_utc_offset # REFACTOR to test both methods
359
+ end
360
+
361
+ def test_gmtime
362
+ # TODO: this only tests local -> gm
363
+ t = @loc
364
+ loc = Time.at(t)
365
+ assert(!t.gmt?)
366
+ t.gmtime
367
+ assert(t.gmt?)
368
+ assert_not_equal(t.asctime, loc.asctime)
369
+ end
370
+
371
+ def test_gmtoff
372
+ test_utc_offset # REFACTOR to test both methods
373
+ end
374
+
375
+ def test_hash
376
+ t1 = @utc
377
+ t2 = Time.at(t1)
378
+ t3 = @utc + 1
379
+ assert_equal(t1.hash, t2.hash)
380
+ assert_not_equal(t1.hash, t3.hash)
381
+ end
382
+
383
+ def test_hour
384
+ util_check_component(:hour, 3)
385
+ end
386
+
387
+ def test_initialize
388
+ util_class_now(:new) # Time.new
389
+ end
390
+
391
+ def test_inspect
392
+ assert_equal("Sat Feb 03 04:05:06 UTC 2001", @utc.inspect)
393
+ assert_equal("Sat Feb 03 04:05:06 #{@zone} 2001", @loc.inspect)
394
+ end
395
+
396
+ def test_isdst
397
+ # This code is problematic: how do I find out the exact
398
+ # date and time of the dst switch for all the possible
399
+ # timezones in which this code runs? For now, I'll just check
400
+ # midvalues, and add boundary checks for the US. I know this won't
401
+ # work in some parts of the US, even, so I'm looking for
402
+ # better ideas
403
+
404
+ # Are we in the US?
405
+ if ["EST", "EDT",
406
+ "CST", "CDT",
407
+ "MST", "MDT",
408
+ "PST", "PDT"].include? @zone
409
+
410
+ dtest = [
411
+ [false, 2000, 1, 1],
412
+ [true, 2000, 7, 1],
413
+ [true, 2000, 4, 2, 4],
414
+ [false, 2000, 10, 29, 4],
415
+ [false, 2000, 4,2,1,59], # Spring forward
416
+ [true, 2000, 4,2,3,0],
417
+ [true, 2000, 10,29,0,59], # Fall back
418
+ [false, 2000, 10,29,2,0]
419
+ ]
420
+
421
+ dtest.each do |x|
422
+ result = x.shift
423
+ assert_equal(result, Time.local(*x).isdst,
424
+ "\nExpected Time.local(#{x.join(',')}).isdst == #{result}")
425
+ end
426
+ else
427
+ skipping("Don't know how to do timezones");
428
+ end
429
+ end
430
+
431
+ def test_localtime
432
+ # TODO: this only tests gm -> local
433
+ t = @utc
434
+ utc = Time.at(t)
435
+ assert(t.gmt?)
436
+ t.localtime
437
+ assert(!t.gmt?)
438
+ assert_not_equal(t.asctime, utc.asctime)
439
+ end
440
+
441
+ def test_mday
442
+ util_check_component(:mday, 2)
443
+ end
444
+
445
+ def test_min
446
+ util_check_component(:min, 4)
447
+ end
448
+
449
+ def test_minus # '-'
450
+ @@dates.each do |x|
451
+ # Check subtracting an amount in seconds
452
+ assert_equal(Time.local(*x.result) - x.amt, Time.local(*x.orig))
453
+ assert_equal(Time.gm(*x.result) - x.amt, Time.gm(*x.orig))
454
+ # Check subtracting two times
455
+ assert_equal(Time.local(*x.result) - Time.local(*x.orig), x.amt)
456
+ assert_equal(Time.gm(*x.result) - Time.gm(*x.orig), x.amt)
457
+ end
458
+
459
+ # integer argument
460
+ t1 = Time.at(1_234_567, 500_000)
461
+ t2 = t1 - 567
462
+ assert_equal( 1_234_000, t2.tv_sec)
463
+ assert_equal( 500_000, t2.tv_usec)
464
+
465
+ # float argument with fractional part
466
+ t1 = Time.at(1_234_567, 500_000)
467
+ t2 = t1 - 566.75
468
+ assert_equal( 1_234_000, t2.tv_sec)
469
+ assert_equal( 750_000, t2.tv_usec)
470
+
471
+ # Time argument
472
+ t1 = Time.at(1_234_000, 750_000)
473
+ t2 = Time.at(1_234_567, 500_000)
474
+ diff = t2 - t1
475
+ assert_equal( 566.75, diff)
476
+ end
477
+
478
+ def test_mon
479
+ util_check_component(:mon, 1)
480
+ end
481
+
482
+ def test_month
483
+ util_check_component(:month, 1)
484
+ end
485
+
486
+ def test_plus # '+'
487
+ @@dates.each do |x|
488
+ assert_equal(Time.local(*x.orig) + x.amt, Time.local(*x.result))
489
+ assert_equal(Time.gm(*x.orig) + x.amt, Time.gm(*x.result))
490
+ end
491
+
492
+ # integer argument
493
+ t1 = Time.at(1_234_567, 500_000)
494
+ t2 = t1 + 433
495
+ assert_equal( 1_235_000, t2.tv_sec)
496
+ assert_equal( 500_000, t2.tv_usec)
497
+
498
+ # float argument with fractional part
499
+ t1 = Time.at(1_234_567, 500_000)
500
+ t2 = t1 + 433.25
501
+ assert_equal( 1_235_000, t2.tv_sec)
502
+ assert_equal( 750_000, t2.tv_usec)
503
+ end
504
+
505
+ def test_sec
506
+ util_check_component(:sec, 5)
507
+ end
508
+
509
+ def test_spaceship # '<=>'
510
+ @@dates.each do |x|
511
+ if (x.amt != 0)
512
+ assert_equal(1, Time.local(*x.result) <=> Time.local(*x.orig),
513
+ "#{x.result} should be > #{x.orig}")
514
+
515
+ assert_equal(-1, Time.local(*x.orig) <=> Time.local(*x.result))
516
+ assert_equal(0, Time.local(*x.orig) <=> Time.local(*x.orig))
517
+ assert_equal(0, Time.local(*x.result) <=> Time.local(*x.result))
518
+
519
+ assert_equal(1,Time.gm(*x.result) <=> Time.gm(*x.orig))
520
+ assert_equal(-1,Time.gm(*x.orig) <=> Time.gm(*x.result))
521
+ assert_equal(0,Time.gm(*x.orig) <=> Time.gm(*x.orig))
522
+ assert_equal(0,Time.gm(*x.result) <=> Time.gm(*x.result))
523
+ end
524
+ end
525
+
526
+ # microsecond diffs
527
+ assert_equal( 1, Time.at(10_000, 500_000) <=> Time.at(10_000, 499_999))
528
+ assert_equal( 0, Time.at(10_000, 500_000) <=> Time.at(10_000, 500_000))
529
+ assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_000, 500_001))
530
+
531
+ # second diff & microsecond diffs
532
+ assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 499_999))
533
+ assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 500_000))
534
+ assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 500_001))
535
+
536
+ # non-Time object gives nil
537
+ assert_nil(Time.at(10_000) <=> Object.new)
538
+ end
539
+
540
+ def test_strftime
541
+ # Sat Jan 1 14:58:42 2000
542
+ t = Time.local(2000,1,1,14,58,42)
543
+
544
+ stest = {
545
+ '%a' => 'Sat',
546
+ '%A' => 'Saturday',
547
+ '%b' => 'Jan',
548
+ '%B' => 'January',
549
+ #'%c', The preferred local date and time representation,
550
+ '%d' => '01',
551
+ '%H' => '14',
552
+ '%I' => '02',
553
+ '%j' => '001',
554
+ '%m' => '01',
555
+ '%M' => '58',
556
+ '%p' => 'PM',
557
+ '%S' => '42',
558
+ '%U' => '00',
559
+ '%W' => '00',
560
+ '%w' => '6',
561
+ #'%x', Preferred representation for the date alone, no time\\
562
+ #'%X', Preferred representation for the time alone, no date\\
563
+ '%y' => '00',
564
+ '%Y' => '2000',
565
+ #'%Z', Time zone name\\
566
+ '%%' => '%',
567
+ }
568
+
569
+ stest.each {|flag,val|
570
+ assert_equal("Got "+val,t.strftime("Got " + flag))
571
+ }
572
+
573
+ end
574
+
575
+ def test_succ
576
+ t1 = @loc
577
+ t2 = t1 + 1
578
+ t3 = t1.succ
579
+ assert_equal(t2, t3)
580
+ end
581
+
582
+ def test_to_a
583
+ t = @loc
584
+ a = t.to_a
585
+ assert_equal(t.sec, a[0])
586
+ assert_equal(t.min, a[1])
587
+ assert_equal(t.hour, a[2])
588
+ assert_equal(t.day, a[3])
589
+ assert_equal(t.month,a[4])
590
+ assert_equal(t.year, a[5])
591
+ assert_equal(t.wday, a[6])
592
+ assert_equal(t.yday, a[7])
593
+ assert_equal(t.isdst,a[8])
594
+ assert_equal(t.zone, a[9])
595
+ end
596
+
597
+ def test_to_f
598
+ t = Time.at(10000,1066)
599
+ assert_in_delta(10000.001066, t.to_f, 1e-7)
600
+ end
601
+
602
+ def test_to_i
603
+ t = Time.at(0)
604
+ assert_equal(0, t.to_i)
605
+ t = Time.at(10000)
606
+ assert_equal(10000, t.to_i)
607
+ end
608
+
609
+ def test_to_s
610
+ assert_equal("Sat Feb 03 04:05:06 UTC 2001", @utc.to_s)
611
+ assert_equal("Sat Feb 03 04:05:06 #{@zone} 2001", @loc.to_s)
612
+ end
613
+
614
+ def test_tv_sec
615
+ t = Time.at(0)
616
+ assert_equal(0,t.tv_sec)
617
+ t = Time.at(10000)
618
+ assert_equal(10000,t.tv_sec)
619
+ end
620
+
621
+ def util_usec(s, u, method)
622
+ t = Time.at(s,u)
623
+ assert_equal(u,t.send(method))
624
+ end
625
+
626
+ def test_tv_usec
627
+ util_usec(10000, 1066, :tv_usec)
628
+ util_usec(10000, 0, :tv_usec)
629
+ end
630
+
631
+ def test_usec
632
+ util_usec(10000, 1066, :usec)
633
+ util_usec(10000, 0, :usec)
634
+ end
635
+
636
+ def test_utc
637
+ test_gmtime # REFACTOR to test both methods
638
+ end
639
+
640
+ def test_utc_eh
641
+ test_gmt_eh # REFACTOR to test both methods
642
+ end
643
+
644
+ def test_utc_offset
645
+ # TODO: figure out the year, month, & day edgecase setups
646
+ off = @utc - @loc
647
+ assert_equal(0, @utc.utc_offset)
648
+ assert_equal(off, @loc.utc_offset)
649
+ end
650
+
651
+ def test_wday
652
+ t = Time.local(2001, 4, 1)
653
+
654
+ 7.times { |i|
655
+ assert_equal(i,t.wday)
656
+ t += ONEDAYSEC
657
+ }
658
+ end
659
+
660
+ def test_yday
661
+ # non-leap 1/1, 2/28, 3/1, 12/31
662
+ # leap 1/1, 2/28, 2/29, 3/1, 12/31
663
+ # leap century (2000)
664
+ # want to do a non-leap century, but they are out of range.
665
+ # any others?
666
+
667
+ # non-leap year:
668
+ assert_equal( 1, Time.local(1999, 1, 1).yday)
669
+ assert_equal( 59, Time.local(1999, 2, 28).yday)
670
+ assert_equal( 60, Time.local(1999, 3, 1).yday)
671
+ assert_equal(365, Time.local(1999, 12, 31).yday)
672
+
673
+ # leap century:
674
+ assert_equal( 1, Time.local(2000, 1, 1).yday)
675
+ assert_equal( 59, Time.local(2000, 2, 28).yday)
676
+ assert_equal( 60, Time.local(2000, 2, 29).yday)
677
+ assert_equal( 61, Time.local(2000, 3, 1).yday)
678
+ assert_equal(366, Time.local(2000, 12, 31).yday)
679
+
680
+ # leap year:
681
+ assert_equal( 1, Time.local(2004, 1, 1).yday)
682
+ assert_equal( 59, Time.local(2004, 2, 28).yday)
683
+ assert_equal( 60, Time.local(2004, 2, 29).yday)
684
+ assert_equal( 61, Time.local(2004, 3, 1).yday)
685
+ assert_equal(366, Time.local(2004, 12, 31).yday)
686
+ end
687
+
688
+ def test_year
689
+ util_check_component(:year, 0)
690
+ end
691
+
692
+ def test_zone
693
+ gmt = "UTC"
694
+ t = @utc
695
+ assert_equal(gmt, t.zone)
696
+ t = @loc
697
+ assert_not_equal(gmt, t.zone)
698
+ end
699
+
700
+ end