| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require "-test-/time" |
| 4 |
|
| 5 |
class Bug::Time::Test_New < Test::Unit::TestCase |
| 6 |
def test_nano_new |
| 7 |
assert_equal(Time.at(1447087832, 476451.125), Bug::Time.nano_new(1447087832, 476451125)) |
| 8 |
assert_not_equal(Time.at(1447087832, 476451.325), Bug::Time.nano_new(1447087832, 476451125)) |
| 9 |
assert_equal(false, Bug::Time.nano_new(1447087832, 476451125).utc?) |
| 10 |
end |
| 11 |
|
| 12 |
def assert_time_equal(a, b, msg=nil) |
| 13 |
assert_equal(a, b, msg) |
| 14 |
assert_equal(a.gmtoff, b.gmtoff, msg) |
| 15 |
assert_equal(a.utc?, b.utc?, msg) |
| 16 |
end |
| 17 |
|
| 18 |
def test_timespec_new |
| 19 |
assert_time_equal(Time.at(1447087832, 476451.125).localtime(32400), |
| 20 |
Bug::Time.timespec_new(1447087832, 476451125, 32400)) |
| 21 |
assert_not_equal(Time.at(1447087832, 476451.128).localtime(32400), |
| 22 |
Bug::Time.timespec_new(1447087832, 476451125, 32400)) |
| 23 |
assert_equal(false, Bug::Time.timespec_new(1447087832, 476451125, 0).utc?) |
| 24 |
assert_equal(true, Bug::Time.timespec_new(1447087832, 476451125, 0x7ffffffe).utc?) |
| 25 |
assert_equal(false, Bug::Time.timespec_new(1447087832, 476451125, 0x7fffffff).utc?) |
| 26 |
# Cannot compare Time.now.gmtoff with |
| 27 |
# Bug::Time.timespec_new(1447087832, 476451125, 0x7fffffff).gmtoff, because |
| 28 |
# it depends on whether the current time is in summer time (daylight-saving time) or not. |
| 29 |
t = Time.now |
| 30 |
assert_equal(t.gmtoff, Bug::Time.timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff) |
| 31 |
assert_time_equal(Time.at(1447087832, 476451.125).localtime(86399), |
| 32 |
Bug::Time.timespec_new(1447087832, 476451125, 86399)) |
| 33 |
assert_time_equal(Time.at(1447087832, 476451.125).localtime(-86399), |
| 34 |
Bug::Time.timespec_new(1447087832, 476451125, -86399)) |
| 35 |
assert_raise(ArgumentError){Bug::Time.timespec_new(1447087832, 476451125, 86400)} |
| 36 |
assert_raise(ArgumentError){Bug::Time.timespec_new(1447087832, 476451125,-86400)} |
| 37 |
end |
| 38 |
|
| 39 |
def test_timespec_now |
| 40 |
t0 = Time.now.to_r |
| 41 |
t = Bug::Time.timespec_now |
| 42 |
assert_in_delta 3, t0, t |
| 43 |
end |
| 44 |
end |