server-side-compute/test/data-gen.c

64 lines
1.2 KiB
C
Raw Normal View History

2019-08-20 14:54:18 +00:00
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <float.h>
#include <time.h>
#include <errno.h>
2019-08-20 14:54:18 +00:00
/*
* Generate test data
*/
// generate float number between 0 and 99 with at most 2 decimal
float gen_float(void){
u_int32_t my_int = rand() % 10000;
float my_float = (float) my_int / 100;
return my_float;
}
2019-08-20 14:54:18 +00:00
// generate some data such that we can reduce it later
void prepare_data(int size){
float * data = malloc(size * sizeof(float));
for(int i=0; i < size; i++){
data[i] = (float) gen_float();
2019-08-20 14:54:18 +00:00
}
FILE * file = fopen("data.bin", "wb");
fwrite(data, size*sizeof(float), 1, file);
fclose(file);
free(data);
}
// parse generated data
void parse_data(int size){
float data ;
FILE * file = fopen("data.bin", "r");
for(int i=0; i < size; i++){
int res = fread(&data, sizeof(float), 1, file);
if (res != 1){
res = errno;
fprintf(stderr, "fread failed to read the right amount of float: %s\n",strerror(res));
exit(EXIT_FAILURE);
}
fprintf(stdout, "%.2f ", data);
}
fclose(file);
}
2019-08-20 14:54:18 +00:00
int main(){
int size = 40;
srand(time(NULL));
2019-08-20 14:54:18 +00:00
prepare_data(size);
return 0;
}