summaryrefslogtreecommitdiff
path: root/src/rtapi/test_rtapi_vsnprintf.c
blob: d6a8c219067cb1a1a4c441f2f6e59b2d76a6dc34 (plain)
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
/*
 * Copyright (C) 2013 Jeff Epler <jepler@unpythonic.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "rtapi.h"
#include "vsnprintf.h"
#include <stdio.h>
#include <string.h>

int rtapi_snprintf(char *buf, unsigned long size, const char *fmt, ...)
{
    int retval;
    va_list ap;
    va_start(ap, fmt);
    retval = rtapi_vsnprintf(buf, size, fmt, ap);
    va_end(ap);
    return retval;
}

double vectors[] = {
    0.0, -0.0, 3.14, -3.14,
    100, 1e6, 1e300,
    1e-10, 1e-100, 1e-280, 1e-300,
    -__builtin_inf(), __builtin_inf(), __builtin_nan(""), 
};

int nvectors = sizeof(vectors)/sizeof(vectors[0]);

#include <sys/time.h>
#include <sys/resource.h>
double unow()
{
    struct rusage ru;
    getrusage(RUSAGE_SELF, &ru);
    return ru.ru_utime.tv_sec + ru.ru_utime.tv_usec*1e-6;
}

#define N (1000000)

int main(void)
{
    char buf1[1024], buf2[1024];
    int i, fail=0;
    for(i=0; i<nvectors; i++)
    {
	int j;
	double t0, t1;
	t0 = unow();
	for(j=0; j<N; j++)
	{
	    rtapi_snprintf(buf1, sizeof(buf1), "%f", vectors[i]);
	}
	t1 = unow();
	snprintf(buf2, sizeof(buf2), "%A", vectors[i]);
	printf("rtapi=%-30s libc=%-30s", buf1, buf2);
	if(strcasecmp(buf1, buf2)) {
	    fail++;
	    printf(" ****fail****");
	}
	printf(" %.1fns/it\n", (t1-t0)*1e9/N);
    }
    return fail ? 1 : 0;
}