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 #include <stdint.h>
41 
42 using namespace ns3;
43 
44 class SatEstimatorBaseTestCase : public TestCase
45 {
46  public:
48  : TestCase("")
49  {
50  }
51 
52  SatEstimatorBaseTestCase(std::string info)
53  : TestCase(info)
54  {
55  }
56 
58  {
59  }
60 
61  // add sample to estimator
62  void AddSample(double cno);
63 
64  // get C/N0 estimation.
65  void GetCnoEstimation();
66 
67  // create C/N0 estimator.
68  void CreateEstimator(SatCnoEstimator::EstimationMode_t mode, Time window);
69 
70  protected:
71  virtual void DoRun(void) = 0;
72  Ptr<SatCnoEstimator> m_estimator;
73  std::vector<double> m_cnoEstimations;
74 };
75 
76 void
78 {
79  m_estimator->AddSample(cno);
80 }
81 
82 void
84 {
85  m_cnoEstimations.push_back(m_estimator->GetCnoEstimation());
86 }
87 
88 void
90 {
91  m_estimator = Create<SatBasicCnoEstimator>(mode, window);
92 }
93 
113 {
114  public:
116  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode LAST.")
117  {
118  }
119 
121  {
122  }
123 
124  protected:
125  virtual void DoRun(void);
126 };
127 
128 void
130 {
131  // Set simulation output details
132  Singleton<SatEnvVariables>::Get()->DoInitialize();
133  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
134  "last",
135  true);
136 
137  // create estimator with window 100 ms
138  Simulator::Schedule(Seconds(0.05),
140  this,
142  Seconds(0.10));
143 
144  // simulate sample additions with window 100 ms
145  Simulator::Schedule(Seconds(0.10), &SatBasicEstimatorLastTestCase::AddSample, this, -5.0);
146  Simulator::Schedule(Seconds(0.20), &SatBasicEstimatorLastTestCase::AddSample, this, 5.0);
147  Simulator::Schedule(Seconds(0.30), &SatBasicEstimatorLastTestCase::AddSample, this, -15.0);
148 
149  // simulate C/N0 estimations with window 100 ms
150  Simulator::Schedule(Seconds(0.09),
152  this); // NAN expected
153  Simulator::Schedule(Seconds(0.19),
155  this); // -5.0 expected
156  Simulator::Schedule(Seconds(0.25),
158  this); // 5.0 expected
159  Simulator::Schedule(Seconds(0.31),
161  this); // -15.0 expected
162  Simulator::Schedule(Seconds(0.41),
164  this); // NAN expected
165 
166  // create estimator with window 300 ms
167  Simulator::Schedule(Seconds(0.5),
169  this,
171  Seconds(0.30));
172 
173  // simulate sample additions with window 300 ms
174  Simulator::Schedule(Seconds(0.60), &SatBasicEstimatorLastTestCase::AddSample, this, -6.0);
175  Simulator::Schedule(Seconds(0.70), &SatBasicEstimatorLastTestCase::AddSample, this, 1.0);
176  Simulator::Schedule(Seconds(1.10), &SatBasicEstimatorLastTestCase::AddSample, this, -5.0);
177 
178  // simulate C/N0 estimations with window 300 ms
179  Simulator::Schedule(Seconds(0.59),
181  this); // NAN expected
182  Simulator::Schedule(Seconds(0.69),
184  this); // -6.0 expected
185  Simulator::Schedule(Seconds(0.75),
187  this); // 1.0 expected
188  Simulator::Schedule(Seconds(0.91),
190  this); // 1.0 expected
191  Simulator::Schedule(Seconds(1.39),
193  this); // -5.0 expected
194  Simulator::Schedule(Seconds(1.41),
196  this); // NAN expected
197 
198  Simulator::Run();
199 
200  // After simulation check that estimations are as expected
201 
202  // estimations with window 100 ms
203  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
204  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[1], -5.0, "second estimation incorrect");
205  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[2], 5.0, "third estimation incorrect");
206  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[3], -15.0, "fourth estimation incorrect");
207  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[4]), true, "fifth estimation incorrect");
208 
209  // estimations with window 300 ms
210  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
211  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[6], -6.0, "seventh estimation incorrect");
212  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[7], 1.0, "eight estimation incorrect");
213  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[8], 1.0, "ninth estimation incorrect");
214  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[9], -5.0, "tenth estimation incorrect");
215  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[10]), true, "eleventh estimation incorrect");
216 
217  Simulator::Destroy();
218 
219  Singleton<SatEnvVariables>::Get()->DoDispose();
220 }
221 
240 {
241  public:
243  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode MINIMUM.")
244  {
245  }
246 
248  {
249  }
250 
251  protected:
252  virtual void DoRun(void);
253 };
254 
255 void
257 {
258  // Set simulation output details
259  Singleton<SatEnvVariables>::Get()->DoInitialize();
260  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
261  "min",
262  true);
263 
264  // create estimator with window 200 ms
265  Simulator::Schedule(Seconds(0.05),
267  this,
269  Seconds(0.20));
270 
271  // simulate sample additions with window 100 ms
272  Simulator::Schedule(Seconds(0.17), &SatBasicEstimatorLastTestCase::AddSample, this, -4.2);
273  Simulator::Schedule(Seconds(0.22), &SatBasicEstimatorLastTestCase::AddSample, this, 8.1);
274  Simulator::Schedule(Seconds(0.46), &SatBasicEstimatorLastTestCase::AddSample, this, -15.7);
275  Simulator::Schedule(Seconds(0.48), &SatBasicEstimatorLastTestCase::AddSample, this, 2.4);
276 
277  // simulate C/N0 estimations with window 200 ms
278  Simulator::Schedule(Seconds(0.09),
280  this); // NAN expected
281  Simulator::Schedule(Seconds(0.19),
283  this); // -4.2 expected
284  Simulator::Schedule(Seconds(0.35),
286  this); // -4.2 expected
287  Simulator::Schedule(Seconds(0.41),
289  this); // -8.1 expected
290  Simulator::Schedule(Seconds(0.49),
292  this); // -15.7 expected
293  Simulator::Schedule(Seconds(0.69),
295  this); // NAN expected
296 
297  Simulator::Run();
298 
299  // After simulation check that estimations are as expected
300 
301  // estimations with window 200 ms
302  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
303  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[1], -4.2, "second estimation incorrect");
304  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[2], -4.2, "third estimation incorrect");
305  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[3], 8.1, "fourth estimation incorrect");
306  NS_TEST_ASSERT_MSG_EQ(m_cnoEstimations[4], -15.7, "fifth estimation incorrect");
307  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
308 
309  Simulator::Destroy();
310 
311  Singleton<SatEnvVariables>::Get()->DoDispose();
312 }
313 
332 {
333  public:
335  : SatEstimatorBaseTestCase("Test satellite C per N0 basic estimator with mode AVERAGE.")
336  {
337  }
338 
340  {
341  }
342 
343  protected:
344  virtual void DoRun(void);
345 };
346 
347 void
349 {
350  // Set simulation output details
351  Singleton<SatEnvVariables>::Get()->DoInitialize();
352  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-sat-cno-estimator-unit",
353  "average",
354  true);
355 
356  // create estimator with window 200 ms
357  Simulator::Schedule(Seconds(0.05),
359  this,
361  Seconds(0.20));
362 
363  // simulate sample additions with window 100 ms
364  Simulator::Schedule(Seconds(0.17), &SatBasicEstimatorLastTestCase::AddSample, this, -4.2);
365  Simulator::Schedule(Seconds(0.22), &SatBasicEstimatorLastTestCase::AddSample, this, 8.1);
366  Simulator::Schedule(Seconds(0.26), &SatBasicEstimatorLastTestCase::AddSample, this, -15.7);
367  Simulator::Schedule(Seconds(0.43), &SatBasicEstimatorLastTestCase::AddSample, this, 2.4);
368 
369  // simulate C/N0 estimations with window 200 ms
370  Simulator::Schedule(Seconds(0.09),
372  this); // NAN expected
373  Simulator::Schedule(Seconds(0.19),
375  this); // -4.2 expected
376  Simulator::Schedule(Seconds(0.35),
378  this); // (-4.2 + 8.1 -15.7) / 3 expected
379  Simulator::Schedule(Seconds(0.41),
381  this); // (8.1 -15.7) / 2 expected
382  Simulator::Schedule(Seconds(0.49),
384  this); // 2.4 expected
385  Simulator::Schedule(Seconds(0.69),
387  this); // NAN expected
388 
389  Simulator::Run();
390 
391  // After simulation check that estimations are as expected
392 
393  // estimations with window 200 ms
394  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[0]), true, "first estimation incorrect");
395  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[1], -4.2, 0.0001, "second estimation incorrect");
396  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[2],
397  (-4.2 + 8.1 - 15.7) / 3,
398  0.0001,
399  "third estimation incorrect");
400  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[3],
401  (8.1 - 15.7) / 2,
402  0.0001,
403  "fourth estimation incorrect");
404  NS_TEST_ASSERT_MSG_EQ_TOL(m_cnoEstimations[4], 2.4, 0.0001, "fifth estimation incorrect");
405  NS_TEST_ASSERT_MSG_EQ(std::isnan(m_cnoEstimations[5]), true, "sixth estimation incorrect");
406 
407  Simulator::Destroy();
408 
409  Singleton<SatEnvVariables>::Get()->DoDispose();
410 }
411 
416 class SatBasicCnoEstimatorTestSuite : public TestSuite
417 {
418  public:
420 };
421 
423  : TestSuite("sat-cno-estimator-unit-test", Type::UNIT)
424 {
425  AddTestCase(new SatBasicEstimatorLastTestCase, TestCase::Duration::QUICK);
426  AddTestCase(new SatBasicEstimatorMinTestCase, TestCase::Duration::QUICK);
427  AddTestCase(new SatBasicEstimatorAverageTestCase, TestCase::Duration::QUICK);
428 }
429 
430 // 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