-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable-random-test.h
More file actions
124 lines (91 loc) · 3.58 KB
/
table-random-test.h
File metadata and controls
124 lines (91 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//========================================================================
// table-random-test.h
//========================================================================
// This file contains generic random tests for tables. All of the generic
// test functions are templated by an "object creation" function which
// should take as a parameter an integer and return a newly created
// object. For integers, the object creation function can just be the
// identity function. For images, the object creation function can create
// a small image and initialize the pixels based on the given integer.
#include "Table.h"
#include "utst.h"
#include "ece2400-stdlib.h"
#include <iostream>
//------------------------------------------------------------------------
// test_case_add_random
//------------------------------------------------------------------------
// A random test case that tests push back.
template < typename T, typename Func >
void test_case_add_random( int test_case_num, Func f )
{
std::printf( "\n%d: %s\n", test_case_num, __func__ );
srand( 0xdeadbeef );
for ( size_t i = 0; i < 50; i++ ) {
// we need to add one to make sure nbins > 0
int nbins = (rand() % 10) + 1;
Table<T> table( nbins, f(0), f(10000) );
size_t size = rand() % 100;
int* data = new int[size];
printf( "\n - [ -note- ] nbins = %d, size = %lu\n", nbins, size );
for ( size_t j = 0; j < size; j++ ) {
int rand_int = rand() % 10000;
table.add( f(rand_int) );
data[j] = rand_int;
}
UTST_ASSERT_INT_EQ( table.size(), size );
// Check table against array that has same elements
for ( size_t j = 0; j < size; j++ )
UTST_ASSERT_TRUE( table.find( f(data[j]) ) );
delete[] data;
}
}
//------------------------------------------------------------------------
// test_case_copy_random
//------------------------------------------------------------------------
// A random test case that tests copy constructor and assignment operator.
template < typename T, typename Func >
void test_case_copy_random( int test_case_num, Func f )
{
std::printf( "\n%d: %s\n", test_case_num, __func__ );
srand( 0xdeadbeef );
for ( size_t i = 0; i < 50; i++ ) {
// we need to add one to make sure nbins > 0
int nbins0 = (rand() % 10) + 1;
Table<T> table0( nbins0, f(0), f(10000) );
// we need to add one to make sure nbins > 0
int nbins1 = (rand() % 10) + 1;
Table<T> table1( nbins1, f(0), f(10000) );
size_t size0 = rand() % 100;
size_t size1 = rand() % 100;
int* data = new int[size0+size1];
printf( "\n - [ -note- ] nbins = %d, nbins = %d, size = %lu\n", nbins0, nbins1, size0+size1 );
for ( size_t j = 0; j < size0; j++ ) {
int rand_int = rand() % 10000;
table0.add( f(rand_int) );
data[j] = rand_int;
}
// Assignment operator
table1 = table0;
// Copy constructor
Table<T> table2( table1 );
// Add more elements to table0 and table1
for ( size_t j = size0; j < size0+size1; j++ ) {
int rand_int = rand() % 10000;
table1.add( f(rand_int) );
table2.add( f(rand_int) );
data[j] = rand_int;
}
// Check size
UTST_ASSERT_INT_EQ( table0.size(), size0 );
UTST_ASSERT_INT_EQ( table1.size(), size0+size1 );
UTST_ASSERT_INT_EQ( table2.size(), size0+size1 );
// Check table 0
for ( size_t j = 0; j < size0; j++ )
UTST_ASSERT_TRUE( table0.find( f(data[j]) ) );
for ( size_t j = 0; j < size0+size1; j++ ) {
UTST_ASSERT_TRUE( table1.find( f(data[j]) ) );
UTST_ASSERT_TRUE( table2.find( f(data[j]) ) );
}
delete[] data;
}
}