-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort-random-test.h
More file actions
118 lines (81 loc) · 2.88 KB
/
sort-random-test.h
File metadata and controls
118 lines (81 loc) · 2.88 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
//========================================================================
// sort-random-tests.h
//========================================================================
// This file contains generic random tests for the sort function. 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 "sort.h"
#include "utst.h"
#include "ece2400-stdlib.h"
#include <stdio.h>
#include <stdlib.h>
//------------------------------------------------------------------------
// test_case_random
//------------------------------------------------------------------------
// Many random values
template < typename T, typename Func >
void test_case_random( int test_case_num, Func f )
{
printf( "\n%d: %s\n", test_case_num, __func__ );
srand( 0xdeadbeef );
// Do 100 random tests
for ( size_t i = 0; i < 100; i++ ) {
// Choose a random size
size_t size = rand() % 100;
printf( "\n - [ -note- ] size = %lu\n", size );
// Allocate space for our test array and reference array
T* a = new T[size];
T* a_ref = new T[size];
// Fill both arrays with random values
for ( size_t j = 0; j < size; j++ ) {
T value = f( rand() % 100000 );
a[j] = value;
a_ref[j] = value;
}
// Use our sort algorithm on test array
sort( a, size );
// Verify that we sorted things correctly
for ( size_t j = 1; j < size; j++ )
UTST_ASSERT_TRUE( a[j-1] <= a[j] );
// Cleanup
delete[] a;
delete[] a_ref;
}
}
//------------------------------------------------------------------------
// test_case_random_few_unique
//------------------------------------------------------------------------
// Many values, but few unique
template < typename T, typename Func >
void test_case_random_few_unique( int test_case_num, Func f )
{
printf( "\n%d: %s\n", test_case_num, __func__ );
srand( 0xdeadbeef );
// Do 100 random tests
for ( size_t i = 0; i < 100; i++ ) {
// Choose a random size
size_t size = rand() % 100;
printf( "\n - [ -note- ] size = %lu\n", size );
// Allocate space for our test array and reference array
T* a = new T[size];
T* a_ref = new T[size];
// Fill both arrays with random values
for ( size_t j = 0; j < size; j++ ) {
T value = f( rand() % 10 );
a[j] = value;
a_ref[j] = value;
}
// Use our sort algorithm on test array
sort( a, size );
// Verify that we sorted things correctly
for ( size_t j = 1; j < size; j++ )
UTST_ASSERT_TRUE( a[j-1] <= a[j] );
// Cleanup
delete[] a;
delete[] a_ref;
}
}