AUTH's THMMY "Parallel and distributed systems" course assignments.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tests_Bitonic.cpp 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * \file
  3. * \brief PDS HW2 tests
  4. *
  5. * \author
  6. * Christos Choutouridis AEM:8997
  7. * <cchoutou@ece.auth.gr>
  8. */
  9. #include <gtest/gtest.h>
  10. #include <algorithm> // rand/srand
  11. #include <ctime> // rand/srand
  12. #include "distbitonic.hpp"
  13. /* ================================== ascending ================================== */
  14. /*
  15. * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
  16. * depth 0 (the initial ascending pattern)
  17. */
  18. TEST(TdistBitonic_UT, ascending_test1) {
  19. EXPECT_EQ(ascending<SortMode::Bitonic>(0, 0), true);
  20. EXPECT_EQ(ascending<SortMode::Bitonic>(1, 0), false);
  21. EXPECT_EQ(ascending<SortMode::Bitonic>(2, 0), true);
  22. EXPECT_EQ(ascending<SortMode::Bitonic>(3, 0), false);
  23. EXPECT_EQ(ascending<SortMode::Bitonic>(4, 0), true);
  24. EXPECT_EQ(ascending<SortMode::Bitonic>(5, 0), false);
  25. EXPECT_EQ(ascending<SortMode::Bitonic>(6, 0), true);
  26. EXPECT_EQ(ascending<SortMode::Bitonic>(7, 0), false);
  27. for (size_t node = 0 ; node < 256 ; ++node) {
  28. EXPECT_EQ(ascending<SortMode::Bitonic>(node, 0), ((node % 2) ? false : true) );
  29. }
  30. }
  31. /*
  32. * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
  33. * depth 1
  34. */
  35. TEST(TdistBitonic_UT, ascending_test2) {
  36. EXPECT_EQ(ascending<SortMode::Bitonic>(0, 1), true);
  37. EXPECT_EQ(ascending<SortMode::Bitonic>(1, 1), true);
  38. EXPECT_EQ(ascending<SortMode::Bitonic>(2, 1), false);
  39. EXPECT_EQ(ascending<SortMode::Bitonic>(3, 1), false);
  40. EXPECT_EQ(ascending<SortMode::Bitonic>(4, 1), true);
  41. EXPECT_EQ(ascending<SortMode::Bitonic>(5, 1), true);
  42. EXPECT_EQ(ascending<SortMode::Bitonic>(6, 1), false);
  43. EXPECT_EQ(ascending<SortMode::Bitonic>(7, 1), false);
  44. for (size_t node = 0 ; node < 256 ; ++node) {
  45. EXPECT_EQ(ascending<SortMode::Bitonic>(2*node, 1), ((node % 2) ? false:true));
  46. EXPECT_EQ(ascending<SortMode::Bitonic>(2*node+1, 1), ((node % 2) ? false:true));
  47. }
  48. }
  49. /*
  50. * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
  51. * various depths
  52. */
  53. TEST(TdistBitonic_UT, ascending_test3) {
  54. // Depth = 3
  55. size_t ts_depth = 3;
  56. for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
  57. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  58. for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
  59. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  60. for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
  61. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  62. for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
  63. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  64. // Depth = 4
  65. ts_depth = 4;
  66. for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
  67. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  68. for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
  69. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  70. for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
  71. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  72. for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
  73. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  74. // Depth = 8
  75. ts_depth = 8;
  76. for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
  77. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  78. for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
  79. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  80. for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
  81. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
  82. for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
  83. EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
  84. }
  85. /* ================================== partner ================================== */
  86. /*
  87. * size_t partner<SortMode::Bitonic>(size_t node, size_t step);
  88. * step = 0
  89. */
  90. TEST(TdistBitonic_UT, partner_test1) {
  91. EXPECT_EQ(partner<SortMode::Bitonic>(0, 0), 1UL);
  92. EXPECT_EQ(partner<SortMode::Bitonic>(1, 0), 0UL);
  93. EXPECT_EQ(partner<SortMode::Bitonic>(2, 0), 3UL);
  94. EXPECT_EQ(partner<SortMode::Bitonic>(3, 0), 2UL);
  95. EXPECT_EQ(partner<SortMode::Bitonic>(4, 0), 5UL);
  96. EXPECT_EQ(partner<SortMode::Bitonic>(5, 0), 4UL);
  97. EXPECT_EQ(partner<SortMode::Bitonic>(6, 0), 7UL);
  98. EXPECT_EQ(partner<SortMode::Bitonic>(7, 0), 6UL);
  99. for (size_t node = 0 ; node < 256 ; ++node) {
  100. EXPECT_EQ(partner<SortMode::Bitonic>(node, 0), (node % 2) ? node-1 : node+1);
  101. }
  102. }
  103. /*
  104. * size_t partner<SortMode::Bitonic>(size_t node, size_t step);
  105. * step = 1
  106. */
  107. TEST(TdistBitonic_UT, partner_test2) {
  108. EXPECT_EQ(partner<SortMode::Bitonic>(0, 1), 2UL);
  109. EXPECT_EQ(partner<SortMode::Bitonic>(1, 1), 3UL);
  110. EXPECT_EQ(partner<SortMode::Bitonic>(2, 1), 0UL);
  111. EXPECT_EQ(partner<SortMode::Bitonic>(3, 1), 1UL);
  112. EXPECT_EQ(partner<SortMode::Bitonic>(4, 1), 6UL);
  113. EXPECT_EQ(partner<SortMode::Bitonic>(5, 1), 7UL);
  114. EXPECT_EQ(partner<SortMode::Bitonic>(6, 1), 4UL);
  115. EXPECT_EQ(partner<SortMode::Bitonic>(7, 1), 5UL);
  116. for (size_t n1 = 0 ; n1 < 256 ; n1 += 2) {
  117. auto n2 = n1 + 1UL;
  118. EXPECT_EQ(partner<SortMode::Bitonic>(n1, 1), ((n1 % 4) ? n1-2 : n1+2));
  119. EXPECT_EQ(partner<SortMode::Bitonic>(n2, 1), ((n1 % 4) ? n2-2 : n2+2));
  120. }
  121. }
  122. /*
  123. * size_t partner(size_t node, size_t step);
  124. * various steps
  125. */
  126. TEST(TdistBitonic_UT, partner_test3) {
  127. // step = 2
  128. size_t ts_step = 2;
  129. for (size_t n1 = 0 ; n1 < 256 ; n1 += 4) {
  130. auto n2 = n1 + 1UL;
  131. auto n3 = n1 + 2UL;
  132. auto n4 = n1 + 3UL;
  133. EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4));
  134. EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4));
  135. EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4));
  136. EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 8) ? n4-4 : n4+4));
  137. }
  138. // step = 3
  139. ts_step = 3;
  140. for (size_t n1 = 0 ; n1 < 256 ; n1 += 8) {
  141. auto n2 = n1 + 1UL;
  142. auto n3 = n1 + 2UL;
  143. auto n4 = n1 + 3UL;
  144. auto n5 = n1 + 4UL;
  145. auto n6 = n1 + 5UL;
  146. auto n7 = n1 + 6UL;
  147. auto n8 = n1 + 7UL;
  148. EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8));
  149. EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8));
  150. EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8));
  151. EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 16) ? n4-8 : n4+8));
  152. EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 16) ? n5-8 : n5+8));
  153. EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 16) ? n6-8 : n6+8));
  154. EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 16) ? n7-8 : n7+8));
  155. EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 16) ? n8-8 : n8+8));
  156. }
  157. // step = 4
  158. ts_step = 4;
  159. for (size_t n1 = 0 ; n1 < 256 ; n1 += 16) {
  160. auto n2 = n1 + 1UL;
  161. auto n3 = n1 + 2UL;
  162. auto n4 = n1 + 3UL;
  163. auto n5 = n1 + 4UL;
  164. auto n6 = n1 + 5UL;
  165. auto n7 = n1 + 6UL;
  166. auto n8 = n1 + 7UL;
  167. auto n9 = n1 + 8UL;
  168. auto n10 = n1 + 9UL;
  169. auto n11 = n1 + 10UL;
  170. auto n12 = n1 + 11UL;
  171. auto n13 = n1 + 12UL;
  172. auto n14 = n1 + 13UL;
  173. auto n15 = n1 + 14UL;
  174. auto n16 = n1 + 15UL;
  175. EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16));
  176. EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16));
  177. EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16));
  178. EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 32) ? n4-16 : n4+16));
  179. EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 32) ? n5-16 : n5+16));
  180. EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 32) ? n6-16 : n6+16));
  181. EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 32) ? n7-16 : n7+16));
  182. EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 32) ? n8-16 : n8+16));
  183. EXPECT_EQ(partner<SortMode::Bitonic>(n9, ts_step), ((n1 % 32) ? n9-16 : n9+16));
  184. EXPECT_EQ(partner<SortMode::Bitonic>(n10, ts_step), ((n1 % 32) ? n10-16 : n10+16));
  185. EXPECT_EQ(partner<SortMode::Bitonic>(n11, ts_step), ((n1 % 32) ? n11-16 : n11+16));
  186. EXPECT_EQ(partner<SortMode::Bitonic>(n12, ts_step), ((n1 % 32) ? n12-16 : n12+16));
  187. EXPECT_EQ(partner<SortMode::Bitonic>(n13, ts_step), ((n1 % 32) ? n13-16 : n13+16));
  188. EXPECT_EQ(partner<SortMode::Bitonic>(n14, ts_step), ((n1 % 32) ? n14-16 : n14+16));
  189. EXPECT_EQ(partner<SortMode::Bitonic>(n15, ts_step), ((n1 % 32) ? n15-16 : n15+16));
  190. EXPECT_EQ(partner<SortMode::Bitonic>(n16, ts_step), ((n1 % 32) ? n16-16 : n16+16));
  191. }
  192. }
  193. /* ================================== keepsmall ================================== */
  194. /*
  195. * bool keepsmall(size_t node, size_t partner, size_t depth);
  196. * Assertion check
  197. */
  198. TEST(TdistBitonic_UT, keepsmall_test1) {
  199. ASSERT_DEATH(keepsmall<SortMode::Bitonic>(0, 0, 0), "");
  200. ASSERT_DEATH(keepsmall<SortMode::Bitonic>(1, 1, 42), "");
  201. ASSERT_DEATH(keepsmall<SortMode::Bitonic>(7, 7, 42), "");
  202. }
  203. /*
  204. * bool keepsmall(size_t node, size_t partner, size_t depth);
  205. *
  206. * depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 0, 1, 1, 0, 0, 1]
  207. */
  208. TEST(TdistBitonic_UT, keepsmall_test2) {
  209. size_t ts_depth = 1UL;
  210. size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
  211. bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1};
  212. for (size_t node = 0 ; node < 8UL ; ++node ) {
  213. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  214. }
  215. }
  216. /*
  217. * bool keepsmall(size_t node, size_t partner, size_t depth);
  218. *
  219. * depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 0, 0, 1, 1]
  220. */
  221. TEST(TdistBitonic_UT, keepsmall_test3) {
  222. size_t ts_depth = 2UL;
  223. size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
  224. bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1};
  225. for (size_t node = 0 ; node < 8UL ; ++node ) {
  226. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  227. }
  228. }
  229. /*
  230. * bool keepsmall(size_t node, size_t partner, size_t depth);
  231. *
  232. * depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 0, 1, 0, 1]
  233. */
  234. TEST(TdistBitonic_UT, keepsmall_test4) {
  235. size_t ts_depth = 2UL;
  236. size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
  237. bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1};
  238. for (size_t node = 0 ; node < 8UL ; ++node ) {
  239. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  240. }
  241. }
  242. /*
  243. * bool keepsmall(size_t node, size_t partner, size_t depth);
  244. *
  245. * depth: 3 | step: 2 | partner: [4, 5, 6, 7, 0, 1, 2, 3] | keepsmall: Bool[1, 1, 1, 1, 0, 0, 0, 0]
  246. */
  247. TEST(TdistBitonic_UT, keepsmall_test5) {
  248. size_t ts_depth = 3UL;
  249. size_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3};
  250. bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0};
  251. for (size_t node = 0 ; node < 8UL ; ++node ) {
  252. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  253. }
  254. }
  255. /*
  256. * bool keepsmall(size_t node, size_t partner, size_t depth);
  257. *
  258. * depth: 3 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 1, 1, 0, 0]
  259. */
  260. TEST(TdistBitonic_UT, keepsmall_test6) {
  261. size_t ts_depth = 3UL;
  262. size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
  263. bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0};
  264. for (size_t node = 0 ; node < 8UL ; ++node ) {
  265. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  266. }
  267. }
  268. /*
  269. * bool keepsmall(size_t node, size_t partner, size_t depth);
  270. *
  271. * depth: 3 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 1, 0, 1, 0]
  272. */
  273. TEST(TdistBitonic_UT, keepsmall_test7) {
  274. size_t ts_depth = 3UL;
  275. size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
  276. bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0};
  277. for (size_t node = 0 ; node < 8UL ; ++node ) {
  278. EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
  279. }
  280. }
  281. TEST(TdistBitonic_UT, distbitonic_test1) {
  282. AllData_t ts_Data {
  283. Data_t (8), Data_t (8)
  284. };
  285. std::srand(unsigned(std::time(nullptr)));
  286. for (auto& v : ts_Data) {
  287. std::generate(v.begin(), v.end(), std::rand);
  288. }
  289. distbitonic(2, ts_Data);
  290. auto max = std::numeric_limits<Data_t::value_type>::min();
  291. for (auto& v : ts_Data) {
  292. EXPECT_EQ((max <= v[0]), true);
  293. EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
  294. max = v.back();
  295. }
  296. }
  297. TEST(TdistBitonic_UT, distbitonic_test2) {
  298. AllData_t ts_Data {
  299. Data_t (8), Data_t (8), Data_t (8), Data_t (8)
  300. };
  301. std::srand(unsigned(std::time(nullptr)));
  302. for (auto& v : ts_Data) {
  303. std::generate(v.begin(), v.end(), std::rand);
  304. }
  305. distbitonic(4, ts_Data);
  306. auto max = std::numeric_limits<Data_t::value_type>::min();
  307. for (auto& v : ts_Data) {
  308. EXPECT_EQ((max <= v[0]), true);
  309. EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
  310. max = v.back();
  311. }
  312. }
  313. TEST(TdistBitonic_UT, distbitonic_test3) {
  314. AllData_t ts_Data {
  315. Data_t (32), Data_t (32), Data_t (32), Data_t (32),
  316. Data_t (32), Data_t (32), Data_t (32), Data_t (32)
  317. };
  318. std::srand(unsigned(std::time(nullptr)));
  319. for (auto& v : ts_Data) {
  320. std::generate(v.begin(), v.end(), std::rand);
  321. }
  322. distbitonic(8, ts_Data);
  323. auto max = std::numeric_limits<Data_t::value_type>::min();
  324. for (auto& v : ts_Data) {
  325. EXPECT_EQ((max <= v[0]), true);
  326. EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
  327. max = v.back();
  328. }
  329. }