PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/test_ext_math.cpp

https://github.com/kevlund/hiphop-php
C++ | 399 lines | 325 code | 57 blank | 17 comment | 1 complexity | c9025dd32e6727ae63e597b9eb7ee8eb MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <test/test_ext_math.h>
  17. #include <runtime/ext/ext_math.h>
  18. ///////////////////////////////////////////////////////////////////////////////
  19. bool TestExtMath::RunTests(const std::string &which) {
  20. bool ret = true;
  21. RUN_TEST(test_pi);
  22. RUN_TEST(test_min);
  23. RUN_TEST(test_max);
  24. RUN_TEST(test_abs);
  25. RUN_TEST(test_is_finite);
  26. RUN_TEST(test_is_infinite);
  27. RUN_TEST(test_is_nan);
  28. RUN_TEST(test_ceil);
  29. RUN_TEST(test_floor);
  30. RUN_TEST(test_round);
  31. RUN_TEST(test_deg2rad);
  32. RUN_TEST(test_rad2deg);
  33. RUN_TEST(test_decbin);
  34. RUN_TEST(test_dechex);
  35. RUN_TEST(test_decoct);
  36. RUN_TEST(test_bindec);
  37. RUN_TEST(test_hexdec);
  38. RUN_TEST(test_octdec);
  39. RUN_TEST(test_base_convert);
  40. RUN_TEST(test_pow);
  41. RUN_TEST(test_exp);
  42. RUN_TEST(test_expm1);
  43. RUN_TEST(test_log10);
  44. RUN_TEST(test_log1p);
  45. RUN_TEST(test_log);
  46. RUN_TEST(test_cos);
  47. RUN_TEST(test_cosh);
  48. RUN_TEST(test_sin);
  49. RUN_TEST(test_sinh);
  50. RUN_TEST(test_tan);
  51. RUN_TEST(test_tanh);
  52. RUN_TEST(test_acos);
  53. RUN_TEST(test_acosh);
  54. RUN_TEST(test_asin);
  55. RUN_TEST(test_asinh);
  56. RUN_TEST(test_atan);
  57. RUN_TEST(test_atanh);
  58. RUN_TEST(test_atan2);
  59. RUN_TEST(test_hypot);
  60. RUN_TEST(test_fmod);
  61. RUN_TEST(test_sqrt);
  62. RUN_TEST(test_getrandmax);
  63. RUN_TEST(test_srand);
  64. RUN_TEST(test_rand);
  65. RUN_TEST(test_mt_getrandmax);
  66. RUN_TEST(test_mt_srand);
  67. RUN_TEST(test_mt_rand);
  68. RUN_TEST(test_lcg_value);
  69. return ret;
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. bool TestExtMath::test_pi() {
  73. VC(f_pi(), k_M_PI);
  74. return Count(true);
  75. }
  76. bool TestExtMath::test_min() {
  77. VS(f_min(4, 2, CREATE_VECTOR4(3, 1, 6, 7)), 1);
  78. VS(f_min(0, CREATE_VECTOR3(2, 4, 5)), 2);
  79. VS(f_min(1, 0, CREATE_VECTOR1("hello")), 0);
  80. VS(f_min(1, "hello", CREATE_VECTOR1(0)), "hello");
  81. VS(f_min(1, "hello", CREATE_VECTOR1(-1)), -1);
  82. VS(f_min(1, CREATE_VECTOR3(2, 4, 8),
  83. CREATE_VECTOR1(CREATE_VECTOR3(2, 5, 1))),
  84. CREATE_VECTOR3(2, 4, 8));
  85. VS(f_min(1, "string", CREATE_VECTOR2(CREATE_VECTOR3(2, 5, 7), 42)),
  86. "string");
  87. VS(f_min(1, CREATE_MAP1(1, "1236150163")), "1236150163");
  88. return Count(true);
  89. }
  90. bool TestExtMath::test_max() {
  91. VS(f_max(4, 2, CREATE_VECTOR4(3, 1, 6, 7)), 7);
  92. VS(f_max(0, CREATE_VECTOR3(2, 4, 5)), 5);
  93. VS(f_max(1, 0, CREATE_VECTOR1("hello")), 0);
  94. VS(f_max(1, "hello", CREATE_VECTOR1(0)), "hello");
  95. VS(f_max(1, "hello", CREATE_VECTOR1(-1)), "hello");
  96. VS(f_max(1, CREATE_VECTOR3(2, 4, 8),
  97. CREATE_VECTOR1(CREATE_VECTOR3(2, 5, 1))),
  98. CREATE_VECTOR3(2, 5, 1));
  99. VS(f_max(1, "string", CREATE_VECTOR2(CREATE_VECTOR3(2, 5, 7), 42)),
  100. CREATE_VECTOR3(2, 5, 7));
  101. VS(f_max(1, CREATE_MAP1(1, "1236150163")), "1236150163");
  102. return Count(true);
  103. }
  104. bool TestExtMath::test_abs() {
  105. VS(f_abs(-4.2), 4.2);
  106. VS(f_abs(5), 5);
  107. VS(f_abs(-5), 5);
  108. VS(f_abs("-4.2"), 4.2);
  109. VS(f_abs("5"), 5);
  110. VS(f_abs("-5"), 5);
  111. return Count(true);
  112. }
  113. bool TestExtMath::test_is_finite() {
  114. VERIFY(f_is_finite(5));
  115. VERIFY(!f_is_finite(f_log(0)));
  116. return Count(true);
  117. }
  118. bool TestExtMath::test_is_infinite() {
  119. VERIFY(!f_is_infinite(5));
  120. VERIFY(f_is_infinite(f_log(0)));
  121. return Count(true);
  122. }
  123. bool TestExtMath::test_is_nan() {
  124. VERIFY(f_is_nan(f_acos(1.01)));
  125. return Count(true);
  126. }
  127. bool TestExtMath::test_ceil() {
  128. VS(f_ceil(4.3), 5.0);
  129. VS(f_ceil(9.999), 10.0);
  130. VS(f_ceil(-3.14), -3.0);
  131. return Count(true);
  132. }
  133. bool TestExtMath::test_floor() {
  134. VS(f_floor(4.3), 4.0);
  135. VS(f_floor(9.999), 9.0);
  136. VS(f_floor(-3.14), -4.0);
  137. return Count(true);
  138. }
  139. bool TestExtMath::test_round() {
  140. VS(f_round(3.4), 3.0);
  141. VS(f_round(3.5), 4.0);
  142. VS(f_round(3.6), 4.0);
  143. VS(f_round(3.6, 0), 4.0);
  144. VS(f_round(1.95583, 2), 1.96);
  145. VS(f_round(1241757, -3), 1242000.0);
  146. VS(f_round(5.045, 2), 5.05);
  147. VS(f_round(5.055, 2), 5.06);
  148. VS(f_round("3.4"), 3.0);
  149. VS(f_round("3.5"), 4.0);
  150. VS(f_round("3.6"), 4.0);
  151. VS(f_round("3.6", 0), 4.0);
  152. VS(f_round("1.95583", 2), 1.96);
  153. VS(f_round("1241757", -3), 1242000.0);
  154. VS(f_round("5.045", 2), 5.05);
  155. VS(f_round("5.055", 2), 5.06);
  156. return Count(true);
  157. }
  158. bool TestExtMath::test_deg2rad() {
  159. VC(f_deg2rad(45), k_M_PI_4);
  160. return Count(true);
  161. }
  162. bool TestExtMath::test_rad2deg() {
  163. VC(f_rad2deg(k_M_PI_4), 45);
  164. return Count(true);
  165. }
  166. bool TestExtMath::test_decbin() {
  167. VS(f_decbin(12), "1100");
  168. VS(f_decbin(26), "11010");
  169. return Count(true);
  170. }
  171. bool TestExtMath::test_dechex() {
  172. VS(f_dechex(10), "a");
  173. VS(f_dechex(47), "2f");
  174. return Count(true);
  175. }
  176. bool TestExtMath::test_decoct() {
  177. VS(f_decoct(15), "17");
  178. VS(f_decoct(264), "410");
  179. return Count(true);
  180. }
  181. bool TestExtMath::test_bindec() {
  182. VS(f_bindec("110011"), 51);
  183. VS(f_bindec("000110011"), 51);
  184. VS(f_bindec("111"), 7);
  185. return Count(true);
  186. }
  187. bool TestExtMath::test_hexdec() {
  188. VS(f_hexdec("See"), 238);
  189. VS(f_hexdec("ee"), 238);
  190. VS(f_hexdec("that"), 10);
  191. VS(f_hexdec("a0"), 160);
  192. return Count(true);
  193. }
  194. bool TestExtMath::test_octdec() {
  195. VS(f_octdec("77"), 63);
  196. VS(f_octdec(f_decoct(45)), 45);
  197. return Count(true);
  198. }
  199. bool TestExtMath::test_base_convert() {
  200. VS(f_base_convert("A37334", 16, 2), "101000110111001100110100");
  201. return Count(true);
  202. }
  203. bool TestExtMath::test_pow() {
  204. VC(f_pow(2, 8), 256);
  205. VC(f_pow(-1, 20), 1);
  206. VS(f_pow(0, 0), 1);
  207. VERIFY(f_pow(2, 32).isInteger());
  208. VC(f_pow("2", "8"), 256);
  209. VC(f_pow("-1", "20"), 1);
  210. VS(f_pow("0", "0"), 1);
  211. VERIFY(f_pow("2", "32").isInteger());
  212. return Count(true);
  213. }
  214. bool TestExtMath::test_exp() {
  215. VC(f_exp(12), 162754.791419);
  216. VC(f_exp(5.7), 298.86740096706);
  217. return Count(true);
  218. }
  219. bool TestExtMath::test_expm1() {
  220. VC(f_expm1(5.7), 297.8674);
  221. return Count(true);
  222. }
  223. bool TestExtMath::test_log10() {
  224. VS(f_log10(10), 1.0);
  225. VS(f_log10(100), 2.0);
  226. return Count(true);
  227. }
  228. bool TestExtMath::test_log1p() {
  229. VC(f_log1p(9), 2.302585092994);
  230. VC(f_log1p(99), 4.6051701859881);
  231. return Count(true);
  232. }
  233. bool TestExtMath::test_log() {
  234. VC(f_log(8), 2.0794415416798);
  235. VS(f_log(8, 2), 3.0);
  236. return Count(true);
  237. }
  238. bool TestExtMath::test_cos() {
  239. VC(f_cos(k_M_PI), -1);
  240. return Count(true);
  241. }
  242. bool TestExtMath::test_cosh() {
  243. VC(f_cosh(1.23), 1.8567610569853);
  244. return Count(true);
  245. }
  246. bool TestExtMath::test_sin() {
  247. VC(f_sin(f_deg2rad(90)), 1);
  248. return Count(true);
  249. }
  250. bool TestExtMath::test_sinh() {
  251. VC(f_sinh(1.23), 1.5644684793044);
  252. return Count(true);
  253. }
  254. bool TestExtMath::test_tan() {
  255. VC(f_tan(f_deg2rad(45)), 1);
  256. return Count(true);
  257. }
  258. bool TestExtMath::test_tanh() {
  259. VC(f_tanh(1.23), 0.84257932565893);
  260. return Count(true);
  261. }
  262. bool TestExtMath::test_acos() {
  263. VC(f_acos(-1), k_M_PI);
  264. return Count(true);
  265. }
  266. bool TestExtMath::test_acosh() {
  267. VC(f_acosh(1.8567610569853), 1.23);
  268. return Count(true);
  269. }
  270. bool TestExtMath::test_asin() {
  271. VC(f_asin(1), f_deg2rad(90));
  272. return Count(true);
  273. }
  274. bool TestExtMath::test_asinh() {
  275. VC(f_asinh(1.5644684793044), 1.23);
  276. return Count(true);
  277. }
  278. bool TestExtMath::test_atan() {
  279. VC(f_atan(1), f_deg2rad(45));
  280. return Count(true);
  281. }
  282. bool TestExtMath::test_atanh() {
  283. VC(f_atanh(0.84257932565893), 1.23);
  284. return Count(true);
  285. }
  286. bool TestExtMath::test_atan2() {
  287. VC(f_atan2(3, 4), 0.64350110879328);
  288. return Count(true);
  289. }
  290. bool TestExtMath::test_hypot() {
  291. VS(f_hypot(3, 4), 5.0);
  292. return Count(true);
  293. }
  294. bool TestExtMath::test_fmod() {
  295. VS(f_fmod(5.7, 1.3), 0.5);
  296. return Count(true);
  297. }
  298. bool TestExtMath::test_sqrt() {
  299. VS(f_sqrt(9), 3.0);
  300. return Count(true);
  301. }
  302. bool TestExtMath::test_getrandmax() {
  303. VS(f_getrandmax(), 2147483647);
  304. return Count(true);
  305. }
  306. bool TestExtMath::test_srand() {
  307. f_srand();
  308. f_srand(0);
  309. f_srand(1);
  310. return Count(true);
  311. }
  312. bool TestExtMath::test_rand() {
  313. f_rand();
  314. VERIFY(f_rand(5, 15) >= 5);
  315. VERIFY(f_rand(5, 15) <= 15);
  316. int64 n = f_rand(10000000000LL, 19999999999LL);
  317. VERIFY(n >= 10000000000LL);
  318. VERIFY(n <= 19999999999LL);
  319. return Count(true);
  320. }
  321. bool TestExtMath::test_mt_getrandmax() {
  322. VS(f_mt_getrandmax(), 2147483647);
  323. return Count(true);
  324. }
  325. bool TestExtMath::test_mt_srand() {
  326. f_mt_srand();
  327. f_mt_srand(0);
  328. f_mt_srand(1);
  329. VERIFY(f_mt_rand(5, 15) == 11);
  330. return Count(true);
  331. }
  332. bool TestExtMath::test_mt_rand() {
  333. f_mt_rand();
  334. VERIFY(f_mt_rand(5, 15) >= 5);
  335. VERIFY(f_mt_rand(5, 15) <= 15);
  336. return Count(true);
  337. }
  338. bool TestExtMath::test_lcg_value() {
  339. VERIFY(f_lcg_value() >= 0);
  340. VERIFY(f_lcg_value() <= 1);
  341. return Count(true);
  342. }