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