/* This software is released into public domain. It is provided "as is", without warranties or conditions of any kind. Anyone is free to use, modify, redistribute and do anything with this software. All echo functions in here work as if the parameter -e was provided. */ #define _POSIX_SOURCE /* putchar_unlocked */ #include #include #include #include #include #include #include #include #include #ifndef NITER #define NITER 1000000 #endif typedef void (*func)(void); typedef void (*echo_func)(int argc, char **argv); int64_t bench(func f, int niter); void test_init(int argc, char **argv); void test_free(void); void test_echo(echo_func echo); void test_null(void); void test_buf(void); void test_inplace(void); void test_finplace(void); void test_putchar(void); void test_putcharu(void); void test_write(void); void echo_null(int argc, char **argv); void echo_buf(int argc, char **argv); void echo_inplace(int argc, char **argv); void echo_finplace(int argc, char **argv); void echo_putchar(int argc, char **argv); void echo_putcharu(int argc, char **argv); void echo_write(int argc, char **argv); size_t echo_unescape(char *dst, const char *src); int argc_orig; char **argv_orig; char **argv_cpy; char **argv_echo; int64_t bench(func f, int niter) { struct timeval start, end; gettimeofday(&start, NULL); while (niter--) { f(); } gettimeofday(&end, NULL); return (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec); } void test_init(int argc, char **argv) { int i; argc_orig = argc; argv_orig = argv; argv_cpy = malloc(sizeof(*argv) * (argc+1)); if (!argv_cpy) { abort(); } argv_cpy[argc] = NULL; argv_echo = malloc(sizeof(*argv) * (argc+1)); if (!argv_echo) { abort(); } argv_echo[argc] = NULL; for (i=0; i