genclibs/test.c
2024-10-14 15:26:57 +02:00

141 lines
3.6 KiB
C

#define ARRAY_OF int
#include "array.h"
#include "time_it.h"
#include "describe.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
typedef double_arr_t *double_arr;
#define ARRAY_OF double_arr
#define CUSTOM_AUTOFREE double_arr_arr_cleanup
#include "array.h"
void double_arr_arr_cleanup(double_arr_arr_t **arr){
if(*arr != NULL){
for(size_t i = 0; i < (*arr)->len; i++){
double_arr_free((*arr)->data[i]);
}
free(*arr);
}
}
#define SLEEP_DURATION 2
#define STRINGIFY_INTERNAL(x) #x
#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
int hello(int* _){
printf("%d, ", *_);
return 0;
}
#define generic _Generic
#define sum(a, e) generic((a), int : sumi, double : sumd)(a,e)
int sumi(int accu, int e){
return accu + e;
}
double sumd(double accu, double e){
return accu + e;
}
double average(double_arr_t *arr){
return fold(arr, sum, 0)/(arr->len == 0 ? 1 : arr->len);
}
bool over_5(int e){
return e > 5;
}
typedef struct type_{
size_t foo;
float bar;
} type_;
void wrapper(void){
register_type(field_descriptor_t, name, offset, size);
register_type(field_descriptor_t, offset, size);
register_type(field_descriptor_t);
register_type(struct type_);
autofree_array_of(uint8) *ser = serialize_type(struct type_, &((struct type_){ .foo = 5, .bar = .05f}));
if(ser != NULL){
struct type_ *des = deserialize_data(struct type_, ser);
printf("%zu, %f\n", des->foo, des->bar);
free(des);
}
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv){
//#define test_macro(x) (constexpr serializer_function_t _ = (serializer_function_t)(NULL), _)
wrapper();
autofree_array_of(int) *zozo = int_arr_init(10);
for(size_t i = 0; i < zozo->cap; i++){
int_arr_push(zozo, i);
}
autofree_array_of(int) *over = filter(zozo, over_5);
apply(zozo, hello);
printf("\n");
printf("%d\n", fold(zozo, sum, 0));
for(size_t i = 0; i < over->len; i++){
printf("%d, ", over->data[i]);
}
printf("\n");
//free(over);
// for(size_t i = 0; i < 64; i++){
// printf("log2(%zu) = %zu\n", i, log2(i));
// }
//free(zozo);
autofree_array_of(double_arr) *results = double_arr_arr_init(10);
double_arr_arr_push(results, time_it_repeat(sleep(SLEEP_DURATION), .repeat = 0, .number = 2));
double_arr_arr_push(results, time_it_repeat(sleep(SLEEP_DURATION), .number = 7));
double_arr_arr_push(results, time_it_repeat(sleep(SLEEP_DURATION), .repeat = 0));
double_arr_arr_push(results, time_it_repeat(sleep(SLEEP_DURATION)));
printf("##############################\n");
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .repeat = 0, .number = 2 took %f seconds\n", average(results->data[0]));
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .number = 7 took %f seconds\n", average(results->data[1]));
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .repeat = 0 took %f seconds\n", average(results->data[2]));
printf("sleep(" STRINGIFY(SLEEP_DURATION) ") took %f seconds\n", average(results->data[3]));
printf("##############################\n");
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .repeat = 0, .number = 2 took %f seconds\n", time_it(sleep(SLEEP_DURATION), .number = 2));
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .number = 7 took %f seconds\n", time_it(sleep(SLEEP_DURATION), .number = 7));
printf("sleep(" STRINGIFY(SLEEP_DURATION) "), .repeat = 0 took %f seconds\n", time_it(sleep(SLEEP_DURATION), .number = 0));
printf("sleep(" STRINGIFY(SLEEP_DURATION) ") took %f seconds\n", time_it(sleep(SLEEP_DURATION)));
free(double_arr_arr_pop(results));
return 0;
}