summaryrefslogtreecommitdiff
path: root/test/date/test_date_base.rb
blob: a8dc2ee6b2a615c510e7ed05ac11cede94c276e6 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
require 'test/unit'
require 'date'

begin
  require 'calendar'
  include  Calendar
rescue LoadError
end

class TestDateBase < Test::Unit::TestCase

  def setup
    if defined?(Calendar)
      @from ||= julian_day_number_from_absolute(absolute_from_julian(1, 1, 1601))
      @to ||= julian_day_number_from_absolute(absolute_from_julian(12, 31, 2400))
      @from4t ||= julian_day_number_from_absolute(absolute_from_julian(1, 1, 1970))
      @to4t ||= julian_day_number_from_absolute(absolute_from_julian(12, 31, 2037))
    end
  end

  def test__inf
    assert_equal(0, Date::Infinity.new(-1) <=> Date::Infinity.new(-1))
    assert_equal(-1, Date::Infinity.new(-1) <=> Date::Infinity.new(+1))
    assert_equal(-1, Date::Infinity.new(-1) <=> 0)

    assert_equal(1, Date::Infinity.new(+1) <=> Date::Infinity.new(-1))
    assert_equal(0, Date::Infinity.new(+1) <=> Date::Infinity.new(+1))
    assert_equal(1, Date::Infinity.new(+1) <=> 0)

    assert_equal(1, 0 <=> Date::Infinity.new(-1))
    assert_equal(-1, 0 <=> Date::Infinity.new(+1))
    assert_equal(0, 0 <=> 0)

    assert_equal(0, Date::ITALY <=> Date::ITALY)
    assert_equal(-1, Date::ITALY <=> Date::ENGLAND)
    assert_equal(-1, Date::ITALY <=> Date::JULIAN)
    assert_equal(1, Date::ITALY <=> Date::GREGORIAN)

    assert_equal(1, Date::ENGLAND <=> Date::ITALY)
    assert_equal(0, Date::ENGLAND <=> Date::ENGLAND)
    assert_equal(-1, Date::ENGLAND <=> Date::JULIAN)
    assert_equal(1, Date::ENGLAND <=> Date::GREGORIAN)

    assert_equal(1, Date::JULIAN <=> Date::ITALY)
    assert_equal(1, Date::JULIAN <=> Date::ENGLAND)
    assert_equal(0, Date::JULIAN <=> Date::JULIAN)
    assert_equal(1, Date::JULIAN <=> Date::GREGORIAN)

    assert_equal(-1, Date::GREGORIAN <=> Date::ITALY)
    assert_equal(-1, Date::GREGORIAN <=> Date::ENGLAND)
    assert_equal(-1, Date::GREGORIAN <=> Date::JULIAN)
    assert_equal(0, Date::GREGORIAN <=> Date::GREGORIAN)
  end

  def test_ordinal__julian
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      m, d, y = julian_from_absolute(absolute_from_julian_day_number(j))
      j0 = julian_day_number_from_absolute(absolute_from_julian(12, 31, y - 1))
      j2 = julian_day_number_from_absolute(absolute_from_julian(m, d, y))
      assert_equal(j, j2)
      oy, od = Date.__send__(:jd_to_ordinal, j, Date::JULIAN)
      assert_equal(y, oy)
      assert_equal(j2 - j0, od)
      oj = Date.__send__(:ordinal_to_jd, oy, od, Date::JULIAN)
      assert_equal(j, oj)
    end
  end

  def test_ordinal__gregorian
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      m, d, y = gregorian_from_absolute(absolute_from_julian_day_number(j))
      j0 =
	julian_day_number_from_absolute(absolute_from_gregorian(12, 31, y - 1))
      j2 = julian_day_number_from_absolute(absolute_from_gregorian(m, d, y))
      assert_equal(j, j2)
      oy, od = Date.__send__(:jd_to_ordinal, j, Date::GREGORIAN)
      assert_equal(y, oy)
      assert_equal(j2 - j0, od)
      oj = Date.__send__(:ordinal_to_jd, oy, od, Date::GREGORIAN)
      assert_equal(j, oj)
    end
  end

  def test_civil__julian
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      m, d, y = julian_from_absolute(absolute_from_julian_day_number(j))
      j2 = julian_day_number_from_absolute(absolute_from_julian(m, d, y))
      assert_equal(j2, j)
      cy, cm, cd = Date.__send__(:jd_to_civil, j, Date::JULIAN)
      assert_equal(y, cy)
      assert_equal(m, cm)
      assert_equal(d, cd)
      cj = Date.__send__(:civil_to_jd, cy, cm, cd, Date::JULIAN)
      assert_equal(j, cj)
    end
  end

  def test_civil__gregorian
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      m, d, y = gregorian_from_absolute(absolute_from_julian_day_number(j))
      j2 = julian_day_number_from_absolute(absolute_from_gregorian(m, d, y))
      assert_equal(j2, j)
      cy, cm, cd = Date.__send__(:jd_to_civil, j, Date::GREGORIAN)
      assert_equal(y, cy)
      assert_equal(m, cm)
      assert_equal(d, cd)
      cj = Date.__send__(:civil_to_jd, cy, cm, cd, Date::GREGORIAN)
      assert_equal(j, cj)
    end
  end

  def test_commercial__gregorian
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      w, d, y = iso_from_absolute(absolute_from_julian_day_number(j))
      j2 = julian_day_number_from_absolute(absolute_from_iso(w, d, y))
      assert_equal(j2, j)
      cy, cw, cd = Date.__send__(:jd_to_commercial, j, Date::GREGORIAN)
      assert_equal(y, cy)
      assert_equal(w, cw)
      assert_equal(d, cd)
      cj = Date.__send__(:commercial_to_jd, cy, cw, cd, Date::GREGORIAN)
      assert_equal(j, cj)
    end
  end

  def test_weeknum
    unless defined?(Calendar)
      return
    end
    for j in @from..@to
      for k in 0..1
	wy, ww, wd = Date.__send__(:jd_to_weeknum, j, k, Date::GREGORIAN)
	wj = Date.__send__(:weeknum_to_jd, wy, ww, wd, k, Date::GREGORIAN)
	assert_equal(j, wj)
      end
    end
  end

  def test_weeknum__2
    unless defined?(Calendar)
      return
    end
    for j in @from4t..@to4t
      d = Date.jd(j)
      t = Time.mktime(d.year, d.mon, d.mday)
      [
       '%Y %U %w',
       '%Y %U %u',
       '%Y %W %w',
       '%Y %W %u'
      ].each do |fmt|
	s = t.strftime(fmt)
	d2 = Date.strptime(s, fmt)
	assert_equal(j, d2.jd)
      end
    end
  end

  def test_nth_kday
    unless defined?(Calendar)
      return
    end
    for y in 1601..2401
      for m in 1..12
	for n in -5..5
	  next if n == 0
	  for k in 0..6
	    j = julian_day_number_from_absolute(Nth_Kday(n, k, m, y))
	    j2 =  Date.__send__(:nth_kday_to_jd, y, m, n, k, Date::GREGORIAN)
	    assert_equal(j, j2)

	    d1 =  Date.__send__(:jd_to_nth_kday, j2, Date::GREGORIAN)
	    j3 =  Date.__send__(:nth_kday_to_jd, *d1)
	    assert_equal(j, j3)
	  end
	end
      end
    end
  end

  def test_mjd
    jd = Date.__send__(:mjd_to_jd, 51321)
    mjd = Date.__send__(:jd_to_mjd, jd)
    assert_equal(51321, mjd)
  end

  def test_ld
    jd = Date.__send__(:ld_to_jd, 152162)
    ld = Date.__send__(:jd_to_ld, jd)
    assert_equal(152162, ld)
  end

  def test_wday
    assert_equal(4, Date.__send__(:jd_to_wday, 3))
    assert_equal(3, Date.__send__(:jd_to_wday, 2))
    assert_equal(2, Date.__send__(:jd_to_wday, 1))
    assert_equal(1, Date.__send__(:jd_to_wday, 0))
    assert_equal(0, Date.__send__(:jd_to_wday, -1))
    assert_equal(6, Date.__send__(:jd_to_wday, -2))
    assert_equal(5, Date.__send__(:jd_to_wday, -3))
  end

  def test_leap?
    assert_equal(true, Date.julian_leap?(1900))
    assert_equal(false, Date.julian_leap?(1999))
    assert_equal(true, Date.julian_leap?(2000))

    assert_equal(false, Date.gregorian_leap?(1900))
    assert_equal(false, Date.gregorian_leap?(1999))
    assert_equal(true, Date.gregorian_leap?(2000))

    assert_equal(Date.leap?(1990), Date.gregorian_leap?(1900))
    assert_equal(Date.leap?(1999), Date.gregorian_leap?(1999))
    assert_equal(Date.leap?(2000), Date.gregorian_leap?(2000))
  end

  def test_valid_jd
    valid_jd_p =  :_valid_jd?
    assert_equal(-1, Date.__send__(valid_jd_p, -1))
    assert_equal(0, Date.__send__(valid_jd_p, 0))
    assert_equal(1, Date.__send__(valid_jd_p, 1))
    assert_equal(2452348, Date.__send__(valid_jd_p, 2452348))
  end

  def test_valid_ordinal
    valid_ordinal_p = :_valid_ordinal?
    assert_nil(Date.__send__(valid_ordinal_p, 1999,366))
    assert_equal(2451910, Date.__send__(valid_ordinal_p, 2000,366))
    assert_nil(Date.__send__(valid_ordinal_p, 1999,-366))
    assert_equal(2451545, Date.__send__(valid_ordinal_p, 2000,-366))
    assert_equal(2452275, Date.__send__(valid_ordinal_p, 2001,365))
    assert_nil(Date.__send__(valid_ordinal_p, 2001,366))
    assert_equal(Date.__send__(valid_ordinal_p, 2001,1),
		 Date.__send__(valid_ordinal_p, 2001,-365))
    assert_nil(Date.__send__(valid_ordinal_p, 2001,-366))
    assert_equal(2452348, Date.__send__(valid_ordinal_p, 2002,73))
  end

  def test_valid_ordinal__edge
    valid_ordinal_p = :_valid_ordinal?
    (1601..2400).each do |y|
      d = if Date.leap?(y) then 366 else 365 end
      assert_not_nil(Date.__send__(valid_ordinal_p, y,d))
      assert_nil(Date.__send__(valid_ordinal_p, y,d + 1))
      assert_not_nil(Date.__send__(valid_ordinal_p, y,-d))
      assert_nil(Date.__send__(valid_ordinal_p, y,-(d + 1)))
    end
  end

  #        October 1582
  #   S   M  Tu   W  Th   F   S
  #     274 275 276 277 288 289
  # 290 291 292 293 294 295 296
  # 297 298 299 300 301 302 303
  # 304

  #        October 1582
  #   S   M  Tu   W  Th   F   S
  #     -92 -91 -90 -89 -78 -77
  # -76 -75 -74 -73 -72 -71 -70
  # -69 -68 -67 -66 -65 -64 -63
  # -62

  def test_valid_ordinal__italy
    valid_ordinal_p =  :_valid_ordinal?
    (1..355).each do |d|
      assert_not_nil(Date.__send__(valid_ordinal_p, 1582,d,Date::ITALY))
    end
    (356..365).each do |d|
      assert_nil(Date.__send__(valid_ordinal_p, 1582,d,Date::ITALY))
    end
  end

  #       September 1752
  #   S   M  Tu   W  Th   F   S
  #         245 246 258 259 260
  # 261 262 263 264 265 266 267
  # 268 269 270 271 272 273 274

  def test_valid_ordinal__england
    valid_ordinal_p =  :_valid_ordinal?
    (1..355).each do |d|
      assert_not_nil(Date.__send__(valid_ordinal_p, 1752,d,Date::ENGLAND))
    end
    (356..366).each do |d|
      assert_nil(Date.__send__(valid_ordinal_p, 1752,d,Date::ENGLAND))
    end
  end

  def test_valid_civil
    valid_civil_p = :_valid_civil?
    assert_nil(Date.__send__(valid_civil_p, 1999,2,29))
    assert_equal(2451604, Date.__send__(valid_civil_p, 2000,2,29))
    assert_nil(Date.__send__(valid_civil_p, 1999,2,-29))
    assert_equal(2451576, Date.__send__(valid_civil_p, 2000,2,-29))
    assert_equal(2451941, Date.__send__(valid_civil_p, 2001,1,31))
    assert_nil(Date.__send__(valid_civil_p, 2001,1,32))
    assert_equal(Date.__send__(valid_civil_p, 2001,1,1),
		 Date.__send__(valid_civil_p, 2001,1,-31))
    assert_nil(Date.__send__(valid_civil_p, 2001,1,-32))
    assert_equal(2452348, Date.__send__(valid_civil_p, 2002,3,14))
  end

  def test_valid_civil__edge
    valid_civil_p = :_valid_civil?
    (1601..2400).each do |y|
      d = if Date.leap?(y) then 29 else 28 end
      assert_not_nil(Date.__send__(valid_civil_p, y,2,d))
      assert_nil(Date.__send__(valid_civil_p, y,2,d + 1))
      assert_not_nil(Date.__send__(valid_civil_p, y,2,-d))
      assert_nil(Date.__send__(valid_civil_p, y,2,-(d + 1)))
    end
  end

  #     October 1582
  #  S  M Tu  W Th  F  S
  #     1  2  3  4 15 16
  # 17 18 19 20 21 22 23
  # 24 25 26 27 28 29 30
  # 31

  def test_valid_civil__italy
    valid_civil_p = :_valid_civil?
    (1..4).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
    (5..14).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
    (15..31).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
    (32..100).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
    (-31..-22).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
    (-21..-1).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY))
    end
  end

  #    September 1752
  #  S  M Tu  W Th  F  S
  #        1  2 14 15 16
  # 17 18 19 20 21 22 23
  # 24 25 26 27 28 29 30

  def test_valid_civil__england
    valid_civil_p = :_valid_civil?
    (1..2).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
    (3..13).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
    (14..30).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
    (31..100).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
    (-31..-20).each do |d|
      assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
    (-19..-1).each do |d|
      assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND))
    end
  end

  def test_valid_commercial
    valid_commercial_p = :_valid_commercial?
    assert_nil(Date.__send__(valid_commercial_p, 1999,53,1))
    assert_equal(2453367, Date.__send__(valid_commercial_p, 2004,53,1))
    assert_nil(Date.__send__(valid_commercial_p, 1999,-53,-1))
    assert_equal(2453009, Date.__send__(valid_commercial_p, 2004,-53,-1))
    assert_equal(2452348, Date.__send__(valid_commercial_p, 2002,11,4))
  end

  def test_valid_weeknum
    valid_weeknum_p = :_valid_weeknum?
    assert_nil(Date.__send__(valid_weeknum_p, 1999,53,0, 0))
    assert_equal(2454101, Date.__send__(valid_weeknum_p, 2006,53,0, 0))
    assert_nil(Date.__send__(valid_weeknum_p, 1999,-53,-1, 0))
    assert_equal(2453743, Date.__send__(valid_weeknum_p, 2006,-53,-1, 0))
    assert_equal(2452355, Date.__send__(valid_weeknum_p, 2002,11,4, 0))
    assert_nil(Date.__send__(valid_weeknum_p, 1999,53,0, 1))
    assert_equal(2454101, Date.__send__(valid_weeknum_p, 2006,52,6, 1))
    assert_nil(Date.__send__(valid_weeknum_p, 1999,-53,-1, 1))
    assert_equal(2453743, Date.__send__(valid_weeknum_p, 2006,-52,-2, 1))
    assert_equal(2452355, Date.__send__(valid_weeknum_p, 2002,11,3, 1))
  end

  def test_valid_nth_kday
    valid_nth_kday_p = :_valid_nth_kday?
    assert_nil(Date.__send__(valid_nth_kday_p, 1992,2, 5,0))
    assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, 5,6))
    assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, 5,-1))
    assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, -1,6))
    assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, -1,-1))
  end

  def test_valid_time
    valid_time_p = :_valid_time?
    assert_equal(Rational(0), DateTime.__send__(valid_time_p, 0,0,0))
    assert_nil(DateTime.__send__(valid_time_p, 25,59,59))
    assert_nil(DateTime.__send__(valid_time_p, 23,60,59))
    assert_nil(DateTime.__send__(valid_time_p, 23,59,60))
    assert_equal(Rational(86399, 86400),
		 DateTime.__send__(valid_time_p, 23,59,59))
    assert_equal(Rational(86399, 86400),
		 DateTime.__send__(valid_time_p, -1,-1,-1))
    assert_equal(Rational(1), DateTime.__send__(valid_time_p, 24,0,0))
    assert_nil(DateTime.__send__(valid_time_p, 24,0,1))
    assert_nil(DateTime.__send__(valid_time_p, 24,1,0))
    assert_nil(DateTime.__send__(valid_time_p, 24,1,1))
  end

end