Project

General

Profile

Bug #166 » test.c

Jason King, 2011-01-26 06:56 PM

 
1
/*
2
 * Copyright 2011 Jason King.  All rights reserved.
3
 *
4
 * cc [-m64] -o test test.c -lnsl [-L${CODEMGR_WS}/root_$(uname -p)/lib[/64]]
5
 *     [-R${CODEMGR_WS}/root/$(uname -p)/lib[64]]
6
 */
7

    
8
#include <sys/types.h>
9
#include <rpc/xdr.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include <sys/stat.h>
14
#include <fcntl.h>
15
#include <unistd.h>
16
#include <errno.h>
17
#include <math.h>
18

    
19
static unsigned int seed;
20
static int ret;
21

    
22
static void load_seed(const char *filename);
23
static boolean_t test(const void *, const void *, size_t);
24
static void initbuf(uint32_t *, size_t);
25
static void printbuf(const char *, uint32_t *, size_t);
26
static boolean_t float_test(float);
27
static boolean_t double_test(double);
28

    
29
int
30
main(int argc, char** argv)
31
{
32
	float floatval;
33
	double doubleval;
34
	uint32_t *ip;
35
	boolean_t ret;
36

    
37
	if (argc > 3) {
38
		long val;
39
		ip = (uint32_t *)&floatval;
40
		val = strtol(argv[1], NULL, 0);
41
		*ip = val;
42

    
43
		ip = (uint32_t *)&doubleval;
44
		val = strtol(argv[2], NULL, 0);
45
		*ip++ = val;
46
		val = strtol(argv[3], NULL, 0);
47
		*ip = val;
48
	} else {
49
		load_seed(NULL);
50
		(void) srandom(seed);
51
		ip = (uint32_t *)&floatval;
52
		*ip = random();
53
		*ip = random();
54

    
55
		ip = (uint32_t *)&doubleval;
56
		ip[0] = random();
57
		ip[1] = random();
58
	}
59

    
60
	(void) printf("  Float test\n");
61
	ret = float_test(floatval);
62
	ret |= float_test(NAN);
63
	ret |= float_test(-NAN);
64
	ret |= float_test(INFINITY);
65
	ret |= float_test(-INFINITY);
66
	(void) printf("\n");
67

    
68
	(void) printf("  Double test\n");
69
	ret |= double_test(doubleval);
70
	ret |= double_test(NAN);
71
	ret |= double_test(-NAN);
72
	ret |= double_test(INFINITY);
73
	ret |= double_test(-INFINITY);
74

    
75
	(void) printf("==> %s\n", (ret) ? "PASSED" : "FAILED");
76

    
77
	return ((ret) ? 0 : 1);
78
}
79

    
80
static boolean_t
81
float_test(float val)
82
{
83
	uint32_t *valp = (uint32_t *)&val;
84
	uint32_t buf[4];
85
	uint32_t cmp[4];
86
	float val2;
87
	XDR xdr;
88
	boolean_t ret, ret2;
89

    
90
	(void) printf("\t%15s: 0x%08x = %e\n", "Test value", *valp, val);
91

    
92
	initbuf(buf, sizeof (buf) / sizeof (uint32_t));
93
	initbuf(cmp, sizeof (cmp) / sizeof (uint32_t));
94

    
95
	/* use the middle of the buffer to detect any under/overruns */
96
	cmp[1] = *valp;
97

    
98
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
99
	    sizeof (buf) - sizeof (uint32_t), XDR_ENCODE);
100
	xdr_float(&xdr, &val);
101
	ret = test(cmp, buf, sizeof (buf));
102
	(void) printf("\t%15s: %s\n", "Encode test",
103
	    (ret) ? "PASSED" : "FAILED");
104
	if (!ret) {
105
		printbuf("Expected", cmp, sizeof (cmp) / sizeof (uint32_t));
106
		printbuf("Actual", buf, sizeof (buf) / sizeof (uint32_t));
107
	}
108

    
109
	(void) memcpy(buf, cmp, sizeof (buf));
110
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
111
	    sizeof (buf) - sizeof (uint32_t), XDR_DECODE);
112
	xdr_float(&xdr, &val2);
113
	valp = (uint32_t *)&val2;
114
	ret2 = test(&val2, &val, sizeof (val));
