Project

General

Profile

Bug #1232 » test.c

Test case - Jason King, 2011-07-21 02:59 AM

 
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
#include <math.h>
8

    
9
/* XDR stream genereated on Solaris 10 sparc, and validated on Solaris 10 x86 */
10
static uchar_t test_data[] = {
11
    0x40, 0xc8, 0x1c, 0xd6,
12
    0xe6, 0x31, 0xf8, 0xa1, 0x49, 0x09, 0xe0, 0x23,
13
    0x7f, 0xff, 0xff, 0xff,
14
    0xff, 0xff, 0xff, 0xff,
15
    0x7f, 0x80, 0x00, 0x00,
16
    0xff, 0x80, 0x00, 0x00
17
};
18
static double expected_d = 12345.6789;
19
static float expected_f = 564738.201928;
20
static float expected_fs[] = {
21
	NAN, -NAN, INFINITY, -INFINITY
22
};
23

    
24
void
25
print_float(float f)
26
{
27
	uint32_t *p = (uint32_t *)&f;
28
	printf("f = %f\n", f);
29
	printf("    %a\n", f);
30
	printf("    0x%08x\n", *p);
31
}
32

    
33
void
34
print_double(double d)
35
{
36
	uint64_t *p = (uint64_t *)&d;
37
	printf("d = %f\n", d);
38
	printf("    %a\n", d);
39
	printf("    0x%016llx\n", *p);
40
}
41

    
42

    
43
int
44
main(int argc, char **argv)
45
{
46
	uchar_t testbuf[sizeof (test_data)];
47

    
48
	XDR xdr;
49
	double d = 42.42;
50
	float f = 42.42;
51
	float fs[4];
52

    
53
	xdrmem_create(&xdr, (caddr_t)&test_data, sizeof (test_data),
54
	    XDR_DECODE);
55
	if (xdr_double(&xdr, &d) == FALSE)
56
		errx(EXIT_FAILURE, "Unable to convert double value");
57
	if (xdr_float(&xdr, &f) == FALSE)
58
		errx(EXIT_FAILURE, "Unable to convert float value");
59
	for (int i = 0; i < 4; i++)
60
		xdr_float(&xdr, &fs[i]);
61

    
62
	(void) printf("Expected value:\n");
63
	print_double(expected_d);
64
	(void) printf("Actual value:\n");
65
	print_double(d);
66
	(void) printf("%s\n", (d == expected_d) ? "PASS" : "FAIL");
67
	(void) printf("Expected value:\n");
68
	print_float(expected_f);
69
	(void) printf("Actual value:\n");
70
	print_float(f);
71
	(void) printf("%s\n", (f == expected_f) ? "PASS" : "FAIL");
72
	for (int i = 0; i < 4; i++) {
73
		(void) printf("Special values: \n");
74
		(void) printf("    %f: %f\n", expected_fs[i], fs[i]);
75
	}
76

    
77
	print_float(expected_fs[0]);
78
	print_float(fs[0]);
79

    
80
	(void) printf("ENCODE TEST\n");
81
	xdrmem_create(&xdr, (caddr_t)&testbuf, sizeof (testbuf), XDR_ENCODE);
82
	xdr_double(&xdr, &expected_d);
83
	xdr_float(&xdr, &expected_f);
84

    
85
	if (memcmp(testbuf, test_data, sizeof (testbuf)) == 0)
86
		(void) printf("PASS\n");
87
	else
88
		(void) printf("FAIL\n");
89

    
90
	return (0);
91
}
(3-3/3)