hextolfp.c revision 290001
1#include "config.h"
2
3#include "ntp_stdlib.h"
4#include "ntp_calendar.h"
5
6#include "unity.h"
7#include "lfptest.h"
8
9void test_PositiveInteger(void);
10void test_NegativeInteger(void);
11void test_PositiveFraction(void);
12void test_NegativeFraction(void);
13void test_IllegalNumberOfInteger(void);
14void test_IllegalChar(void);
15
16
17void
18test_PositiveInteger(void) {
19	const char *str = "00001000.00000000";
20	l_fp actual;
21
22	l_fp expected = {{4096}, 0}; /* 16^3, no fraction part. */
23
24	TEST_ASSERT_TRUE(hextolfp(str, &actual));
25	TEST_ASSERT_TRUE(IsEqual(expected, actual));
26}
27
28void
29test_NegativeInteger(void) {
30	const char *str = "ffffffff.00000000"; /* -1 decimal */
31	l_fp actual;
32
33	l_fp expected = {{-1}, 0};
34
35	TEST_ASSERT_TRUE(hextolfp(str, &actual));
36	TEST_ASSERT_TRUE(IsEqual(expected, actual));
37}
38
39void
40test_PositiveFraction(void) {
41	const char *str = "00002000.80000000"; /* 8196.5 decimal */
42	l_fp actual;
43
44	l_fp expected = {{8192}, HALF};
45
46	TEST_ASSERT_TRUE(hextolfp(str, &actual));
47	TEST_ASSERT_TRUE(IsEqual(expected, actual));
48}
49
50void
51test_NegativeFraction(void) {
52	const char *str = "ffffffff.40000000"; /* -1 + 0.25 decimal */
53	l_fp actual;
54
55	l_fp expected = {{-1}, QUARTER}; /* -1 + 0.25 */
56
57	TEST_ASSERT_TRUE(hextolfp(str, &actual));
58	TEST_ASSERT_TRUE(IsEqual(expected, actual));
59}
60
61void
62test_IllegalNumberOfInteger(void) {
63	const char *str = "1000000.00000000"; /* Missing one digit in integral part. */
64	l_fp actual;
65
66	TEST_ASSERT_FALSE(hextolfp(str, &actual));
67}
68
69void
70test_IllegalChar(void) {
71	const char *str = "10000000.0000h000"; /* Illegal character h. */
72	l_fp actual;
73
74	TEST_ASSERT_FALSE(hextolfp(str, &actual));
75}
76