aboutsummaryrefslogtreecommitdiff
path: root/contrib/ntp/tests/libntp/strtolfp.c
blob: 6855d9ba3a8accf21eba4807289e140a43efd8db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "config.h"

#include "ntp_stdlib.h"
#include "ntp_calendar.h"

#include "unity.h"
#include "lfptest.h"

/* This file tests both atolfp and mstolfp */

void setUp(void);
void test_PositiveInteger(void);
void test_NegativeInteger(void);
void test_PositiveFraction(void);
void test_NegativeFraction(void);
void test_PositiveMsFraction(void);
void test_NegativeMsFraction(void);
void test_InvalidChars(void);


void
setUp(void)
{
	init_lib();

	return;
}


void test_PositiveInteger(void) {
	const char *str = "500";
	const char *str_ms = "500000";

	l_fp expected = {{500},0};
	l_fp actual, actual_ms;

	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
}

void test_NegativeInteger(void) {
	const char *str = "-300";
	const char *str_ms = "-300000";

	l_fp expected;
	expected.l_i = -300;
	expected.l_uf = 0;

	l_fp actual, actual_ms;

	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
}

void test_PositiveFraction(void) {
	const char *str = "+500.5";
	const char *str_ms = "500500.0";

	l_fp expected = {{500}, HALF};
	l_fp actual, actual_ms;

	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
}

void test_NegativeFraction(void) {
	const char *str = "-300.75";
	const char *str_ms = "-300750";

	l_fp expected;
	expected.l_i = -301;
	expected.l_uf = QUARTER;

	l_fp actual, actual_ms;

	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
}

void test_PositiveMsFraction(void) {
	const char *str = "300.00025";
	const char *str_ms = "300000.25";

	l_fp expected = {{300}, QUARTER_PROMILLE_APPRX};
	l_fp actual, actual_ms;


	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));

}

void test_NegativeMsFraction(void) {
	const char *str = "-199.99975";
	const char *str_ms = "-199999.75";

	l_fp expected;
	expected.l_i = -200;
	expected.l_uf = QUARTER_PROMILLE_APPRX;

	l_fp actual, actual_ms;

	TEST_ASSERT_TRUE(atolfp(str, &actual));
	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));

	TEST_ASSERT_TRUE(IsEqual(expected, actual));
	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));

}

void test_InvalidChars(void) {
	const char *str = "500.4a2";
	l_fp actual, actual_ms;

	TEST_ASSERT_FALSE(atolfp(str, &actual));
	TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
}