PageRenderTime 122ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/date/test_date_new.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 271 lines | 225 code | 46 blank | 0 comment | 0 complexity | 95dac73608dc4553501019272325b9d8 MD5 | raw file
  1. require 'test/unit'
  2. require 'date'
  3. class TestDateNew < Test::Unit::TestCase
  4. def test_jd
  5. d = Date.jd
  6. dt = DateTime.jd
  7. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  8. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  9. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  10. d2 = Date.jd
  11. dt2 = DateTime.jd
  12. assert_equal(d, d2)
  13. assert_equal(dt, dt2)
  14. d = Date.jd(0)
  15. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  16. d = DateTime.jd(0, 0,0,0, 0)
  17. assert_equal([-4712, 1, 1, 0, 0, 0, 0],
  18. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  19. d = DateTime.jd(0, 0,0,0, '+0900')
  20. assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
  21. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  22. end
  23. def test_jd__ex
  24. assert_raise(ArgumentError) do
  25. DateTime.jd(0, 23,59,60,0)
  26. end
  27. end
  28. def test_ordinal
  29. d = Date.ordinal
  30. dt = DateTime.ordinal
  31. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  32. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  33. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  34. d2 = Date.ordinal
  35. dt2 = DateTime.ordinal
  36. assert_equal(d, d2)
  37. assert_equal(dt, dt2)
  38. d = Date.ordinal(-4712,1)
  39. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  40. d = Date.ordinal(-4712,1.0)
  41. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  42. d = DateTime.ordinal(-4712,1, 0,0,0, 0)
  43. assert_equal([-4712, 1, 1, 0, 0, 0, 0],
  44. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  45. d = DateTime.ordinal(-4712,1, 0,0,0, '+0900')
  46. assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
  47. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  48. end
  49. def test_ordinal__neg
  50. d = Date.ordinal(-1,-1)
  51. assert_equal([-1, 365], [d.year, d.yday])
  52. d = DateTime.ordinal(-1,-1, -1,-1,-1, 0)
  53. assert_equal([-1, 365, 23, 59, 59, 0],
  54. [d.year, d.yday, d.hour, d.min, d.sec, d.offset])
  55. end
  56. def test_ordinal__ex
  57. assert_raise(ArgumentError) do
  58. Date.ordinal(2001,366)
  59. end
  60. assert_raise(ArgumentError) do
  61. DateTime.ordinal(2001,365, 23,59,60, 0)
  62. end
  63. end
  64. def test_civil
  65. d = Date.civil
  66. dt = DateTime.civil
  67. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  68. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  69. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  70. d2 = Date.civil
  71. dt2 = DateTime.civil
  72. assert_equal(d, d2)
  73. assert_equal(dt, dt2)
  74. d = Date.civil(-4712,1,1)
  75. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  76. d = Date.civil(-4712,1,1.0)
  77. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  78. d = DateTime.civil(-4712,1,1, 0,0,0, 0)
  79. assert_equal([-4712, 1, 1, 0, 0, 0, 0],
  80. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  81. d = DateTime.civil(-4712,1,1, 0,0,0, '+0900')
  82. assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
  83. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  84. d = DateTime.civil(2001,2,3 + 1.to_r/2)
  85. assert_equal([2001, 2, 3, 12, 0, 0, 0],
  86. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  87. d = DateTime.civil(2001,2,3, 4 + 1.to_r/2)
  88. assert_equal([2001, 2, 3, 4, 30, 0, 0],
  89. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  90. d = DateTime.civil(2001,2,3, 4,5 + 1.to_r/2)
  91. assert_equal([2001, 2, 3, 4, 5, 30, 0],
  92. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  93. d = DateTime.civil(2001,2,3, 4,5,6 + 1.to_r/2)
  94. assert_equal([2001, 2, 3, 4, 5, 6, 0],
  95. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  96. assert_equal(1.to_r/2, d.sec_fraction)
  97. end
  98. def test_civil__neg
  99. d = Date.civil(-1,-1,-1)
  100. assert_equal([-1, 12, 31], [d.year, d.mon, d.mday])
  101. d = DateTime.civil(-1,-1,-1, -1,-1,-1, 0)
  102. assert_equal([-1, 12, 31, 23, 59, 59, 0],
  103. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  104. end
  105. def test_civil__ex
  106. assert_raise(ArgumentError) do
  107. Date.civil(2001,2,29)
  108. end
  109. assert_raise(ArgumentError) do
  110. DateTime.civil(2001,2,28, 23,59,60, 0)
  111. end
  112. assert_raise(ArgumentError) do
  113. DateTime.civil(2001,2,28, 24,59,59, 0)
  114. end
  115. end
  116. def test_civil__reform
  117. d = Date.jd(Date::ENGLAND, Date::ENGLAND)
  118. dt = DateTime.jd(Date::ENGLAND, 0,0,0,0, Date::ENGLAND)
  119. assert_equal([1752, 9, 14], [d.year, d.mon, d.mday])
  120. assert_equal([1752, 9, 14], [dt.year, dt.mon, dt.mday])
  121. d -= 1
  122. dt -= 1
  123. assert_equal([1752, 9, 2], [d.year, d.mon, d.mday])
  124. assert_equal([1752, 9, 2], [dt.year, dt.mon, dt.mday])
  125. d = Date.jd(Date::ITALY, Date::ITALY)
  126. dt = DateTime.jd(Date::ITALY, 0,0,0,0, Date::ITALY)
  127. assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
  128. assert_equal([1582, 10, 15], [dt.year, dt.mon, dt.mday])
  129. d -= 1
  130. dt -= 1
  131. assert_equal([1582, 10, 4], [d.year, d.mon, d.mday])
  132. assert_equal([1582, 10, 4], [dt.year, dt.mon, dt.mday])
  133. end
  134. def test_commercial
  135. d = Date.commercial
  136. dt = DateTime.commercial
  137. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  138. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  139. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  140. d2 = Date.commercial
  141. dt2 = DateTime.commercial
  142. assert_equal(d, d2)
  143. assert_equal(dt, dt2)
  144. d = Date.commercial(1582,40,5)
  145. assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
  146. d = Date.commercial(1582,40,5.0)
  147. assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
  148. d = DateTime.commercial(1582,40,5, 0,0,0, 0)
  149. assert_equal([1582, 10, 15, 0, 0, 0, 0],
  150. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  151. d = DateTime.commercial(1582,40,5, 0,0,0, '+0900')
  152. assert_equal([1582, 10, 15, 0, 0, 0, 9.to_r/24],
  153. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  154. end
  155. def test_commercial__neg
  156. d = Date.commercial(1998,-1,-1)
  157. assert_equal([1999, 1, 3], [d.year, d.mon, d.mday])
  158. d = DateTime.commercial(1998,-1,-1, -1,-1,-1, 0)
  159. assert_equal([1999, 1, 3, 23, 59, 59, 0],
  160. [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  161. end
  162. def test_commercial__ex
  163. assert_raise(ArgumentError) do
  164. Date.commercial(1997,53,1)
  165. end
  166. assert_raise(ArgumentError) do
  167. DateTime.commercial(1997,52,1, 23,59,60, 0)
  168. end
  169. end
  170. def test_weeknum
  171. skip unless Date.respond_to?(:weeknum, true)
  172. d = Date.__send__(:weeknum)
  173. dt = DateTime.__send__(:weeknum)
  174. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  175. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  176. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  177. d = Date.__send__(:weeknum, 2002,11,4, 0)
  178. assert_equal(2452355, d.jd)
  179. d = DateTime.__send__(:weeknum, 2002,11,4, 0, 11,22,33)
  180. assert_equal(2452355, d.jd)
  181. assert_equal([11,22,33], [d.hour, d.min, d.sec])
  182. assert_raise(ArgumentError) do
  183. Date.__send__(:weeknum, 1999,53,0, 0)
  184. end
  185. assert_raise(ArgumentError) do
  186. Date.__send__(:weeknum, 1999,-53,-1, 0)
  187. end
  188. end
  189. def test_nth_kday
  190. skip unless Date.respond_to?(:nth_kday, true)
  191. d = Date.__send__(:nth_kday)
  192. dt = DateTime.__send__(:nth_kday)
  193. assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
  194. assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
  195. assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
  196. d = Date.__send__(:nth_kday, 1992,2, 5,6)
  197. assert_equal(2448682, d.jd)
  198. d = DateTime.__send__(:nth_kday, 1992,2, 5,6, 11,22,33)
  199. assert_equal(2448682, d.jd)
  200. assert_equal([11,22,33], [d.hour, d.min, d.sec])
  201. assert_raise(ArgumentError) do
  202. Date.__send__(:nth_kday, 2006,5, 5,0)
  203. end
  204. assert_raise(ArgumentError) do
  205. Date.__send__(:nth_kday, 2006,5, -5,0)
  206. end
  207. end
  208. def test_today
  209. z = Time.now
  210. d = Date.today
  211. t = Time.now
  212. t2 = Time.utc(t.year, t.mon, t.mday)
  213. t3 = Time.utc(d.year, d.mon, d.mday)
  214. assert_in_delta(t2, t3, t - z + 2)
  215. assert_equal(false, DateTime.respond_to?(:today))
  216. end
  217. def test_now
  218. assert_equal(false, Date.respond_to?(:now))
  219. z = Time.now
  220. d = DateTime.now
  221. t = Time.now
  222. t2 = Time.local(d.year, d.mon, d.mday, d.hour, d.min, d.sec)
  223. assert_in_delta(t, t2, t - z + 2)
  224. end
  225. end