Project

General

Profile

Bug #166 » test.c

Jason King, 2011-01-30 01:46 PM

 
1
#include <stdio.h>
2
#include <rpc/xdr.h>
3
#include <sys/types.h>
4
#include <err.h>
5
#include <string.h>
6
#include <stdlib.h>
7

    
8
/* XDR stream genereated on Solaris 10 sparc, and validated on Solaris 10 x86 */
9
static uchar_t test_data[] = {
10
    0x40, 0xc8, 0x1c, 0xd6, 0xe6, 0x31, 0xf8, 0xa1, 0x49, 0x09, 0xe0, 0x23 
11
};
12
static double expected_d = 12345.6789;
13
static float expected_f = 564738.201928;
14

    
15
void
16
print_float(float f)
17
{
18
	uint32_t *p = (uint32_t *)&f;
19
	printf("f = %f\n", f);
20
	printf("    %a\n", f);
21
	printf("    0x%08x\n", *p);
22
}
23

    
24
void
25
print_double(double d)
26
{
27
	uint64_t *p = (uint64_t *)&d;
28
	printf("d = %f\n", d);
29
	printf("    %a\n", d);
30
	printf("    0x%016llx\n", *p);
31
}
32

    
33

    
34
int
35
main(int argc, char **argv)
36
{
37
	uchar_t testbuf[sizeof (test_data)];
38

    
39
	XDR xdr;
40
	double d = 42.42;
41
	float f = 42.42;
42

    
43
	xdrmem_create(&xdr, (caddr_t)&test_data, sizeof (test_data),
44
	    XDR_DECODE);
45
	if (xdr_double(&xdr, &d) == FALSE)
46
		errx(EXIT_FAILURE, "Unable to convert double value");
47
	if (xdr_float(&xdr, &f) == FALSE)
48
		errx(EXIT_FAILURE, "Unable to convert float value");
49

    
50
	(void) printf("Expected value:\n");
51
	print_double(expected_d);
52
	(void) printf("Actual value:\n");
53
	print_double(d);
54
	(void) printf("%s\n", (d == expected_d) ? "PASS" : "FAIL");
55
	(void) printf("Expected value:\n");
56
	print_float(expected_f);
57
	(void) printf("Actual value:\n");
58
	print_float(f);
59
	(void) printf("%s\n", (f == expected_f) ? "PASS" : "FAIL");
60

    
61
	(void) printf("ENCODE TEST\n");
62
	xdrmem_create(&xdr, (caddr_t)&testbuf, sizeof (testbuf), XDR_ENCODE);
63
	xdr_double(&xdr, &expected_d);
64
	xdr_float(&xdr, &expected_f);
65

    
66
	if (memcmp(testbuf, test_data, sizeof (testbuf)) == 0)
67
		(void) printf("PASS\n");
68
	else
69
		(void) printf("FAIL\n");
70

    
71
	return (0);
72
}
(4-4/4)