hextolfp.c revision 285612
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
9
10void test_PositiveInteger(void) {
11	const char *str = "00001000.00000000";
12	l_fp actual;
13
14	l_fp expected = {4096, 0}; // 16^3, no fraction part.
15
16	TEST_ASSERT_TRUE(hextolfp(str, &actual));
17	TEST_ASSERT_TRUE(IsEqual(expected, actual));
18}
19
20void test_NegativeInteger(void) {
21	const char *str = "ffffffff.00000000"; // -1 decimal
22	l_fp actual;
23
24	l_fp expected = {-1, 0};
25
26	TEST_ASSERT_TRUE(hextolfp(str, &actual));
27	TEST_ASSERT_TRUE(IsEqual(expected, actual));
28}
29
30void test_PositiveFraction(void) {
31	const char *str = "00002000.80000000"; // 8196.5 decimal
32	l_fp actual;
33
34	l_fp expected = {8192, HALF};
35
36	TEST_ASSERT_TRUE(hextolfp(str, &actual));
37	TEST_ASSERT_TRUE(IsEqual(expected, actual));
38}
39
40void test_NegativeFraction(void) {
41	const char *str = "ffffffff.40000000"; // -1 + 0.25 decimal
42	l_fp actual;
43
44	l_fp expected = {-1, QUARTER}; //-1 + 0.25
45
46	TEST_ASSERT_TRUE(hextolfp(str, &actual));
47	TEST_ASSERT_TRUE(IsEqual(expected, actual));
48}
49
50void test_IllegalNumberOfInteger(void) {
51	const char *str = "1000000.00000000"; // Missing one digit in integral part.
52	l_fp actual;
53
54	TEST_ASSERT_FALSE(hextolfp(str, &actual));
55}
56
57void test_IllegalChar(void) {
58	const char *str = "10000000.0000h000"; // Illegal character h.
59	l_fp actual;
60
61	TEST_ASSERT_FALSE(hextolfp(str, &actual));
62}
63
64