| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "stk_util/parallel/BroadcastArg.hpp" // for BroadcastArg |
| 37 |
#include "stk_util/parallel/Parallel.hpp" // for MPI_Comm_rank, MPI_COMM_WORLD |
| 38 |
#include "stk_util/stk_config.h" // for STK_HAS_MPI |
| 39 |
#include <algorithm> // for copy |
| 40 |
#include <iostream> // for operator<<, basic_ostream::operator<<, bas... |
| 41 |
#include <memory> // for allocator_traits<>::value_type |
| 42 |
#include <string> // for string, basic_string, char_traits |
| 43 |
#include <vector> // for vector |
| 44 |
|
| 45 |
|
| 46 |
#if defined(STK_HAS_MPI) // means that MPI is available |
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
void makeArg(const std::vector<std::string>& strings, int& argc, char**& argv) { |
| 51 |
argc = strings.size(); |
| 52 |
if(argc == 0) { |
| 53 |
argv=nullptr; |
| 54 |
} else { |
| 55 |
argv = new char*[argc]; |
| 56 |
} |
| 57 |
for(int i=0; i<argc; ++i) { |
| 58 |
argv[i] = new char[strings[i].size()+1]; |
| 59 |
std::copy(strings[i].begin(), strings[i].end(), argv[i]); |
| 60 |
argv[i][strings[i].size()] = '\0'; |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
void delArg(int& argc, char**& argv) { |
| 65 |
for(int i=0; i<argc; ++i) { |
| 66 |
delete [] argv[i]; |
| 67 |
} |
| 68 |
delete [] argv; |
| 69 |
argv=nullptr; |
| 70 |
argc=0; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
TEST(BroadcastArg, emptyList) |
| 75 |
{ |
| 76 |
int parallel_rank; |
| 77 |
MPI_Comm_rank(MPI_COMM_WORLD, ¶llel_rank); |
| 78 |
int argc=0; |
| 79 |
char** argv = nullptr; |
| 80 |
if(parallel_rank==0) { |
| 81 |
makeArg({}, argc, argv); |
| 82 |
} |
| 83 |
stk::BroadcastArg bcast(MPI_COMM_WORLD, argc, argv); |
| 84 |
delArg(argc, argv); |
| 85 |
EXPECT_EQ(bcast.m_argc, 0); |
| 86 |
EXPECT_EQ(bcast.m_argv, nullptr); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(BroadcastArg, oneEntry) |
| 90 |
{ |
| 91 |
int parallel_rank; |
| 92 |
MPI_Comm_rank(MPI_COMM_WORLD, ¶llel_rank); |
| 93 |
int argc=0; |
| 94 |
char** argv = nullptr; |
| 95 |
if(parallel_rank==0) { |
| 96 |
makeArg({"hello"}, argc, argv); |
| 97 |
} |
| 98 |
stk::BroadcastArg bcast(MPI_COMM_WORLD, argc, argv); |
| 99 |
|
| 100 |
std::cout<<parallel_rank<<": AAA: "<<bcast.m_argv[0]<<std::endl; |
| 101 |
|
| 102 |
|
| 103 |
delArg(argc, argv); |
| 104 |
EXPECT_EQ(bcast.m_argc, 1); |
| 105 |
EXPECT_EQ(std::string(bcast.m_argv[0]),"hello"); |
| 106 |
} |
| 107 |
|
| 108 |
TEST(BroadcastArg, twoEntry) |
| 109 |
{ |
| 110 |
int parallel_rank; |
| 111 |
MPI_Comm_rank(MPI_COMM_WORLD, ¶llel_rank); |
| 112 |
int argc=0; |
| 113 |
char** argv = nullptr; |
| 114 |
if(parallel_rank==0) { |
| 115 |
makeArg({"hello","world"}, argc, argv); |
| 116 |
} |
| 117 |
stk::BroadcastArg bcast(MPI_COMM_WORLD, argc, argv); |
| 118 |
|
| 119 |
delArg(argc, argv); |
| 120 |
EXPECT_EQ(bcast.m_argc, 2); |
| 121 |
EXPECT_EQ(std::string(bcast.m_argv[0]),"hello"); |
| 122 |
EXPECT_EQ(std::string(bcast.m_argv[1]),"world"); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(BroadcastArg, emptyStrings) |
| 126 |
{ |
| 127 |
int parallel_rank; |
| 128 |
MPI_Comm_rank(MPI_COMM_WORLD, ¶llel_rank); |
| 129 |
int argc=0; |
| 130 |
char** argv = nullptr; |
| 131 |
if(parallel_rank==0) { |
| 132 |
makeArg({"", "hello", "", "world", ""}, argc, argv); |
| 133 |
} |
| 134 |
stk::BroadcastArg bcast(MPI_COMM_WORLD, argc, argv); |
| 135 |
|
| 136 |
delArg(argc, argv); |
| 137 |
EXPECT_EQ(bcast.m_argc, 5); |
| 138 |
EXPECT_EQ(std::string(bcast.m_argv[0]),""); |
| 139 |
EXPECT_EQ(std::string(bcast.m_argv[1]),"hello"); |
| 140 |
EXPECT_EQ(std::string(bcast.m_argv[2]),""); |
| 141 |
EXPECT_EQ(std::string(bcast.m_argv[3]),"world"); |
| 142 |
EXPECT_EQ(std::string(bcast.m_argv[4]),""); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
#endif // STK_HAS_MPI |
| 149 |
|