FreeNOS
TestInt.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_TESTINT_H
19 #define __LIBTEST_TESTINT_H
20 
21 #include "TestData.h"
22 
34 template <class T> class TestInt : public TestData<T>
35 {
36  public:
37 
41  TestInt(T min, T max) : TestData<T>()
42  {
43  m_min = min;
44  m_max = max;
45  }
46 
54  virtual T random(Size count = 1);
55 
63  virtual T unique(Size count = 1);
64 
65  private:
66 
68  T m_max;
69 
71  T m_min;
72 };
73 
74 template<> int TestInt<int>::random(Size count)
75 {
76  int value = 0;
77 
78  while (count--)
79  {
80  value = (::random() % m_max);
81 
82  if (value < m_min)
83  value = m_min;
84 
85  m_values.insert(value);
86  break;
87  }
88  return value;
89 }
90 
91 template<> int TestInt<int>::unique(Size count)
92 {
93  // Save current count
94  int offset = (int) m_values.count();
95 
96  // First put values sequentially
97  for (int i = 0; i < (int)count; i++)
98  m_values.insert(i + m_min);
99 
100  // Randomize values by swapping
101  for (int i = offset; i < ((int)count + offset); i++)
102  {
103  int tmp = m_values[i];
104 
105  int idx = (::random() % (int)count - 1);
106  if (idx < offset)
107  idx = offset;
108 
109  m_values[i] = m_values[idx];
110  m_values[idx] = tmp;
111  }
112  // Success
113  return m_values[offset + (int)count - 1];
114 }
115 
116 template<> uint TestInt<uint>::random(Size count)
117 {
118  uint value = 0;
119 
120  while (count--)
121  {
122  value = (((unsigned)::random() + (unsigned)::random()) % m_max);
123 
124  if (value < m_min)
125  value = m_min;
126 
127  m_values.insert(value);
128  break;
129  }
130  return value;
131 }
132 
133 template<> uint TestInt<uint>::unique(Size count)
134 {
135  // Save current count
136  Size offset = m_values.count();
137 
138  // First put values sequentially
139  for (Size i = 0; i < count; i++)
140  m_values.insert(i + m_min);
141 
142  // Randomize values by swapping
143  for (Size i = offset; i < (count + offset); i++)
144  {
145  Size tmp = m_values[i];
146 
147  Size idx = (::random() % (count - 1));
148  if (idx < offset)
149  idx = offset;
150 
151  m_values[i] = m_values[idx];
152  m_values[idx] = tmp;
153  }
154  // Success
155  return m_values[offset + count - 1];
156 }
157 
163 #endif /* __LIBTEST_TESTINT_H */
TestInt::TestInt
TestInt(T min, T max)
Constructor.
Definition: TestInt.h:41
TestData.h
TestInt::unique
virtual T unique(Size count=1)
Get unique random test value(s).
TestInt::random
virtual T random(Size count=1)
Get random test value(s).
TestData
Generate test data for a certain data type.
Definition: TestData.h:44
random
C long int random(void)
Random number generator.
Definition: random.cpp:26
TestData::count
Size count() const
The number of generated values.
Definition: TestData.h:87
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
TestInt
Generate test data for integers.
Definition: TestInt.h:34
TestInt::m_max
T m_max
Maximum value.
Definition: TestInt.h:68
TestInt::m_min
T m_min
Minimum value.
Definition: TestInt.h:71