Project

General

Profile

Bug #166 » test.c

Jason King, 2011-01-26 04:41 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

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

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

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

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

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

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

    
59
	ret = float_test(floatval);
60
	ret |= double_test(doubleval);
61

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

    
64
	return ((ret) ? 0 : 1);
65
}
66

    
67
static boolean_t
68
float_test(float val)
69
{
70
	uint32_t *valp = (uint32_t *)&val;
71
	uint32_t buf[4];
72
	uint32_t cmp[4];
73
	float val2;
74
	XDR xdr;
75
	boolean_t ret;
76

    
77
	(void) printf("==> Float test\n");
78
	(void) printf("%12s: 0x%08x = %e\n\n", "test value", *valp, val);
79

    
80
	(void) printf(" Encode\n");
81
	initbuf(buf, sizeof (buf) / sizeof (uint32_t));
82
	initbuf(cmp, sizeof (cmp) / sizeof (uint32_t));
83

    
84
	/* use the middle of the buffer to detect any under/overruns */
85
	cmp[1] = *valp;
86

    
87
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
88
	    sizeof (buf) - sizeof (uint32_t), XDR_ENCODE);
89
	xdr_float(&xdr, &val);
90
	printbuf("Expected", cmp, sizeof (cmp) / sizeof (uint32_t));
91
	printbuf("Actual", buf, sizeof (buf) / sizeof (uint32_t));
92
	ret = test(cmp, buf, sizeof (buf));
93

    
94
	(void) printf(" Decode\n");
95
	(void) memcpy(buf, cmp, sizeof (buf));
96
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
97
	    sizeof (buf) - sizeof (uint32_t), XDR_DECODE);
98
	xdr_float(&xdr, &val2);
99
	(void) printf("%12s: 0x%08x = %e\n", "Expected", *valp, val);
100
	valp = (uint32_t *)&val2;
101
	(void) printf("%12s: 0x%08x = %e\n", "Actual", *valp, val2);
102
	ret &= test(&val2, &val, sizeof (val));
103

    
104
	return (ret);
105
}
106

    
107
static boolean_t
108
double_test(double val)
109
{
110
	uint32_t *valp = (uint32_t *)&val;
111
	uint32_t buf[4];
112
	uint32_t cmp[4];
113
	double val2;
114
	XDR xdr;
115
	boolean_t ret;
116

    
117
	(void) printf("==> Double test\n");
118
	(void) printf("%12s: 0x%0x 0x%0x = %e\n\n", "test value", valp[0],
119
	    valp[1], val);
120

    
121
	(void) printf(" Encode\n");
122
	initbuf(buf, sizeof (buf) / sizeof (uint32_t));
123
	initbuf(cmp, sizeof (cmp) / sizeof (uint32_t));
124
	/* use the middle of the buffer to detect any under/overruns */
125
	cmp[1] = valp[0];
126
	cmp[2] = valp[1];
127

    
128
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
129
	    sizeof (buf) - sizeof (uint32_t), XDR_ENCODE);
130
	xdr_double(&xdr, &val);
131
	printbuf("Expected", cmp, sizeof (cmp) / sizeof (uint32_t));
132
	printbuf("Actual", buf, sizeof (buf) / sizeof (uint32_t));
133
	ret = test(cmp, buf, sizeof (cmp));
134

    
135
	(void) printf(" Decode\n");
136
	(void) memcpy(buf, cmp, sizeof (buf));
137
	xdrmem_create(&xdr, (const caddr_t)&buf[1],
138
	    sizeof (buf) - sizeof (uint32_t), XDR_DECODE);
139
	xdr_double(&xdr, &val2);
140
	(void) printf("%12s: 0x%08x 0x%08x = %e\n", "Expected", valp[0],
141
	    valp[1], val);
142
	valp = (uint32_t *)&val2;
143
	(void) printf("%12s: 0x%08x 0x%08x = %e\n", "Actual", valp[0], valp[1],
144
	    val2);
145
	ret &= test(&val2, &val, sizeof (val));
146

    
147
	return (ret);
148
}
149
static boolean_t
150
test(const void *s1, const void *s2, size_t len)
151
{
152
	int result = memcmp(s1, s2, len);
153
	(void) printf("%12s: %s\n\n", "Result",
154
		(result == 0) ? "PASSED" : "FAILED");
155
	return ((result == 0) ? B_TRUE : B_FALSE);
156
}
157

    
158
static void
159
initbuf(uint32_t *buf, size_t len)
160
{
161
	int i;
162

    
163
	for (i = 0; i < len; i++)
164
		buf[i] = 0xfeedface;
165
}
166

    
167
static void
168
printbuf(const char *label, uint32_t *buf, size_t len)
169
{
170
	int i;
171
	(void) printf("%12s: ", label);
172
	for (i = 0; i < len; i++)
173
		(void) printf("0x%08x ", buf[i]);
174
	(void) printf("\n");
175
}
176

    
177
static void
178
load_seed(const char *filename)
179
{
180
	ssize_t n;
181
	int fd;
182

    
183
	if (filename == NULL)
184
		filename = "/dev/random";
185

    
186
	fd = open(filename, O_RDONLY);
187
	if (fd == -1) {
188
		(void) fprintf(stderr, "Unable to read seed from %s: %s\n",
189
		    filename, strerror(errno));
190
		exit(1);
191
	}
192

    
193
	n = read(fd, &seed, sizeof (seed));
194
	if (n != sizeof (seed)) {
195
		(void) fprintf(stderr, "Unable to read seed from %s: %s\n",
196
		    filename, strerror(errno));
197
		exit(1);
198
	}
199
}
(2-2/4)