smeartest.c revision 293896
1#include <config.h>
2
3#include <ntp.h>
4#include <ntp_fp.h>
5#include <ntp_assert.h>
6
7/*
8 * we want to test a refid format of:
9 * 254.x.y.x
10 *
11 * where x.y.z are 24 bits containing 2 (signed) integer bits
12 * and 22 fractional bits.
13 *
14 * we want functions to convert to/from this format, with unit tests.
15 *
16 * Interesting test cases include:
17 * 254.0.0.0
18 * 254.0.0.1
19 * 254.127.255.255
20 * 254.128.0.0
21 * 254.255.255.255
22 */
23
24char *progname = "";
25
26l_fp convertRefIDToLFP(uint32_t r);
27uint32_t convertLFPToRefID(l_fp num);
28
29
30/*
31 * The smear data in the refid is the bottom 3 bytes of the refid,
32 * 2 bits of integer
33 * 22 bits of fraction
34 */
35l_fp
36convertRefIDToLFP(uint32_t r)
37{
38	l_fp temp;
39
40	r = ntohl(r);
41
42	printf("%03d %08x: ", (r >> 24) & 0xFF, (r & 0x00FFFFFF) );
43
44	temp.l_uf = (r << 10);	/* 22 fractional bits */
45
46	temp.l_ui = (r >> 22) & 0x3;
47	temp.l_ui |= ~(temp.l_ui & 2) + 1;
48
49	return temp;
50}
51
52
53uint32_t
54convertLFPToRefID(l_fp num)
55{
56	uint32_t temp;
57
58	/* round the input with the highest bit to shift out from the
59	 * fraction, then keep just two bits from the integral part.
60	 *
61	 * TODO: check for overflows; should we clamp/saturate or just
62	 * complain?
63	 */
64	L_ADDUF(&num, 0x200);
65	num.l_ui &= 3;
66
67	/* combine integral and fractional part to 24 bits */
68	temp  = (num.l_ui << 22) | (num.l_uf >> 10);
69
70	/* put in the leading 254.0.0.0 */
71	temp |= UINT32_C(0xFE000000);
72
73	printf("%03d %08x: ", (temp >> 24) & 0xFF, (temp & 0x00FFFFFF) );
74
75	return htonl(temp);
76}
77
78/* Tests start here */
79
80void rtol(uint32_t r);
81
82void
83rtol(uint32_t r)
84{
85	l_fp l;
86
87	printf("rtol: ");
88
89	l = convertRefIDToLFP(htonl(r));
90	printf("refid %#x, smear %s\n", r, lfptoa(&l, 8));
91
92	return;
93}
94
95
96void rtoltor(uint32_t r);
97
98void
99rtoltor(uint32_t r)
100{
101	l_fp l;
102
103	printf("rtoltor: ");
104	l = convertRefIDToLFP(htonl(r));
105
106	r = convertLFPToRefID(l);
107	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
108
109	return;
110}
111
112
113void ltor(l_fp l);
114
115void
116ltor(l_fp l)
117{
118	uint32_t r;
119
120	printf("ltor: ");
121
122	r = convertLFPToRefID(l);
123	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
124
125	return;
126}
127
128
129int
130main()
131{
132	l_fp l;
133	int rc;
134
135	init_lib();
136
137	rtol(0xfe800000);
138	rtol(0xfe800001);
139	rtol(0xfe8ffffe);
140	rtol(0xfe8fffff);
141	rtol(0xfef00000);
142	rtol(0xfef00001);
143	rtol(0xfefffffe);
144	rtol(0xfeffffff);
145
146	rtol(0xfe000000);
147	rtol(0xfe000001);
148	rtol(0xfe6ffffe);
149	rtol(0xfe6fffff);
150	rtol(0xfe700000);
151	rtol(0xfe700001);
152	rtol(0xfe7ffffe);
153	rtol(0xfe7fffff);
154
155	rtoltor(0xfe800000);
156	rtoltor(0xfe800001);
157	rtoltor(0xfe8ffffe);
158	rtoltor(0xfe8fffff);
159	rtoltor(0xfef00000);
160	rtoltor(0xfef00001);
161	rtoltor(0xfefffffe);
162	rtoltor(0xfeffffff);
163
164	rtoltor(0xfe000000);
165	rtoltor(0xfe000001);
166	rtoltor(0xfe6ffffe);
167	rtoltor(0xfe6fffff);
168	rtoltor(0xfe700000);
169	rtoltor(0xfe700001);
170	rtoltor(0xfe7ffffe);
171	rtoltor(0xfe7fffff);
172
173	rc = atolfp("-.932087", &l);
174	INSIST(1 == rc);
175
176	ltor(l);
177	rtol(0xfec458b0);
178	printf("%x -> %d.%d.%d.%d\n",
179		0xfec458b0,
180		0xfe,
181		  0xc4,
182		    0x58,
183		      0xb0);
184
185	return 0;
186}
187