forked from capablevms/cheri-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-timsort_purecap.c
98 lines (71 loc) · 2.04 KB
/
test-timsort_purecap.c
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
#include "lib/timsort_lib_purecap.h"
#include "lib/timsortdata.h"
#include <assert.h>
#include <cheriintrin.h>
bool arrEq(int arr_a[], int arr_b[])
{
assert(cheri_length_get(arr_a) == cheri_length_get(arr_b));
for (size_t ix = cheri_base_get(arr_a); ix <= cheri_length_get(arr_a); ix++)
{
if (arr_a[ix] != arr_b[ix])
{
return false;
}
}
return true;
}
void test_merge()
{
int input_arr_control[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int input_arr_mutate_a_input[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int input_arr_mutate_a_expected[] = {6, 5, 4, 3, 2, 1, 10, 9, 8, 7};
int input_arr_mutate_b_input[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int input_arr_mutate_b_expected[] = {5, 4, 3, 2, 1, 10, 9, 8, 7, 6};
size_t mid_a = 4;
size_t mid_b = 5;
int *arr_a_base_set = cheri_offset_set(input_arr_mutate_a_input, mid_a * sizeof(int));
int *arr_b_base_set = cheri_offset_set(input_arr_mutate_b_input, mid_b * sizeof(int));
merge(arr_a_base_set);
merge(arr_b_base_set);
assert(arrEq(input_arr_mutate_a_input, input_arr_mutate_a_expected));
assert(arrEq(input_arr_mutate_b_input, input_arr_mutate_b_expected));
return;
}
void test_isSorted()
{
// positive cases
int sorted_array_empty[] = {};
int sorted_array_singleton[] = {42};
assert(isSorted(sorted_array_empty));
assert(isSorted(sorted_array_singleton));
int sorted_array_small[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assert(isSorted(sorted_array_small));
// negative cases
sorted_array_small[5] = 42;
assert(!isSorted(sorted_array_small));
}
void test_timsort()
{
int *arr = random_chunk(8192);
// place the chunk of data on the heap
assert(NULL != arr);
// ensure we start fair ( the data is not initially sorted )
assert(!isSorted(arr));
// sort the data
timSort(arr);
// check that have done real work
assert(isSorted(arr));
// clean up
free(arr);
}
/**
* Test harness for `timsort.c`.
* @return EXIT_SUCCESS when all tests pass. Assertion failure otherwise.
*/
int main(int argc, char *argv[])
{
test_isSorted();
test_merge();
test_timsort();
return EXIT_SUCCESS;
}