test_rtapi_vsnprintf.c
1 /* 2 * Copyright (C) 2013 Jeff Epler <jepler@unpythonic.net> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 */ 18 #include "rtapi.h" 19 #include "vsnprintf.h" 20 #include <stdio.h> 21 #include <string.h> 22 23 int rtapi_snprintf(char *buf, unsigned long size, const char *fmt, ...) 24 { 25 int retval; 26 va_list ap; 27 va_start(ap, fmt); 28 retval = rtapi_vsnprintf(buf, size, fmt, ap); 29 va_end(ap); 30 return retval; 31 } 32 33 double vectors[] = { 34 0.0, -0.0, 3.14, -3.14, 35 100, 1e6, 1e300, 36 1e-10, 1e-100, 1e-280, 1e-300, 37 -__builtin_inf(), __builtin_inf(), __builtin_nan(""), 38 }; 39 40 int nvectors = sizeof(vectors)/sizeof(vectors[0]); 41 42 #include <sys/time.h> 43 #include <sys/resource.h> 44 double unow() 45 { 46 struct rusage ru; 47 getrusage(RUSAGE_SELF, &ru); 48 return ru.ru_utime.tv_sec + ru.ru_utime.tv_usec*1e-6; 49 } 50 51 #define N (1000000) 52 53 int main(void) 54 { 55 char buf1[1024], buf2[1024]; 56 int i, fail=0; 57 for(i=0; i<nvectors; i++) 58 { 59 int j; 60 double t0, t1; 61 t0 = unow(); 62 for(j=0; j<N; j++) 63 { 64 rtapi_snprintf(buf1, sizeof(buf1), "%f", vectors[i]); 65 } 66 t1 = unow(); 67 snprintf(buf2, sizeof(buf2), "%A", vectors[i]); 68 printf("rtapi=%-30s libc=%-30s", buf1, buf2); 69 if(strcasecmp(buf1, buf2)) { 70 fail++; 71 printf(" ****fail****"); 72 } 73 printf(" %.1fns/it\n", (t1-t0)*1e9/N); 74 } 75 return fail ? 1 : 0; 76 }