File: test_new.rb

package info (click to toggle)
ruby3.4 3.4.5-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 154,784 kB
  • sloc: ruby: 1,259,653; ansic: 829,955; yacc: 28,233; pascal: 7,359; sh: 3,864; python: 1,799; cpp: 1,158; asm: 808; makefile: 801; javascript: 414; lisp: 109; perl: 62; awk: 36; sed: 4; xml: 4
file content (44 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download | duplicates (10)
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
# frozen_string_literal: false
require 'test/unit'
require "-test-/time"

class  Bug::Time::Test_New < Test::Unit::TestCase
  def test_nano_new
    assert_equal(Time.at(1447087832, 476451.125), Bug::Time.nano_new(1447087832, 476451125))
    assert_not_equal(Time.at(1447087832, 476451.325), Bug::Time.nano_new(1447087832, 476451125))
    assert_equal(false, Bug::Time.nano_new(1447087832, 476451125).utc?)
  end

  def assert_time_equal(a, b, msg=nil)
    assert_equal(a, b, msg)
    assert_equal(a.gmtoff, b.gmtoff, msg)
    assert_equal(a.utc?, b.utc?, msg)
  end

  def test_timespec_new
    assert_time_equal(Time.at(1447087832, 476451.125).localtime(32400),
                 Bug::Time.timespec_new(1447087832, 476451125, 32400))
    assert_not_equal(Time.at(1447087832, 476451.128).localtime(32400),
                 Bug::Time.timespec_new(1447087832, 476451125, 32400))
    assert_equal(false, Bug::Time.timespec_new(1447087832, 476451125, 0).utc?)
    assert_equal(true,  Bug::Time.timespec_new(1447087832, 476451125, 0x7ffffffe).utc?)
    assert_equal(false, Bug::Time.timespec_new(1447087832, 476451125, 0x7fffffff).utc?)
    # Cannot compare Time.now.gmtoff with
    # Bug::Time.timespec_new(1447087832, 476451125, 0x7fffffff).gmtoff, because
    # it depends on whether the current time is in summer time (daylight-saving time) or not.
    t = Time.now
    assert_equal(t.gmtoff, Bug::Time.timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff)
    assert_time_equal(Time.at(1447087832, 476451.125).localtime(86399),
                 Bug::Time.timespec_new(1447087832, 476451125, 86399))
    assert_time_equal(Time.at(1447087832, 476451.125).localtime(-86399),
                 Bug::Time.timespec_new(1447087832, 476451125, -86399))
    assert_raise(ArgumentError){Bug::Time.timespec_new(1447087832, 476451125, 86400)}
    assert_raise(ArgumentError){Bug::Time.timespec_new(1447087832, 476451125,-86400)}
  end

  def test_timespec_now
    t0 = Time.now.to_r
    t = Bug::Time.timespec_now
    assert_in_delta 3, t0, t
  end
end