A Discrete-Event Network Simulator
API
pair-value-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018 Caliola Engineering, LLC.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Jared Dulmage <jared.dulmage@caliola.com>
19  */
20 
21 #include <ns3/test.h>
22 #include <ns3/log.h>
23 #include <ns3/pair.h>
24 #include <ns3/double.h>
25 #include <ns3/integer.h>
26 #include <ns3/string.h>
27 #include <ns3/ptr.h>
28 #include <ns3/object.h>
29 #include <ns3/type-id.h>
30 
31 #include <algorithm>
32 #include <iterator>
33 #include <sstream>
34 #include <utility>
35 
36 
37 using namespace ns3;
38 
39 NS_LOG_COMPONENT_DEFINE ("PairTestSuite");
40 
57 class PairObject : public Object
58 {
59 public:
60  PairObject ();
61  virtual ~PairObject ();
62 
67  static
68  TypeId GetTypeId ();
69 
70  friend std::ostream &operator <<(std::ostream &os, const PairObject &obj);
71 
72 private:
73  std::pair <std::string, std::string> m_stringPair;
74  std::pair <double, int> m_doubleIntPair;
75 };
76 
78 {
79 
80 }
81 
83 {
84 
85 }
86 
87 TypeId
89 {
90  static TypeId tid = TypeId ("ns3::PairObject")
91  .SetParent<Object> ()
92  .SetGroupName("Test")
93  .AddConstructor<PairObject> ()
94  .AddAttribute ("StringPair", "Pair: string, string",
96  MakePairAccessor <StringValue, StringValue> (&PairObject::m_stringPair),
97  MakePairChecker<StringValue, StringValue> (MakeStringChecker (), MakeStringChecker ()))
98  .AddAttribute ("DoubleIntPair", "Pair: double int",
99  // the container value container differs from the underlying object
101  MakePairAccessor <DoubleValue, IntegerValue> (&PairObject::m_doubleIntPair),
102  MakePairChecker<DoubleValue, IntegerValue> (MakeDoubleChecker<double> (), MakeIntegerChecker<int> ()))
103  ;
104  return tid;
105 }
106 
114 std::ostream &
115 operator << (std::ostream &os, const PairObject &obj)
116 {
117  os << "StringPair = { " << obj.m_stringPair << " } ";
118  os << "DoubleIntPair = { " << obj.m_doubleIntPair << " }";
119  return os;
120 }
121 
128 {
129 public:
131  virtual ~PairValueTestCase () {}
132 
133 private:
134  virtual void DoRun ();
135 };
136 
138  : TestCase ("test instantiation, initialization, access")
139 {
140 
141 }
142 
143 void
145 {
146  {
147  std::pair<const int, double> ref = {1, 2.4};
148 
150 
151  std::pair<const int, double> rv = ac.Get ();
152  NS_TEST_ASSERT_MSG_EQ (rv, ref, "Attribute value does not equal original");
153  }
154 
155  {
156  std::pair<const std::string, double> ref = {"hello", 3.14};
157 
159  ac.Set (ref);
160  std::pair<const std::string, double> rv = ac.Get ();
161  NS_TEST_ASSERT_MSG_EQ (rv, ref, "Attribute value does not equal original");
162  }
163 
164 }
165 
172 {
173 public:
175 
176  void DoRun ();
177 };
178 
180  : TestCase ("test setting through attribute interface")
181 {}
182 
183 void
185 {
186  auto p = CreateObject <PairObject> ();
187  p->SetAttribute ("StringPair", PairValue<StringValue, StringValue> (std::make_pair ("hello", "world")));
188  p->SetAttribute ("DoubleIntPair", PairValue<DoubleValue, IntegerValue> (std::make_pair (3.14, 31)));
189 
190  std::ostringstream oss;
191  oss << *p;
192 
193  std::ostringstream ref;
194  ref << "StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }";
195 
196  NS_TEST_ASSERT_MSG_EQ ((oss.str ()), (ref.str ()), "Pairs not correctly set");
197 }
198 
205 {
206  public:
208 };
209 
211  : TestSuite ("pair-value-test-suite", UNIT)
212 {
213  AddTestCase (new PairValueTestCase (), TestCase::QUICK);
214  AddTestCase (new PairValueSettingsTestCase (), TestCase::QUICK);
215 }
216 
Object holding pairs of values.
static TypeId GetTypeId()
Get the type ID.
std::pair< double, int > m_doubleIntPair
A pair of double, int.
std::pair< std::string, std::string > m_stringPair
A string pair.
Pair test - test setting through attribute interface.
void DoRun()
Implementation to actually run this TestCase.
Pair test - Test instantiation, initialization, access.
virtual void DoRun()
Implementation to actually run this TestCase.
The pair-value Test Suite.
A base class which provides memory management and object aggregation.
Definition: object.h:88
Hold objects of type std::pair<A, B>.
Definition: pair.h:56
void Set(const result_type &value)
Set the value.
Definition: pair.h:337
result_type Get(void) const
Get the stored value as a std::pair.
Definition: pair.h:330
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
static PairValueTestSuite g_pairValueTestSuite
Static variable for test initialization.