voronoi_ctypes_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Boost.Polygon library voronoi_ctypes_test.cpp file
  2. // Copyright Andrii Sydorchuk 2010-2012.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/polygon/detail/voronoi_ctypes.hpp>
  9. #include <boost/random/mersenne_twister.hpp>
  10. #include <vector>
  11. #include <ctime>
  12. using namespace boost::polygon::detail;
  13. type_converter_fpt to_fpt;
  14. void ulp_comparison_test1()
  15. {
  16. ulp_comparison<double> ulp_cmp;
  17. uint64 a = 22;
  18. uint64 b = 27;
  19. fpt64 da, db;
  20. std::memcpy(&da, &a, sizeof(uint64));
  21. std::memcpy(&db, &b, sizeof(uint64));
  22. BOOST_TEST_EQ(ulp_cmp(da, db, 1), ulp_cmp.LESS);
  23. BOOST_TEST_EQ(ulp_cmp(db, da, 1), ulp_cmp.MORE);
  24. BOOST_TEST_EQ(ulp_cmp(da, db, 4), ulp_cmp.LESS);
  25. BOOST_TEST_EQ(ulp_cmp(da, db, 5), ulp_cmp.EQUAL);
  26. BOOST_TEST_EQ(ulp_cmp(da, db, 6), ulp_cmp.EQUAL);
  27. }
  28. void ulp_comparison_test2()
  29. {
  30. ulp_comparison<fpt64> ulp_cmp;
  31. uint64 a = 0ULL;
  32. uint64 b = 0x8000000000000002ULL;
  33. fpt64 da, db;
  34. std::memcpy(&da, &a, sizeof(uint64));
  35. std::memcpy(&db, &b, sizeof(uint64));
  36. BOOST_TEST_EQ(ulp_cmp(da, db, 1), ulp_cmp.MORE);
  37. BOOST_TEST_EQ(ulp_cmp(db, da, 1), ulp_cmp.LESS);
  38. BOOST_TEST_EQ(ulp_cmp(da, db, 2), ulp_cmp.EQUAL);
  39. BOOST_TEST_EQ(ulp_cmp(da, db, 3), ulp_cmp.EQUAL);
  40. }
  41. void extended_exponent_fpt_test1()
  42. {
  43. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  44. fpt64 b = 0.0;
  45. efpt64 eeb(b);
  46. for (int i = 0; i < 1000; ++i) {
  47. fpt64 a = to_fpt(static_cast<int64>(gen()));
  48. efpt64 eea(a);
  49. efpt64 neg = -eea;
  50. efpt64 sum = eea + eeb;
  51. efpt64 dif = eea - eeb;
  52. efpt64 mul = eea * eeb;
  53. BOOST_TEST_EQ(to_fpt(neg), -a);
  54. BOOST_TEST_EQ(to_fpt(sum), a + b);
  55. BOOST_TEST_EQ(to_fpt(dif), a - b);
  56. BOOST_TEST_EQ(to_fpt(mul), a * b);
  57. }
  58. }
  59. void extended_exponent_fpt_test2()
  60. {
  61. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  62. fpt64 a = 0.0;
  63. efpt64 eea(a);
  64. for (int i = 0; i < 1000; ++i) {
  65. fpt64 b = to_fpt(static_cast<int64>(gen()));
  66. if (b == 0.0)
  67. continue;
  68. efpt64 eeb(b);
  69. efpt64 neg = -eea;
  70. efpt64 sum = eea + eeb;
  71. efpt64 dif = eea - eeb;
  72. efpt64 mul = eea * eeb;
  73. efpt64 div = eea / eeb;
  74. BOOST_TEST_EQ(to_fpt(neg), -a);
  75. BOOST_TEST_EQ(to_fpt(sum), a + b);
  76. BOOST_TEST_EQ(to_fpt(dif), a - b);
  77. BOOST_TEST_EQ(to_fpt(mul), a * b);
  78. BOOST_TEST_EQ(to_fpt(div), a / b);
  79. }
  80. }
  81. void extended_exponent_fpt_test3()
  82. {
  83. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  84. for (int i = 0; i < 1000; ++i) {
  85. fpt64 a = to_fpt(static_cast<int64>(gen()));
  86. fpt64 b = to_fpt(static_cast<int64>(gen()));
  87. if (b == 0.0)
  88. continue;
  89. efpt64 eea(a);
  90. efpt64 eeb(b);
  91. efpt64 neg = -eea;
  92. efpt64 sum = eea + eeb;
  93. efpt64 dif = eea - eeb;
  94. efpt64 mul = eea * eeb;
  95. efpt64 div = eea / eeb;
  96. BOOST_TEST_EQ(to_fpt(neg), -a);
  97. BOOST_TEST_EQ(to_fpt(sum), a + b);
  98. BOOST_TEST_EQ(to_fpt(dif), a - b);
  99. BOOST_TEST_EQ(to_fpt(mul), a * b);
  100. BOOST_TEST_EQ(to_fpt(div), a / b);
  101. }
  102. }
  103. void extended_exponent_fpt_test4()
  104. {
  105. for (int exp = 0; exp < 64; ++exp)
  106. for (int i = 1; i < 100; ++i) {
  107. fpt64 a = i;
  108. fpt64 b = to_fpt(1LL << exp);
  109. efpt64 eea(a);
  110. efpt64 eeb(b);
  111. efpt64 neg = -eea;
  112. efpt64 sum = eea + eeb;
  113. efpt64 dif = eea - eeb;
  114. efpt64 mul = eea * eeb;
  115. efpt64 div = eea / eeb;
  116. BOOST_TEST_EQ(to_fpt(neg), -a);
  117. BOOST_TEST_EQ(to_fpt(sum), a + b);
  118. BOOST_TEST_EQ(to_fpt(dif), a - b);
  119. BOOST_TEST_EQ(to_fpt(mul), a * b);
  120. BOOST_TEST_EQ(to_fpt(div), a / b);
  121. }
  122. }
  123. void extended_exponent_fpt_test5()
  124. {
  125. for (int i = 0; i < 100; ++i) {
  126. efpt64 a(to_fpt(i * i));
  127. efpt64 b = a.sqrt();
  128. BOOST_TEST_EQ(to_fpt(b), to_fpt(i));
  129. }
  130. }
  131. void extended_exponent_fpt_test6()
  132. {
  133. for (int i = -10; i <= 10; ++i) {
  134. efpt64 a(to_fpt(i));
  135. BOOST_TEST_EQ(is_pos(a), i > 0);
  136. BOOST_TEST_EQ(is_neg(a), i < 0);
  137. BOOST_TEST_EQ(is_zero(a), !i);
  138. }
  139. }
  140. void extended_int_test1()
  141. {
  142. typedef extended_int<1> eint32;
  143. eint32 e1(0), e2(32), e3(-32);
  144. BOOST_TEST_EQ(e1.count(), 0);
  145. BOOST_TEST_EQ(e1.size(), 0U);
  146. BOOST_TEST_EQ(e2.count(), 1);
  147. BOOST_TEST_EQ(e2.chunks()[0], 32U);
  148. BOOST_TEST_EQ(e2.size(), 1U);
  149. BOOST_TEST_EQ(e3.count(), -1);
  150. BOOST_TEST_EQ(e3.chunks()[0], 32U);
  151. BOOST_TEST_EQ(e3.size(), 1U);
  152. }
  153. void extended_int_test2()
  154. {
  155. typedef extended_int<2> eint64;
  156. int64 val64 = 0x7fffffffffffffffLL;
  157. eint64 e1(0), e2(32), e3(-32), e4(val64), e5(-val64);
  158. BOOST_TEST_EQ(e1.count(), 0);
  159. BOOST_TEST_EQ(e2.count(), 1);
  160. BOOST_TEST_EQ(e2.chunks()[0], 32U);
  161. BOOST_TEST_EQ(e3.count(), -1);
  162. BOOST_TEST_EQ(e3.chunks()[0], 32U);
  163. BOOST_TEST_EQ(e4.count(), 2);
  164. BOOST_TEST_EQ(e4.chunks()[0], 0xffffffff);
  165. BOOST_TEST_EQ(e4.chunks()[1], val64 >> 32);
  166. BOOST_TEST_EQ(e5.count(), -2);
  167. BOOST_TEST_EQ(e5.chunks()[0], 0xffffffff);
  168. BOOST_TEST_EQ(e5.chunks()[1], val64 >> 32);
  169. }
  170. void extended_int_test3()
  171. {
  172. typedef extended_int<2> eint64;
  173. std::vector<uint32> chunks;
  174. chunks.push_back(1);
  175. chunks.push_back(2);
  176. eint64 e1(chunks, true), e2(chunks, false);
  177. BOOST_TEST_EQ(e1.count(), 2);
  178. BOOST_TEST_EQ(e1.chunks()[0], 2U);
  179. BOOST_TEST_EQ(e1.chunks()[1], 1U);
  180. BOOST_TEST_EQ(e2.count(), -2);
  181. BOOST_TEST_EQ(e2.chunks()[0], 2U);
  182. BOOST_TEST_EQ(e2.chunks()[1], 1U);
  183. }
  184. void extended_int_test4()
  185. {
  186. typedef extended_int<2> eint64;
  187. std::vector<uint32> chunks;
  188. chunks.push_back(1);
  189. chunks.push_back(2);
  190. eint64 e1(chunks, true), e2(chunks, false);
  191. BOOST_TEST_EQ(e1 == e2, false);
  192. BOOST_TEST_EQ(e1 == -e2, true);
  193. BOOST_TEST_EQ(e1 != e2, true);
  194. BOOST_TEST_EQ(e1 != -e2, false);
  195. BOOST_TEST_EQ(e1 < e2, false);
  196. BOOST_TEST_EQ(e1 < -e2, false);
  197. BOOST_TEST_EQ(e1 <= e2, false);
  198. BOOST_TEST_EQ(e1 <= -e2, true);
  199. BOOST_TEST_EQ(e1 > e2, true);
  200. BOOST_TEST_EQ(e1 > -e2, false);
  201. BOOST_TEST_EQ(e1 >= e2, true);
  202. BOOST_TEST_EQ(e1 >= -e2, true);
  203. }
  204. void extended_int_test5()
  205. {
  206. typedef extended_int<2> eint64;
  207. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  208. for (int i = 0; i < 1000; ++i) {
  209. int64 i1 = static_cast<int64>(gen());
  210. int64 i2 = static_cast<int64>(gen());
  211. eint64 e1(i1), e2(i2);
  212. BOOST_TEST_EQ(e1 == e2, i1 == i2);
  213. BOOST_TEST_EQ(e1 != e2, i1 != i2);
  214. BOOST_TEST_EQ(e1 > e2, i1 > i2);
  215. BOOST_TEST_EQ(e1 >= e2, i1 >= i2);
  216. BOOST_TEST_EQ(e1 < e2, i1 < i2);
  217. BOOST_TEST_EQ(e1 <= e2, i1 <= i2);
  218. }
  219. }
  220. void extended_int_test6()
  221. {
  222. typedef extended_int<1> eint32;
  223. eint32 e1(32);
  224. eint32 e2 = -e1;
  225. BOOST_TEST_EQ(e2.count(), -1);
  226. BOOST_TEST_EQ(e2.size(), 1U);
  227. BOOST_TEST_EQ(e2.chunks()[0], 32U);
  228. }
  229. void extended_int_test7()
  230. {
  231. typedef extended_int<2> eint64;
  232. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  233. for (int i = 0; i < 1000; ++i) {
  234. int64 i1 = static_cast<int64>(gen()) >> 2;
  235. int64 i2 = static_cast<int64>(gen()) >> 2;
  236. eint64 e1(i1), e2(i2), e3(i1 + i2), e4(i1 - i2);
  237. BOOST_TEST(e1 + e2 == e3);
  238. BOOST_TEST(e1 - e2 == e4);
  239. }
  240. }
  241. void extended_int_test8()
  242. {
  243. typedef extended_int<2> eint64;
  244. boost::mt19937 gen(static_cast<uint32>(time(NULL)));
  245. for (int i = 0; i < 1000; ++i) {
  246. int64 i1 = static_cast<int32>(gen());
  247. int64 i2 = static_cast<int32>(gen());
  248. eint64 e1(i1), e2(i2), e3(i1 * i2);
  249. BOOST_TEST(e1 * e2 == e3);
  250. }
  251. }
  252. void extended_int_test9()
  253. {
  254. typedef extended_int<1> eint32;
  255. for (int i = -10; i <= 10; ++i) {
  256. for (int j = -10; j <= 10; ++j) {
  257. eint32 e1(i), e2(j), e3(i+j), e4(i-j), e5(i*j);
  258. BOOST_TEST(e1 + e2 == e3);
  259. BOOST_TEST(e1 - e2 == e4);
  260. BOOST_TEST(e1 * e2 == e5);
  261. }
  262. }
  263. }
  264. void extended_int_test10()
  265. {
  266. typedef extended_int<2> eint64;
  267. boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
  268. for (int i = 0; i < 100; ++i) {
  269. int64 i1 = static_cast<int64>(gen()) >> 20;
  270. int64 i2 = i1 >> 32;
  271. eint64 e1(i1), e2(i2);
  272. BOOST_TEST(to_fpt(e1) == static_cast<fpt64>(i1));
  273. BOOST_TEST(to_fpt(e2) == static_cast<fpt64>(i2));
  274. }
  275. }
  276. void extened_int_test11()
  277. {
  278. typedef extended_int<64> eint2048;
  279. eint2048 two(2), value(1);
  280. for (int i = 0; i < 1024; ++i)
  281. value = value * two;
  282. BOOST_TEST_EQ(value.count(), 33);
  283. for (std::size_t i = 1; i < value.size(); ++i)
  284. BOOST_TEST_EQ(value.chunks()[i-1], 0U);
  285. BOOST_TEST_EQ(value.chunks()[32], 1U);
  286. }
  287. int main()
  288. {
  289. ulp_comparison_test1();
  290. ulp_comparison_test2();
  291. extended_exponent_fpt_test1();
  292. extended_exponent_fpt_test2();
  293. extended_exponent_fpt_test3();
  294. extended_exponent_fpt_test4();
  295. extended_exponent_fpt_test5();
  296. extended_exponent_fpt_test6();
  297. extended_int_test1();
  298. extended_int_test2();
  299. extended_int_test3();
  300. extended_int_test4();
  301. extended_int_test5();
  302. extended_int_test6();
  303. extended_int_test7();
  304. extended_int_test8();
  305. extended_int_test9();
  306. extended_int_test10();
  307. extened_int_test11();
  308. return boost::report_errors();
  309. }