PageRenderTime 26ms CodeModel.GetById 48ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/ruby/test_float.rb

https://github.com/rizkyrukmana/jruby
Ruby | 453 lines | 397 code | 55 blank | 1 comment | 14 complexity | b3763a63aa927fd9d68df9435adea943 MD5 | raw file
  1. require 'test/unit'
  2. class TestFloat < Test::Unit::TestCase
  3. def test_float
  4. assert_equal(2, 2.6.floor)
  5. assert_equal(-3, (-2.6).floor)
  6. assert_equal(3, 2.6.ceil)
  7. assert_equal(-2, (-2.6).ceil)
  8. assert_equal(2, 2.6.truncate)
  9. assert_equal(-2, (-2.6).truncate)
  10. assert_equal(3, 2.6.round)
  11. assert_equal(-2, (-2.4).truncate)
  12. assert((13.4 % 1 - 0.4).abs < 0.0001)
  13. assert_equal(36893488147419111424,
  14. 36893488147419107329.0.to_i)
  15. end
  16. def nan_test(x,y)
  17. extend Test::Unit::Assertions
  18. assert(x != y)
  19. assert_equal(false, (x < y))
  20. assert_equal(false, (x > y))
  21. assert_equal(false, (x <= y))
  22. assert_equal(false, (x >= y))
  23. end
  24. def test_nan
  25. nan = Float::NAN
  26. nan_test(nan, nan)
  27. nan_test(nan, 0)
  28. nan_test(nan, 1)
  29. nan_test(nan, -1)
  30. nan_test(nan, 1000)
  31. nan_test(nan, -1000)
  32. nan_test(nan, 1_000_000_000_000)
  33. nan_test(nan, -1_000_000_000_000)
  34. nan_test(nan, 100.0);
  35. nan_test(nan, -100.0);
  36. nan_test(nan, 0.001);
  37. nan_test(nan, -0.001);
  38. nan_test(nan, 1.0/0);
  39. nan_test(nan, -1.0/0);
  40. end
  41. def test_precision
  42. u = 3.7517675036461267e+17
  43. v = sprintf("%.16e", u).to_f
  44. assert_in_delta(u, v, u.abs * Float::EPSILON)
  45. assert_in_delta(u, v, v.abs * Float::EPSILON)
  46. end
  47. def test_symmetry_bignum # [ruby-bugs-ja:118]
  48. a = 100000000000000000000000
  49. b = 100000000000000000000000.0
  50. assert_equal(a == b, b == a)
  51. end
  52. def test_strtod
  53. a = Float("0")
  54. assert(a.abs < Float::EPSILON)
  55. a = Float("0.0")
  56. assert(a.abs < Float::EPSILON)
  57. a = Float("+0.0")
  58. assert(a.abs < Float::EPSILON)
  59. a = Float("-0.0")
  60. assert(a.abs < Float::EPSILON)
  61. a = Float("0.0000000000000000001")
  62. assert(a != 0.0)
  63. a = Float("+0.0000000000000000001")
  64. assert(a != 0.0)
  65. a = Float("-0.0000000000000000001")
  66. assert(a != 0.0)
  67. a = Float(".0")
  68. assert(a.abs < Float::EPSILON)
  69. a = Float("+.0")
  70. assert(a.abs < Float::EPSILON)
  71. a = Float("-.0")
  72. assert(a.abs < Float::EPSILON)
  73. assert_raise(ArgumentError){Float("0.")}
  74. assert_raise(ArgumentError){Float("+0.")}
  75. assert_raise(ArgumentError){Float("-0.")}
  76. assert_raise(ArgumentError){Float(".")}
  77. assert_raise(ArgumentError){Float("+")}
  78. assert_raise(ArgumentError){Float("+.")}
  79. assert_raise(ArgumentError){Float("-")}
  80. assert_raise(ArgumentError){Float("-.")}
  81. assert_raise(ArgumentError){Float("1e")}
  82. assert_raise(ArgumentError){Float("1__1")}
  83. # add expected behaviour here.
  84. assert_equal(10, Float("1_0"))
  85. assert_equal([ 0.0].pack('G'), [Float(" 0x0p+0").to_f].pack('G'))
  86. assert_equal([-0.0].pack('G'), [Float("-0x0p+0").to_f].pack('G'))
  87. assert_equal(255.0, Float("0Xff"))
  88. assert_equal(255.5, Float("0Xff.8"))
  89. assert_equal(1.0, Float("0X1.P+0"))
  90. assert_equal(1024.0, Float("0x1p10"))
  91. assert_equal(1024.0, Float("0x1p+10"))
  92. assert_equal(0.0009765625, Float("0x1p-10"))
  93. assert_equal(2.6881171418161356e+43, Float("0x1.3494a9b171bf5p+144"))
  94. assert_equal(-3.720075976020836e-44, Float("-0x1.a8c1f14e2af5dp-145"))
  95. end
  96. def test_divmod
  97. assert_equal([2, 3.5], 11.5.divmod(4))
  98. assert_equal([-3, -0.5], 11.5.divmod(-4))
  99. assert_equal([-3, 0.5], (-11.5).divmod(4))
  100. assert_equal([2, -3.5], (-11.5).divmod(-4))
  101. end
  102. def test_div
  103. assert_equal(2, 11.5.div(4))
  104. assert_equal(-3, 11.5.div(-4))
  105. assert_equal(-3, (-11.5).div(4))
  106. assert_equal(2, (-11.5).div(-4))
  107. end
  108. def test_modulo
  109. assert_equal(3.5, 11.5.modulo(4))
  110. assert_equal(-0.5, 11.5.modulo(-4))
  111. assert_equal(0.5, (-11.5).modulo(4))
  112. assert_equal(-3.5, (-11.5).modulo(-4))
  113. end
  114. def test_remainder
  115. assert_equal(3.5, 11.5.remainder(4))
  116. assert_equal(3.5, 11.5.remainder(-4))
  117. assert_equal(-3.5, (-11.5).remainder(4))
  118. assert_equal(-3.5, (-11.5).remainder(-4))
  119. end
  120. def test_to_s
  121. inf = Float::INFINITY
  122. assert_equal("Infinity", inf.to_s)
  123. assert_equal("-Infinity", (-inf).to_s)
  124. assert_equal("NaN", (inf / inf).to_s)
  125. assert_equal("1.0e+18", 1000_00000_00000_00000.0.to_s)
  126. bug3273 = '[ruby-core:30145]'
  127. [0.21611564636388508, 0.56].each do |f|
  128. s = f.to_s
  129. assert_equal(f, s.to_f, bug3273)
  130. assert_not_equal(f, s.chop.to_f, bug3273)
  131. end
  132. end
  133. def test_coerce
  134. assert_equal(Float, 1.0.coerce(1).first.class)
  135. end
  136. def test_plus
  137. assert_equal(4.0, 2.0.send(:+, 2))
  138. assert_equal(4.0, 2.0.send(:+, (2**32).coerce(2).first))
  139. assert_equal(4.0, 2.0.send(:+, 2.0))
  140. assert_raise(TypeError) { 2.0.send(:+, nil) }
  141. end
  142. def test_minus
  143. assert_equal(0.0, 2.0.send(:-, 2))
  144. assert_equal(0.0, 2.0.send(:-, (2**32).coerce(2).first))
  145. assert_equal(0.0, 2.0.send(:-, 2.0))
  146. assert_raise(TypeError) { 2.0.send(:-, nil) }
  147. end
  148. def test_mul
  149. assert_equal(4.0, 2.0.send(:*, 2))
  150. assert_equal(4.0, 2.0.send(:*, (2**32).coerce(2).first))
  151. assert_equal(4.0, 2.0.send(:*, 2.0))
  152. assert_raise(TypeError) { 2.0.send(:*, nil) }
  153. end
  154. def test_div2
  155. assert_equal(1.0, 2.0.send(:/, 2))
  156. assert_equal(1.0, 2.0.send(:/, (2**32).coerce(2).first))
  157. assert_equal(1.0, 2.0.send(:/, 2.0))
  158. assert_raise(TypeError) { 2.0.send(:/, nil) }
  159. end
  160. def test_modulo2
  161. assert_equal(0.0, 2.0.send(:%, 2))
  162. assert_equal(0.0, 2.0.send(:%, (2**32).coerce(2).first))
  163. assert_equal(0.0, 2.0.send(:%, 2.0))
  164. assert_raise(TypeError) { 2.0.send(:%, nil) }
  165. end
  166. def test_divmod2
  167. assert_equal([1.0, 0.0], 2.0.divmod(2))
  168. assert_equal([1.0, 0.0], 2.0.divmod((2**32).coerce(2).first))
  169. assert_equal([1.0, 0.0], 2.0.divmod(2.0))
  170. assert_raise(TypeError) { 2.0.divmod(nil) }
  171. inf = Float::INFINITY
  172. assert_raise(ZeroDivisionError) {inf.divmod(0)}
  173. a, b = (2.0**32).divmod(1.0)
  174. assert_equal(2**32, a)
  175. assert_equal(0, b)
  176. end
  177. def test_pow
  178. assert_equal(1.0, 1.0 ** (2**32))
  179. assert_equal(1.0, 1.0 ** 1.0)
  180. assert_raise(TypeError) { 1.0 ** nil }
  181. end
  182. def test_eql
  183. inf = Float::INFINITY
  184. nan = Float::NAN
  185. assert(1.0.eql?(1.0))
  186. assert(inf.eql?(inf))
  187. assert(!(nan.eql?(nan)))
  188. assert(!(1.0.eql?(nil)))
  189. assert(1.0 == 1)
  190. assert(1.0 != 2**32)
  191. assert(1.0 != nan)
  192. assert(1.0 != nil)
  193. end
  194. def test_cmp
  195. inf = Float::INFINITY
  196. nan = Float::NAN
  197. assert_equal(0, 1.0 <=> 1.0)
  198. assert_equal(1, 1.0 <=> 0.0)
  199. assert_equal(-1, 1.0 <=> 2.0)
  200. assert_nil(1.0 <=> nil)
  201. assert_nil(1.0 <=> nan)
  202. assert_nil(nan <=> 1.0)
  203. assert_equal(0, 1.0 <=> 1)
  204. assert_equal(1, 1.0 <=> 0)
  205. assert_equal(-1, 1.0 <=> 2)
  206. assert_equal(-1, 1.0 <=> 2**32)
  207. assert_equal(1, inf <=> (Float::MAX.to_i*2))
  208. assert_equal(-1, -inf <=> (-Float::MAX.to_i*2))
  209. assert_equal(-1, (Float::MAX.to_i*2) <=> inf)
  210. assert_equal(1, (-Float::MAX.to_i*2) <=> -inf)
  211. assert_raise(ArgumentError) { 1.0 > nil }
  212. assert_raise(ArgumentError) { 1.0 >= nil }
  213. assert_raise(ArgumentError) { 1.0 < nil }
  214. assert_raise(ArgumentError) { 1.0 <= nil }
  215. end
  216. def test_zero_p
  217. assert(0.0.zero?)
  218. assert(!(1.0.zero?))
  219. end
  220. def test_infinite_p
  221. inf = Float::INFINITY
  222. assert_equal(1, inf.infinite?)
  223. assert_equal(-1, (-inf).infinite?)
  224. assert_nil(1.0.infinite?)
  225. end
  226. def test_finite_p
  227. inf = Float::INFINITY
  228. assert(!(inf.finite?))
  229. assert(!((-inf).finite?))
  230. assert(1.0.finite?)
  231. end
  232. def test_floor_ceil_round_truncate
  233. assert_equal(1, 1.5.floor)
  234. assert_equal(2, 1.5.ceil)
  235. assert_equal(2, 1.5.round)
  236. assert_equal(1, 1.5.truncate)
  237. assert_equal(2, 2.0.floor)
  238. assert_equal(2, 2.0.ceil)
  239. assert_equal(2, 2.0.round)
  240. assert_equal(2, 2.0.truncate)
  241. assert_equal(-2, (-1.5).floor)
  242. assert_equal(-1, (-1.5).ceil)
  243. assert_equal(-2, (-1.5).round)
  244. assert_equal(-1, (-1.5).truncate)
  245. assert_equal(-2, (-2.0).floor)
  246. assert_equal(-2, (-2.0).ceil)
  247. assert_equal(-2, (-2.0).round)
  248. assert_equal(-2, (-2.0).truncate)
  249. inf = Float::INFINITY
  250. assert_raise(FloatDomainError) { inf.floor }
  251. assert_raise(FloatDomainError) { inf.ceil }
  252. assert_raise(FloatDomainError) { inf.round }
  253. assert_raise(FloatDomainError) { inf.truncate }
  254. assert_equal(1.100, 1.111.round(1))
  255. assert_equal(1.110, 1.111.round(2))
  256. assert_equal(11110.0, 11111.1.round(-1))
  257. assert_equal(11100.0, 11111.1.round(-2))
  258. end
  259. VS = [
  260. 18446744073709551617.0,
  261. 18446744073709551616.0,
  262. 18446744073709551615.8,
  263. 18446744073709551615.5,
  264. 18446744073709551615.2,
  265. 18446744073709551615.0,
  266. 18446744073709551614.0,
  267. 4611686018427387905.0,
  268. 4611686018427387904.0,
  269. 4611686018427387903.8,
  270. 4611686018427387903.5,
  271. 4611686018427387903.2,
  272. 4611686018427387903.0,
  273. 4611686018427387902.0,
  274. 4294967297.0,
  275. 4294967296.0,
  276. 4294967295.8,
  277. 4294967295.5,
  278. 4294967295.2,
  279. 4294967295.0,
  280. 4294967294.0,
  281. 1073741825.0,
  282. 1073741824.0,
  283. 1073741823.8,
  284. 1073741823.5,
  285. 1073741823.2,
  286. 1073741823.0,
  287. 1073741822.0,
  288. -1073741823.0,
  289. -1073741824.0,
  290. -1073741824.2,
  291. -1073741824.5,
  292. -1073741824.8,
  293. -1073741825.0,
  294. -1073741826.0,
  295. -4294967295.0,
  296. -4294967296.0,
  297. -4294967296.2,
  298. -4294967296.5,
  299. -4294967296.8,
  300. -4294967297.0,
  301. -4294967298.0,
  302. -4611686018427387903.0,
  303. -4611686018427387904.0,
  304. -4611686018427387904.2,
  305. -4611686018427387904.5,
  306. -4611686018427387904.8,
  307. -4611686018427387905.0,
  308. -4611686018427387906.0,
  309. -18446744073709551615.0,
  310. -18446744073709551616.0,
  311. -18446744073709551616.2,
  312. -18446744073709551616.5,
  313. -18446744073709551616.8,
  314. -18446744073709551617.0,
  315. -18446744073709551618.0,
  316. ]
  317. def test_truncate
  318. VS.each {|f|
  319. i = f.truncate
  320. assert_equal(i, f.to_i)
  321. if f < 0
  322. assert_operator(i, :<, 0)
  323. else
  324. assert_operator(i, :>, 0)
  325. end
  326. assert_operator(i.abs, :<=, f.abs)
  327. d = f.abs - i.abs
  328. assert_operator(0, :<=, d)
  329. assert_operator(d, :<, 1)
  330. }
  331. end
  332. def test_ceil
  333. VS.each {|f|
  334. i = f.ceil
  335. if f < 0
  336. assert_operator(i, :<, 0)
  337. else
  338. assert_operator(i, :>, 0)
  339. end
  340. assert_operator(i, :>=, f)
  341. d = f - i
  342. assert_operator(-1, :<, d)
  343. assert_operator(d, :<=, 0)
  344. }
  345. end
  346. def test_floor
  347. VS.each {|f|
  348. i = f.floor
  349. if f < 0
  350. assert_operator(i, :<, 0)
  351. else
  352. assert_operator(i, :>, 0)
  353. end
  354. assert_operator(i, :<=, f)
  355. d = f - i
  356. assert_operator(0, :<=, d)
  357. assert_operator(d, :<, 1)
  358. }
  359. end
  360. def test_round
  361. VS.each {|f|
  362. i = f.round
  363. if f < 0
  364. assert_operator(i, :<, 0)
  365. else
  366. assert_operator(i, :>, 0)
  367. end
  368. d = f - i
  369. assert_operator(-0.5, :<=, d)
  370. assert_operator(d, :<=, 0.5)
  371. }
  372. end
  373. def test_Float
  374. assert_in_delta(0.125, Float("0.1_2_5"), 0.00001)
  375. assert_in_delta(0.125, "0.1_2_5__".to_f, 0.00001)
  376. assert_equal(1, Float(([1] * 10000).join).infinite?)
  377. assert(!Float(([1] * 10000).join("_")).infinite?) # is it really OK?
  378. assert_raise(ArgumentError) { Float("1.0\x001") }
  379. assert_equal(1, Float("1e10_00").infinite?)
  380. assert_raise(TypeError) { Float(nil) }
  381. o = Object.new
  382. def o.to_f; inf = Float::INFINITY; inf/inf; end
  383. assert(Float(o).nan?)
  384. end
  385. def test_num2dbl
  386. assert_raise(TypeError) do
  387. 1.0.step(2.0, "0.5") {}
  388. end
  389. assert_raise(TypeError) do
  390. 1.0.step(2.0, nil) {}
  391. end
  392. end
  393. def test_sleep_with_Float
  394. assert_nothing_raised("[ruby-core:23282]") do
  395. sleep(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)
  396. end
  397. end
  398. end