summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-21 12:47:07 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-21 12:47:07 +0000
commitd39f52e87b3583a0bf2b565deaccec5634cf51db (patch)
tree5f27f88fbec7c83c70a704a8138378b458601057
parent559c025a22e7a1df0182c505017eec7cb3d2228e (diff)
* test/date/test_switch_hitter.rb: new.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31669 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--test/date/test_switch_hitter.rb275
2 files changed, 279 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b0fae303a32..76dcac9e9a3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sat May 21 21:44:14 2011 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * test/date/test_switch_hitter.rb: new.
+
Sat May 21 21:18:29 2011 Tadayoshi Funaba <tadf@dotrb.org>
* ext/date/date_{core,parse}.c: moved nearly all core code from ext/date/lib.
diff --git a/test/date/test_switch_hitter.rb b/test/date/test_switch_hitter.rb
new file mode 100644
index 00000000000..0ca45e4c308
--- /dev/null
+++ b/test/date/test_switch_hitter.rb
@@ -0,0 +1,275 @@
+require 'test/unit'
+require 'date'
+
+class TestSH < Test::Unit::TestCase
+
+ def test_new
+ [Date.new,
+ Date.civil,
+ DateTime.new,
+ DateTime.civil
+ ].each do |d|
+ assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
+ end
+
+ [Date.new(2001),
+ Date.civil(2001),
+ DateTime.new(2001),
+ DateTime.civil(2001)
+ ].each do |d|
+ assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
+ end
+
+ d = Date.new(2001, 2, 3)
+ assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+ d = Date.new(2001, 2, Rational('3.5'))
+ assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+ d = Date.new(2001,2, 3, Date::JULIAN)
+ assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+ d = Date.new(2001,2, 3, Date::GREGORIAN)
+ assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+
+ d = Date.new(2001,-12, -31)
+ assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
+ d = Date.new(2001,-12, -31, Date::JULIAN)
+ assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
+ d = Date.new(2001,-12, -31, Date::GREGORIAN)
+ assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
+
+ d = DateTime.new(2001, 2, 3, 4, 5, 6)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, 2, 3, 4, 5, 6, 0)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, 2, 3, 4, 5, 6, Rational(9,24))
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, 2, 3, 4, 5, 6, 0.375)
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, 2, 3, 4, 5, 6, '+09:00')
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, 2, 3, 4, 5, 6, '-09:00')
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(-9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00')
+ assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00', Date::JULIAN)
+ assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00', Date::GREGORIAN)
+ assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ end
+
+ def test_jd
+ d = Date.jd
+ assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
+ d = Date.jd(0)
+ assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
+ d = Date.jd(2451944)
+ assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+
+ d = DateTime.jd
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(0)
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(2451944)
+ assert_equal([2001, 2, 3, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(2451944, 4, 5, 6)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(2451944, 4, 5, 6, 0)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(2451944, 4, 5, 6, '+9:00')
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.jd(2451944, -4, -5, -6, '-9:00')
+ assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ end
+
+ def test_ordinal
+ d = Date.ordinal
+ assert_equal([-4712, 1], [d.year, d.yday])
+ d = Date.ordinal(-4712, 1)
+ assert_equal([-4712, 1], [d.year, d.yday])
+
+ d = Date.ordinal(2001, 2)
+ assert_equal([2001, 2], [d.year, d.yday])
+ d = Date.ordinal(2001, 2, Date::JULIAN)
+ assert_equal([2001, 2], [d.year, d.yday])
+ d = Date.ordinal(2001, 2, Date::GREGORIAN)
+ assert_equal([2001, 2], [d.year, d.yday])
+
+ d = Date.ordinal(2001, -2, Date::JULIAN)
+ assert_equal([2001, 364], [d.year, d.yday])
+ d = Date.ordinal(2001, -2, Date::GREGORIAN)
+ assert_equal([2001, 364], [d.year, d.yday])
+
+ d = DateTime.ordinal
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(-4712, 1)
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(2001, 34)
+ assert_equal([2001, 2, 3, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(2001, 34, 4, 5, 6)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(2001, 34, 4, 5, 6, 0)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(2001, 34, 4, 5, 6, '+9:00')
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.ordinal(2001, 34, -4, -5, -6, '-9:00')
+ assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ end
+
+ def test_commercial
+ d = Date.commercial
+ assert_equal([-4712, 1, 1], [d.cwyear, d.cweek, d.cwday])
+ d = Date.commercial(-4712, 1, 1)
+ assert_equal([-4712, 1, 1], [d.cwyear, d.cweek, d.cwday])
+
+ d = Date.commercial(2001, 2, 3)
+ assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+ d = Date.commercial(2001, 2, 3, Date::JULIAN)
+ assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+ d = Date.commercial(2001, 2, 3, Date::GREGORIAN)
+ assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+
+ d = Date.commercial(2001, -2, -3)
+ assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
+ d = Date.commercial(2001, -2, -3, Date::JULIAN)
+ assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
+ d = Date.commercial(2001, -2, -3, Date::GREGORIAN)
+ assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
+
+ d = DateTime.commercial
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(-4712, 1, 1)
+ assert_equal([-4712, 1, 1, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(2001, 5, 6)
+ assert_equal([2001, 2, 3, 0, 0, 0, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(2001, 5, 6, 4, 5, 6)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(2001, 5, 6, 4, 5, 6, 0)
+ assert_equal([2001, 2, 3, 4, 5, 6, 0],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(2001, 5, 6, 4, 5, 6, '+9:00')
+ assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ d = DateTime.commercial(2001, 5, 6, -4, -5, -6, '-9:00')
+ assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
+ end
+
+ def test_zone
+ d = Date.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.send(:zone).encoding)
+ d = DateTime.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.send(:zone).encoding)
+ end
+
+ def test_to_s
+ d = Date.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.to_s.encoding)
+ assert_equal(Encoding::US_ASCII, d.strftime.encoding)
+ d = DateTime.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.to_s.encoding)
+ assert_equal(Encoding::US_ASCII, d.strftime.encoding)
+ end
+
+ def test_inspect
+ d = Date.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.inspect.encoding)
+ d = DateTime.new(2001, 2, 3)
+ assert_equal(Encoding::US_ASCII, d.inspect.encoding)
+ end
+
+ def test_cmp
+ assert_equal(-1, Date.new(2001,2,3) <=> Date.new(2001,2,4))
+ assert_equal(0, Date.new(2001,2,3) <=> Date.new(2001,2,3))
+ assert_equal(1, Date.new(2001,2,3) <=> Date.new(2001,2,2))
+
+ assert_equal(-1, Date.new(2001,2,3) <=> 2451944.0)
+ assert_equal(-1, Date.new(2001,2,3) <=> 2451944)
+ assert_equal(0, Date.new(2001,2,3) <=> 2451943.5)
+ assert_equal(1, Date.new(2001,2,3) <=> 2451943.0)
+ assert_equal(1, Date.new(2001,2,3) <=> 2451943)
+
+ assert_equal(-1, Date.new(2001,2,3) <=> Rational('4903888/2'))
+ assert_equal(0, Date.new(2001,2,3) <=> Rational('4903887/2'))
+ assert_equal(1, Date.new(2001,2,3) <=> Rational('4903886/2'))
+ end
+
+ def test_eqeqeq
+ assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,4))
+ assert_equal(true, Date.new(2001,2,3) === Date.new(2001,2,3))
+ assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,2))
+
+ assert_equal(true, Date.new(2001,2,3) === 2451944.0)
+ assert_equal(true, Date.new(2001,2,3) === 2451944)
+ assert_equal(false, Date.new(2001,2,3) === 2451943.5)
+ assert_equal(false, Date.new(2001,2,3) === 2451943.0)
+ assert_equal(false, Date.new(2001,2,3) === 2451943)
+
+ assert_equal(true, Date.new(2001,2,3) === Rational('4903888/2'))
+ assert_equal(false, Date.new(2001,2,3) === Rational('4903887/2'))
+ assert_equal(false, Date.new(2001,2,3) === Rational('4903886/2'))
+ end
+
+ def test_marshal
+ s = "\x04\bU:\tDate[\bU:\rRational[\ai\x03\xCF\xD3Ji\ai\x00o:\x13Date::Infinity\x06:\a@di\xFA"
+ d = Marshal.load(s)
+ assert_equal(Date.new(2001,2,3,Date::GREGORIAN), d)
+
+ s = "\x04\bU:\rDateTime[\bU:\rRational[\al+\b\xC9\xB0\x81\xBD\x02\x00i\x02\xC0\x12U;\x06[\ai\bi\ro:\x13Date::Infinity\x06:\a@di\xFA"
+ d = Marshal.load(s)
+ assert_equal(DateTime.new(2001,2,3,4,5,6,Rational(9,24),Date::GREGORIAN), d)
+ end
+
+ def test_base
+ skip unless defined?(Date.test_all)
+ assert_equal(true, Date.test_all)
+ end
+
+ def test_taint
+ h = Date._strptime('15:43+09:00', '%R%z')
+ assert_equal(false, h[:zone].tainted?)
+ h = Date._strptime('15:43+09:00'.taint, '%R%z')
+ assert_equal(true, h[:zone].tainted?)
+
+ h = Date._parse('15:43+09:00')
+ assert_equal(false, h[:zone].tainted?)
+ h = Date._parse('15:43+09:00'.taint)
+ assert_equal(true, h[:zone].tainted?)
+
+ s = Date.today.strftime('new 105')
+ assert_equal(false, s.tainted?)
+ s = Date.today.strftime('new 105'.taint)
+ assert_equal(true, s.tainted?)
+
+ s = DateTime.now.strftime('super $record')
+ assert_equal(false, s.tainted?)
+ s = DateTime.now.strftime('super $record'.taint)
+ assert_equal(true, s.tainted?)
+ end
+
+end