|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /**
- * \file
- * \brief PDS HW2 tests
- *
- * \author
- * Christos Choutouridis AEM:8997
- * <cchoutou@ece.auth.gr>
- */
-
- #include <gtest/gtest.h>
-
- #include <algorithm> // rand/srand
- #include <ctime> // rand/srand
- #include "distbitonic.hpp"
-
-
-
- /* ================================== ascending ================================== */
-
- /*
- * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
- * depth 0 (the initial ascending pattern)
- */
- TEST(TdistBitonic_UT, ascending_test1) {
-
- EXPECT_EQ(ascending<SortMode::Bitonic>(0, 0), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(1, 0), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(2, 0), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(3, 0), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(4, 0), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(5, 0), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(6, 0), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(7, 0), false);
-
- for (size_t node = 0 ; node < 256 ; ++node) {
- EXPECT_EQ(ascending<SortMode::Bitonic>(node, 0), ((node % 2) ? false : true) );
- }
- }
-
- /*
- * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
- * depth 1
- */
- TEST(TdistBitonic_UT, ascending_test2) {
-
- EXPECT_EQ(ascending<SortMode::Bitonic>(0, 1), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(1, 1), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(2, 1), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(3, 1), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(4, 1), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(5, 1), true);
- EXPECT_EQ(ascending<SortMode::Bitonic>(6, 1), false);
- EXPECT_EQ(ascending<SortMode::Bitonic>(7, 1), false);
-
- for (size_t node = 0 ; node < 256 ; ++node) {
- EXPECT_EQ(ascending<SortMode::Bitonic>(2*node, 1), ((node % 2) ? false:true));
- EXPECT_EQ(ascending<SortMode::Bitonic>(2*node+1, 1), ((node % 2) ? false:true));
- }
- }
-
- /*
- * bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
- * various depths
- */
- TEST(TdistBitonic_UT, ascending_test3) {
-
- // Depth = 3
- size_t ts_depth = 3;
-
- for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
- for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
-
- // Depth = 4
- ts_depth = 4;
-
- for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
- for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
-
- // Depth = 8
- ts_depth = 8;
-
- for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
- for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
- for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
- EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
- }
-
-
-
-
- /* ================================== partner ================================== */
-
- /*
- * size_t partner<SortMode::Bitonic>(size_t node, size_t step);
- * step = 0
- */
- TEST(TdistBitonic_UT, partner_test1) {
-
- EXPECT_EQ(partner<SortMode::Bitonic>(0, 0), 1UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(1, 0), 0UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(2, 0), 3UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(3, 0), 2UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(4, 0), 5UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(5, 0), 4UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(6, 0), 7UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(7, 0), 6UL);
-
- for (size_t node = 0 ; node < 256 ; ++node) {
- EXPECT_EQ(partner<SortMode::Bitonic>(node, 0), (node % 2) ? node-1 : node+1);
- }
- }
-
- /*
- * size_t partner<SortMode::Bitonic>(size_t node, size_t step);
- * step = 1
- */
- TEST(TdistBitonic_UT, partner_test2) {
-
- EXPECT_EQ(partner<SortMode::Bitonic>(0, 1), 2UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(1, 1), 3UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(2, 1), 0UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(3, 1), 1UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(4, 1), 6UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(5, 1), 7UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(6, 1), 4UL);
- EXPECT_EQ(partner<SortMode::Bitonic>(7, 1), 5UL);
-
- for (size_t n1 = 0 ; n1 < 256 ; n1 += 2) {
- auto n2 = n1 + 1UL;
- EXPECT_EQ(partner<SortMode::Bitonic>(n1, 1), ((n1 % 4) ? n1-2 : n1+2));
- EXPECT_EQ(partner<SortMode::Bitonic>(n2, 1), ((n1 % 4) ? n2-2 : n2+2));
- }
- }
-
- /*
- * size_t partner(size_t node, size_t step);
- * various steps
- */
- TEST(TdistBitonic_UT, partner_test3) {
- // step = 2
- size_t ts_step = 2;
-
- for (size_t n1 = 0 ; n1 < 256 ; n1 += 4) {
- auto n2 = n1 + 1UL;
- auto n3 = n1 + 2UL;
- auto n4 = n1 + 3UL;
- EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4));
- EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4));
- EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4));
- EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 8) ? n4-4 : n4+4));
- }
-
- // step = 3
- ts_step = 3;
-
- for (size_t n1 = 0 ; n1 < 256 ; n1 += 8) {
- auto n2 = n1 + 1UL;
- auto n3 = n1 + 2UL;
- auto n4 = n1 + 3UL;
- auto n5 = n1 + 4UL;
- auto n6 = n1 + 5UL;
- auto n7 = n1 + 6UL;
- auto n8 = n1 + 7UL;
- EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 16) ? n4-8 : n4+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 16) ? n5-8 : n5+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 16) ? n6-8 : n6+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 16) ? n7-8 : n7+8));
- EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 16) ? n8-8 : n8+8));
- }
-
- // step = 4
- ts_step = 4;
-
- for (size_t n1 = 0 ; n1 < 256 ; n1 += 16) {
- auto n2 = n1 + 1UL;
- auto n3 = n1 + 2UL;
- auto n4 = n1 + 3UL;
- auto n5 = n1 + 4UL;
- auto n6 = n1 + 5UL;
- auto n7 = n1 + 6UL;
- auto n8 = n1 + 7UL;
- auto n9 = n1 + 8UL;
- auto n10 = n1 + 9UL;
- auto n11 = n1 + 10UL;
- auto n12 = n1 + 11UL;
- auto n13 = n1 + 12UL;
- auto n14 = n1 + 13UL;
- auto n15 = n1 + 14UL;
- auto n16 = n1 + 15UL;
- EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 32) ? n4-16 : n4+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 32) ? n5-16 : n5+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 32) ? n6-16 : n6+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 32) ? n7-16 : n7+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 32) ? n8-16 : n8+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n9, ts_step), ((n1 % 32) ? n9-16 : n9+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n10, ts_step), ((n1 % 32) ? n10-16 : n10+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n11, ts_step), ((n1 % 32) ? n11-16 : n11+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n12, ts_step), ((n1 % 32) ? n12-16 : n12+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n13, ts_step), ((n1 % 32) ? n13-16 : n13+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n14, ts_step), ((n1 % 32) ? n14-16 : n14+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n15, ts_step), ((n1 % 32) ? n15-16 : n15+16));
- EXPECT_EQ(partner<SortMode::Bitonic>(n16, ts_step), ((n1 % 32) ? n16-16 : n16+16));
- }
- }
-
-
-
- /* ================================== keepsmall ================================== */
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- * Assertion check
- */
- TEST(TdistBitonic_UT, keepsmall_test1) {
- ASSERT_DEATH(keepsmall<SortMode::Bitonic>(0, 0, 0), "");
- ASSERT_DEATH(keepsmall<SortMode::Bitonic>(1, 1, 42), "");
- ASSERT_DEATH(keepsmall<SortMode::Bitonic>(7, 7, 42), "");
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 0, 1, 1, 0, 0, 1]
- */
- TEST(TdistBitonic_UT, keepsmall_test2) {
- size_t ts_depth = 1UL;
- size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
- bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 0, 0, 1, 1]
- */
- TEST(TdistBitonic_UT, keepsmall_test3) {
- size_t ts_depth = 2UL;
- size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
- bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 0, 1, 0, 1]
- */
- TEST(TdistBitonic_UT, keepsmall_test4) {
- size_t ts_depth = 2UL;
- size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
- bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 3 | step: 2 | partner: [4, 5, 6, 7, 0, 1, 2, 3] | keepsmall: Bool[1, 1, 1, 1, 0, 0, 0, 0]
- */
- TEST(TdistBitonic_UT, keepsmall_test5) {
- size_t ts_depth = 3UL;
- size_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3};
- bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 3 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 1, 1, 0, 0]
- */
- TEST(TdistBitonic_UT, keepsmall_test6) {
- size_t ts_depth = 3UL;
- size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
- bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- /*
- * bool keepsmall(size_t node, size_t partner, size_t depth);
- *
- * depth: 3 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 1, 0, 1, 0]
- */
- TEST(TdistBitonic_UT, keepsmall_test7) {
- size_t ts_depth = 3UL;
- size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
- bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0};
-
- for (size_t node = 0 ; node < 8UL ; ++node ) {
- EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
- }
- }
-
- TEST(TdistBitonic_UT, distbitonic_test1) {
- AllData_t ts_Data {
- Data_t (8), Data_t (8)
- };
-
- std::srand(unsigned(std::time(nullptr)));
- for (auto& v : ts_Data) {
- std::generate(v.begin(), v.end(), std::rand);
- }
-
- distbitonic(2, ts_Data);
-
- auto max = std::numeric_limits<Data_t::value_type>::min();
- for (auto& v : ts_Data) {
- EXPECT_EQ((max <= v[0]), true);
- EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
- max = v.back();
- }
- }
-
- TEST(TdistBitonic_UT, distbitonic_test2) {
- AllData_t ts_Data {
- Data_t (8), Data_t (8), Data_t (8), Data_t (8)
- };
-
- std::srand(unsigned(std::time(nullptr)));
- for (auto& v : ts_Data) {
- std::generate(v.begin(), v.end(), std::rand);
- }
-
- distbitonic(4, ts_Data);
-
- auto max = std::numeric_limits<Data_t::value_type>::min();
- for (auto& v : ts_Data) {
- EXPECT_EQ((max <= v[0]), true);
- EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
- max = v.back();
- }
- }
-
- TEST(TdistBitonic_UT, distbitonic_test3) {
- AllData_t ts_Data {
- Data_t (32), Data_t (32), Data_t (32), Data_t (32),
- Data_t (32), Data_t (32), Data_t (32), Data_t (32)
- };
-
- std::srand(unsigned(std::time(nullptr)));
- for (auto& v : ts_Data) {
- std::generate(v.begin(), v.end(), std::rand);
- }
-
- distbitonic(8, ts_Data);
-
- auto max = std::numeric_limits<Data_t::value_type>::min();
- for (auto& v : ts_Data) {
- EXPECT_EQ((max <= v[0]), true);
- EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
- max = v.back();
- }
- }
|