satellite-cno-estimator-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions Ltd
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: Sami Rantanen <sami.rantanen@magister.fi>
19  */
20 
27 // Include a header file from your module to test.
28 #include "../model/satellite-cno-estimator.h"
29 #include "../utils/satellite-env-variables.h"
30 
31 #include "ns3/boolean.h"
32 #include "ns3/config.h"
33 #include "ns3/log.h"
34 #include "ns3/simulator.h"
35 #include "ns3/singleton.h"
36 #include "ns3/string.h"
37 #include "ns3/test.h"
38 #include "ns3/timer.h"
39 
40 using namespace ns3;
41 
42 class SatEstimatorBaseTestCase : public TestCase
43 {
44  public:
46  : TestCase("")
47  {
48  }
49 
50  SatEstimatorBaseTestCase(std::string info)
51  : TestCase(info)
52  {
53  }
54 
56  {
57  }
58 
59  // add sample to estimator
60  void AddSample(double cno);
61 
62  // get C/N0 estimation.
63  void GetCnoEstimation();
64 
65  // create C/N0 estimator.
66  void CreateEstimator(SatCnoEstimator::EstimationMode_t mode, Time window);
67 
68  protected:
69  virtual void DoRun(void) = 0;
70  Ptr<SatCnoEstimator> m_estimator;
71  std::vector<double> m_cnoEstimations;
72 };
73 
74 void
76 {
77  m_estimator->AddSample(cno);
78 }
79 
80 void
82 {
83  m_cnoEstimations.push_back(m_estimator->GetCnoEstimation());
84 }
85 
86 void
88 {
89  m_estimator = Create<SatBasicCnoEstimator>(mode, window);
90 }
91 
111 {
112  public:
114  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode LAST.")
115  {
116  }
117 
119  {
120  }
121 
122  protected:
123  virtual void DoRun(void);
124 };
125 
126 void
128 {
129  // Set simulation output details
130  Singleton<SatEnvVariables>::Get()->DoInitialize();
131  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
132  "last",
133  true);
134 
135  // create estimator with window 100 ms
136  Simulator::Schedule(Seconds(0.05),
138  this,
140  Seconds(0.10));
141 
142  // simulate sample additions with window 100 ms
143  Simulator::Schedule(Seconds(0.10), &SatBasicEstimatorLastTestCase::AddSample, this, -5.0);
144  Simulator::Schedule(Seconds(0.20), &SatBasicEstimatorLastTestCase::AddSample, this, 5.0);
145  Simulator::Schedule(Seconds(0.30), &SatBasicEstimatorLastTestCase::AddSample, this, -15.0);
146 
147  // simulate C/N0 estimations with window 100 ms
148  Simulator::Schedule(Seconds(0.09),
150  this); // NAN expected
151  Simulator::Schedule(Seconds(0.19),
153  this); // -5.0 expected
154  Simulator::Schedule(Seconds(0.25),
156  this); // 5.0 expected
157  Simulator::Schedule(Seconds(0.31),
159  this); // -15.0 expected
160  Simulator::Schedule(Seconds(0.41),
162  this); // NAN expected
163 
164  // create estimator with window 300 ms
165  Simulator::Schedule(Seconds(0.5),
167  this,
169  Seconds(0.30));
170 
171  // simulate sample additions with window 300 ms
172  Simulator::Schedule(Seconds(0.60), &SatBasicEstimatorLastTestCase::AddSample, this, -6.0);
173  Simulator::Schedule(Seconds(0.70), &SatBasicEstimatorLastTestCase::AddSample, this, 1.0);
174  Simulator::Schedule(Seconds(1.10), &SatBasicEstimatorLastTestCase::AddSample, this, -5.0);
175 
176  // simulate C/N0 estimations with window 300 ms
177  Simulator::Schedule(Seconds(0.59),
179  this); // NAN expected
180  Simulator::Schedule(Seconds(0.69),
182  this); // -6.0 expected
183  Simulator::Schedule(Seconds(0.75),
185  this); // 1.0 expected
186  Simulator::Schedule(Seconds(0.91),
188  this); // 1.0 expected
189  Simulator::Schedule(Seconds(1.39),
191  this); // -5.0 expected
192  Simulator::Schedule(Seconds(1.41),
194  this); // NAN expected
195 
196  Simulator::Run();
197 
198  // After simulation check that estimations are as expected
199 
200  // estimations with window 100 ms
201  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
202  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[1], -5.0, "second estimation incorrect");
203  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[2], 5.0, "third estimation incorrect");
204  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[3], -15.0, "fourth estimation incorrect");
205  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[4]), true, "fifth estimation incorrect");
206 
207  // estimations with window 300 ms
208  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
209  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[6], -6.0, "seventh estimation incorrect");
210  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[7], 1.0, "eight estimation incorrect");
211  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[8], 1.0, "ninth estimation incorrect");
212  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[9], -5.0, "tenth estimation incorrect");
213  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[10]), true, "eleventh estimation incorrect");
214 
215  Simulator::Destroy();
216 
217  Singleton<SatEnvVariables>::Get()->DoDispose();
218 }
219 
238 {
239  public:
241  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode MINIMUM.")
242  {
243  }
244 
246  {
247  }
248 
249  protected:
250  virtual void DoRun(void);
251 };
252 
253 void
255 {
256  // Set simulation output details
257  Singleton<SatEnvVariables>::Get()->DoInitialize();
258  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
259  "min",
260  true);
261 
262  // create estimator with window 200 ms
263  Simulator::Schedule(Seconds(0.05),
265  this,
267  Seconds(0.20));
268 
269  // simulate sample additions with window 100 ms
270  Simulator::Schedule(Seconds(0.17), &SatBasicEstimatorLastTestCase::AddSample, this, -4.2);
271  Simulator::Schedule(Seconds(0.22), &SatBasicEstimatorLastTestCase::AddSample, this, 8.1);
272  Simulator::Schedule(Seconds(0.46), &SatBasicEstimatorLastTestCase::AddSample, this, -15.7);
273  Simulator::Schedule(Seconds(0.48), &SatBasicEstimatorLastTestCase::AddSample, this, 2.4);
274 
275  // simulate C/N0 estimations with window 200 ms
276  Simulator::Schedule(Seconds(0.09),
278  this); // NAN expected
279  Simulator::Schedule(Seconds(0.19),
281  this); // -4.2 expected
282  Simulator::Schedule(Seconds(0.35),
284  this); // -4.2 expected
285  Simulator::Schedule(Seconds(0.41),
287  this); // -8.1 expected
288  Simulator::Schedule(Seconds(0.49),
290  this); // -15.7 expected
291  Simulator::Schedule(Seconds(0.69),
293  this); // NAN expected
294 
295  Simulator::Run();
296 
297  // After simulation check that estimations are as expected
298 
299  // estimations with window 200 ms
300  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
301  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[1], -4.2, "second estimation incorrect");
302  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[2], -4.2, "third estimation incorrect");
303  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[3], 8.1, "fourth estimation incorrect");
304  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[4], -15.7, "fifth estimation incorrect");
305  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
306 
307  Simulator::Destroy();
308 
309  Singleton<SatEnvVariables>::Get()->DoDispose();
310 }
311 
330 {
331  public:
333  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode AVERAGE.")
334  {
335  }
336 
338  {
339  }
340 
341  protected:
342  virtual void DoRun(void);
343 };
344 
345 void
347 {
348  // Set simulation output details
349  Singleton<SatEnvVariables>::Get()->DoInitialize();
350  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
351  "average",
352  true);
353 
354  // create estimator with window 200 ms
355  Simulator::Schedule(Seconds(0.05),
357  this,
359  Seconds(0.20));
360 
361  // simulate sample additions with window 100 ms
362  Simulator::Schedule(Seconds(0.17), &SatBasicEstimatorLastTestCase::AddSample, this, -4.2);
363  Simulator::Schedule(Seconds(0.22), &SatBasicEstimatorLastTestCase::AddSample, this, 8.1);
364  Simulator::Schedule(Seconds(0.26), &SatBasicEstimatorLastTestCase::AddSample, this, -15.7);
365  Simulator::Schedule(Seconds(0.43), &SatBasicEstimatorLastTestCase::AddSample, this, 2.4);
366 
367  // simulate C/N0 estimations with window 200 ms
368  Simulator::Schedule(Seconds(0.09),
370  this); // NAN expected
371  Simulator::Schedule(Seconds(0.19),
373  this); // -4.2 expected
374  Simulator::Schedule(Seconds(0.35),
376  this); // (-4.2 + 8.1 -15.7) / 3 expected
377  Simulator::Schedule(Seconds(0.41),
379  this); // (8.1 -15.7) / 2 expected
380  Simulator::Schedule(Seconds(0.49),
382  this); // 2.4 expected
383  Simulator::Schedule(Seconds(0.69),
385  this); // NAN expected
386 
387  Simulator::Run();
388 
389  // After simulation check that estimations are as expected
390 
391  // estimations with window 200 ms
392  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
393  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[1], -4.2, 0.0001, "second estimation incorrect");
394  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[2],
395  (-4.2 + 8.1 - 15.7) / 3,
396  0.0001,
397  "third estimation incorrect");
398  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[3],
399  (8.1 - 15.7) / 2,
400  0.0001,
401  "fourth estimation incorrect");
402  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[4], 2.4, 0.0001, "fifth estimation incorrect");
403  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
404 
405  Simulator::Destroy();
406 
407  Singleton<SatEnvVariables>::Get()->DoDispose();
408 }
409 
414 class SatBasicCnoEstimatorTestSuite : public TestSuite
415 {
416  public:
418 };
419 
421  : TestSuite("sat-cno-estimator-unit-test", UNIT)
422 {
423  AddTestCase(new SatBasicEstimatorLastTestCase, TestCase::QUICK);
424  AddTestCase(new SatBasicEstimatorMinTestCase, TestCase::QUICK);
425  AddTestCase(new SatBasicEstimatorAverageTestCase, TestCase::QUICK);
426 }
427 
428 // Do allocate an instance of this TestSuite
Test suite for Satellite C/N0 estimator unit test cases.
Test case to unit test satellite C/N0 estimator with mode AVERAGE.
Test case to unit test satellite C/N0 estimator with mode LAST.
Test case to unit test satellite C/N0 estimator with mode MINIMUM.
void CreateEstimator(SatCnoEstimator::EstimationMode_t mode, Time window)
virtual void DoRun(void)=0
EstimationMode_t
Definition of modes for estimator.
@ MINIMUM
Minimum value in the given window returned.
@ LAST
Last value in the given window returned.
@ AVERAGE
Average value in the given window returned.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
static SatBasicCnoEstimatorTestSuite satCnoEstimatorUnit