1/*-
2 * Copyright (c) 2007 Diomidis Spinellis. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * 1. Redistributions of source code must retain the above copyright
7 *    notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 *    notice, this list of conditions and the following disclaimer in the
10 *    documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22 * SUCH DAMAGE.
23 */
24
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD$");
27
28#include <sys/types.h>
29
30#include <assert.h>
31#include <float.h>
32#include <limits.h>
33#include <math.h>
34#include <stdio.h>
35#include <stdint.h>
36#include <stdlib.h>
37#include <strings.h>
38#include <syslog.h>
39#include <time.h>
40
41#define KASSERT(val, msg) assert(val)
42
43typedef u_int32_t comp_t;
44
45#define AHZ 1000000
46
47#include "convert.c"
48
49static int nerr;
50
51union cf {
52	comp_t c;
53	float f;
54};
55
56static void
57check_result(const char *name, float expected, union cf v)
58{
59	double eps;
60
61	eps = fabs(expected - v.f) / expected;
62	if (eps > FLT_EPSILON) {
63		printf("Error in %s\n", name);
64		printf("Got      0x%08x %12g\n", v.c, v.f);
65		v.f = expected;
66		printf("Expected 0x%08x %12g (%.15lg)\n", v.c, v.f, expected);
67		printf("Epsilon=%lg, rather than %g\n", eps, FLT_EPSILON);
68		nerr++;
69	}
70}
71
72int
73main(int argc, char *argv[])
74{
75	union cf v;
76	long l;
77	int i, end;
78	struct timeval tv;
79
80	if (argc == 2) {
81		/* Loop test */
82		end = atoi(argv[1]);
83		for (i = 0; i < end; i++) {
84			tv.tv_sec = random();
85			tv.tv_usec = (random() % 1000000);
86			v.c = encode_timeval(tv);
87			check_result("encode_timeval",
88			    (float)tv.tv_sec * AHZ + tv.tv_usec, v);
89			l = random();
90			v.c = encode_long(l);
91			check_result("encode_long", l, v);
92		}
93	} else if (argc == 3) {
94		/* Single-value timeval/long test */
95		tv.tv_sec = atol(argv[1]);
96		tv.tv_usec = atol(argv[2]);
97		v.c = encode_timeval(tv);
98		check_result("encode_timeval",
99		    (float)tv.tv_sec * AHZ + tv.tv_usec, v);
100		v.c = encode_long(tv.tv_sec);
101		check_result("encode_long", tv.tv_sec, v);
102	} else {
103		fprintf(stderr, "usage:\n%s repetitions\n%s sec usec\n",
104		    argv[0], argv[0]);
105		return (1);
106	}
107	return (nerr);
108}
109