-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_utilities.c
200 lines (160 loc) · 6.47 KB
/
array_utilities.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Necessary libraries and headers for functions
#include "array_utilities.h"
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define RAND_NUM_SEED 32899
struct Array_With_Length_Padded* get_rand_padded_array(
const unsigned int array_len) {
// Array length has to be greater than zero
assert(array_len > 0);
// Variable declarations
struct Array_With_Length_Padded* array_w_len_padded =
malloc(sizeof(*array_w_len_padded));
unsigned int padded_2n_length = (unsigned int)exp2(ceil(log2(array_len)));
// Edge case handling where array_len = 1
if (array_len == 1) {
++padded_2n_length;
}
const float range_min_val = -1.0f;
const float range_interval = 2.0f;
const int even_test_num = 2;
array_w_len_padded->array_len_actual = array_len;
array_w_len_padded->padded_2n_length = padded_2n_length;
array_w_len_padded->padding_location_indicator = PAD_ARRAY_AT_END;
// Retrieve current system nanoseconds and use as seed for random array
// generation
srand(RAND_NUM_SEED);
/*
* Generate the appropriate random array of characters/numbers and then
* pad the array with extra depending on the ARRAY_TYPE
*/
#if (ARRAY_TYPE == CHAR)
char* rand_char_array = malloc(padded_2n_length * sizeof(*rand_char_array));
for (unsigned int curr_entry = 0; curr_entry < array_len; ++curr_entry) {
rand_char_array[curr_entry] = (char)(rand() % UCHAR_MAX);
}
for (unsigned int curr_entry = array_len; curr_entry < padded_2n_length;
++curr_entry) {
rand_char_array[curr_entry] = CHAR_MAX;
}
array_w_len_padded->contents = rand_char_array;
#elif (ARRAY_TYPE == INT)
int* rand_int_array = malloc(padded_2n_length * sizeof(*rand_int_array));
for (unsigned int curr_entry = 0; curr_entry < array_len; ++curr_entry) {
rand_int_array[curr_entry] =
(int)((range_min_val +
((double)rand() * range_interval / (double)RAND_MAX)) *
(double)UINT_MAX);
// compensation for above algorithm generating only even numbers
if (rand() % even_test_num != 0) {
++rand_int_array[curr_entry];
}
}
for (unsigned int curr_entry = array_len; curr_entry < padded_2n_length;
++curr_entry) {
rand_int_array[curr_entry] = INT_MAX;
}
array_w_len_padded->contents = rand_int_array;
#elif (ARRAY_TYPE == LONG)
long* rand_long_array = malloc(padded_2n_length * sizeof(*rand_long_array));
for (unsigned int curr_entry = 0; curr_entry < array_len; ++curr_entry) {
rand_long_array[curr_entry] =
(long)((range_min_val +
((double)rand() * range_interval / (double)RAND_MAX)) *
(double)ULONG_MAX);
// compensation for above algorithm generating only even numbers
if (rand() % even_test_num != 0) {
++rand_long_array[curr_entry];
}
}
for (unsigned int curr_entry = array_len; curr_entry < padded_2n_length;
++curr_entry) {
rand_long_array[curr_entry] = LONG_MAX;
}
array_w_len_padded->contents = rand_long_array;
#elif (ARRAY_TYPE == FLOAT)
float* rand_float_array =
malloc(padded_2n_length * sizeof(*rand_float_array));
for (unsigned int curr_entry = 0; curr_entry < array_len; ++curr_entry) {
rand_float_array[curr_entry] =
range_min_val + ((float)rand() * range_interval / (float)RAND_MAX);
}
for (unsigned int curr_entry = array_len; curr_entry < padded_2n_length;
++curr_entry) {
rand_float_array[curr_entry] = FLT_MAX;
}
array_w_len_padded->contents = rand_float_array;
#elif (ARRAY_TYPE == DOUBLE)
double* rand_double_array =
malloc(padded_2n_length * sizeof(*rand_double_array));
for (unsigned int curr_entry = 0; curr_entry < array_len; ++curr_entry) {
rand_double_array[curr_entry] =
range_min_val + ((double)rand() * range_interval / (double)RAND_MAX);
}
for (unsigned int curr_entry = array_len; curr_entry < padded_2n_length;
++curr_entry) {
rand_double_array[curr_entry] = DBL_MAX;
}
array_w_len_padded->contents = rand_double_array;
#endif
return array_w_len_padded;
}
struct Array_With_Length_Padded* deep_cp_padded_array(
struct Array_With_Length_Padded* padded_array) {
// No null pointers allowed for parameter
assert(padded_array != NULL);
// Allocate memory for deep copy of function parameter
const unsigned int array_with_padding_len = padded_array->padded_2n_length;
struct Array_With_Length_Padded* padded_array_deep_cp =
malloc(sizeof(*padded_array_deep_cp));
padded_array_deep_cp->contents = malloc(
array_with_padding_len * sizeof(*(padded_array_deep_cp->contents)));
// Copy over non-pointer fields' values
padded_array_deep_cp->array_len_actual = padded_array->array_len_actual;
padded_array_deep_cp->padded_2n_length = array_with_padding_len;
padded_array_deep_cp->padding_location_indicator =
padded_array->padding_location_indicator;
// Copy over contents of array
memcpy(padded_array_deep_cp->contents, padded_array->contents,
array_with_padding_len * sizeof(*(padded_array->contents)));
return padded_array_deep_cp;
}
void print_array(struct Array_With_Length_Padded* array) {
// No null pointers allowed for parameter
assert(array != NULL);
unsigned int array_index_begin = 0;
unsigned int array_index_end = array->array_len_actual;
if (array->padding_location_indicator) {
array_index_begin = array->padded_2n_length - array->array_len_actual;
array_index_end = array->padded_2n_length;
}
for (unsigned int array_index = array_index_begin;
array_index < array_index_end; ++array_index) {
printf(DISPLAY_FORMAT_STR, array->contents[array_index]);
}
printf("\n");
}
void assert_padded_arrays_equality(
struct Array_With_Length_Padded* first_padded_array,
struct Array_With_Length_Padded* second_padded_array) {
assert(first_padded_array != NULL);
assert(second_padded_array != NULL);
assert(first_padded_array->array_len_actual ==
second_padded_array->array_len_actual);
assert(first_padded_array->padded_2n_length ==
second_padded_array->padded_2n_length);
assert(first_padded_array->padding_location_indicator ==
second_padded_array->padding_location_indicator);
for (unsigned int curr_index = 0;
curr_index < first_padded_array->padded_2n_length; ++curr_index) {
assert(first_padded_array->contents[curr_index] ==
second_padded_array->contents[curr_index]);
}
printf(ASSERTION_PASSED_INFORM_USER);
}