FreeNOS
TestData.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBTEST_TESTDATA_H
19 #define __LIBTEST_TESTDATA_H
20 
21 #include <Types.h>
22 #include <Macros.h>
23 #include <Vector.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 
27 #ifdef __HOST__
28 #include <sys/time.h>
29 #else
30 #include <FreeNOS/System.h>
31 #endif /* __HOST__ */
32 
44 template <class T> class TestData
45 {
46  public:
47 
52  {
53  seed();
54  }
55 
59  virtual ~TestData()
60  {
61  }
62 
66  void seed()
67  {
68  // Collect seed from process id.
69  pid_t pid = getpid();
70  unsigned int seed = pid;
71 
72  // Collect seed from the current time
73 #ifdef __HOST__
74  struct timeval tv;
75  gettimeofday(&tv, (struct timezone *)NULL);
76  seed += tv.tv_sec + tv.tv_usec;
77 #else
78  seed += timestamp();
79 #endif
80  // Seed the random generator
81  srandom(seed);
82  }
83 
87  Size count() const
88  {
89  return m_values.count();
90  }
91 
99  T & get(Size index)
100  {
101  return m_values[index];
102  }
103 
107  T & operator[](Size index)
108  {
109  return m_values[index];
110  }
111 
119  virtual T random(Size count = 1) = 0;
120 
128  virtual T unique(Size count = 1) = 0;
129 
130  protected:
131 
134 };
135 
141 #endif /* __LIBTEST_TESTDATA_H */
timezone
Time zone information.
Definition: time.h:47
srandom
C void srandom(unsigned int new_seed)
Random number generator.
Definition: random.cpp:20
Macros.h
Vector.h
Types.h
TestData::get
T & get(Size index)
Retrieve previously random generated test data by index.
Definition: TestData.h:99
timestamp
u64 timestamp()
Reads the CPU's timestamp counter.
Definition: IntelCore.h:41
TestData
Generate test data for a certain data type.
Definition: TestData.h:44
timeval::tv_usec
uint tv_usec
Microseconds.
Definition: time.h:41
getpid
pid_t getpid()
Get the process ID.
Definition: getpid.cpp:21
TestData::count
Size count() const
The number of generated values.
Definition: TestData.h:87
time.h
gettimeofday
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get current time of day.
Definition: gettimeofday.cpp:22
TestData::m_values
Vector< T > m_values
Vector with generated values.
Definition: TestData.h:133
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
TestData::seed
void seed()
Initialize the random number generator.
Definition: TestData.h:66
TestData::operator[]
T & operator[](Size index)
Retrieve previously random generated test data by index.
Definition: TestData.h:107
TestData::~TestData
virtual ~TestData()
Destructor.
Definition: TestData.h:59
TestData::unique
virtual T unique(Size count=1)=0
Get unique random test value(s).
timeval::tv_sec
time_t tv_sec
Seconds.
Definition: time.h:38
pid_t
ProcessID pid_t
Used for process IDs and process group IDs.
Definition: types.h:32
timeval
Time value information.
Definition: time.h:35
unistd.h
stdlib.h
TestData::random
virtual T random(Size count=1)=0
Get random test value(s).
Vector
Vectors are dynamically resizeable Arrays.
Definition: Vector.h:41
TestData::TestData
TestData()
Constructor.
Definition: TestData.h:51