vi64ops.c revision 290001
1#include "config.h"
2
3#include "ntp_stdlib.h"
4#include "vint64ops.h"
5
6#include "unity.h"
7
8
9int IsEqual(const vint64 expected, const vint64 actual);
10void test_ParseVUI64_pos(void);
11void test_ParseVUI64_neg(void);
12void test_ParseVUI64_case(void);
13
14
15// technically bool
16int
17IsEqual(const vint64 expected, const vint64 actual) {
18	if (0 == memcmp(&expected, &actual, sizeof(vint64))) {
19		printf( "%x.", expected.D_s.hi);
20		printf("%x", expected.D_s.lo);
21		printf(" but was ");
22		printf("%x.", actual.D_s.hi);
23		printf("%x\n", actual.D_s.lo);
24		return TRUE;
25	} else {
26		printf("expected: ");
27		printf( "%d.", expected.D_s.hi);
28		printf("%d", expected.D_s.lo);
29		printf(" but was ");
30		printf("%d", actual.D_s.lo);
31		printf("%d", actual.D_s.lo);
32		return FALSE;
33	}
34}
35
36// ----------------------------------------------------------------------
37// test number parser
38void
39test_ParseVUI64_pos(void) {
40	vint64 act, exp;
41	const char *sp;
42	char       *ep;
43
44	sp         = "1234x";
45	exp.D_s.hi = 0;
46	exp.D_s.lo = 1234;
47	act        = strtouv64(sp, &ep, 0);
48
49	TEST_ASSERT_TRUE(IsEqual(exp, act));
50	TEST_ASSERT_EQUAL(*ep, 'x');
51}
52
53
54void
55test_ParseVUI64_neg(void) {
56	vint64 act, exp;
57	const char *sp;
58	char       *ep;
59
60	sp         = "-1234x";
61	exp.D_s.hi = ~0;
62	exp.D_s.lo = -1234;
63	act        = strtouv64(sp, &ep, 0);
64	TEST_ASSERT_TRUE(IsEqual(exp, act));
65	TEST_ASSERT_EQUAL(*ep, 'x');
66}
67
68void
69test_ParseVUI64_case(void) {
70	vint64 act, exp;
71	const char *sp;
72	char       *ep;
73
74	sp         = "0123456789AbCdEf";
75	exp.D_s.hi = 0x01234567;
76	exp.D_s.lo = 0x89ABCDEF;
77	act        = strtouv64(sp, &ep, 16);
78	TEST_ASSERT_TRUE(IsEqual(exp, act));
79	TEST_ASSERT_EQUAL(*ep, '\0');
80}
81