115
	(void) printf("\t%15s: %s\n", "Decode test",
116
	    (ret) ? "PASSED" : "FAILED");
117
	if (!ret2) {
118
		valp = (uint32_t *)&val;
119
		(void) printf("\t%15s: 0x%08x = %e\n", "Expected", *valp, val);
120
		valp = (uint32_t *)&val2;
121
		(void) printf("\t%15s: 0x%08x = %e\n", "Actual", *valp, val2);
122
	}
123
	(void) printf("\n");
124

    
125
	return ((ret && ret2));
126
}
127

    
128
static boolean_t
129
double_test(double val)
130
{
131
	uint32_t *valp = (uint32_t *)&val;
132
	uint32_t buf[4];
133
	uint32_t cmp[4];
134
	double val2;
135
	XDR xdr;
136
	boolean_t ret, ret2;
137

    
138
	(void) printf("\t%15s: 0x%0x 0x%0x = %e\n", "Test value", valp[0],
139
	    valp[1], val);
140

    
141
	initbuf(buf, sizeof (buf) / sizeof (uint32_t));
142
	initbuf(cmp, sizeof (cmp) / sizeof (uint32_t));
143
	/* use the middle of the buffer to detect any under/overruns */
144
	cmp[1] = valp[0];
145
	cmp[2] = valp[1];
146

    
147
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
148
	    sizeof (buf) - sizeof (uint32_t), XDR_ENCODE);
149
	xdr_double(&xdr, &val);
150
	ret = test(cmp, buf, sizeof (cmp));
151
	(void) printf("\t%15s: %s\n", "Encode test",
152
	    (ret) ? "PASSED" : "FAILED");
153
	if (!ret) {
154
		printbuf("Expected", cmp, sizeof (cmp) / sizeof (uint32_t));
155
		printbuf("Actual", buf, sizeof (buf) / sizeof (uint32_t));
156
	}
157

    
158
	(void) memcpy(buf, cmp, sizeof (buf));
159
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
160
	    sizeof (buf) - sizeof (uint32_t), XDR_DECODE);
161
	xdr_double(&xdr, &val2);
162
	ret2 = test(&val2, &val, sizeof (val));
163
	(void) printf("\t%15s: %s\n", "Decode test",
164
	    (ret) ? "PASSED" : "FAILED");
165
	if (!ret2) {
166
		(void) printf("\t%15s: 0x%08x 0x%08x = %e\n", "Expected",
167
		   valp[0], valp[1], val);
168
		valp = (uint32_t *)&val2;
169
		(void) printf("\t%15s: 0x%08x 0x%08x = %e\n", "Actual", valp[0],
170
		    valp[1], val2);
171
	}
172
	(void) printf("\n");
173

    
174
	return ((ret && ret2));
175
}
176
static boolean_t
177
test(const void *s1, const void *s2, size_t len)
178
{
179
	int result = memcmp(s1, s2, len);
180
	return ((result == 0) ? B_TRUE : B_FALSE);
181
}
182

    
183
static void
184
initbuf(uint32_t *buf, size_t len)
185
{
186
	int i;
187

    
188
	for (i = 0; i < len; i++)
189
		buf[i] = 0xfeedface;
190
}
191

    
192
static void
193
printbuf(const char *label, uint32_t *buf, size_t len)
194
{
195
	int i;
196
	(void) printf("\t%15s: ", label);
197
	for (i = 0; i < len; i++)
198
		(void) printf("0x%08x ", buf[i]);
199
	(void) printf("\n");
200
}
201

    
202
static void
203
load_seed(const char *filename)
204
{
205
	ssize_t n;
206
	int fd;
207

    
208
	if (filename == NULL)
209
		filename = "/dev/random";
210

    
211
	fd = open(filename, O_RDONLY);
212
	if (fd == -1) {
213
		(void) fprintf(stderr, "Unable to read seed from %s: %s\n",
214
		    filename, strerror(errno));
215
		exit(1);
216
	}
217

    
218
	n = read(fd, &seed, sizeof (seed));
219
	if (n != sizeof (seed)) {
220
		(void) fprintf(stderr, "Unable to read seed from %s: %s\n",
221
		    filename, strerror(errno));
222
		exit(1);
223
	}
224
}
(3-3/4)