Skip to content

Commit

Permalink
Fix test cases on platforms that only support 32-bit Times.
Browse files Browse the repository at this point in the history
  • Loading branch information
philr committed Sep 24, 2013
1 parent caf47b8 commit 5118250
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions test/tc_zoneinfo_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ def test_load_timezone_info_invalid_file_2
zone = File.join(dir, 'Zone')

File.open(File.join(@data_source.zoneinfo_dir, 'EST')) do |src|
# Change format to 3 (which is not a valid format).
# Change header to TZif1 (which is not a valid header).
File.open(zone, 'wb') do |dest|
dest.write('TZif3')
dest.write('TZif1')
src.pos = 5
FileUtils.copy_stream(src, dest)
end
Expand Down
61 changes: 33 additions & 28 deletions test/tc_zoneinfo_timezone_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@

class TCZoneinfoTimezoneInfo < Test::Unit::TestCase

begin
Time.at(-2147483649)
Time.at(2147483648)
SUPPORTS_64BIT = true
rescue RangeError
SUPPORTS_64BIT = false
end

def assert_period(abbreviation, utc_offset, std_offset, dst, start_at, end_at, info)
if start_at
period = info.period_for_utc(start_at)
Expand Down Expand Up @@ -328,28 +336,33 @@ def test_load_invalid_magic
end
end

def test_load_invalid_section2_magic
['TZif4', 'tzif2', '12345'].each do |section2_magic|
offsets = [{:gmtoff => -12094, :isdst => false, :abbrev => 'LT'}]

tzif_test(offsets, [], [], :min_format => 2, :section2_magic => section2_magic) do |path, format|
assert_raises(InvalidZoneinfoFile) do
ZoneinfoTimezoneInfo.new('Zone4', path)
# These tests can only be run if the platform supports 64-bit Times. When
# 64-bit support is unavailable, the second section will not be read, so no
# error will be raised.
if SUPPORTS_64BIT
def test_load_invalid_section2_magic
['TZif4', 'tzif2', '12345'].each do |section2_magic|
offsets = [{:gmtoff => -12094, :isdst => false, :abbrev => 'LT'}]

tzif_test(offsets, [], [], :min_format => 2, :section2_magic => section2_magic) do |path, format|
assert_raises(InvalidZoneinfoFile) do
ZoneinfoTimezoneInfo.new('Zone4', path)
end
end
end
end
end

def test_load_mismatched_section2_magic
minus_one = Proc.new {|f| f == 2 ? "TZif\0" : "TZif#{f - 1}" }
plus_one = Proc.new {|f| "TZif#{f + 1}" }

[minus_one, plus_one].each do |section2_magic|
offsets = [{:gmtoff => -12094, :isdst => false, :abbrev => 'LT'}]

tzif_test(offsets, [], [], :min_format => 2, :section2_magic => section2_magic) do |path, format|
assert_raises(InvalidZoneinfoFile) do
ZoneinfoTimezoneInfo.new('Zone5', path)
def test_load_mismatched_section2_magic
minus_one = Proc.new {|f| f == 2 ? "TZif\0" : "TZif#{f - 1}" }
plus_one = Proc.new {|f| "TZif#{f + 1}" }

[minus_one, plus_one].each do |section2_magic|
offsets = [{:gmtoff => -12094, :isdst => false, :abbrev => 'LT'}]

tzif_test(offsets, [], [], :min_format => 2, :section2_magic => section2_magic) do |path, format|
assert_raises(InvalidZoneinfoFile) do
ZoneinfoTimezoneInfo.new('Zone5', path)
end
end
end
end
Expand Down Expand Up @@ -436,22 +449,14 @@ def test_load_before_epoch
assert_period(:XNST, 0, 0, false, Time.utc(2000, 12, 31), nil, info)
end
end

def test_load_64bit
# Some platforms support 64-bit Times, others only 32-bit. The TZif version
# 2 and later format contains both 32-bit and 64-bit times.

# Where 64-bit is supported and a TZif 2 or later file is provided, the
# 64-bit times should be used, otherwise the 32-bit information should be
# used.

begin
Time.at(-2147483649)
Time.at(2147483648)
supports_64bit = true
rescue RangeError
supports_64bit = false
end

offsets = [
{:gmtoff => 3542, :isdst => false, :abbrev => 'LMT'},
Expand All @@ -469,7 +474,7 @@ def test_load_64bit
info = ZoneinfoTimezoneInfo.new('Zone/SixtyFour', path)
assert_equal('Zone/SixtyFour', info.identifier)

if supports_64bit && format >= 2
if SUPPORTS_64BIT && format >= 2
assert_period(:LMT, 3542, 0, false, nil, Time.utc(1850, 1, 2), info)
assert_period(:XST, 3600, 0, false, Time.utc(1850, 1, 2), Time.utc(2003, 4, 22), info)
assert_period(:XDT, 3600, 3600, true, Time.utc(2003, 4, 22), Time.utc(2003, 10, 21), info)
Expand Down

0 comments on commit 5118250

Please sign in to comment.