FreeNOS
TestChar.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_TESTCHAR_H
19 #define __LIBTEST_TESTCHAR_H
20 
21 #include "TestData.h"
22 #include "TestInt.h"
23 
35 template <class T> class TestChar : public TestData<T>
36 {
37  public:
38 
45  TestChar(Size min, Size max) : TestData<T>()
46  {
47  m_max = max;
48  m_min = min;
49  }
50 
54  virtual ~TestChar()
55  {
56  for (Size i = 0; i < this->m_values.count(); i++)
57  delete[] this->m_values[i];
58  }
59 
67  virtual T random(Size count = 1);
68 
76  virtual T unique(Size count = 1);
77 
81  virtual Size length(Size position)
82  {
83  return m_lengths[position];
84  }
85 
86  private:
87 
90 
93 
96 };
97 
98 template<> char * TestChar<char *>::random(Size count)
99 {
100  char * value = 0;
101 
102  while (count--)
103  {
104  TestInt<uint> sizes(m_min, m_max);
105  TestInt<uint> chars(32, 126); /* ' ' till '~' */
106  Size len = sizes.random();
107 
108  // Fill the buffer with random ASCII characters.
109  value = new char[len + 1];
110  for (Size i = 0; i < len; i++)
111  value[i] = (char) chars.random();
112 
113  value[len] = ZERO;
114 
115  // Insert to administration
116  m_values.insert(value);
117  m_lengths.insert(len);
118  }
119  return value;
120 }
121 
122 template<> char * TestChar<char *>::unique(Size count)
123 {
124  const Size charMin = 32, charMax = 126; /* ' ' till '~' */
125  TestInt<uint> chars(charMin, charMax);
126  char value[m_max + 1];
127 
128  // Clear string buffer
129  MemoryBlock::set(value, 0, sizeof(value));
130 
131  // Generate 'count' unique strings using the prefix.
132  for (Size i = 0; i < count; i++)
133  {
134  const Size len = (i + 1) < m_max ? (i + 1) : m_max;
135  char *buf = new char[sizeof(value)];
136 
137  // String is filled by incrementing characters
138  const Size numToAdd = i < m_min ? m_min : 1;
139 
140  for (Size j = 0; j < numToAdd; j++)
141  {
142  const char c = (char) ((i / (sizeof(value) - 1)) + charMin);
143  value[i % (sizeof(value) - 1)] = c;
144  }
145  MemoryBlock::copy(buf, value, sizeof(value));
146 
147  // Add to administration
148  m_values.insert(buf);
149  m_lengths.insert(len);
150  }
151 
152  return m_values[0];
153 }
154 
160 #endif /* __LIBTEST_TESTCHAR_H */
TestChar::unique
virtual T unique(Size count=1)
Get unique random test value(s).
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
TestChar::random
virtual T random(Size count=1)
Get random test value(s).
TestData.h
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
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
TestChar::~TestChar
virtual ~TestChar()
Destructor.
Definition: TestChar.h:54
TestChar::length
virtual Size length(Size position)
Get the length of the generated character string.
Definition: TestChar.h:81
TestData::count
Size count() const
The number of generated values.
Definition: TestData.h:87
TestInt.h
TestData::m_values
Vector< T > m_values
Vector with generated values.
Definition: TestData.h:133
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
TestChar::TestChar
TestChar(Size min, Size max)
Constructor.
Definition: TestChar.h:45
TestChar::m_max
Size m_max
Maximum value.
Definition: TestChar.h:89
TestInt
Generate test data for integers.
Definition: TestInt.h:34
TestChar::m_lengths
Vector< Size > m_lengths
String lengths.
Definition: TestChar.h:95
TestChar
Generate test data for character strings.
Definition: TestChar.h:35
Vector< Size >
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
TestChar::m_min
Size m_min
Minimum value.
Definition: TestChar.h:92