MathTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // MathTest.cs
  2. //
  3. // Jon Guymon ([email protected])
  4. //
  5. // (C) 2002 Jon Guymon
  6. //
  7. using System;
  8. using NUnit.Framework;
  9. namespace MonoTests.System
  10. {
  11. public class MathTest : TestCase {
  12. public MathTest() {}
  13. protected override void SetUp() {}
  14. protected override void TearDown() {}
  15. static double x = 0.1234;
  16. static double y = 12.345;
  17. public void TestDecimalAbs() {
  18. decimal a = -9.0M;
  19. Assert(9.0M == Math.Abs(a));
  20. }
  21. public void TestDoubleAbs() {
  22. double a = -9.0D;
  23. Assert(9.0D == Math.Abs(a));
  24. }
  25. public void TestFloatAbs() {
  26. float a = -9.0F;
  27. Assert(9.0F == Math.Abs(a));
  28. }
  29. public void TestLongAbs() {
  30. long a = -9L;
  31. long b = Int64.MinValue;
  32. Assert(9L == Math.Abs(a));
  33. try {
  34. Math.Abs(b);
  35. Fail("Should raise System.OverflowException");
  36. } catch(Exception e) {
  37. Assert(typeof(OverflowException) == e.GetType());
  38. }
  39. }
  40. public void TestIntAbs() {
  41. int a = -9;
  42. int b = Int32.MinValue;
  43. Assert(9 == Math.Abs(a));
  44. try {
  45. Math.Abs(b);
  46. Fail("Should raise System.OverflowException");
  47. } catch(Exception e) {
  48. Assert(typeof(OverflowException) == e.GetType());
  49. }
  50. }
  51. public void TestSbyteAbs() {
  52. sbyte a = -9;
  53. sbyte b = SByte.MinValue;
  54. Assert(9 == Math.Abs(a));
  55. try {
  56. Math.Abs(b);
  57. Fail("Should raise System.OverflowException");
  58. } catch(Exception e) {
  59. Assert(typeof(OverflowException) == e.GetType());
  60. }
  61. }
  62. public void TestShortAbs() {
  63. short a = -9;
  64. short b = Int16.MinValue;
  65. Assert(9 == Math.Abs(a));
  66. try {
  67. Math.Abs(b);
  68. Fail("Should raise System.OverflowException");
  69. } catch(Exception e) {
  70. Assert(typeof(OverflowException) == e.GetType());
  71. }
  72. }
  73. public void TestAcos() {
  74. double a = Math.Acos(x);
  75. double b = 1.4470809809523457;
  76. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  77. (Math.Abs(a - b) <= double.Epsilon));
  78. Assert(double.IsNaN(Math.Acos(-1.01D)));
  79. Assert(double.IsNaN(Math.Acos(1.01D)));
  80. }
  81. public void TestAsin() {
  82. double a = Math.Asin(x);
  83. double b = 0.12371534584255098;
  84. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  85. (Math.Abs(a - b) <= double.Epsilon));
  86. Assert(double.IsNaN(Math.Asin(-1.01D)));
  87. Assert(double.IsNaN(Math.Asin(1.01D)));
  88. }
  89. public void TestAtan() {
  90. double a = Math.Atan(x);
  91. double b = 0.12277930094473837;
  92. double c = 1.5707963267948966;
  93. double d = -1.5707963267948966;
  94. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  95. (Math.Abs(a - b) <= double.Epsilon));
  96. Assert("should return NaN",
  97. double.IsNaN(Math.Atan(double.NaN)));
  98. Assert(Math.Atan(double.PositiveInfinity).ToString("G99")+" != "+c.ToString("G99"),
  99. Math.Abs((double)Math.Atan(double.PositiveInfinity) - c) <= double.Epsilon);
  100. Assert(Math.Atan(double.NegativeInfinity).ToString("G99")+" != "+d.ToString("G99"),
  101. Math.Abs((double)Math.Atan(double.NegativeInfinity) - d) <= double.Epsilon);
  102. }
  103. public void TestAtan2() {
  104. double a = Math.Atan2(x, y);
  105. double b = 0.0099956168687207747;
  106. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  107. (Math.Abs(a - b) <= double.Epsilon));
  108. Assert(double.IsNaN(Math.Acos(-2D)));
  109. Assert(double.IsNaN(Math.Acos(2D)));
  110. }
  111. public void TestCos() {
  112. double a = Math.Cos(x);
  113. double b = 0.99239587670489104;
  114. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  115. (Math.Abs(a - b) <= double.Epsilon));
  116. }
  117. public void TestCosh() {
  118. double a = Math.Cosh(x);
  119. double b = 1.0076234465130722;
  120. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  121. (Math.Abs(a - b) <= double.Epsilon));
  122. Assert(Math.Cosh(double.NegativeInfinity) == double.PositiveInfinity);
  123. Assert(Math.Cosh(double.PositiveInfinity) == double.PositiveInfinity);
  124. Assert(double.IsNaN(Math.Cosh(double.NaN)));
  125. }
  126. public void TestSin() {
  127. double a = Math.Sin(x);
  128. double b = 0.12308705821137626;
  129. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  130. (Math.Abs(a - b) <= double.Epsilon));
  131. }
  132. public void TestSinh() {
  133. double a = Math.Sinh(x);
  134. double b = 0.12371341868561381;
  135. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  136. (Math.Abs(a - b) <= double.Epsilon));
  137. }
  138. public void TestTan() {
  139. double a = Math.Tan(x);
  140. double b = 0.12403019913793806;
  141. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  142. (Math.Abs(a - b) <= double.Epsilon));
  143. }
  144. public void TestTanh() {
  145. double a = Math.Tanh(x);
  146. double b = 0.12277743150353424;
  147. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  148. (Math.Abs(a - b) <= double.Epsilon));
  149. }
  150. public void TestSqrt() {
  151. double a = Math.Sqrt(x);
  152. double b = 0.35128336140500593;
  153. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  154. (Math.Abs(a - b) <= double.Epsilon));
  155. }
  156. public void TestExp() {
  157. double a = Math.Exp(x);
  158. double b = 1.1313368651986859;
  159. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  160. (Math.Abs(a - b) <= double.Epsilon));
  161. Assert(double.IsNaN(Math.Exp(double.NaN)));
  162. Assert(Math.Exp(double.NegativeInfinity) == 0);
  163. Assert(Math.Exp(double.PositiveInfinity) == double.PositiveInfinity);
  164. }
  165. public void TestCeiling() {
  166. int iTest = 1;
  167. try {
  168. double a = Math.Ceiling(1.5);
  169. double b = 2;
  170. iTest++;
  171. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  172. (Math.Abs(a - b) <= double.Epsilon));
  173. iTest++;
  174. Assert(Math.Ceiling(double.NegativeInfinity) == double.NegativeInfinity);
  175. iTest++;
  176. Assert(Math.Ceiling(double.PositiveInfinity) == double.PositiveInfinity);
  177. iTest++;
  178. Assert(double.IsNaN(Math.Ceiling(double.NaN)));
  179. } catch (Exception e) {
  180. Fail("Unexpected Exception at iTest=" + iTest + ": " + e);
  181. }
  182. }
  183. public void TestFloor() {
  184. try {
  185. double a = Math.Floor(1.5);
  186. double b = 1;
  187. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  188. (Math.Abs(a - b) <= double.Epsilon));
  189. Assert(Math.Floor(double.NegativeInfinity) == double.NegativeInfinity);
  190. Assert(Math.Floor(double.PositiveInfinity) == double.PositiveInfinity);
  191. Assert(double.IsNaN(Math.Floor(double.NaN)));
  192. } catch (Exception e) {
  193. Fail("Unexpected Exception: " + e.ToString());
  194. }
  195. }
  196. public void TestIEEERemainder() {
  197. double a = Math.IEEERemainder(y, x);
  198. double b = 0.0050000000000007816;
  199. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  200. (Math.Abs(a - b) <= double.Epsilon));
  201. Assert(double.IsNaN(Math.IEEERemainder(y, 0)));
  202. }
  203. public void TestLog() {
  204. double a = Math.Log(y);
  205. double b = 2.513251122797143;
  206. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  207. (Math.Abs(a - b) <= double.Epsilon));
  208. Assert(double.IsNaN(Math.Log(-1)));
  209. Assert(double.IsNaN(Math.Log(double.NaN)));
  210. // MS docs say this should be PositiveInfinity
  211. Assert(Math.Log(0) == double.NegativeInfinity);
  212. Assert(Math.Log(double.PositiveInfinity) == double.PositiveInfinity);
  213. }
  214. public void TestLog2() {
  215. double a = Math.Log(x, y);
  216. double b = -0.83251695325303621;
  217. Assert(a + " != " + b + " because diff is " + Math.Abs(a - b), (Math.Abs(a - b) <= 1e-14));
  218. Assert(double.IsNaN(Math.Log(-1, y)));
  219. Assert(double.IsNaN(Math.Log(double.NaN, y)));
  220. Assert(double.IsNaN(Math.Log(x, double.NaN)));
  221. Assert(double.IsNaN(Math.Log(double.NegativeInfinity, y)));
  222. Assert(double.IsNaN(Math.Log(x, double.NegativeInfinity)));
  223. Assert(double.IsNaN(Math.Log(double.PositiveInfinity, double.PositiveInfinity)));
  224. // MS docs say this should be PositiveInfinity
  225. Assert(Math.Log(0, y) == double.NegativeInfinity);
  226. Assert(Math.Log(double.PositiveInfinity, y) == double.PositiveInfinity);
  227. Assert(Math.Log(x, double.PositiveInfinity) == 0);
  228. }
  229. public void TestLog10() {
  230. double a = Math.Log10(x);
  231. double b = -0.90868484030277719;
  232. Assert(a.ToString("G99") + " != " + b.ToString("G99"),
  233. (Math.Abs(a - b) <= double.Epsilon));
  234. Assert(double.IsNaN(Math.Log10(-1)));
  235. Assert(double.IsNaN(Math.Log10(double.NaN)));
  236. // MS docs say this should be PositiveInfinity
  237. Assert(Math.Log10(0) == double.NegativeInfinity);
  238. Assert(Math.Log10(double.PositiveInfinity) == double.PositiveInfinity);
  239. }
  240. public void TestPow() {
  241. int iTest = 1;
  242. try {
  243. double a = Math.Pow(y, x);
  244. double b = 1.363609446060212;
  245. Assert(a.ToString("G99") + " != " + b.ToString("G99"), (Math.Abs(a - b) <= double.Epsilon));
  246. iTest++;
  247. Assert (double.IsNaN(Math.Pow(y, double.NaN)));
  248. iTest++;
  249. Assert (double.IsNaN(Math.Pow(double.NaN, x)));
  250. iTest++;
  251. Assert ("Math.Pow(double.NegativeInfinity, 1) should be NegativeInfinity", double.IsNegativeInfinity(Math.Pow(double.NegativeInfinity, 1)));
  252. iTest++;
  253. Assert ("Math.Pow(double.NegativeInfinity, 2) should be PositiveInfinity", double.IsPositiveInfinity(Math.Pow(double.NegativeInfinity, 2)));
  254. // MS docs say this should be 0
  255. iTest++;
  256. Assert(double.IsNaN(Math.Pow(1, double.NegativeInfinity)));
  257. iTest++;
  258. AssertEquals ("Math.Pow(double.PositiveInfinity, double.NegativeInfinity)", (double)0, Math.Pow(double.PositiveInfinity, double.NegativeInfinity));
  259. iTest++;
  260. Assert ("Math.Pow(double.PositiveInfinity, 1) should be PositiveInfinity", double.IsPositiveInfinity(Math.Pow(double.PositiveInfinity, 1)));
  261. // MS docs say this should be PositiveInfinity
  262. iTest++;
  263. Assert ("Math.Pow(1, double.PositiveInfinity) should be NaN", double.IsNaN(Math.Pow(1, double.PositiveInfinity)));
  264. } catch (Exception e) {
  265. Fail ("Unexpected exception at iTest=" + iTest + ". e=" + e);
  266. }
  267. }
  268. public void TestByteMax() {
  269. byte a = 1;
  270. byte b = 2;
  271. Assert(b == Math.Max(a, b));
  272. Assert(b == Math.Max(b, a));
  273. }
  274. public void TestDecimalMax() {
  275. decimal a = 1.5M;
  276. decimal b = 2.5M;
  277. Assert(b == Math.Max(a, b));
  278. Assert(b == Math.Max(b, a));
  279. }
  280. public void TestDoubleMax() {
  281. double a = 1.5D;
  282. double b = 2.5D;
  283. Assert(b == Math.Max(a, b));
  284. Assert(b == Math.Max(b, a));
  285. }
  286. public void TestFloatMax() {
  287. float a = 1.5F;
  288. float b = 2.5F;
  289. Assert(b == Math.Max(a, b));
  290. Assert(b == Math.Max(b, a));
  291. }
  292. public void TestIntMax() {
  293. int a = 1;
  294. int b = 2;
  295. Assert(b == Math.Max(a, b));
  296. Assert(b == Math.Max(b, a));
  297. }
  298. public void TestLongMax() {
  299. long a = 1L;
  300. long b = 2L;
  301. Assert(b == Math.Max(a, b));
  302. Assert(b == Math.Max(b, a));
  303. }
  304. public void TestSbyteMax() {
  305. sbyte a = 1;
  306. sbyte b = 2;
  307. Assert(b == Math.Max(a, b));
  308. Assert(b == Math.Max(b, a));
  309. }
  310. public void TestShortMax() {
  311. short a = 1;
  312. short b = 2;
  313. Assert(b == Math.Max(a, b));
  314. Assert(b == Math.Max(b, a));
  315. }
  316. public void TestUintMax() {
  317. uint a = 1U;
  318. uint b = 2U;
  319. Assert(b == Math.Max(a, b));
  320. Assert(b == Math.Max(b, a));
  321. }
  322. public void TestUlongMax() {
  323. ulong a = 1UL;
  324. ulong b = 2UL;
  325. Assert(b == Math.Max(a, b));
  326. Assert(b == Math.Max(b, a));
  327. }
  328. public void TestUshortMax() {
  329. ushort a = 1;
  330. ushort b = 2;
  331. Assert(b == Math.Max(a, b));
  332. Assert(b == Math.Max(b, a));
  333. }
  334. public void TestByteMin() {
  335. byte a = 1;
  336. byte b = 2;
  337. Assert(a == Math.Min(a, b));
  338. Assert(a == Math.Min(b, a));
  339. }
  340. public void TestDecimalMin() {
  341. decimal a = 1.5M;
  342. decimal b = 2.5M;
  343. Assert(a == Math.Min(a, b));
  344. Assert(a == Math.Min(b, a));
  345. }
  346. public void TestDoubleMin() {
  347. double a = 1.5D;
  348. double b = 2.5D;
  349. Assert(a == Math.Min(a, b));
  350. Assert(a == Math.Min(b, a));
  351. }
  352. public void TestFloatMin() {
  353. float a = 1.5F;
  354. float b = 2.5F;
  355. Assert(a == Math.Min(a, b));
  356. Assert(a == Math.Min(b, a));
  357. }
  358. public void TestIntMin() {
  359. int a = 1;
  360. int b = 2;
  361. Assert(a == Math.Min(a, b));
  362. Assert(a == Math.Min(b, a));
  363. }
  364. public void TestLongMin() {
  365. long a = 1L;
  366. long b = 2L;
  367. Assert(a == Math.Min(a, b));
  368. Assert(a == Math.Min(b, a));
  369. }
  370. public void TestSbyteMin() {
  371. sbyte a = 1;
  372. sbyte b = 2;
  373. Assert(a == Math.Min(a, b));
  374. Assert(a == Math.Min(b, a));
  375. }
  376. public void TestShortMin() {
  377. short a = 1;
  378. short b = 2;
  379. Assert(a == Math.Min(a, b));
  380. Assert(a == Math.Min(b, a));
  381. }
  382. public void TestUintMin() {
  383. uint a = 1U;
  384. uint b = 2U;
  385. Assert(a == Math.Min(a, b));
  386. Assert(a == Math.Min(b, a));
  387. }
  388. public void TestUlongMin() {
  389. ulong a = 1UL;
  390. ulong b = 2UL;
  391. Assert(a == Math.Min(a, b));
  392. Assert(a == Math.Min(b, a));
  393. }
  394. public void TestUshortMin() {
  395. ushort a = 1;
  396. ushort b = 2;
  397. Assert(a == Math.Min(a, b));
  398. Assert(a == Math.Min(b, a));
  399. }
  400. public void TestDecimalRound() {
  401. decimal a = 1.5M;
  402. decimal b = 2.5M;
  403. Assert(Math.Round(a) + " != 2", Math.Round(a) == 2);
  404. Assert(Math.Round(b) + " != 2", Math.Round(b) == 2);
  405. }
  406. public void TestDecimalRound2() {
  407. decimal a = 3.45M;
  408. decimal b = 3.46M;
  409. AssertEquals ("Should round down", Math.Round(a, 1), 3.4M);
  410. AssertEquals ("Should round up", Math.Round(b, 1), 3.5M);
  411. }
  412. public void TestDoubleRound() {
  413. double a = 1.5D;
  414. double b = 2.5D;
  415. AssertEquals ("Should round up", Math.Round(a), 2D);
  416. AssertEquals ("Should round down", Math.Round(b), 2D);
  417. }
  418. public void TestDoubleRound2() {
  419. double a = 3.45D;
  420. double b = 3.46D;
  421. AssertEquals ("Should round down", Math.Round(a, 1), 3.4D);
  422. AssertEquals ("Should round up", Math.Round(b, 1), 3.5D);
  423. }
  424. public void TestDecimalSign() {
  425. decimal a = -5M;
  426. decimal b = 5M;
  427. Assert(Math.Sign(a) == -1);
  428. Assert(Math.Sign(b) == 1);
  429. Assert(Math.Sign(0M) == 0);
  430. }
  431. public void TestDoubleSign() {
  432. double a = -5D;
  433. double b = 5D;
  434. Assert(Math.Sign(a) == -1);
  435. Assert(Math.Sign(b) == 1);
  436. Assert(Math.Sign(0D) == 0);
  437. }
  438. public void TestFloatSign() {
  439. float a = -5F;
  440. float b = 5F;
  441. Assert(Math.Sign(a) == -1);
  442. Assert(Math.Sign(b) == 1);
  443. Assert(Math.Sign(0F) == 0);
  444. }
  445. public void TestIntSign() {
  446. int a = -5;
  447. int b = 5;
  448. Assert(Math.Sign(a) == -1);
  449. Assert(Math.Sign(b) == 1);
  450. }
  451. public void TestLongSign() {
  452. long a = -5L;
  453. long b = 5L;
  454. Assert(Math.Sign(a) == -1);
  455. Assert(Math.Sign(b) == 1);
  456. Assert(Math.Sign(0L) == 0);
  457. }
  458. public void TestSbyteSign() {
  459. sbyte a = -5;
  460. sbyte b = 5;
  461. Assert(Math.Sign(a) == -1);
  462. Assert(Math.Sign(b) == 1);
  463. Assert(Math.Sign(0) == 0);
  464. }
  465. public void TestShortSign() {
  466. short a = -5;
  467. short b = 5;
  468. Assert(Math.Sign(a) == -1);
  469. Assert(Math.Sign(b) == 1);
  470. Assert(Math.Sign(0) == 0);
  471. }
  472. }
  473. }