summaryrefslogtreecommitdiffstats
path: root/spec/age_spec.rb
blob: 7c447e07b0d5217859ff06cf847d389a48af4c2e (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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

require 'ayk/age'
#
def age_spec birth, dy, dm, dd
    at = (birth>>(12*dy+dm))+dd
    y,m,d,hh,mm,ss = birth.age(at)
    y.should eql dy
    m.should eql dm
    d.should eql dd
    hh.should eql 0
    mm.should eql 0
    ss.should eql 0
end
def full_check birth, at, y, m, d, hh, mm, ss
    r = birth.age at
    r[0].should eql y
    r[1].should eql m
    r[2].should eql d
    r[3].should eql hh
    r[4].should eql mm
    r[5].should eql ss
    x = ((birth>>(12*y+m))+d+(hh/24.0)+(mm/1440.0)+(ss/86400.0))
    x.year.should eql at.year
    x.month.should eql at.month
    x.day.should eql at.day
    x.hour.should eql at.hour
#    x.minute.should eql at.minute
#    x.second.should eql at.second
end
#
describe DateTime do
    #
    before(:all) do
        @b = DateTime.civil 2009,7,27,4,40,0 # Róisín Mannion-Zurcher [IRL|CH] )
    end
    it "should pass basic futur checks" do
        age_spec(@b,0,0,0)
        age_spec(@b,0,0,1)
        age_spec(@b,0,1,0)
        age_spec(@b,0,1,1)
        age_spec(@b,1,0,0)
        age_spec(@b,1,0,1)
        age_spec(@b,1,1,0)
        age_spec(@b,1,1,1)
    end
    it "should pass 10000 days computation" do
        (0..10000).each do |n|
            at = @b+n
            r = @b.age at
            x = (@b>>(12*r[0]+r[1]))+r[2]
            x.year.should eql at.year
            x.month.should eql at.month
            x.day.should eql at.day
        end
    end
    it "should pass time computation" do
        full_check @b, DateTime.civil( 2010, 9, 25, 3, 40,  0), 1,  1, 28,  23,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 25, 4, 39,  0), 1,  1, 28,  23, 59,  0
        full_check @b, DateTime.civil( 2010, 9, 25, 4, 39, 59), 1,  1, 28,  23, 59, 59
        full_check @b, DateTime.civil( 2010, 9, 25, 4, 40,  0), 1,  1, 29,   0,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 26, 3, 40,  0), 1,  1, 29,  23,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 26, 4, 39,  0), 1,  1, 29,  23, 59,  0
        full_check @b, DateTime.civil( 2010, 9, 26, 4, 39, 59), 1,  1, 29,  23, 59, 59
        full_check @b, DateTime.civil( 2010, 9, 26, 4, 40,  0), 1,  1, 30,   0,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 27, 3, 40,  0), 1,  1, 30,  23,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 27, 4, 39,  0), 1,  1, 30,  23, 59,  0
        full_check @b, DateTime.civil( 2010, 9, 27, 4, 39, 59), 1,  1, 30,  23, 59, 59
        full_check @b, DateTime.civil( 2010, 9, 27, 4, 40,  0), 1,  2,  0,   0,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 28, 3, 40,  0), 1,  2,  0,  23,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 28, 4, 39,  0), 1,  2,  0,  23, 59,  0
        full_check @b, DateTime.civil( 2010, 9, 28, 4, 39, 59), 1,  2,  0,  23, 59, 59
        full_check @b, DateTime.civil( 2010, 9, 28, 4, 40,  0), 1,  2,  1,   0,  0,  0
        full_check @b, DateTime.civil( 2010, 9, 29, 3, 40,  0), 1,  2,  1,  23,  0,  0
        # change years
        full_check @b, DateTime.civil( 2010, 7, 27, 4, 40,  0), 1,  0,  0,   0,  0,  0
        full_check @b, DateTime.civil( 2010, 7, 27, 3, 40,  0), 0, 11, 29,  23,  0,  0
        full_check @b, DateTime.civil( 2010, 7, 27, 4, 39,  0), 0, 11, 29,  23, 59,  0
        full_check @b, DateTime.civil( 2010, 7, 27, 4, 39, 59), 0, 11, 29,  23, 59, 59
    end
end