author | Vedran Miletić <rivanvx@gmail.com> |
Sat, 01 Sep 2012 20:57:21 +0200 | |
changeset 9063 | 32755d0516f4 |
parent 7385 | 10beb0e53130 |
child 9266 | d26408b17360 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7256
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6429 | 2 |
/* |
3 |
* Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
|
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 |
* |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
18 |
* Author: He Wu <mdzz@u.washington.edu> |
6429 | 19 |
*/ |
20 |
||
21 |
#include "ns3/basic-energy-source.h" |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
22 |
#include "ns3/wifi-radio-energy-model.h" |
6429 | 23 |
#include "ns3/basic-energy-source-helper.h" |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
24 |
#include "ns3/wifi-radio-energy-model-helper.h" |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
25 |
#include "ns3/energy-source-container.h" |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
26 |
#include "ns3/device-energy-model-container.h" |
6429 | 27 |
#include "ns3/log.h" |
28 |
#include "ns3/test.h" |
|
29 |
#include "ns3/node.h" |
|
30 |
#include "ns3/simulator.h" |
|
31 |
#include "ns3/double.h" |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
32 |
#include "ns3/config.h" |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
33 |
#include "ns3/string.h" |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
34 |
#include "ns3/yans-wifi-helper.h" |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
35 |
#include "ns3/nqos-wifi-mac-helper.h" |
9063
32755d0516f4
Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents:
7385
diff
changeset
|
36 |
#include <cmath> |
6429 | 37 |
|
6546
44bb46a4a35f
fix namespace usage in energy test
Josh Pelkey <jpelkey@gatech.edu>
parents:
6544
diff
changeset
|
38 |
using namespace ns3; |
6429 | 39 |
|
40 |
NS_LOG_COMPONENT_DEFINE ("BasicEnergyModelTestSuite"); |
|
41 |
||
42 |
/** |
|
43 |
* Test case of update remaining energy for BasicEnergySource and |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
44 |
* WifiRadioEnergyModel. |
6429 | 45 |
*/ |
46 |
class BasicEnergyUpdateTest : public TestCase |
|
47 |
{ |
|
48 |
public: |
|
49 |
BasicEnergyUpdateTest (); |
|
50 |
virtual ~BasicEnergyUpdateTest (); |
|
51 |
||
52 |
private: |
|
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
53 |
void DoRun (void); |
6429 | 54 |
|
55 |
/** |
|
56 |
* \param state Radio state to switch to. |
|
57 |
* \return False if no error occurs. |
|
58 |
* |
|
59 |
* Runs simulation for a while, check if final state & remaining energy is |
|
60 |
* correctly updated. |
|
61 |
*/ |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
62 |
bool StateSwitchTest (WifiPhy::State state); |
6429 | 63 |
|
64 |
private: |
|
65 |
double m_timeS; // in seconds |
|
66 |
double m_tolerance; // tolerance for power estimation |
|
67 |
||
68 |
ObjectFactory m_energySource; |
|
69 |
ObjectFactory m_deviceEnergyModel; |
|
70 |
}; |
|
71 |
||
72 |
BasicEnergyUpdateTest::BasicEnergyUpdateTest () |
|
73 |
: TestCase ("Basic energy model update remaining energy test case") |
|
74 |
{ |
|
75 |
m_timeS = 15.5; // idle for 15 seconds before changing state |
|
76 |
m_tolerance = 1.0e-13; // |
|
77 |
} |
|
78 |
||
79 |
BasicEnergyUpdateTest::~BasicEnergyUpdateTest () |
|
80 |
{ |
|
81 |
} |
|
82 |
||
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
83 |
void |
6429 | 84 |
BasicEnergyUpdateTest::DoRun (void) |
85 |
{ |
|
86 |
// set types |
|
87 |
m_energySource.SetTypeId ("ns3::BasicEnergySource"); |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
88 |
m_deviceEnergyModel.SetTypeId ("ns3::WifiRadioEnergyModel"); |
6429 | 89 |
|
90 |
// run state switch tests |
|
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
91 |
NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::IDLE), false, "Problem with state switch test (WifiPhy idle)."); |
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
92 |
NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::CCA_BUSY), false, "Problem with state switch test (WifiPhy cca busy)."); |
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
93 |
NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::TX), false, "Problem with state switch test (WifiPhy tx)."); |
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
94 |
NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::RX), false, "Problem with state switch test (WifiPhy rx)."); |
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
95 |
NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::SWITCHING), false, "Problem with state switch test (WifiPhy switching)."); |
6429 | 96 |
} |
97 |
||
98 |
bool |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
99 |
BasicEnergyUpdateTest::StateSwitchTest (WifiPhy::State state) |
6429 | 100 |
{ |
101 |
// create node |
|
102 |
Ptr<Node> node = CreateObject<Node> (); |
|
103 |
||
104 |
// create energy source |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
105 |
Ptr<BasicEnergySource> source = m_energySource.Create<BasicEnergySource> (); |
6429 | 106 |
// aggregate energy source to node |
107 |
node->AggregateObject (source); |
|
108 |
||
109 |
// create device energy model |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
110 |
Ptr<WifiRadioEnergyModel> model = |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
111 |
m_deviceEnergyModel.Create<WifiRadioEnergyModel> (); |
6429 | 112 |
// set energy source pointer |
113 |
model->SetEnergySource (source); |
|
114 |
// add device energy model to model list in energy source |
|
115 |
source->AppendDeviceEnergyModel (model); |
|
116 |
||
117 |
// retrieve device energy model from energy source |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
118 |
DeviceEnergyModelContainer models = |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
119 |
source->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel"); |
6429 | 120 |
// check list |
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
121 |
NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (false, (models.GetN () == 0), "Model list is empty!"); |
6429 | 122 |
// get pointer |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
123 |
Ptr<WifiRadioEnergyModel> devModel = |
7174
e97fba349adf
energy coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7118
diff
changeset
|
124 |
DynamicCast<WifiRadioEnergyModel> (models.Get (0)); |
6429 | 125 |
// check pointer |
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
126 |
NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL (0, devModel, "NULL pointer to device model!"); |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
127 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
128 |
/* |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
129 |
* The radio will stay IDLE for m_timeS seconds. Then it will switch into a |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
130 |
* different state. |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
131 |
*/ |
6429 | 132 |
|
133 |
// schedule change of state |
|
134 |
Simulator::Schedule (Seconds (m_timeS), |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
135 |
&WifiRadioEnergyModel::ChangeState, devModel, state); |
6429 | 136 |
|
6739
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
137 |
// Calculate remaining energy at simulation stop time |
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
138 |
Simulator::Schedule (Seconds (m_timeS * 2), |
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
139 |
&BasicEnergySource::UpdateEnergySource, source); |
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
140 |
|
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
141 |
double timeDelta = 0.000000001; // 1 nanosecond |
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
142 |
// run simulation; stop just after last scheduled event |
1b903a05007c
don't update model state from a DoDispose() method
Tom Henderson <tomh@tomh.org>
parents:
6699
diff
changeset
|
143 |
Simulator::Stop (Seconds (m_timeS * 2 + timeDelta)); |
6429 | 144 |
Simulator::Run (); |
145 |
||
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
146 |
// energy = current * voltage * time |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
147 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
148 |
// calculate idle power consumption |
6429 | 149 |
double estRemainingEnergy = source->GetInitialEnergy (); |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
150 |
double voltage = source->GetSupplyVoltage (); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
151 |
estRemainingEnergy -= devModel->GetIdleCurrentA () * voltage * m_timeS; |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
152 |
|
6429 | 153 |
/* |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
154 |
* Manually calculate the number of periodic updates performed by the source. |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
155 |
* This is to check if the periodic updates are performed correctly. |
6429 | 156 |
*/ |
157 |
double actualTime = m_timeS; |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
158 |
actualTime /= source->GetEnergyUpdateInterval ().GetSeconds (); |
6429 | 159 |
actualTime = floor (actualTime); // rounding for update interval |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
160 |
actualTime *= source->GetEnergyUpdateInterval ().GetSeconds (); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
161 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
162 |
// calculate new state power consumption |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
163 |
double current = 0.0; |
6429 | 164 |
switch (state) |
165 |
{ |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
166 |
case WifiPhy::IDLE: |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
167 |
current = devModel->GetIdleCurrentA (); |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
168 |
break; |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
169 |
case WifiPhy::CCA_BUSY: |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
170 |
current = devModel->GetCcaBusyCurrentA (); |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
171 |
break; |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
172 |
case WifiPhy::TX: |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
173 |
current = devModel->GetTxCurrentA (); |
6429 | 174 |
break; |
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
175 |
case WifiPhy::RX: |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
176 |
current = devModel->GetRxCurrentA (); |
6429 | 177 |
break; |
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
178 |
case WifiPhy::SWITCHING: |
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
179 |
current = devModel->GetSwitchingCurrentA (); |
6429 | 180 |
break; |
181 |
default: |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
182 |
NS_FATAL_ERROR ("Undefined radio state: " << state); |
6429 | 183 |
break; |
184 |
} |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
185 |
estRemainingEnergy -= current * voltage * m_timeS; |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
186 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
187 |
// obtain remaining energy from source |
6429 | 188 |
double remainingEnergy = source->GetRemainingEnergy (); |
7118
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
189 |
NS_LOG_DEBUG ("Remaining energy is " << remainingEnergy); |
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
190 |
NS_LOG_DEBUG ("Estimated remaining energy is " << estRemainingEnergy); |
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
191 |
NS_LOG_DEBUG ("Difference is " << estRemainingEnergy - remainingEnergy); |
6429 | 192 |
// check remaining energy |
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
193 |
NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (remainingEnergy, estRemainingEnergy, m_tolerance, |
7174
e97fba349adf
energy coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7118
diff
changeset
|
194 |
"Incorrect remaining energy!"); |
6429 | 195 |
|
196 |
// obtain radio state |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
197 |
WifiPhy::State endState = devModel->GetCurrentState (); |
7118
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
198 |
NS_LOG_DEBUG ("Radio state is " << endState); |
6429 | 199 |
// check end state |
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
200 |
NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (endState, state, "Incorrect end state!"); |
6548 | 201 |
Simulator::Destroy (); |
6429 | 202 |
|
203 |
return false; // no error |
|
204 |
} |
|
205 |
||
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
206 |
// -------------------------------------------------------------------------- // |
6429 | 207 |
|
208 |
/** |
|
209 |
* Test case of energy depletion handling for BasicEnergySource and |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
210 |
* WifiRadioEnergyModel. |
6429 | 211 |
*/ |
212 |
class BasicEnergyDepletionTest : public TestCase |
|
213 |
{ |
|
214 |
public: |
|
215 |
BasicEnergyDepletionTest (); |
|
216 |
virtual ~BasicEnergyDepletionTest (); |
|
217 |
||
218 |
private: |
|
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
219 |
void DoRun (void); |
6429 | 220 |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
221 |
/** |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
222 |
* Callback invoked when energy is drained from source. |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
223 |
*/ |
6429 | 224 |
void DepletionHandler (void); |
225 |
||
226 |
/** |
|
227 |
* \param simTimeS Simulation time, in seconds. |
|
228 |
* \param updateIntervalS Device model update interval, in seconds. |
|
229 |
* \return False if all is good. |
|
230 |
* |
|
231 |
* Runs simulation with specified simulation time and update interval. |
|
232 |
*/ |
|
233 |
bool DepletionTestCase (double simTimeS, double updateIntervalS); |
|
234 |
||
235 |
private: |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
236 |
int m_numOfNodes; // number of nodes in simulation |
6429 | 237 |
int m_callbackCount; // counter for # of callbacks invoked |
238 |
double m_simTimeS; // maximum simulation time, in seconds |
|
239 |
double m_timeStepS; // simulation time step size, in seconds |
|
240 |
double m_updateIntervalS; // update interval of each device model |
|
241 |
||
242 |
}; |
|
243 |
||
244 |
BasicEnergyDepletionTest::BasicEnergyDepletionTest () |
|
245 |
: TestCase ("Basic energy model energy depletion test case") |
|
246 |
{ |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
247 |
m_numOfNodes = 10; |
6429 | 248 |
m_callbackCount = 0; |
249 |
m_simTimeS = 4.5; |
|
250 |
m_timeStepS = 0.5; |
|
251 |
m_updateIntervalS = 1.5; |
|
252 |
} |
|
253 |
||
254 |
BasicEnergyDepletionTest::~BasicEnergyDepletionTest () |
|
255 |
{ |
|
256 |
} |
|
257 |
||
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
258 |
void |
6429 | 259 |
BasicEnergyDepletionTest::DoRun (void) |
260 |
{ |
|
261 |
/* |
|
262 |
* Run simulation with different simulation time and update interval. |
|
263 |
*/ |
|
264 |
for (double simTimeS = 0.0; simTimeS <= m_simTimeS; simTimeS += m_timeStepS) |
|
265 |
{ |
|
266 |
for (double updateIntervalS = 0.5; updateIntervalS <= m_updateIntervalS; |
|
267 |
updateIntervalS += m_timeStepS) |
|
268 |
{ |
|
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
269 |
NS_TEST_ASSERT_MSG_EQ (DepletionTestCase (simTimeS, updateIntervalS), false, "Depletion test case problem."); |
6429 | 270 |
// reset callback count |
271 |
m_callbackCount = 0; |
|
272 |
} |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
void |
|
277 |
BasicEnergyDepletionTest::DepletionHandler (void) |
|
278 |
{ |
|
279 |
m_callbackCount++; |
|
280 |
} |
|
281 |
||
282 |
bool |
|
283 |
BasicEnergyDepletionTest::DepletionTestCase (double simTimeS, |
|
284 |
double updateIntervalS) |
|
285 |
{ |
|
286 |
// create node |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
287 |
NodeContainer c; |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
288 |
c.Create (m_numOfNodes); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
289 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
290 |
std::string phyMode ("DsssRate1Mbps"); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
291 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
292 |
// disable fragmentation for frames below 2200 bytes |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
293 |
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
294 |
StringValue ("2200")); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
295 |
// turn off RTS/CTS for frames below 2200 bytes |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
296 |
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
297 |
StringValue ("2200")); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
298 |
// Fix non-unicast data rate to be the same as that of unicast |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
299 |
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
300 |
StringValue (phyMode)); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
301 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
302 |
// install YansWifiPhy |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
303 |
WifiHelper wifi; |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
304 |
wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
305 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
306 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
307 |
/* |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
308 |
* This is one parameter that matters when using FixedRssLossModel, set it to |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
309 |
* zero; otherwise, gain will be added. |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
310 |
*/ |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
311 |
wifiPhy.Set ("RxGain", DoubleValue (0)); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
312 |
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
313 |
wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
314 |
|
7252
c8200621e252
rerun check-style.py with uncrustify-0.58
Tom Henderson <tomh@tomh.org>
parents:
7174
diff
changeset
|
315 |
YansWifiChannelHelper wifiChannel; |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
316 |
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
317 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
318 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
319 |
// Add a non-QoS upper MAC, and disable rate control |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
320 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
321 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7252
diff
changeset
|
322 |
"DataMode", StringValue (phyMode), |
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7252
diff
changeset
|
323 |
"ControlMode", StringValue (phyMode)); |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
324 |
// Set it to ad-hoc mode |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
325 |
wifiMac.SetType ("ns3::AdhocWifiMac"); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
326 |
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c); |
6429 | 327 |
|
328 |
/* |
|
329 |
* Create and install energy source and a single basic radio energy model on |
|
330 |
* the node using helpers. |
|
331 |
*/ |
|
332 |
// source helper |
|
333 |
BasicEnergySourceHelper basicSourceHelper; |
|
334 |
// set energy to 0 so that we deplete energy at the beginning of simulation |
|
335 |
basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0)); |
|
336 |
// set update interval |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
337 |
basicSourceHelper.Set ("PeriodicEnergyUpdateInterval", |
6429 | 338 |
TimeValue (Seconds (updateIntervalS))); |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
339 |
// install source |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
340 |
EnergySourceContainer sources = basicSourceHelper.Install (c); |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
341 |
|
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
342 |
// device energy model helper |
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
343 |
WifiRadioEnergyModelHelper radioEnergyHelper; |
6429 | 344 |
// set energy depletion callback |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
345 |
WifiRadioEnergyModel::WifiRadioEnergyDepletionCallback callback = |
6429 | 346 |
MakeCallback (&BasicEnergyDepletionTest::DepletionHandler, this); |
347 |
radioEnergyHelper.SetDepletionCallback (callback); |
|
348 |
// install on node |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
349 |
DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources); |
6429 | 350 |
|
351 |
// run simulation |
|
352 |
Simulator::Stop (Seconds (simTimeS)); |
|
353 |
Simulator::Run (); |
|
354 |
Simulator::Destroy (); |
|
355 |
||
7118
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
356 |
NS_LOG_DEBUG ("Simulation time = " << simTimeS << "s"); |
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
357 |
NS_LOG_DEBUG ("Update interval = " << updateIntervalS << "s"); |
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
358 |
NS_LOG_DEBUG ("Expected callback count is " << m_numOfNodes); |
a18895058cb1
do not dump stuff on std::cout during tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
7061
diff
changeset
|
359 |
NS_LOG_DEBUG ("Actual callback count is " << m_callbackCount); |
6429 | 360 |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
361 |
// check result, call back should only be invoked once |
6775
0783f42a364b
Make test cases not return bool valuesMake all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6774
diff
changeset
|
362 |
NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (m_numOfNodes, m_callbackCount, "Not all callbacks are invoked!"); |
6429 | 363 |
|
364 |
return false; |
|
365 |
} |
|
366 |
||
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
367 |
// -------------------------------------------------------------------------- // |
6429 | 368 |
|
369 |
/** |
|
370 |
* Unit test suite for energy model. Although the test suite involves 2 modules |
|
371 |
* it is still considered a unit test. Because a DeviceEnergyModel cannot live |
|
372 |
* without an EnergySource. |
|
373 |
*/ |
|
374 |
class BasicEnergyModelTestSuite : public TestSuite |
|
375 |
{ |
|
376 |
public: |
|
377 |
BasicEnergyModelTestSuite (); |
|
378 |
}; |
|
379 |
||
380 |
BasicEnergyModelTestSuite::BasicEnergyModelTestSuite () |
|
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
381 |
: TestSuite ("basic-energy-model", UNIT) |
6429 | 382 |
{ |
383 |
AddTestCase (new BasicEnergyUpdateTest); |
|
6699
d8909a1fd0ff
RV battery model and WiFi energy example
He Wu <mdzz@u.washington.edu>
parents:
6548
diff
changeset
|
384 |
AddTestCase (new BasicEnergyDepletionTest); |
6429 | 385 |
} |
386 |
||
387 |
// create an instance of the test suite |
|
6774
034f362af24d
Make all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6739
diff
changeset
|
388 |
static BasicEnergyModelTestSuite g_energyModelTestSuite; |
6535
0a90ef2b29a9
Apply energy model changes of http://codereview.appspot.com/1008043/show patchset 13
He Wu <mdzz@u.washington.edu>
parents:
6430
diff
changeset
|
389 |