~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/ruby/test_time.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'test/unit'
 
2
require 'rational'
2
3
 
3
4
class TestTime < Test::Unit::TestCase
4
5
  def test_time_add()
63
64
    end
64
65
  end
65
66
 
66
 
  def test_huge_difference # [ruby-dev:22619]
 
67
  def test_huge_difference
67
68
    if negative_time_t?
68
 
      assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff)
 
69
      assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
69
70
      assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
70
 
      assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff)
 
71
      assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
71
72
      assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
72
73
    end
73
74
  end
 
75
 
 
76
  def test_big_minus
 
77
    begin
 
78
      bigtime0 = Time.at(2**60)
 
79
      bigtime1 = Time.at(2**60+1)
 
80
    rescue RangeError
 
81
      return
 
82
    end
 
83
    assert_equal(1.0, bigtime1 - bigtime0)
 
84
  end
 
85
 
 
86
  def test_at
 
87
    assert_equal(100000, Time.at(0.1).usec)
 
88
    assert_equal(10000, Time.at(0.01).usec)
 
89
    assert_equal(1000, Time.at(0.001).usec)
 
90
    assert_equal(100, Time.at(0.0001).usec)
 
91
    assert_equal(10, Time.at(0.00001).usec)
 
92
    assert_equal(1, Time.at(0.000001).usec)
 
93
    assert_equal(100000000, Time.at(0.1).nsec)
 
94
    assert_equal(10000000, Time.at(0.01).nsec)
 
95
    assert_equal(1000000, Time.at(0.001).nsec)
 
96
    assert_equal(100000, Time.at(0.0001).nsec)
 
97
    assert_equal(10000, Time.at(0.00001).nsec)
 
98
    assert_equal(1000, Time.at(0.000001).nsec)
 
99
    assert_equal(100, Time.at(0.0000001).nsec)
 
100
    assert_equal(10, Time.at(0.00000001).nsec)
 
101
    assert_equal(1, Time.at(0.000000001).nsec)
 
102
  end
 
103
 
 
104
  def test_at2
 
105
    assert_equal(100, Time.at(0, 0.1).nsec)
 
106
    assert_equal(10, Time.at(0, 0.01).nsec)
 
107
    assert_equal(1, Time.at(0, 0.001).nsec)
 
108
  end
 
109
 
 
110
  def test_at_rational
 
111
    assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
 
112
    assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
 
113
  end
 
114
 
 
115
  def test_utc_subsecond
 
116
    assert_equal(100000, Time.utc(2007,1,1,0,0,1.1).usec)
 
117
    assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
 
118
  end
 
119
 
 
120
  def test_eq_nsec
 
121
    assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
 
122
    assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
 
123
  end
 
124
 
 
125
  def assert_marshal_roundtrip(t)
 
126
    iv_names = t.instance_variables
 
127
    iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
 
128
    m = Marshal.dump(t)
 
129
    t2 = Marshal.load(m)
 
130
    iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
 
131
    assert_equal(t, t2)
 
132
    assert_equal(iv_vals1, iv_vals2)
 
133
    t2
 
134
  end
 
135
 
 
136
  def test_marshal_nsec
 
137
    assert_marshal_roundtrip(Time.at(0, 0.123))
 
138
    assert_marshal_roundtrip(Time.at(0, 0.120))
 
139
  end
 
140
 
 
141
  def test_marshal_ivar
 
142
    t = Time.at(123456789, 987654.321)
 
143
    t.instance_eval { @var = 135 }
 
144
    assert_marshal_roundtrip(t)
 
145
    assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
 
146
  end
 
147
 
74
148
end