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