author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Wed, 14 Nov 2007 12:36:08 +0100 | |
changeset 2104 | 1ba670fdde06 |
parent 2103 | ae7144199052 |
child 2105 | e883a100109a |
permissions | -rw-r--r-- |
2097 | 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
#ifdef RUN_SELF_TESTS |
|
3 |
||
4 |
#include "ns3/test.h" |
|
5 |
#include "ns3/simulator.h" |
|
6 |
#include "dcf-manager.h" |
|
7 |
#include "mac-parameters.h" |
|
8 |
||
9 |
namespace ns3 { |
|
10 |
||
11 |
class DcfManagerTest; |
|
12 |
||
13 |
class DcfStateTest : public DcfState |
|
14 |
{ |
|
15 |
public: |
|
16 |
DcfStateTest (DcfManagerTest *test, uint32_t i); |
|
17 |
private: |
|
18 |
virtual bool NeedsAccess (void) const; |
|
19 |
virtual void NotifyAccessGranted (void); |
|
20 |
virtual void NotifyInternalCollision (void); |
|
21 |
virtual void NotifyCollision (void); |
|
22 |
||
23 |
DcfManagerTest *m_test; |
|
24 |
uint32_t m_i; |
|
25 |
}; |
|
26 |
||
27 |
||
28 |
class DcfManagerTest : public Test |
|
29 |
{ |
|
30 |
public: |
|
31 |
DcfManagerTest (); |
|
32 |
virtual bool RunTests (void); |
|
33 |
||
34 |
||
35 |
void NotifyAccessGranted (uint32_t i); |
|
36 |
void NotifyInternalCollision (uint32_t i); |
|
37 |
void NotifyCollision (uint32_t i); |
|
38 |
||
39 |
||
40 |
private: |
|
2104
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
41 |
void StartTest (uint64_t slotTime, uint64_t ackTxDuration); |
2097 | 42 |
void AddDcfState (uint32_t cwMin, uint32_t cwMax, uint32_t aifsn); |
43 |
void EndTest (void); |
|
44 |
void ExpectAccessGranted (uint64_t time, uint32_t from); |
|
2098 | 45 |
void ExpectInternalCollision (uint64_t time, uint32_t from); |
46 |
void ExpectCollision (uint64_t time, uint32_t from); |
|
47 |
void AddRxOkEvt (uint64_t at, uint64_t duration); |
|
48 |
void AddRxErrorEvt (uint64_t at, uint64_t duration); |
|
49 |
void AddTxEvt (uint64_t at, uint64_t duration); |
|
50 |
void AddNavReset (uint64_t at, uint64_t duration); |
|
51 |
void AddNavStart (uint64_t at, uint64_t duration); |
|
52 |
void AddAccessRequest (uint64_t time, uint32_t from); |
|
53 |
||
2101
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
54 |
typedef std::vector<DcfStateTest *> DcfStates; |
2098 | 55 |
typedef std::list<std::pair<uint64_t, uint32_t> > ExpectedEvent; |
2097 | 56 |
|
57 |
DcfManager *m_dcfManager; |
|
58 |
MacParameters *m_parameters; |
|
59 |
DcfStates m_dcfStates; |
|
2098 | 60 |
ExpectedEvent m_expectedAccessGranted; |
61 |
ExpectedEvent m_expectedInternalCollision; |
|
62 |
ExpectedEvent m_expectedCollision; |
|
2097 | 63 |
bool m_result; |
64 |
}; |
|
65 |
||
66 |
||
67 |
||
68 |
DcfStateTest::DcfStateTest (DcfManagerTest *test, uint32_t i) |
|
69 |
: m_test (test), m_i(i) |
|
70 |
{} |
|
71 |
bool |
|
72 |
DcfStateTest::NeedsAccess (void) const |
|
73 |
{ |
|
74 |
return true; |
|
75 |
} |
|
76 |
void |
|
77 |
DcfStateTest::NotifyAccessGranted (void) |
|
78 |
{ |
|
79 |
m_test->NotifyAccessGranted (m_i); |
|
80 |
} |
|
81 |
void |
|
82 |
DcfStateTest::NotifyInternalCollision (void) |
|
83 |
{ |
|
2103
ae7144199052
add debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2102
diff
changeset
|
84 |
UpdateFailedCw (); |
ae7144199052
add debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2102
diff
changeset
|
85 |
StartBackoffNow (0); |
2097 | 86 |
m_test->NotifyInternalCollision (m_i); |
87 |
} |
|
88 |
void |
|
89 |
DcfStateTest::NotifyCollision (void) |
|
90 |
{ |
|
2103
ae7144199052
add debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2102
diff
changeset
|
91 |
UpdateFailedCw (); |
ae7144199052
add debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2102
diff
changeset
|
92 |
StartBackoffNow (0); |
2097 | 93 |
m_test->NotifyCollision (m_i); |
94 |
} |
|
95 |
||
96 |
||
97 |
||
98 |
DcfManagerTest::DcfManagerTest () |
|
99 |
: Test ("DcfManager") |
|
100 |
{} |
|
101 |
||
102 |
void |
|
103 |
DcfManagerTest::NotifyAccessGranted (uint32_t i) |
|
104 |
{ |
|
105 |
bool result = true; |
|
2102
694a2c0ccacc
a few more test asserts
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2101
diff
changeset
|
106 |
NS_TEST_ASSERT (!m_expectedAccessGranted.empty ()); |
2097 | 107 |
std::pair<uint64_t, uint32_t> expected = m_expectedAccessGranted.front (); |
2098 | 108 |
m_expectedAccessGranted.pop_front (); |
2097 | 109 |
NS_TEST_ASSERT_EQUAL (MicroSeconds (expected.first), Simulator::Now ()); |
110 |
NS_TEST_ASSERT_EQUAL (expected.second, i); |
|
111 |
if (!result) |
|
112 |
{ |
|
113 |
m_result = result; |
|
114 |
} |
|
115 |
} |
|
116 |
void |
|
117 |
DcfManagerTest::NotifyInternalCollision (uint32_t i) |
|
2098 | 118 |
{ |
119 |
bool result = true; |
|
2102
694a2c0ccacc
a few more test asserts
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2101
diff
changeset
|
120 |
NS_TEST_ASSERT (!m_expectedInternalCollision.empty ()); |
2098 | 121 |
std::pair<uint64_t, uint32_t> expected = m_expectedInternalCollision.front (); |
122 |
m_expectedInternalCollision.pop_front (); |
|
123 |
NS_TEST_ASSERT_EQUAL (MicroSeconds (expected.first), Simulator::Now ()); |
|
124 |
NS_TEST_ASSERT_EQUAL (expected.second, i); |
|
125 |
if (!result) |
|
126 |
{ |
|
127 |
m_result = result; |
|
128 |
} |
|
129 |
} |
|
2097 | 130 |
void |
131 |
DcfManagerTest::NotifyCollision (uint32_t i) |
|
2098 | 132 |
{ |
133 |
bool result = true; |
|
2102
694a2c0ccacc
a few more test asserts
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2101
diff
changeset
|
134 |
NS_TEST_ASSERT (!m_expectedCollision.empty ()); |
2098 | 135 |
std::pair<uint64_t, uint32_t> expected = m_expectedCollision.front (); |
136 |
m_expectedCollision.pop_front (); |
|
137 |
NS_TEST_ASSERT_EQUAL (MicroSeconds (expected.first), Simulator::Now ()); |
|
138 |
NS_TEST_ASSERT_EQUAL (expected.second, i); |
|
139 |
if (!result) |
|
140 |
{ |
|
141 |
m_result = result; |
|
142 |
} |
|
143 |
} |
|
2097 | 144 |
|
145 |
||
146 |
void |
|
147 |
DcfManagerTest::ExpectAccessGranted (uint64_t time, uint32_t from) |
|
148 |
{ |
|
149 |
m_expectedAccessGranted.push_back (std::make_pair (time, from)); |
|
150 |
} |
|
2098 | 151 |
void |
152 |
DcfManagerTest::ExpectInternalCollision (uint64_t time, uint32_t from) |
|
153 |
{ |
|
154 |
m_expectedInternalCollision.push_back (std::make_pair (time, from)); |
|
155 |
} |
|
156 |
void |
|
157 |
DcfManagerTest::ExpectCollision (uint64_t time, uint32_t from) |
|
158 |
{ |
|
159 |
m_expectedCollision.push_back (std::make_pair (time, from)); |
|
160 |
} |
|
2097 | 161 |
|
162 |
void |
|
2104
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
163 |
DcfManagerTest::StartTest (uint64_t slotTime, uint64_t ackTxDuration) |
2097 | 164 |
{ |
165 |
m_dcfManager = new DcfManager (); |
|
166 |
m_parameters = new MacParameters (); |
|
2104
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
167 |
m_parameters->SetSlotTime (MicroSeconds (slotTime)); |
2097 | 168 |
m_dcfManager->SetParameters (m_parameters); |
2104
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
169 |
m_dcfManager->SetAckTxDuration (MicroSeconds (ackTxDuration)); |
2097 | 170 |
} |
171 |
||
172 |
void |
|
173 |
DcfManagerTest::AddDcfState (uint32_t cwMin, uint32_t cwMax, uint32_t aifsn) |
|
174 |
{ |
|
2101
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
175 |
DcfStateTest *state = new DcfStateTest (this, m_dcfStates.size ()); |
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
176 |
state->SetCwBounds (cwMin, cwMax); |
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
177 |
state->SetAifsn (aifsn); |
2097 | 178 |
m_dcfStates.push_back (state); |
2101
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
179 |
m_dcfManager->Add (state); |
2097 | 180 |
} |
181 |
||
182 |
void |
|
183 |
DcfManagerTest::EndTest (void) |
|
184 |
{ |
|
185 |
bool result = true; |
|
186 |
Simulator::Run (); |
|
187 |
NS_TEST_ASSERT (!m_expectedAccessGranted.empty ()); |
|
2098 | 188 |
NS_TEST_ASSERT (!m_expectedInternalCollision.empty ()); |
189 |
NS_TEST_ASSERT (!m_expectedCollision.empty ()); |
|
2097 | 190 |
m_expectedAccessGranted.clear (); |
2098 | 191 |
m_expectedInternalCollision.clear (); |
192 |
m_expectedCollision.clear (); |
|
2097 | 193 |
Simulator::Destroy (); |
2101
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
194 |
for (DcfStates::const_iterator i = m_dcfStates.begin (); i != m_dcfStates.end (); i++) |
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
195 |
{ |
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
196 |
delete *i; |
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
197 |
} |
2097 | 198 |
m_dcfStates.clear (); |
199 |
delete m_dcfManager; |
|
200 |
delete m_parameters; |
|
201 |
if (!result) |
|
202 |
{ |
|
203 |
m_result = result; |
|
204 |
} |
|
205 |
} |
|
206 |
||
2098 | 207 |
void |
208 |
DcfManagerTest::AddRxOkEvt (uint64_t at, uint64_t duration) |
|
209 |
{ |
|
210 |
Simulator::Schedule (MicroSeconds (at) - Now (), |
|
211 |
&DcfManager::NotifyRxStartNow, m_dcfManager, |
|
212 |
MicroSeconds (duration)); |
|
213 |
Simulator::Schedule (MicroSeconds (at+duration) - Now (), |
|
214 |
&DcfManager::NotifyRxEndOkNow, m_dcfManager); |
|
215 |
} |
|
216 |
void |
|
217 |
DcfManagerTest::AddRxErrorEvt (uint64_t at, uint64_t duration) |
|
218 |
{ |
|
219 |
Simulator::Schedule (MicroSeconds (at) - Now (), |
|
220 |
&DcfManager::NotifyRxStartNow, m_dcfManager, |
|
221 |
MicroSeconds (duration)); |
|
222 |
Simulator::Schedule (MicroSeconds (at+duration) - Now (), |
|
223 |
&DcfManager::NotifyRxEndErrorNow, m_dcfManager); |
|
224 |
} |
|
225 |
void |
|
226 |
DcfManagerTest::AddTxEvt (uint64_t at, uint64_t duration) |
|
227 |
{ |
|
228 |
Simulator::Schedule (MicroSeconds (at) - Now (), |
|
229 |
&DcfManager::NotifyTxStartNow, m_dcfManager, |
|
230 |
MicroSeconds (duration)); |
|
231 |
} |
|
232 |
void |
|
233 |
DcfManagerTest::AddNavReset (uint64_t at, uint64_t duration) |
|
234 |
{ |
|
235 |
Simulator::Schedule (MicroSeconds (at) - Now (), |
|
236 |
&DcfManager::NotifyNavResetNow, m_dcfManager, |
|
237 |
MicroSeconds (duration)); |
|
238 |
} |
|
239 |
void |
|
240 |
DcfManagerTest::AddNavStart (uint64_t at, uint64_t duration) |
|
241 |
{ |
|
242 |
Simulator::Schedule (MicroSeconds (at) - Now (), |
|
243 |
&DcfManager::NotifyNavStartNow, m_dcfManager, |
|
244 |
MicroSeconds (duration)); |
|
245 |
} |
|
246 |
void |
|
247 |
DcfManagerTest::AddAccessRequest (uint64_t time, uint32_t from) |
|
248 |
{ |
|
249 |
Simulator::Schedule (MicroSeconds (time) - Now (), |
|
250 |
&DcfManager::RequestAccess, |
|
2101
05c4a31c0a58
simplify memory management
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2098
diff
changeset
|
251 |
m_dcfManager, m_dcfStates[from]); |
2098 | 252 |
} |
253 |
||
254 |
||
255 |
||
256 |
||
257 |
bool |
|
258 |
DcfManagerTest::RunTests (void) |
|
259 |
{ |
|
260 |
m_result = true; |
|
261 |
||
2104
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
262 |
StartTest (1 /* slot time */, 10 /* ack tx dur */); |
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
263 |
AddDcfState (8 /* cwmin */, 64 /* cwmax */, 1 /* aifsn */); |
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
264 |
AddAccessRequest (10 /* at */ , 0 /* from */); |
1ba670fdde06
more debugging.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2103
diff
changeset
|
265 |
ExpectAccessGranted (10 /* at */, 0 /* from */); |
2098 | 266 |
EndTest (); |
267 |
||
268 |
return m_result; |
|
269 |
} |
|
270 |
||
271 |
||
2097 | 272 |
|
273 |
static DcfManagerTest g_dcf_manager_test; |
|
274 |
||
275 |
} // namespace ns3 |
|
276 |
||
277 |
#endif /* RUN_SELF_TESTS */ |