-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathrunner.cpp
308 lines (273 loc) · 7.02 KB
/
runner.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Generate Boost test fixtures.
*
* ## Notes
*
* - Run this script from the directory in which fixtures should be written.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
// #include <boost/TODO/TODO.hpp>
using boost::random::uniform_real_distribution;
using boost::random::uniform_int_distribution;
using boost::random::mt19937;
// Define a new pseudorandom number generator:
mt19937 rng;
/**
* Generates a linearly spaced numeric array of doubles.
*
* ## Notes
*
* - Assumes that the output array has at least 2 elements.
* - Output array is guaranteed to include both the start and end values.
*
*
* @param out output array
* @param len array length
* @param start first value
* @param end last value
*/
void linspace_f64( double *out, const unsigned int len, const double start, const double end ) {
unsigned int i;
double incr;
incr = (end-start) / (len-1);
for ( i = 0; i < len-1; i++ ) {
out[ i ] = start + (incr*i);
}
out[ i ] = end;
}
/**
* Generates a linearly spaced numeric array of integers.
*
* ## Notes
*
* - Assumes that the output array has at least 2 elements.
* - Output array is guaranteed to include both the start and end values.
*
*
* @param out output array
* @param len array length
* @param start first value
* @param end last value
*/
void linspace_i32( int *out, const unsigned int len, const int start, const int end ) {
unsigned int i;
int incr;
incr = (end-start) / (len-1);
for ( i = 0; i < len-1; i++ ) {
out[ i ] = start + (incr*i);
}
out[ i ] = end;
}
/**
* Generates an array of pseudorandom doubles drawn from a uniform distribution.
*
* @param out output array
* @param len array length
* @param a lower bound (inclusive)
* @param b upper bound (exclusive)
*/
void rand_array_f64( double *out, const unsigned int len, const double a, const double b ) {
unsigned int i;
// Define a uniform distribution for generating pseudorandom numbers:
uniform_real_distribution<> randu( a, b );
for ( i = 0; i < len; i++ ) {
out[ i ] = randu( rng );
}
}
/**
* Generates an array of pseudorandom integers drawn from a uniform distribution.
*
* @param out output array
* @param len array length
* @param a lower bound (inclusive)
* @param b upper bound (exclusive)
*/
void rand_array_i32( int *out, const unsigned int len, const int a, const int b ) {
unsigned int i;
// Define a uniform distribution for generating pseudorandom numbers:
uniform_int_distribution<> randi( a, b );
for ( i = 0; i < len; i++ ) {
out[ i ] = randi( rng );
}
}
/**
* Casts an array of integers to an array of doubles.
*
* @param out output array
* @param x input array
* @param len array length
*/
void i32_to_f64( double *out, int *x, unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
out[ i ] = (double) x[ i ];
}
}
/**
* Casts an array of doubles to an array of integers.
*
* @param out output array
* @param x input array
* @param len array length
*/
void f64_to_i32( int *out, double *x, unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
out[ i ] = (int) x[ i ];
}
}
/**
* Writes an array of doubles to a file as a series of comma-separated values.
*
* @param f file to write to
* @param x array of doubles
* @param len array length
*/
void write_array_f64( FILE *f, const double *x, const unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
fprintf( f, "%.17g", x[ i ] );
if ( i < len-1 ) {
fprintf( f, "," );
}
}
}
/**
* Writes an array of integers to a file as a series of comma-separated values.
*
* @param f file to write to
* @param x array of integers
* @param len array length
*/
void write_array_i32( FILE *f, const int *x, const unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
fprintf( f, "%d", x[ i ] );
if ( i < len-1 ) {
fprintf( f, "," );
}
}
}
/**
* Writes a named array of doubles to a file as JSON.
*
* @param f file to write to
* @param name array name
* @param x data
* @param len array length
*/
void write_named_array_f64( FILE *f, const char *name, const double *x, const unsigned int len ) {
fprintf( f, "\"%s\":[", name );
write_array_f64( f, x, len );
fprintf( f, "]" );
}
/**
* Writes a named array of integers to a file as JSON.
*
* @param f file to write to
* @param name array name
* @param x data
* @param len array length
*/
void write_named_array_i32( FILE *f, const char *name, const int *x, const unsigned int len ) {
fprintf( f, "\"%s\":[", name );
write_array_i32( f, x, len );
fprintf( f, "]" );
}
/**
* Writes data to a file as JSON.
*
* ## Notes
*
* - This function SHOULD be tailored to the input data (e.g., input types, output types, number of arguments, etc) and may vary from use case to use case.
*
*
* @param f file to write to
* @param x domain
* @param y results
* @param len array length
*/
void write_data_as_json( FILE *f, const double *x, const double *y, const unsigned int len ) {
fprintf( f, "{" );
write_named_array_f64( f, "x", x, len );
fprintf( f, "," );
write_named_array_f64( f, "expected", y, len ); // TODO: update or erase this comment
fprintf( f, "}" );
}
/**
* Generates test fixtures.
*
* @param x domain
* @param len number of values in the domain
* @param name output filename
*/
void generate( double *x, const unsigned int len, const char *name ) {
unsigned int i;
double *y;
FILE *f;
// Allocate an output array:
y = (double*) malloc( len * sizeof(double) ); // TODO
if ( y == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
// Generate fixture data:
for ( i = 0; i < len; i++ ) {
y[ i ] = 3.14; // TODO
}
// Open a new file:
f = fopen( name, "w" );
if ( f == NULL ) {
printf( "Error opening file.\n" );
exit( 1 );
}
// Write data as JSON:
write_data_as_json( f, x, y, len ); // TODO: tailor to fixture data
// Close the file:
fclose( f );
// Free allocated memory:
free( y );
}
/**
* Main execution sequence.
*/
int main( void ) {
unsigned int len;
double *x;
// Define the array length:
len = 1000;
// Allocate an array:
x = (double*) malloc( len * sizeof(double) );
if ( x == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
// Generate fixture data:
rand_array_f64( x, len, 0.0, 1.0 ); // TODO
generate( x, len, "TODO.json" );
// Free allocated memory:
free( x );
return 0;
}