TIMPI
message_tag_unit.C
Go to the documentation of this file.
1 #include <timpi/communicator.h>
2 #include <timpi/message_tag.h>
3 #include <timpi/timpi_init.h>
4 
5 #define TIMPI_UNIT_ASSERT(expr) \
6  if (!(expr)) \
7  timpi_error();
8 
9 using namespace TIMPI;
10 
12 
14  {
15  // We need to duplicate the communicator first, because the
16  // original might already have tags used by other unit tests
17 
18  TIMPI::Communicator newcomm;
19 
20  TestCommWorld->duplicate(newcomm);
21 
22  const int n_vals = 5;
23  const int n_vals_in_scope = 3;
24  std::vector<int> vals(n_vals);
25 
26  {
27  std::vector<TIMPI::MessageTag> tags(n_vals_in_scope);
28  for (int i=0; i != n_vals_in_scope; ++i)
29  {
30  tags[i] = newcomm.get_unique_tag();
31  vals[i] = tags[i].value();
32  for (int j=0; j != i; ++j)
33  {
34  TIMPI_UNIT_ASSERT(vals[i] != vals[j]);
35  }
36  }
37  }
38 
39  // Even after we go out of scope those values should be used up
40  for (int i=n_vals_in_scope; i != n_vals; ++i)
41  {
42  TIMPI::MessageTag another_tag = newcomm.get_unique_tag();
43  vals[i] = another_tag.value();
44  for (int j=0; j != i; ++j)
45  {
46  TIMPI_UNIT_ASSERT(vals[i] != vals[j]);
47  }
48  }
49  }
50 
51 
52 
54  {
55  // Here we'll use the standard communicator, because even if it
56  // used these tags in other contexts it should have freed them for
57  // reuse later.
58 
59  const int requests[] = {2, 4, 6, 8, 8, 6, 8, 123, 3141, 3142};
60 
61  for (const int i : requests)
62  {
63  TIMPI::MessageTag manual_tag =
65  TIMPI_UNIT_ASSERT(i == manual_tag.value());
66 
67  TIMPI::MessageTag tag_copy = manual_tag;
68  TIMPI_UNIT_ASSERT(i == tag_copy.value());
69 
70  TIMPI::MessageTag tag_move = std::move(tag_copy);
71  TIMPI_UNIT_ASSERT(i == tag_move.value());
72 
73  TIMPI::MessageTag stupidly_manual_tag(i);
74  TIMPI_UNIT_ASSERT(i == stupidly_manual_tag.value());
75  }
76  }
77 
78 
80  {
81  // Try to request the same tag twice, make sure we don't get it
82  // the second time.
83 
84  const int requests[] = {2, 4, 6, 8, 8, 6, 8, 123, 3141, 3142};
85 
86  for (const int i : requests)
87  {
88  TIMPI::MessageTag manual_tag =
90  TIMPI_UNIT_ASSERT(i == manual_tag.value());
91 
92  TIMPI::MessageTag dup_manual_tag =
94  TIMPI_UNIT_ASSERT(i != dup_manual_tag.value());
95 
96  TIMPI::MessageTag tag_copy = manual_tag;
97  TIMPI_UNIT_ASSERT(i == tag_copy.value());
98 
99  TIMPI::MessageTag tag_move = std::move(tag_copy);
100  TIMPI_UNIT_ASSERT(i == tag_move.value());
101  }
102  }
103 
104 int main(int argc, const char * const * argv)
105 {
106  TIMPI::TIMPIInit init(argc, argv);
107  TestCommWorld = &init.comm();
108 
112 
113  return 0;
114 }
int main(int argc, const char *const *argv)
int value() const
Definition: message_tag.h:92
MessageTag get_unique_tag(int tagvalue=MessageTag::invalid_tag) const
Get a tag that is unique to this Communicator.
Definition: communicator.C:251
The TIMPIInit class, when constructed, initializes any dependent libraries (e.g.
Definition: timpi_init.h:57
Communicator * TestCommWorld
Encapsulates the MPI_Comm object.
Definition: communicator.h:108
Encapsulates the MPI tag integers.
Definition: message_tag.h:46
void testGetUniqueTagManual()
void testGetUniqueTagOverlap()
const Communicator & comm() const
Returns the Communicator created by this object, which will be a compatibility shim if MPI is not ena...
Definition: timpi_init.h:100
void duplicate(const Communicator &comm)
Definition: communicator.C:137
void testGetUniqueTagAuto()