PageRenderTime 76ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/macruby/0.4/test/ruby/test_time.rb

https://github.com/Sophrinix/iphone-macruby
Ruby | 366 lines | 318 code | 41 blank | 7 comment | 4 complexity | 1905d71554383e22a07c165105f948a5 MD5 | raw file
  1. require 'test/unit'
  2. require 'rational'
  3. class TestTime < Test::Unit::TestCase
  4. def test_time_add()
  5. assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
  6. Time.utc(2000, 3, 21, 6, 30))
  7. assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
  8. Time.utc(2000, 3, 21, 0, 30))
  9. assert_equal(0, (Time.at(1.1) + 0.9).usec)
  10. end
  11. def test_time_subt()
  12. assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
  13. Time.utc(2000, 3, 21, 0, 30))
  14. assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
  15. Time.utc(2000, 3, 21, 6, 30))
  16. assert_equal(900000, (Time.at(1.1) - 0.2).usec)
  17. end
  18. def test_time_time()
  19. assert_equal(Time.utc(2000, 3, 21, 3, 30) \
  20. -Time.utc(2000, 3, 21, 0, 30), 3*3600)
  21. assert_equal(Time.utc(2000, 3, 21, 0, 30) \
  22. -Time.utc(2000, 3, 21, 3, 30), -3*3600)
  23. end
  24. def negative_time_t?
  25. begin
  26. Time.at(-1)
  27. true
  28. rescue ArgumentError
  29. false
  30. end
  31. end
  32. def test_timegm
  33. if negative_time_t?
  34. assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
  35. assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
  36. assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
  37. end
  38. assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
  39. assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
  40. assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
  41. assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
  42. assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)
  43. # 1972-06-30T23:59:60Z is the first leap second.
  44. if Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
  45. # no leap second.
  46. assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  47. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  48. assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  49. assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
  50. else
  51. # leap seconds supported.
  52. assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
  53. assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
  54. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  55. assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  56. assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  57. end
  58. end
  59. def test_strtime
  60. t = nil
  61. assert_nothing_raised { t = Time.utc("2000", "1", "2" , "3", "4", "5") }
  62. assert_equal(Time.utc(2000,1,2,3,4,5), t)
  63. end
  64. def test_huge_difference
  65. if negative_time_t?
  66. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
  67. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
  68. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
  69. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
  70. end
  71. end
  72. def test_big_minus
  73. begin
  74. bigtime0 = Time.at(2**60)
  75. bigtime1 = Time.at(2**60+1)
  76. rescue RangeError
  77. return
  78. end
  79. assert_equal(1.0, bigtime1 - bigtime0)
  80. end
  81. def test_at
  82. assert_equal(100000, Time.at(0.1).usec)
  83. assert_equal(10000, Time.at(0.01).usec)
  84. assert_equal(1000, Time.at(0.001).usec)
  85. assert_equal(100, Time.at(0.0001).usec)
  86. assert_equal(10, Time.at(0.00001).usec)
  87. assert_equal(1, Time.at(0.000001).usec)
  88. assert_equal(100000000, Time.at(0.1).nsec)
  89. assert_equal(10000000, Time.at(0.01).nsec)
  90. assert_equal(1000000, Time.at(0.001).nsec)
  91. assert_equal(100000, Time.at(0.0001).nsec)
  92. assert_equal(10000, Time.at(0.00001).nsec)
  93. assert_equal(1000, Time.at(0.000001).nsec)
  94. assert_equal(100, Time.at(0.0000001).nsec)
  95. assert_equal(10, Time.at(0.00000001).nsec)
  96. assert_equal(1, Time.at(0.000000001).nsec)
  97. end
  98. def test_at2
  99. assert_equal(100, Time.at(0, 0.1).nsec)
  100. assert_equal(10, Time.at(0, 0.01).nsec)
  101. assert_equal(1, Time.at(0, 0.001).nsec)
  102. end
  103. def test_at_rational
  104. assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
  105. assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
  106. end
  107. def test_utc_subsecond
  108. assert_equal(100000, Time.utc(2007,1,1,0,0,1.1).usec)
  109. assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
  110. end
  111. def test_eq_nsec
  112. assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
  113. assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
  114. end
  115. def assert_marshal_roundtrip(t)
  116. iv_names = t.instance_variables
  117. iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
  118. m = Marshal.dump(t)
  119. t2 = Marshal.load(m)
  120. iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
  121. assert_equal(t, t2)
  122. assert_equal(iv_vals1, iv_vals2)
  123. t2
  124. end
  125. def test_marshal_nsec
  126. assert_marshal_roundtrip(Time.at(0, 0.123))
  127. assert_marshal_roundtrip(Time.at(0, 0.120))
  128. end
  129. def test_marshal_ivar
  130. t = Time.at(123456789, 987654.321)
  131. t.instance_eval { @var = 135 }
  132. assert_marshal_roundtrip(t)
  133. assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
  134. end
  135. # Sat Jan 01 00:00:00 UTC 2000
  136. T2000 = Time.at(946684800).gmtime
  137. def test_security_error
  138. assert_raise(SecurityError) do
  139. Thread.new do
  140. t = Time.gm(2000)
  141. $SAFE = 4
  142. t.localtime
  143. end.join
  144. end
  145. end
  146. def test_at3
  147. assert_equal(T2000, Time.at(T2000))
  148. assert_raise(RangeError) do
  149. Time.at(2**31-1, 1_000_000)
  150. Time.at(2**63-1, 1_000_000)
  151. end
  152. assert_raise(RangeError) do
  153. Time.at(-2**31, -1_000_000)
  154. Time.at(-2**63, -1_000_000)
  155. end
  156. end
  157. def test_utc_or_local
  158. assert_equal(T2000, Time.gm(2000))
  159. assert_equal(T2000, Time.gm(0, 0, 0, 1, 1, 2000, :foo, :bar, false, :baz))
  160. assert_equal(T2000, Time.gm(0))
  161. assert_equal(T2000, Time.gm(100))
  162. assert_equal(T2000, Time.gm(2000, "jan"))
  163. assert_equal(T2000, Time.gm(2000, "1"))
  164. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, 0))
  165. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, "0"))
  166. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, "0", :foo, :foo))
  167. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1, :foo, :foo) }
  168. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1.0, :foo, :foo) }
  169. assert_raise(RangeError) do
  170. Time.gm(2000, 1, 1, 0, 0, 10_000_000_000_000_000_001.0, :foo, :foo)
  171. end
  172. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -(2**31), :foo, :foo) }
  173. o = Object.new
  174. def o.divmod(x); nil; end
  175. assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  176. def o.divmod(x); [-1, 0]; end
  177. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  178. assert_raise(ArgumentError) { Time.gm(2000, 13) }
  179. t = Time.local(2000)
  180. assert_equal(t.gmt_offset, T2000 - t)
  181. end
  182. def test_time_interval
  183. m = Mutex.new.lock
  184. assert_nothing_raised { m.sleep(0) }
  185. assert_raise(ArgumentError) { m.sleep(-1) }
  186. end
  187. def test_to_f
  188. assert_equal(946684800.0, T2000.to_f)
  189. end
  190. def test_cmp
  191. assert_equal(-1, T2000 <=> Time.gm(2001))
  192. assert_equal(1, T2000 <=> Time.gm(1999))
  193. assert_nil(T2000 <=> 0)
  194. end
  195. def test_eql
  196. assert(T2000.eql?(T2000))
  197. assert(!T2000.eql?(Time.gm(2001)))
  198. end
  199. def test_utc_p
  200. assert(Time.gm(2000).gmt?)
  201. assert(!Time.local(2000).gmt?)
  202. assert(!Time.at(0).gmt?)
  203. end
  204. def test_hash
  205. assert_kind_of(Integer, T2000.hash)
  206. end
  207. def test_init_copy
  208. assert_equal(T2000, T2000.dup)
  209. assert_raise(TypeError) do
  210. T2000.instance_eval { initialize_copy(nil) }
  211. end
  212. end
  213. def test_localtime_gmtime
  214. assert_nothing_raised do
  215. t = Time.gm(2000)
  216. assert(t.gmt?)
  217. t.localtime
  218. assert(!t.gmt?)
  219. t.localtime
  220. assert(!t.gmt?)
  221. t.gmtime
  222. assert(t.gmt?)
  223. t.gmtime
  224. assert(t.gmt?)
  225. end
  226. t1 = Time.gm(2000)
  227. t2 = t1.getlocal
  228. assert_equal(t1, t2)
  229. t1.localtime
  230. assert_equal(t1, t2)
  231. assert_equal(t1.gmt?, t2.gmt?)
  232. t1 = Time.local(2000)
  233. t2 = t1.getgm
  234. assert_equal(t1, t2)
  235. t1.gmtime
  236. assert_equal(t1, t2)
  237. assert_equal(t1.gmt?, t2.gmt?)
  238. end
  239. def test_asctime
  240. assert_equal("Sat Jan 1 00:00:00 2000", T2000.asctime)
  241. assert_kind_of(String, Time.at(0).asctime)
  242. end
  243. def test_to_s
  244. assert_equal("2000-01-01 00:00:00 UTC", T2000.to_s)
  245. assert_kind_of(String, Time.at(946684800).getlocal.to_s)
  246. assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
  247. end
  248. def test_plus_minus_succ
  249. # assert_raise(RangeError) { T2000 + 10000000000 }
  250. # assert_raise(RangeError) T2000 - 3094168449 }
  251. # assert_raise(RangeError) { T2000 + 1200798848 }
  252. assert_raise(TypeError) { T2000 + Time.now }
  253. assert_equal(T2000 + 1, T2000.succ)
  254. end
  255. def test_readers
  256. assert_equal(0, T2000.sec)
  257. assert_equal(0, T2000.min)
  258. assert_equal(0, T2000.hour)
  259. assert_equal(1, T2000.mday)
  260. assert_equal(1, T2000.mon)
  261. assert_equal(2000, T2000.year)
  262. assert_equal(6, T2000.wday)
  263. assert_equal(1, T2000.yday)
  264. assert_equal(false, T2000.isdst)
  265. assert_equal("UTC", T2000.zone)
  266. assert_equal(0, T2000.gmt_offset)
  267. assert(!T2000.sunday?)
  268. assert(!T2000.monday?)
  269. assert(!T2000.tuesday?)
  270. assert(!T2000.wednesday?)
  271. assert(!T2000.thursday?)
  272. assert(!T2000.friday?)
  273. assert(T2000.saturday?)
  274. assert_equal([0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], T2000.to_a)
  275. t = Time.at(946684800).getlocal
  276. assert_equal(t.sec, Time.at(946684800).sec)
  277. assert_equal(t.min, Time.at(946684800).min)
  278. assert_equal(t.hour, Time.at(946684800).hour)
  279. assert_equal(t.mday, Time.at(946684800).mday)
  280. assert_equal(t.mon, Time.at(946684800).mon)
  281. assert_equal(t.year, Time.at(946684800).year)
  282. assert_equal(t.wday, Time.at(946684800).wday)
  283. assert_equal(t.yday, Time.at(946684800).yday)
  284. assert_equal(t.isdst, Time.at(946684800).isdst)
  285. assert_equal(t.zone, Time.at(946684800).zone)
  286. assert_equal(t.gmt_offset, Time.at(946684800).gmt_offset)
  287. assert_equal(t.sunday?, Time.at(946684800).sunday?)
  288. assert_equal(t.monday?, Time.at(946684800).monday?)
  289. assert_equal(t.tuesday?, Time.at(946684800).tuesday?)
  290. assert_equal(t.wednesday?, Time.at(946684800).wednesday?)
  291. assert_equal(t.thursday?, Time.at(946684800).thursday?)
  292. assert_equal(t.friday?, Time.at(946684800).friday?)
  293. assert_equal(t.saturday?, Time.at(946684800).saturday?)
  294. assert_equal(t.to_a, Time.at(946684800).to_a)
  295. end
  296. def test_strftime
  297. t = Time.at(946684800).getlocal
  298. assert_equal("Sat", T2000.strftime("%a"))
  299. assert_equal("Saturday", T2000.strftime("%A"))
  300. assert_equal("Jan", T2000.strftime("%b"))
  301. assert_equal("January", T2000.strftime("%B"))
  302. assert_kind_of(String, T2000.strftime("%c"))
  303. assert_equal("01", T2000.strftime("%d"))
  304. assert_equal("00", T2000.strftime("%H"))
  305. assert_equal("12", T2000.strftime("%I"))
  306. assert_equal("001", T2000.strftime("%j"))
  307. assert_equal("01", T2000.strftime("%m"))
  308. assert_equal("00", T2000.strftime("%M"))
  309. assert_equal("AM", T2000.strftime("%p"))
  310. assert_equal("00", T2000.strftime("%S"))
  311. assert_equal("00", T2000.strftime("%U"))
  312. assert_equal("00", T2000.strftime("%W"))
  313. assert_equal("6", T2000.strftime("%w"))
  314. assert_equal("01/01/00", T2000.strftime("%x"))
  315. assert_equal("00:00:00", T2000.strftime("%X"))
  316. assert_equal("00", T2000.strftime("%y"))
  317. assert_equal("2000", T2000.strftime("%Y"))
  318. assert(["GMT", "UTC"].include?(T2000.strftime("%Z")))
  319. assert_equal("%", T2000.strftime("%%"))
  320. assert_equal("", T2000.strftime(""))
  321. assert_equal("foo\0bar\x0000\x0000\x0000", T2000.strftime("foo\0bar\0%H\0%M\0%S"))
  322. assert_equal("foo" * 1000, T2000.strftime("foo" * 1000))
  323. assert_equal("Sat", Time.at(946684800).strftime("%a"))
  324. end
  325. end