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 
41 class PairObject : public Object
42 {
43 public:
44  PairObject ();
45  virtual ~PairObject ();
46 
47  static
48  TypeId GetTypeId ();
49 
50  friend std::ostream &operator <<(std::ostream &os, const PairObject &obj);
51 
52 private:
53  std::pair <std::string, std::string> m_stringPair;
54  std::pair <double, int> m_doubleIntPair;
55 };
56 
58 {
59 
60 }
61 
63 {
64 
65 }
66 
67 TypeId
69 {
70  static TypeId tid = TypeId ("ns3::PairObject")
71  .SetParent<Object> ()
72  .SetGroupName("Test")
73  .AddConstructor<PairObject> ()
74  .AddAttribute ("StringPair", "Pair: string, string",
76  MakePairAccessor <StringValue, StringValue> (&PairObject::m_stringPair),
77  MakePairChecker<StringValue, StringValue> (MakeStringChecker (), MakeStringChecker ()))
78  .AddAttribute ("DoubleIntPair", "Pair: double int",
79  // the container value container differs from the underlying object
81  MakePairAccessor <DoubleValue, IntegerValue> (&PairObject::m_doubleIntPair),
82  MakePairChecker<DoubleValue, IntegerValue> (MakeDoubleChecker<double> (), MakeIntegerChecker<int> ()))
83  ;
84  return tid;
85 }
86 
87 std::ostream &
88 operator << (std::ostream &os, const PairObject &obj)
89 {
90  os << "StringPair = { " << obj.m_stringPair << " } ";
91  os << "DoubleIntPair = { " << obj.m_doubleIntPair << " }";
92  return os;
93 }
94 
95 /* Test instantiation, initialization, access */
97 {
98 public:
100  virtual ~PairValueTestCase () {}
101 
102 private:
103  virtual void DoRun ();
104 };
105 
107  : TestCase ("test instantiation, initialization, access")
108 {
109 
110 }
111 
112 void
114 {
115  {
116  std::pair<const int, double> ref = {1, 2.4};
117 
119 
120  std::pair<const int, double> rv = ac.Get ();
121  NS_TEST_ASSERT_MSG_EQ (rv, ref, "Attribute value does not equal original");
122  }
123 
124  {
125  std::pair<const std::string, double> ref = {"hello", 3.14};
126 
128  ac.Set (ref);
129  std::pair<const std::string, double> rv = ac.Get ();
130  NS_TEST_ASSERT_MSG_EQ (rv, ref, "Attribute value does not equal original");
131  }
132 
133 }
134 
136 {
137 public:
139 
140  void DoRun ();
141 };
142 
144  : TestCase ("test setting through attribute interface")
145 {}
146 
147 void
149 {
150  auto p = CreateObject <PairObject> ();
151  p->SetAttribute ("StringPair", PairValue<StringValue, StringValue> (std::make_pair ("hello", "world")));
152  p->SetAttribute ("DoubleIntPair", PairValue<DoubleValue, IntegerValue> (std::make_pair (3.14, 31)));
153 
154  std::ostringstream oss;
155  oss << *p;
156 
157  std::ostringstream ref;
158  ref << "StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }";
159 
160  NS_TEST_ASSERT_MSG_EQ ((oss.str ()), (ref.str ()), "Pairs not correctly set");
161 }
162 
164 {
165  public:
167 };
168 
170  : TestSuite ("pair-value-test-suite", UNIT)
171 {
172  AddTestCase (new PairValueTestCase (), TestCase::QUICK);
173  AddTestCase (new PairValueSettingsTestCase (), TestCase::QUICK);
174 }
175 
A suite of tests to run.
Definition: test.h:1343
static PairValueTestSuite pairValueTestSuite
Static variable for test initialization.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
virtual void DoRun()
Implementation to actually run this TestCase.
std::pair< std::string, std::string > m_stringPair
encapsulates test code
Definition: test.h:1153
static TypeId GetTypeId()
void Set(const result_type &value)
Set the stored value.
Definition: pair.h:323
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#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:166
std::pair< double, int > m_doubleIntPair
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
result_type Get(void) const
Get the stored value as a std::pair.
Definition: pair.h:316
A base class which provides memory management and object aggregation.
Definition: object.h:87
Hold objects of type std::pair<A, B>.
Definition: pair.h:47
a unique identifier for an interface.
Definition: type-id.h:58
void DoRun()
Implementation to actually run this TestCase.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923