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
44template <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
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 */
Generate test data for a certain data type.
Definition TestData.h:45
virtual ~TestData()
Destructor.
Definition TestData.h:59
T & get(Size index)
Retrieve previously random generated test data by index.
Definition TestData.h:99
virtual T unique(Size count=1)=0
Get unique random test value(s).
void seed()
Initialize the random number generator.
Definition TestData.h:66
virtual T random(Size count=1)=0
Get random test value(s).
Size count() const
The number of generated values.
Definition TestData.h:87
Vector< T > m_values
Vector with generated values.
Definition TestData.h:133
T & operator[](Size index)
Retrieve previously random generated test data by index.
Definition TestData.h:107
TestData()
Constructor.
Definition TestData.h:51
Vectors are dynamically resizeable Arrays.
Definition Vector.h:42
#define timestamp()
Reads the CPU's timestamp counter.
Definition ARMCore.h:97
C pid_t getpid()
Get the process ID.
Definition getpid.cpp:21
ProcessID pid_t
Used for process IDs and process group IDs.
Definition types.h:32
C int gettimeofday(struct timeval *tv, struct timezone *tz)
Get current time of day.
C void srandom(unsigned int seed)
Random number generator.
Definition random.cpp:20
#define NULL
NULL means zero.
Definition Macros.h:39
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
Time value information.
Definition time.h:36
uint tv_usec
Microseconds.
Definition time.h:41
time_t tv_sec
Seconds.
Definition time.h:38
Time zone information.
Definition time.h:48