/** * \file * \brief PDS HW2 tests * * \author * Christos Choutouridis AEM:8997 * */ #include #include // rand/srand #include // rand/srand #include "distsort.hpp" /* ================================== ascending ================================== */ /* * bool ascending(mpi_id_t node, size_t depth); * depth 0 (the initial ascending pattern) */ TEST(TdistBitonic_UT, ascending_test1) { EXPECT_EQ(ascending(0, 0), true); EXPECT_EQ(ascending(1, 0), false); EXPECT_EQ(ascending(2, 0), true); EXPECT_EQ(ascending(3, 0), false); EXPECT_EQ(ascending(4, 0), true); EXPECT_EQ(ascending(5, 0), false); EXPECT_EQ(ascending(6, 0), true); EXPECT_EQ(ascending(7, 0), false); for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(ascending(node, 0), ((node % 2) ? false : true) ); } } /* * bool ascending(mpi_id_t node, size_t depth); * depth 1 */ TEST(TdistBitonic_UT, ascending_test2) { EXPECT_EQ(ascending(0, 1), true); EXPECT_EQ(ascending(1, 1), true); EXPECT_EQ(ascending(2, 1), false); EXPECT_EQ(ascending(3, 1), false); EXPECT_EQ(ascending(4, 1), true); EXPECT_EQ(ascending(5, 1), true); EXPECT_EQ(ascending(6, 1), false); EXPECT_EQ(ascending(7, 1), false); for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(ascending(2*node, 1), ((node % 2) ? false:true)); EXPECT_EQ(ascending(2*node+1, 1), ((node % 2) ? false:true)); } } /* * bool ascending(mpi_id_t node, size_t depth); * various depths */ TEST(TdistBitonic_UT, ascending_test3) { // Depth = 3 size_t ts_depth = 3; for (mpi_id_t n = 0 ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); // Depth = 4 ts_depth = 4; for (mpi_id_t n = 0L ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); // Depth = 8 ts_depth = 8; for (mpi_id_t n = 0L ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); } /* ================================== partner ================================== */ /* * mpi_id_t partner(mpi_id_t node, size_t step); * step = 0 */ TEST(TdistBitonic_UT, partner_test1) { EXPECT_EQ(partner(0, 0), 1); EXPECT_EQ(partner(1, 0), 0); EXPECT_EQ(partner(2, 0), 3); EXPECT_EQ(partner(3, 0), 2); EXPECT_EQ(partner(4, 0), 5); EXPECT_EQ(partner(5, 0), 4); EXPECT_EQ(partner(6, 0), 7); EXPECT_EQ(partner(7, 0), 6); for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(partner(node, 0), (node % 2) ? node-1 : node+1); } } /* * mpi_id_t partner(mpi_id_t node, size_t step); * step = 1 */ TEST(TdistBitonic_UT, partner_test2) { EXPECT_EQ(partner(0, 1), 2); EXPECT_EQ(partner(1, 1), 3); EXPECT_EQ(partner(2, 1), 0); EXPECT_EQ(partner(3, 1), 1); EXPECT_EQ(partner(4, 1), 6); EXPECT_EQ(partner(5, 1), 7); EXPECT_EQ(partner(6, 1), 4); EXPECT_EQ(partner(7, 1), 5); for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 2) { auto n2 = n1 + 1; EXPECT_EQ(partner(n1, 1), ((n1 % 4) ? n1-2 : n1+2)); EXPECT_EQ(partner(n2, 1), ((n1 % 4) ? n2-2 : n2+2)); } } /* * mpi_id_t partner(mpi_id_t node, size_t step); * various steps */ TEST(TdistBitonic_UT, partner_test3) { // step = 2 size_t ts_step = 2; for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 4) { auto n2 = n1 + 1; auto n3 = n1 + 2; auto n4 = n1 + 3; EXPECT_EQ(partner(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4)); EXPECT_EQ(partner(n4, ts_step), ((n1 % 8) ? n4-4 : n4+4)); } // step = 3 ts_step = 3; for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 8) { auto n2 = n1 + 1; auto n3 = n1 + 2; auto n4 = n1 + 3; auto n5 = n1 + 4; auto n6 = n1 + 5; auto n7 = n1 + 6; auto n8 = n1 + 7; EXPECT_EQ(partner(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8)); EXPECT_EQ(partner(n4, ts_step), ((n1 % 16) ? n4-8 : n4+8)); EXPECT_EQ(partner(n5, ts_step), ((n1 % 16) ? n5-8 : n5+8)); EXPECT_EQ(partner(n6, ts_step), ((n1 % 16) ? n6-8 : n6+8)); EXPECT_EQ(partner(n7, ts_step), ((n1 % 16) ? n7-8 : n7+8)); EXPECT_EQ(partner(n8, ts_step), ((n1 % 16) ? n8-8 : n8+8)); } // step = 4 ts_step = 4; for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 16) { auto n2 = n1 + 1; auto n3 = n1 + 2; auto n4 = n1 + 3; auto n5 = n1 + 4; auto n6 = n1 + 5; auto n7 = n1 + 6; auto n8 = n1 + 7; auto n9 = n1 + 8; auto n10 = n1 + 9; auto n11 = n1 + 10; auto n12 = n1 + 11; auto n13 = n1 + 12; auto n14 = n1 + 13; auto n15 = n1 + 14; auto n16 = n1 + 15; EXPECT_EQ(partner(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16)); EXPECT_EQ(partner(n4, ts_step), ((n1 % 32) ? n4-16 : n4+16)); EXPECT_EQ(partner(n5, ts_step), ((n1 % 32) ? n5-16 : n5+16)); EXPECT_EQ(partner(n6, ts_step), ((n1 % 32) ? n6-16 : n6+16)); EXPECT_EQ(partner(n7, ts_step), ((n1 % 32) ? n7-16 : n7+16)); EXPECT_EQ(partner(n8, ts_step), ((n1 % 32) ? n8-16 : n8+16)); EXPECT_EQ(partner(n9, ts_step), ((n1 % 32) ? n9-16 : n9+16)); EXPECT_EQ(partner(n10, ts_step), ((n1 % 32) ? n10-16 : n10+16)); EXPECT_EQ(partner(n11, ts_step), ((n1 % 32) ? n11-16 : n11+16)); EXPECT_EQ(partner(n12, ts_step), ((n1 % 32) ? n12-16 : n12+16)); EXPECT_EQ(partner(n13, ts_step), ((n1 % 32) ? n13-16 : n13+16)); EXPECT_EQ(partner(n14, ts_step), ((n1 % 32) ? n14-16 : n14+16)); EXPECT_EQ(partner(n15, ts_step), ((n1 % 32) ? n15-16 : n15+16)); EXPECT_EQ(partner(n16, ts_step), ((n1 % 32) ? n16-16 : n16+16)); } } /* ================================== keepSmall ================================== */ /* * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * Throw check (Not assert - ASSERT_DEATH) */ TEST(TdistBitonic_UT, keepsmall_test1) { // node and partner must differ or else ... EXPECT_THROW(keepSmall(0, 0, 0), std::runtime_error); EXPECT_THROW(keepSmall(1, 1, 42), std::runtime_error); EXPECT_THROW(keepSmall(7, 7, 42), std::runtime_error); } /* * bool keepsmall(mpi_id_t node, mpi_id_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 = 1; mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* * bool keepsmall(mpi_id_t node, mpi_id_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 = 2; mpi_id_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* * bool keepsmall(mpi_id_t node, mpi_id_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 = 2; mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* * bool keepSmall(mpi_id_t node, mpi_id_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 = 3; mpi_id_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3}; bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* * bool keepSmall(mpi_id_t node, mpi_id_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 = 3; mpi_id_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* * bool keepSmall(mpi_id_t node, mpi_id_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 = 3; mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0}; for (mpi_id_t node = 0 ; node < 8 ; ++node ) { EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } #if 0 TEST(TdistBitonic_UT, distBitonic_test1) { AllData_t ts_Data { ShadowedVec_t (8), ShadowedVec_t (8) }; (unsigned(std::time(nullptr))); for (auto std::srand& v : ts_Data) { std::generate(v.begin(), v.end(), std::rand); } distBitonic(2, ts_Data); auto max = std::numeric_limits::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 { ShadowedVec_t (8), ShadowedVec_t (8), ShadowedVec_t (8), ShadowedVec_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::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 { ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_t (32), ShadowedVec_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::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(); } } #endif