PageRenderTime 31ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_complex.rb

http://github.com/ruby/ruby
Ruby | 1145 lines | 936 code | 208 blank | 1 comment | 29 complexity | d365bae9736d36c33211c710207ebc04 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class ComplexSub < Complex; end
  4. class Complex_Test < Test::Unit::TestCase
  5. def test_rationalize
  6. assert_equal(1.quo(3), Complex(1/3.0, 0).rationalize, '[ruby-core:38885]')
  7. assert_equal(1.quo(5), Complex(0.2, 0).rationalize, '[ruby-core:38885]')
  8. assert_equal(5.quo(2), Complex(2.5, 0).rationalize(0), '[ruby-core:40667]')
  9. end
  10. def test_compsub
  11. c = ComplexSub.__send__(:convert, 1)
  12. assert_kind_of(Numeric, c)
  13. assert_instance_of(ComplexSub, c)
  14. c2 = c + 1
  15. assert_instance_of(ComplexSub, c2)
  16. c2 = c - 1
  17. assert_instance_of(ComplexSub, c2)
  18. c3 = c - c2
  19. assert_instance_of(ComplexSub, c3)
  20. s = Marshal.dump(c)
  21. c5 = Marshal.load(s)
  22. assert_equal(c, c5)
  23. assert_instance_of(ComplexSub, c5)
  24. c1 = Complex(1)
  25. assert_equal(c1.hash, c.hash, '[ruby-dev:38850]')
  26. assert_equal([true, true], [c.eql?(c1), c1.eql?(c)])
  27. end
  28. def test_eql_p
  29. c = Complex(0)
  30. c2 = Complex(0)
  31. c3 = Complex(1)
  32. assert_operator(c, :eql?, c2)
  33. assert_not_operator(c, :eql?, c3)
  34. assert_not_operator(c, :eql?, 0)
  35. end
  36. def test_hash
  37. h = Complex(1,2).hash
  38. assert_kind_of(Integer, h)
  39. assert_nothing_raised {h.to_s}
  40. h = Complex(1.0,2.0).hash
  41. assert_kind_of(Integer, h)
  42. assert_nothing_raised {h.to_s}
  43. h = {}
  44. h[Complex(0)] = 0
  45. h[Complex(0,1)] = 1
  46. h[Complex(1,0)] = 2
  47. h[Complex(1,1)] = 3
  48. assert_equal(4, h.size)
  49. assert_equal(2, h[Complex(1,0)])
  50. h[Complex(0,0)] = 9
  51. assert_equal(4, h.size)
  52. h[Complex(0.0,0.0)] = 9.0
  53. assert_equal(5, h.size)
  54. if (0.0/0).nan? && !((0.0/0).eql?(0.0/0))
  55. h = {}
  56. 3.times{h[Complex(0.0/0)] = 1}
  57. assert_equal(3, h.size)
  58. end
  59. end
  60. def test_freeze
  61. c = Complex(1)
  62. assert_predicate(c, :frozen?)
  63. assert_instance_of(String, c.to_s)
  64. end
  65. def test_conv
  66. c = Complex(0,0)
  67. assert_equal(Complex(0,0), c)
  68. c = Complex(2**32, 2**32)
  69. assert_equal(Complex(2**32,2**32), c)
  70. assert_equal([2**32,2**32], [c.real,c.imag])
  71. c = Complex(-2**32, 2**32)
  72. assert_equal(Complex(-2**32,2**32), c)
  73. assert_equal([-2**32,2**32], [c.real,c.imag])
  74. c = Complex(2**32, -2**32)
  75. assert_equal(Complex(2**32,-2**32), c)
  76. assert_equal([2**32,-2**32], [c.real,c.imag])
  77. c = Complex(-2**32, -2**32)
  78. assert_equal(Complex(-2**32,-2**32), c)
  79. assert_equal([-2**32,-2**32], [c.real,c.imag])
  80. c = Complex(Complex(1,2),2)
  81. assert_equal(Complex(1,4), c)
  82. c = Complex(2,Complex(1,2))
  83. assert_equal(Complex(0,1), c)
  84. c = Complex(Complex(1,2),Complex(1,2))
  85. assert_equal(Complex(-1,3), c)
  86. c = Complex::I
  87. assert_equal(Complex(0,1), c)
  88. assert_equal(Complex(1),Complex(1))
  89. assert_equal(Complex(1),Complex('1'))
  90. assert_equal(Complex(3.0,3.0),Complex('3.0','3.0'))
  91. assert_equal(Complex(1,1),Complex('3/3','3/3'))
  92. assert_raise(TypeError){Complex(nil)}
  93. assert_raise(TypeError){Complex(Object.new)}
  94. assert_raise(ArgumentError){Complex()}
  95. assert_raise(ArgumentError){Complex(1,2,3)}
  96. c = Complex(1,0)
  97. assert_same(c, Complex(c))
  98. assert_same(c, Complex(c, exception: false))
  99. assert_raise(ArgumentError){Complex(c, bad_keyword: true)}
  100. if (0.0/0).nan?
  101. assert_nothing_raised{Complex(0.0/0)}
  102. end
  103. if (1.0/0).infinite?
  104. assert_nothing_raised{Complex(1.0/0)}
  105. end
  106. end
  107. def test_attr
  108. c = Complex(4)
  109. assert_equal(4, c.real)
  110. assert_equal(0, c.imag)
  111. c = Complex(4,5)
  112. assert_equal(4, c.real)
  113. assert_equal(5, c.imag)
  114. if -0.0.to_s == '-0.0'
  115. c = Complex(-0.0,-0.0)
  116. assert_equal('-0.0', c.real.to_s)
  117. assert_equal('-0.0', c.imag.to_s)
  118. end
  119. c = Complex(4)
  120. assert_equal(4, c.real)
  121. assert_equal(0, c.imag)
  122. assert_equal(c.imag, c.imaginary)
  123. c = Complex(4,5)
  124. assert_equal(4, c.real)
  125. assert_equal(5, c.imag)
  126. assert_equal(c.imag, c.imaginary)
  127. if -0.0.to_s == '-0.0'
  128. c = Complex(-0.0,-0.0)
  129. assert_equal('-0.0', c.real.to_s)
  130. assert_equal('-0.0', c.imag.to_s)
  131. assert_equal(c.imag.to_s, c.imaginary.to_s)
  132. end
  133. c = Complex(4)
  134. assert_equal(4, c.real)
  135. assert_equal(c.imag, c.imaginary)
  136. assert_equal(0, c.imag)
  137. c = Complex(4,5)
  138. assert_equal(4, c.real)
  139. assert_equal(5, c.imag)
  140. assert_equal(c.imag, c.imaginary)
  141. c = Complex(-0.0,-0.0)
  142. assert_equal('-0.0', c.real.to_s)
  143. assert_equal('-0.0', c.imag.to_s)
  144. assert_equal(c.imag.to_s, c.imaginary.to_s)
  145. end
  146. def test_attr2
  147. c = Complex(1)
  148. assert_not_predicate(c, :integer?)
  149. assert_not_predicate(c, :real?)
  150. assert_predicate(Complex(0), :zero?)
  151. assert_predicate(Complex(0,0), :zero?)
  152. assert_not_predicate(Complex(1,0), :zero?)
  153. assert_not_predicate(Complex(0,1), :zero?)
  154. assert_not_predicate(Complex(1,1), :zero?)
  155. assert_equal(nil, Complex(0).nonzero?)
  156. assert_equal(nil, Complex(0,0).nonzero?)
  157. assert_equal(Complex(1,0), Complex(1,0).nonzero?)
  158. assert_equal(Complex(0,1), Complex(0,1).nonzero?)
  159. assert_equal(Complex(1,1), Complex(1,1).nonzero?)
  160. end
  161. def test_rect
  162. assert_equal([1,2], Complex.rectangular(1,2).rectangular)
  163. assert_equal([1,2], Complex.rect(1,2).rect)
  164. end
  165. def test_polar
  166. assert_equal([1,2], Complex.polar(1,2).polar)
  167. assert_equal(Complex.polar(1.0, Math::PI * 2 / 3), Complex.polar(1, Math::PI * 2 / 3))
  168. end
  169. def test_uplus
  170. assert_equal(Complex(1), +Complex(1))
  171. assert_equal(Complex(-1), +Complex(-1))
  172. assert_equal(Complex(1,1), +Complex(1,1))
  173. assert_equal(Complex(-1,1), +Complex(-1,1))
  174. assert_equal(Complex(1,-1), +Complex(1,-1))
  175. assert_equal(Complex(-1,-1), +Complex(-1,-1))
  176. if -0.0.to_s == '-0.0'
  177. c = +Complex(0.0,0.0)
  178. assert_equal('0.0', c.real.to_s)
  179. assert_equal('0.0', c.imag.to_s)
  180. c = +Complex(-0.0,-0.0)
  181. assert_equal('-0.0', c.real.to_s)
  182. assert_equal('-0.0', c.imag.to_s)
  183. end
  184. end
  185. def test_negate
  186. assert_equal(Complex(-1), -Complex(1))
  187. assert_equal(Complex(1), -Complex(-1))
  188. assert_equal(Complex(-1,-1), -Complex(1,1))
  189. assert_equal(Complex(1,-1), -Complex(-1,1))
  190. assert_equal(Complex(-1,1), -Complex(1,-1))
  191. assert_equal(Complex(1,1), -Complex(-1,-1))
  192. if -0.0.to_s == '-0.0'
  193. c = -Complex(0.0,0.0)
  194. assert_equal('-0.0', c.real.to_s)
  195. assert_equal('-0.0', c.imag.to_s)
  196. c = -Complex(-0.0,-0.0)
  197. assert_equal('0.0', c.real.to_s)
  198. assert_equal('0.0', c.imag.to_s)
  199. end
  200. end
  201. def test_add
  202. c = Complex(1,2)
  203. c2 = Complex(2,3)
  204. assert_equal(Complex(3,5), c + c2)
  205. assert_equal(Complex(3,2), c + 2)
  206. assert_equal(Complex(3.0,2), c + 2.0)
  207. assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2))
  208. assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3))
  209. end
  210. def test_add_with_redefining_int_plus
  211. assert_in_out_err([], <<-'end;', ['true'], [])
  212. class Integer
  213. remove_method :+
  214. def +(other); 42; end
  215. end
  216. a = Complex(1, 2) + Complex(0, 1)
  217. puts a == Complex(42, 42)
  218. end;
  219. end
  220. def test_add_with_redefining_float_plus
  221. assert_in_out_err([], <<-'end;', ['true'], [])
  222. class Float
  223. remove_method :+
  224. def +(other); 42.0; end
  225. end
  226. a = Complex(1.0, 2.0) + Complex(0, 1)
  227. puts a == Complex(42.0, 42.0)
  228. end;
  229. end
  230. def test_add_with_redefining_rational_plus
  231. assert_in_out_err([], <<-'end;', ['true'], [])
  232. class Rational
  233. remove_method :+
  234. def +(other); 355/113r; end
  235. end
  236. a = Complex(1r, 2r) + Complex(0, 1)
  237. puts a == Complex(355/113r, 355/113r)
  238. end;
  239. end
  240. def test_sub
  241. c = Complex(1,2)
  242. c2 = Complex(2,3)
  243. assert_equal(Complex(-1,-1), c - c2)
  244. assert_equal(Complex(-1,2), c - 2)
  245. assert_equal(Complex(-1.0,2), c - 2.0)
  246. assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2))
  247. assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3))
  248. end
  249. def test_sub_with_redefining_int_minus
  250. assert_in_out_err([], <<-'end;', ['true'], [])
  251. class Integer
  252. remove_method :-
  253. def -(other); 42; end
  254. end
  255. a = Complex(1, 2) - Complex(0, 1)
  256. puts a == Complex(42, 42)
  257. end;
  258. end
  259. def test_sub_with_redefining_float_minus
  260. assert_in_out_err([], <<-'end;', ['true'], [])
  261. class Float
  262. remove_method :-
  263. def -(other); 42.0; end
  264. end
  265. a = Complex(1.0, 2.0) - Complex(0, 1)
  266. puts a == Complex(42.0, 42.0)
  267. end;
  268. end
  269. def test_sub_with_redefining_rational_minus
  270. assert_in_out_err([], <<-'end;', ['true'], [])
  271. class Rational
  272. remove_method :-
  273. def -(other); 355/113r; end
  274. end
  275. a = Complex(1r, 2r) - Complex(0, 1)
  276. puts a == Complex(355/113r, 355/113r)
  277. end;
  278. end
  279. def test_mul
  280. c = Complex(1,2)
  281. c2 = Complex(2,3)
  282. assert_equal(Complex(-4,7), c * c2)
  283. assert_equal(Complex(2,4), c * 2)
  284. assert_equal(Complex(2.0,4.0), c * 2.0)
  285. assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2))
  286. assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3))
  287. c = Complex(Float::INFINITY, 0)
  288. assert_equal(Complex(Float::INFINITY, 0), c * Complex(1, 0))
  289. assert_equal(Complex(0, Float::INFINITY), c * Complex(0, 1))
  290. c = Complex(0, Float::INFINITY)
  291. assert_equal(Complex(0, Float::INFINITY), c * Complex(1, 0))
  292. assert_equal(Complex(-Float::INFINITY, 0), c * Complex(0, 1))
  293. assert_equal(Complex(-0.0, -0.0), Complex(-0.0, 0) * Complex(0, 0))
  294. end
  295. def test_mul_with_redefining_int_mult
  296. assert_in_out_err([], <<-'end;', ['true'], [])
  297. class Integer
  298. remove_method :*
  299. def *(other); 42; end
  300. end
  301. a = Complex(2, 0) * Complex(1, 2)
  302. puts a == Complex(0, 84)
  303. end;
  304. end
  305. def test_mul_with_redefining_float_mult
  306. assert_in_out_err([], <<-'end;', ['true'], [])
  307. class Float
  308. remove_method :*
  309. def *(other); 42.0; end
  310. end
  311. a = Complex(2.0, 0.0) * Complex(1, 2)
  312. puts a == Complex(0.0, 84.0)
  313. end;
  314. end
  315. def test_mul_with_redefining_rational_mult
  316. assert_in_out_err([], <<-'end;', ['true'], [])
  317. class Rational
  318. remove_method :*
  319. def *(other); 355/113r; end
  320. end
  321. a = Complex(2r, 0r) * Complex(1, 2)
  322. puts a == Complex(0r, 2*355/113r)
  323. end;
  324. end
  325. def test_div
  326. c = Complex(1,2)
  327. c2 = Complex(2,3)
  328. assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
  329. c = Complex(1.0,2.0)
  330. c2 = Complex(2.0,3.0)
  331. r = c / c2
  332. assert_in_delta(0.615, r.real, 0.001)
  333. assert_in_delta(0.076, r.imag, 0.001)
  334. c = Complex(1,2)
  335. c2 = Complex(2,3)
  336. assert_equal(Complex(Rational(1,2),1), c / 2)
  337. assert_equal(Complex(0.5,1.0), c / 2.0)
  338. assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
  339. assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
  340. c = Complex(1)
  341. [ 1, Rational(1), c ].each do |d|
  342. r = c / d
  343. assert_instance_of(Complex, r)
  344. assert_equal(1, r)
  345. assert_predicate(r.real, :integer?)
  346. assert_predicate(r.imag, :integer?)
  347. end
  348. end
  349. def test_quo
  350. c = Complex(1,2)
  351. c2 = Complex(2,3)
  352. assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
  353. c = Complex(1.0,2.0)
  354. c2 = Complex(2.0,3.0)
  355. r = c.quo(c2)
  356. assert_in_delta(0.615, r.real, 0.001)
  357. assert_in_delta(0.076, r.imag, 0.001)
  358. c = Complex(1,2)
  359. c2 = Complex(2,3)
  360. assert_equal(Complex(Rational(1,2),1), c.quo(2))
  361. assert_equal(Complex(0.5,1.0), c.quo(2.0))
  362. assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
  363. assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
  364. end
  365. def test_fdiv
  366. c = Complex(1,2)
  367. c2 = Complex(2,3)
  368. r = c.fdiv(c2)
  369. assert_in_delta(0.615, r.real, 0.001)
  370. assert_in_delta(0.076, r.imag, 0.001)
  371. c = Complex(1.0,2.0)
  372. c2 = Complex(2.0,3.0)
  373. r = c.fdiv(c2)
  374. assert_in_delta(0.615, r.real, 0.001)
  375. assert_in_delta(0.076, r.imag, 0.001)
  376. c = Complex(1,2)
  377. c2 = Complex(2,3)
  378. assert_equal(Complex(0.5,1.0), c.fdiv(2))
  379. assert_equal(Complex(0.5,1.0), c.fdiv(2.0))
  380. end
  381. def test_expt
  382. c = Complex(1,2)
  383. c2 = Complex(2,3)
  384. r = c ** c2
  385. assert_in_delta(-0.015, r.real, 0.001)
  386. assert_in_delta(-0.179, r.imag, 0.001)
  387. assert_equal(Complex(-3,4), c ** 2)
  388. assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
  389. r = c ** 2.0
  390. assert_in_delta(-3.0, r.real, 0.001)
  391. assert_in_delta(4.0, r.imag, 0.001)
  392. r = c ** -2.0
  393. assert_in_delta(-0.12, r.real, 0.001)
  394. assert_in_delta(-0.16, r.imag, 0.001)
  395. assert_equal(Complex(-3,4), c ** Rational(2))
  396. assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
  397. c ** Rational(-2)) # why failed?
  398. r = c ** Rational(2,3)
  399. assert_in_delta(1.264, r.real, 0.001)
  400. assert_in_delta(1.150, r.imag, 0.001)
  401. r = c ** Rational(-2,3)
  402. assert_in_delta(0.432, r.real, 0.001)
  403. assert_in_delta(-0.393, r.imag, 0.001)
  404. c = Complex(0.0, -888888888888888.0)**8888
  405. assert_not_predicate(c.real, :nan?)
  406. assert_not_predicate(c.imag, :nan?)
  407. end
  408. def test_cmp
  409. assert_nil(Complex(5, 1) <=> Complex(2))
  410. assert_nil(5 <=> Complex(2, 1))
  411. assert_equal(1, Complex(5) <=> Complex(2))
  412. assert_equal(-1, Complex(2) <=> Complex(3))
  413. assert_equal(0, Complex(2) <=> Complex(2))
  414. assert_equal(1, Complex(5) <=> 2)
  415. assert_equal(-1, Complex(2) <=> 3)
  416. assert_equal(0, Complex(2) <=> 2)
  417. end
  418. def test_eqeq
  419. assert_equal(Complex(1), Complex(1,0))
  420. assert_equal(Complex(-1), Complex(-1,0))
  421. assert_not_equal(Complex(1), Complex(2,1))
  422. assert_operator(Complex(2,1), :!=, Complex(1))
  423. assert_not_equal(nil, Complex(1))
  424. assert_not_equal('', Complex(1))
  425. nan = 0.0 / 0
  426. if nan.nan? && nan != nan
  427. assert_not_equal(Complex(nan, 0), Complex(nan, 0))
  428. assert_not_equal(Complex(0, nan), Complex(0, nan))
  429. assert_not_equal(Complex(nan, nan), Complex(nan, nan))
  430. end
  431. end
  432. def test_coerce
  433. assert_equal([Complex(2),Complex(1)], Complex(1).coerce(2))
  434. assert_equal([Complex(2.2),Complex(1)], Complex(1).coerce(2.2))
  435. assert_equal([Complex(Rational(2)),Complex(1)],
  436. Complex(1).coerce(Rational(2)))
  437. assert_equal([Complex(2),Complex(1)], Complex(1).coerce(Complex(2)))
  438. obj = eval("class C\u{1f5ff}; self; end").new
  439. assert_raise_with_message(TypeError, /C\u{1f5ff}/) { Complex(1).coerce(obj) }
  440. end
  441. class ObjectX
  442. def +(x) Rational(1) end
  443. alias - +
  444. alias * +
  445. alias / +
  446. alias quo +
  447. alias ** +
  448. def coerce(x) [x, Complex(1)] end
  449. end
  450. def test_coerce2
  451. x = ObjectX.new
  452. %w(+ - * / quo **).each do |op|
  453. assert_kind_of(Numeric, Complex(1).__send__(op, x))
  454. end
  455. end
  456. def test_math
  457. c = Complex(1,2)
  458. assert_in_delta(2.236, c.abs, 0.001)
  459. assert_in_delta(2.236, c.magnitude, 0.001)
  460. assert_equal(5, c.abs2)
  461. assert_equal(c.abs, Math.sqrt(c * c.conj))
  462. assert_equal(c.abs, Math.sqrt(c.real**2 + c.imag**2))
  463. assert_equal(c.abs2, c * c.conj)
  464. assert_equal(c.abs2, c.real**2 + c.imag**2)
  465. assert_in_delta(1.107, c.arg, 0.001)
  466. assert_in_delta(1.107, c.angle, 0.001)
  467. assert_in_delta(1.107, c.phase, 0.001)
  468. r = c.polar
  469. assert_in_delta(2.236, r[0], 0.001)
  470. assert_in_delta(1.107, r[1], 0.001)
  471. assert_equal(Complex(1,-2), c.conjugate)
  472. assert_equal(Complex(1,-2), c.conj)
  473. assert_equal(Complex(1,2), c.numerator)
  474. assert_equal(1, c.denominator)
  475. end
  476. def test_to_s
  477. c = Complex(1,2)
  478. assert_instance_of(String, c.to_s)
  479. assert_equal('1+2i', c.to_s)
  480. assert_equal('0+2i', Complex(0,2).to_s)
  481. assert_equal('0-2i', Complex(0,-2).to_s)
  482. assert_equal('1+2i', Complex(1,2).to_s)
  483. assert_equal('-1+2i', Complex(-1,2).to_s)
  484. assert_equal('-1-2i', Complex(-1,-2).to_s)
  485. assert_equal('1-2i', Complex(1,-2).to_s)
  486. assert_equal('-1-2i', Complex(-1,-2).to_s)
  487. assert_equal('0+2.0i', Complex(0,2.0).to_s)
  488. assert_equal('0-2.0i', Complex(0,-2.0).to_s)
  489. assert_equal('1.0+2.0i', Complex(1.0,2.0).to_s)
  490. assert_equal('-1.0+2.0i', Complex(-1.0,2.0).to_s)
  491. assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
  492. assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s)
  493. assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
  494. assert_equal('0+2/1i', Complex(0,Rational(2)).to_s)
  495. assert_equal('0-2/1i', Complex(0,Rational(-2)).to_s)
  496. assert_equal('1+2/1i', Complex(1,Rational(2)).to_s)
  497. assert_equal('-1+2/1i', Complex(-1,Rational(2)).to_s)
  498. assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
  499. assert_equal('1-2/1i', Complex(1,Rational(-2)).to_s)
  500. assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
  501. assert_equal('0+2/3i', Complex(0,Rational(2,3)).to_s)
  502. assert_equal('0-2/3i', Complex(0,Rational(-2,3)).to_s)
  503. assert_equal('1+2/3i', Complex(1,Rational(2,3)).to_s)
  504. assert_equal('-1+2/3i', Complex(-1,Rational(2,3)).to_s)
  505. assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
  506. assert_equal('1-2/3i', Complex(1,Rational(-2,3)).to_s)
  507. assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
  508. nan = 0.0 / 0
  509. inf = 1.0 / 0
  510. if nan.nan?
  511. assert_equal('NaN+NaN*i', Complex(nan,nan).to_s)
  512. end
  513. if inf.infinite?
  514. assert_equal('Infinity+Infinity*i', Complex(inf,inf).to_s)
  515. assert_equal('Infinity-Infinity*i', Complex(inf,-inf).to_s)
  516. end
  517. end
  518. def test_inspect
  519. c = Complex(1,2)
  520. assert_instance_of(String, c.inspect)
  521. assert_equal('(1+2i)', c.inspect)
  522. end
  523. def test_marshal
  524. c = Complex(1,2)
  525. s = Marshal.dump(c)
  526. c2 = Marshal.load(s)
  527. assert_equal(c, c2)
  528. assert_instance_of(Complex, c2)
  529. c = Complex(Rational(1,2),Rational(2,3))
  530. s = Marshal.dump(c)
  531. c2 = Marshal.load(s)
  532. assert_equal(c, c2)
  533. assert_instance_of(Complex, c2)
  534. bug3656 = '[ruby-core:31622]'
  535. c = Complex(1,2)
  536. assert_predicate(c, :frozen?)
  537. result = c.marshal_load([2,3]) rescue :fail
  538. assert_equal(:fail, result, bug3656)
  539. assert_equal(Complex(1,2), c)
  540. end
  541. def test_marshal_compatibility
  542. bug6625 = '[ruby-core:45775]'
  543. dump = "\x04\x08o:\x0cComplex\x07:\x0a@reali\x06:\x0b@imagei\x07"
  544. assert_nothing_raised(bug6625) do
  545. assert_equal(Complex(1, 2), Marshal.load(dump), bug6625)
  546. end
  547. end
  548. def test_parse
  549. assert_equal(Complex(5), '5'.to_c)
  550. assert_equal(Complex(-5), '-5'.to_c)
  551. assert_equal(Complex(5,3), '5+3i'.to_c)
  552. assert_equal(Complex(-5,3), '-5+3i'.to_c)
  553. assert_equal(Complex(5,-3), '5-3i'.to_c)
  554. assert_equal(Complex(-5,-3), '-5-3i'.to_c)
  555. assert_equal(Complex(0,3), '3i'.to_c)
  556. assert_equal(Complex(0,-3), '-3i'.to_c)
  557. assert_equal(Complex(5,1), '5+i'.to_c)
  558. assert_equal(Complex(0,1), 'i'.to_c)
  559. assert_equal(Complex(0,1), '+i'.to_c)
  560. assert_equal(Complex(0,-1), '-i'.to_c)
  561. assert_equal(Complex(5,3), '5+3I'.to_c)
  562. assert_equal(Complex(5,3), '5+3j'.to_c)
  563. assert_equal(Complex(5,3), '5+3J'.to_c)
  564. assert_equal(Complex(0,3), '3I'.to_c)
  565. assert_equal(Complex(0,3), '3j'.to_c)
  566. assert_equal(Complex(0,3), '3J'.to_c)
  567. assert_equal(Complex(0,1), 'I'.to_c)
  568. assert_equal(Complex(0,1), 'J'.to_c)
  569. assert_equal(Complex(5.0), '5.0'.to_c)
  570. assert_equal(Complex(-5.0), '-5.0'.to_c)
  571. assert_equal(Complex(5.0,3.0), '5.0+3.0i'.to_c)
  572. assert_equal(Complex(-5.0,3.0), '-5.0+3.0i'.to_c)
  573. assert_equal(Complex(5.0,-3.0), '5.0-3.0i'.to_c)
  574. assert_equal(Complex(-5.0,-3.0), '-5.0-3.0i'.to_c)
  575. assert_equal(Complex(0.0,3.0), '3.0i'.to_c)
  576. assert_equal(Complex(0.0,-3.0), '-3.0i'.to_c)
  577. assert_equal(Complex(5.1), '5.1'.to_c)
  578. assert_equal(Complex(-5.2), '-5.2'.to_c)
  579. assert_equal(Complex(5.3,3.4), '5.3+3.4i'.to_c)
  580. assert_equal(Complex(-5.5,3.6), '-5.5+3.6i'.to_c)
  581. assert_equal(Complex(5.3,-3.4), '5.3-3.4i'.to_c)
  582. assert_equal(Complex(-5.5,-3.6), '-5.5-3.6i'.to_c)
  583. assert_equal(Complex(0.0,3.1), '3.1i'.to_c)
  584. assert_equal(Complex(0.0,-3.2), '-3.2i'.to_c)
  585. assert_equal(Complex(5.0), '5e0'.to_c)
  586. assert_equal(Complex(-5.0), '-5e0'.to_c)
  587. assert_equal(Complex(5.0,3.0), '5e0+3e0i'.to_c)
  588. assert_equal(Complex(-5.0,3.0), '-5e0+3e0i'.to_c)
  589. assert_equal(Complex(5.0,-3.0), '5e0-3e0i'.to_c)
  590. assert_equal(Complex(-5.0,-3.0), '-5e0-3e0i'.to_c)
  591. assert_equal(Complex(0.0,3.0), '3e0i'.to_c)
  592. assert_equal(Complex(0.0,-3.0), '-3e0i'.to_c)
  593. assert_equal(Complex(5e1), '5e1'.to_c)
  594. assert_equal(Complex(-5e2), '-5e2'.to_c)
  595. assert_equal(Complex(5e3,3e4), '5e003+3e4i'.to_c)
  596. assert_equal(Complex(-5e5,3e6), '-5e5+3e006i'.to_c)
  597. assert_equal(Complex(5e3,-3e4), '5e003-3e4i'.to_c)
  598. assert_equal(Complex(-5e5,-3e6), '-5e5-3e006i'.to_c)
  599. assert_equal(Complex(0.0,3e1), '3e1i'.to_c)
  600. assert_equal(Complex(0.0,-3e2), '-3e2i'.to_c)
  601. assert_equal(Complex(0.33), '.33'.to_c)
  602. assert_equal(Complex(0.33), '0.33'.to_c)
  603. assert_equal(Complex(-0.33), '-.33'.to_c)
  604. assert_equal(Complex(-0.33), '-0.33'.to_c)
  605. assert_equal(Complex(-0.33), '-0.3_3'.to_c)
  606. assert_equal(Complex.polar(10,10), '10@10'.to_c)
  607. assert_equal(Complex.polar(-10,-10), '-10@-10'.to_c)
  608. assert_equal(Complex.polar(10.5,10.5), '10.5@10.5'.to_c)
  609. assert_equal(Complex.polar(-10.5,-10.5), '-10.5@-10.5'.to_c)
  610. assert_equal(Complex(5), Complex('5'))
  611. assert_equal(Complex(-5), Complex('-5'))
  612. assert_equal(Complex(5,3), Complex('5+3i'))
  613. assert_equal(Complex(-5,3), Complex('-5+3i'))
  614. assert_equal(Complex(5,-3), Complex('5-3i'))
  615. assert_equal(Complex(-5,-3), Complex('-5-3i'))
  616. assert_equal(Complex(0,3), Complex('3i'))
  617. assert_equal(Complex(0,-3), Complex('-3i'))
  618. assert_equal(Complex(5,1), Complex('5+i'))
  619. assert_equal(Complex(0,1), Complex('i'))
  620. assert_equal(Complex(0,1), Complex('+i'))
  621. assert_equal(Complex(0,-1), Complex('-i'))
  622. assert_equal(Complex(5,3), Complex('5+3I'))
  623. assert_equal(Complex(5,3), Complex('5+3j'))
  624. assert_equal(Complex(5,3), Complex('5+3J'))
  625. assert_equal(Complex(0,3), Complex('3I'))
  626. assert_equal(Complex(0,3), Complex('3j'))
  627. assert_equal(Complex(0,3), Complex('3J'))
  628. assert_equal(Complex(0,1), Complex('I'))
  629. assert_equal(Complex(0,1), Complex('J'))
  630. assert_equal(Complex(5.0), Complex('5.0'))
  631. assert_equal(Complex(-5.0), Complex('-5.0'))
  632. assert_equal(Complex(5.0,3.0), Complex('5.0+3.0i'))
  633. assert_equal(Complex(-5.0,3.0), Complex('-5.0+3.0i'))
  634. assert_equal(Complex(5.0,-3.0), Complex('5.0-3.0i'))
  635. assert_equal(Complex(-5.0,-3.0), Complex('-5.0-3.0i'))
  636. assert_equal(Complex(0.0,3.0), Complex('3.0i'))
  637. assert_equal(Complex(0.0,-3.0), Complex('-3.0i'))
  638. assert_equal(Complex(5.1), Complex('5.1'))
  639. assert_equal(Complex(-5.2), Complex('-5.2'))
  640. assert_equal(Complex(5.3,3.4), Complex('5.3+3.4i'))
  641. assert_equal(Complex(-5.5,3.6), Complex('-5.5+3.6i'))
  642. assert_equal(Complex(5.3,-3.4), Complex('5.3-3.4i'))
  643. assert_equal(Complex(-5.5,-3.6), Complex('-5.5-3.6i'))
  644. assert_equal(Complex(0.0,3.1), Complex('3.1i'))
  645. assert_equal(Complex(0.0,-3.2), Complex('-3.2i'))
  646. assert_equal(Complex(5.0), Complex('5e0'))
  647. assert_equal(Complex(-5.0), Complex('-5e0'))
  648. assert_equal(Complex(5.0,3.0), Complex('5e0+3e0i'))
  649. assert_equal(Complex(-5.0,3.0), Complex('-5e0+3e0i'))
  650. assert_equal(Complex(5.0,-3.0), Complex('5e0-3e0i'))
  651. assert_equal(Complex(-5.0,-3.0), Complex('-5e0-3e0i'))
  652. assert_equal(Complex(0.0,3.0), Complex('3e0i'))
  653. assert_equal(Complex(0.0,-3.0), Complex('-3e0i'))
  654. assert_equal(Complex(5e1), Complex('5e1'))
  655. assert_equal(Complex(-5e2), Complex('-5e2'))
  656. assert_equal(Complex(5e3,3e4), Complex('5e003+3e4i'))
  657. assert_equal(Complex(-5e5,3e6), Complex('-5e5+3e006i'))
  658. assert_equal(Complex(5e3,-3e4), Complex('5e003-3e4i'))
  659. assert_equal(Complex(-5e5,-3e6), Complex('-5e5-3e006i'))
  660. assert_equal(Complex(0.0,3e1), Complex('3e1i'))
  661. assert_equal(Complex(0.0,-3e2), Complex('-3e2i'))
  662. assert_equal(Complex(0.33), Complex('.33'))
  663. assert_equal(Complex(0.33), Complex('0.33'))
  664. assert_equal(Complex(-0.33), Complex('-.33'))
  665. assert_equal(Complex(-0.33), Complex('-0.33'))
  666. assert_equal(Complex(-0.33), Complex('-0.3_3'))
  667. assert_equal(Complex.polar(10,10), Complex('10@10'))
  668. assert_equal(Complex.polar(-10,-10), Complex('-10@-10'))
  669. assert_equal(Complex.polar(10.5,10.5), Complex('10.5@10.5'))
  670. assert_equal(Complex.polar(-10.5,-10.5), Complex('-10.5@-10.5'))
  671. assert_equal(Complex(0), ''.to_c)
  672. assert_equal(Complex(0), ' '.to_c)
  673. assert_equal(Complex(5), "\f\n\r\t\v5\0".to_c)
  674. assert_equal(Complex(0), '_'.to_c)
  675. assert_equal(Complex(0), '_5'.to_c)
  676. assert_equal(Complex(5), '5_'.to_c)
  677. assert_equal(Complex(5), '5x'.to_c)
  678. assert_equal(Complex(5), '5+_3i'.to_c)
  679. assert_equal(Complex(5), '5+3_i'.to_c)
  680. assert_equal(Complex(5,3), '5+3i_'.to_c)
  681. assert_equal(Complex(5,3), '5+3ix'.to_c)
  682. assert_raise(ArgumentError){ Complex('')}
  683. assert_raise(ArgumentError){ Complex('_')}
  684. assert_raise(ArgumentError){ Complex("\f\n\r\t\v5\0")}
  685. assert_raise(ArgumentError){ Complex('_5')}
  686. assert_raise(ArgumentError){ Complex('5_')}
  687. assert_raise(ArgumentError){ Complex('5x')}
  688. assert_raise(ArgumentError){ Complex('5+_3i')}
  689. assert_raise(ArgumentError){ Complex('5+3_i')}
  690. assert_raise(ArgumentError){ Complex('5+3i_')}
  691. assert_raise(ArgumentError){ Complex('5+3ix')}
  692. assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
  693. assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c)
  694. assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c)
  695. assert_equal(Complex(Rational(1,5),-3), '1/5-3i'.to_c)
  696. assert_equal(Complex(Rational(-1,5),3), '-1/5+3i'.to_c)
  697. assert_equal(Complex(Rational(-1,5),-3), '-1/5-3i'.to_c)
  698. assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
  699. assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
  700. assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
  701. assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
  702. assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
  703. assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
  704. assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
  705. assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
  706. assert_equal(Complex.polar(Rational(1,5),Rational(3,2)), Complex('1/5@3/2'))
  707. assert_equal(Complex.polar(Rational(-1,5),Rational(-3,2)), Complex('-1/5@-3/2'))
  708. end
  709. def test_Complex_with_invalid_exception
  710. assert_raise(ArgumentError) {
  711. Complex("0", exception: 1)
  712. }
  713. end
  714. def test_Complex_without_exception
  715. assert_nothing_raised(ArgumentError){
  716. assert_equal(nil, Complex('5x', exception: false))
  717. }
  718. assert_nothing_raised(ArgumentError){
  719. assert_equal(nil, Complex(nil, exception: false))
  720. }
  721. assert_nothing_raised(ArgumentError){
  722. assert_equal(nil, Complex(Object.new, exception: false))
  723. }
  724. assert_nothing_raised(ArgumentError){
  725. assert_equal(nil, Complex(1, nil, exception: false))
  726. }
  727. assert_nothing_raised(ArgumentError){
  728. assert_equal(nil, Complex(1, Object.new, exception: false))
  729. }
  730. o = Object.new
  731. def o.to_c; raise; end
  732. assert_nothing_raised(ArgumentError){
  733. assert_equal(nil, Complex(o, exception: false))
  734. }
  735. assert_nothing_raised(ArgumentError){
  736. assert_equal(nil, Complex(1, o, exception: false))
  737. }
  738. end
  739. def test_respond
  740. c = Complex(1,1)
  741. assert_not_respond_to(c, :%)
  742. assert_not_respond_to(c, :div)
  743. assert_not_respond_to(c, :divmod)
  744. assert_not_respond_to(c, :floor)
  745. assert_not_respond_to(c, :ceil)
  746. assert_not_respond_to(c, :modulo)
  747. assert_not_respond_to(c, :remainder)
  748. assert_not_respond_to(c, :round)
  749. assert_not_respond_to(c, :step)
  750. assert_not_respond_to(c, :tunrcate)
  751. assert_not_respond_to(c, :positive?)
  752. assert_not_respond_to(c, :negative?)
  753. assert_not_respond_to(c, :sign)
  754. assert_not_respond_to(c, :quotient)
  755. assert_not_respond_to(c, :quot)
  756. assert_not_respond_to(c, :quotrem)
  757. assert_not_respond_to(c, :gcd)
  758. assert_not_respond_to(c, :lcm)
  759. assert_not_respond_to(c, :gcdlcm)
  760. (Comparable.instance_methods(false) - Complex.instance_methods(false)).each do |n|
  761. assert_not_respond_to(c, n, "Complex##{n}")
  762. end
  763. end
  764. def test_to_i
  765. assert_equal(3, Complex(3).to_i)
  766. assert_equal(3, Integer(Complex(3)))
  767. assert_raise(RangeError){Complex(3,2).to_i}
  768. assert_raise(RangeError){Integer(Complex(3,2))}
  769. end
  770. def test_to_f
  771. assert_equal(3.0, Complex(3).to_f)
  772. assert_equal(3.0, Float(Complex(3)))
  773. assert_raise(RangeError){Complex(3,2).to_f}
  774. assert_raise(RangeError){Float(Complex(3,2))}
  775. end
  776. def test_to_r
  777. assert_equal(Rational(3), Complex(3).to_r)
  778. assert_equal(Rational(3), Rational(Complex(3)))
  779. assert_raise(RangeError){Complex(3,2).to_r}
  780. assert_raise(RangeError){Rational(Complex(3,2))}
  781. end
  782. def test_to_c
  783. c = nil.to_c
  784. assert_equal([0,0], [c.real, c.imag])
  785. c = 0.to_c
  786. assert_equal([0,0], [c.real, c.imag])
  787. c = 1.to_c
  788. assert_equal([1,0], [c.real, c.imag])
  789. c = 1.1.to_c
  790. assert_equal([1.1, 0], [c.real, c.imag])
  791. c = Rational(1,2).to_c
  792. assert_equal([Rational(1,2), 0], [c.real, c.imag])
  793. c = Complex(1,2).to_c
  794. assert_equal([1, 2], [c.real, c.imag])
  795. if (0.0/0).nan?
  796. assert_nothing_raised{(0.0/0).to_c}
  797. end
  798. if (1.0/0).infinite?
  799. assert_nothing_raised{(1.0/0).to_c}
  800. end
  801. end
  802. def test_finite_p
  803. assert_predicate(1+1i, :finite?)
  804. assert_predicate(1-1i, :finite?)
  805. assert_predicate(-1+1i, :finite?)
  806. assert_predicate(-1-1i, :finite?)
  807. assert_not_predicate(Float::INFINITY + 1i, :finite?)
  808. assert_not_predicate(Complex(1, Float::INFINITY), :finite?)
  809. assert_predicate(Complex(Float::MAX, 0.0), :finite?)
  810. assert_predicate(Complex(0.0, Float::MAX), :finite?)
  811. assert_predicate(Complex(Float::MAX, Float::MAX), :finite?)
  812. assert_not_predicate(Complex(Float::NAN, 0), :finite?)
  813. assert_not_predicate(Complex(0, Float::NAN), :finite?)
  814. assert_not_predicate(Complex(Float::NAN, Float::NAN), :finite?)
  815. end
  816. def test_infinite_p
  817. assert_nil((1+1i).infinite?)
  818. assert_nil((1-1i).infinite?)
  819. assert_nil((-1+1i).infinite?)
  820. assert_nil((-1-1i).infinite?)
  821. assert_equal(1, (Float::INFINITY + 1i).infinite?)
  822. assert_equal(1, (Float::INFINITY - 1i).infinite?)
  823. assert_equal(1, (-Float::INFINITY + 1i).infinite?)
  824. assert_equal(1, (-Float::INFINITY - 1i).infinite?)
  825. assert_equal(1, Complex(1, Float::INFINITY).infinite?)
  826. assert_equal(1, Complex(-1, Float::INFINITY).infinite?)
  827. assert_equal(1, Complex(1, -Float::INFINITY).infinite?)
  828. assert_equal(1, Complex(-1, -Float::INFINITY).infinite?)
  829. assert_nil(Complex(Float::MAX, 0.0).infinite?)
  830. assert_nil(Complex(0.0, Float::MAX).infinite?)
  831. assert_nil(Complex(Float::MAX, Float::MAX).infinite?)
  832. assert_nil(Complex(Float::NAN, 0).infinite?)
  833. assert_nil(Complex(0, Float::NAN).infinite?)
  834. assert_nil(Complex(Float::NAN, Float::NAN).infinite?)
  835. end
  836. def test_supp
  837. assert_predicate(1, :real?)
  838. assert_predicate(1.1, :real?)
  839. assert_equal(1, 1.real)
  840. assert_equal(0, 1.imag)
  841. assert_equal(0, 1.imaginary)
  842. assert_equal(1.1, 1.1.real)
  843. assert_equal(0, 1.1.imag)
  844. assert_equal(0, 1.1.imaginary)
  845. assert_equal(1, 1.magnitude)
  846. assert_equal(1, -1.magnitude)
  847. assert_equal(1, 1.0.magnitude)
  848. assert_equal(1, -1.0.magnitude)
  849. assert_equal(4, 2.abs2)
  850. assert_equal(4, -2.abs2)
  851. assert_equal(4.0, 2.0.abs2)
  852. assert_equal(4.0, -2.0.abs2)
  853. assert_equal(0, 1.arg)
  854. assert_equal(0, 1.angle)
  855. assert_equal(0, 1.phase)
  856. assert_equal(0, 1.0.arg)
  857. assert_equal(0, 1.0.angle)
  858. assert_equal(0, 1.0.phase)
  859. if (0.0/0).nan?
  860. nan = 0.0/0
  861. assert_same(nan, nan.arg)
  862. assert_same(nan, nan.angle)
  863. assert_same(nan, nan.phase)
  864. end
  865. assert_equal(Math::PI, -1.arg)
  866. assert_equal(Math::PI, -1.angle)
  867. assert_equal(Math::PI, -1.phase)
  868. assert_equal(Math::PI, -1.0.arg)
  869. assert_equal(Math::PI, -1.0.angle)
  870. assert_equal(Math::PI, -1.0.phase)
  871. assert_equal([1,0], 1.rect)
  872. assert_equal([-1,0], -1.rect)
  873. assert_equal([1,0], 1.rectangular)
  874. assert_equal([-1,0], -1.rectangular)
  875. assert_equal([1.0,0], 1.0.rect)
  876. assert_equal([-1.0,0], -1.0.rect)
  877. assert_equal([1.0,0], 1.0.rectangular)
  878. assert_equal([-1.0,0], -1.0.rectangular)
  879. assert_equal([1,0], 1.polar)
  880. assert_equal([1, Math::PI], -1.polar)
  881. assert_equal([1.0,0], 1.0.polar)
  882. assert_equal([1.0, Math::PI], -1.0.polar)
  883. assert_equal(1, 1.conjugate)
  884. assert_equal(-1, -1.conjugate)
  885. assert_equal(1, 1.conj)
  886. assert_equal(-1, -1.conj)
  887. assert_equal(1.1, 1.1.conjugate)
  888. assert_equal(-1.1, -1.1.conjugate)
  889. assert_equal(1.1, 1.1.conj)
  890. assert_equal(-1.1, -1.1.conj)
  891. assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
  892. assert_equal(0.5, 1.fdiv(2))
  893. assert_equal(5000000000.0, 10000000000.fdiv(2))
  894. assert_equal(0.5, 1.0.fdiv(2))
  895. assert_equal(0.25, Rational(1,2).fdiv(2))
  896. assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
  897. end
  898. def test_ruby19
  899. assert_raise(NoMethodError){ Complex.new(1) }
  900. assert_raise(NoMethodError){ Complex.new!(1) }
  901. assert_raise(NoMethodError){ Complex.reduce(1) }
  902. end
  903. def test_fixed_bug
  904. assert_equal(Complex(1), 1 ** Complex(1))
  905. assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s)
  906. assert_in_delta(Math::PI, Complex(-0.0).arg, 0.001)
  907. assert_equal(Complex(2e3, 2e4), '2e3+2e4i'.to_c)
  908. assert_raise(ArgumentError){ Complex('--8i')}
  909. end
  910. def test_known_bug
  911. end
  912. def test_canonicalize_internal
  913. obj = Class.new(Numeric) do
  914. attr_accessor :real
  915. alias real? real
  916. end.new
  917. obj.real = true
  918. c = Complex.rect(obj, 1);
  919. obj.real = false
  920. c = c.conj
  921. assert_equal(obj, c.real)
  922. assert_equal(-1, c.imag)
  923. end
  924. def test_canonicalize_polar
  925. obj = Class.new(Numeric) do
  926. def initialize
  927. @x = 2
  928. end
  929. def real?
  930. (@x -= 1) > 0
  931. end
  932. end.new
  933. assert_raise(TypeError) do
  934. Complex.polar(1, obj)
  935. end
  936. end
  937. end