Home | History | Annotate | Line # | Download | only in libntp
strtolfp.c revision 1.3
      1 /*	$NetBSD: strtolfp.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 "ntp_calendar.h"
      7 
      8 #include "unity.h"
      9 #include "lfptest.h"
     10 
     11 /* This file tests both atolfp and mstolfp */
     12 
     13 void setUp(void);
     14 void test_PositiveInteger(void);
     15 void test_NegativeInteger(void);
     16 void test_PositiveFraction(void);
     17 void test_NegativeFraction(void);
     18 void test_PositiveMsFraction(void);
     19 void test_NegativeMsFraction(void);
     20 void test_InvalidChars(void);
     21 
     22 
     23 void
     24 setUp(void)
     25 {
     26 	init_lib();
     27 
     28 	return;
     29 }
     30 
     31 static const char* fmtLFP(const l_fp *e, const l_fp *a)
     32 {
     33     static char buf[100];
     34     snprintf(buf, sizeof(buf), "e=$%08x.%08x, a=$%08x.%08x",
     35 	     e->l_ui, e->l_uf, a->l_ui, a->l_uf);
     36     return buf;
     37 }
     38 
     39 void test_PositiveInteger(void) {
     40 	const char *str = "500";
     41 	const char *str_ms = "500000";
     42 
     43 	l_fp expected = {{500},0};
     44 	l_fp actual, actual_ms;
     45 
     46 	TEST_ASSERT_TRUE(atolfp(str, &actual));
     47 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
     48 
     49 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
     50 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
     51 }
     52 
     53 void test_NegativeInteger(void) {
     54 	const char *str = "-300";
     55 	const char *str_ms = "-300000";
     56 
     57 	l_fp expected;
     58 	expected.l_i = -300;
     59 	expected.l_uf = 0;
     60 
     61 	l_fp actual, actual_ms;
     62 
     63 	TEST_ASSERT_TRUE(atolfp(str, &actual));
     64 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
     65 
     66 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
     67 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
     68 }
     69 
     70 void test_PositiveFraction(void) {
     71 	const char *str = "+500.5";
     72 	const char *str_ms = "500500.0";
     73 
     74 	l_fp expected = {{500}, HALF};
     75 	l_fp actual, actual_ms;
     76 
     77 	TEST_ASSERT_TRUE(atolfp(str, &actual));
     78 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
     79 
     80 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
     81 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
     82 }
     83 
     84 void test_NegativeFraction(void) {
     85 	const char *str = "-300.75";
     86 	const char *str_ms = "-300750";
     87 
     88 	l_fp expected;
     89 	expected.l_i = -301;
     90 	expected.l_uf = QUARTER;
     91 
     92 	l_fp actual, actual_ms;
     93 
     94 	TEST_ASSERT_TRUE(atolfp(str, &actual));
     95 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
     96 
     97 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
     98 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
     99 }
    100 
    101 void test_PositiveMsFraction(void) {
    102 	const char *str = "300.00025";
    103 	const char *str_ms = "300000.25";
    104 
    105 	l_fp expected = {{300}, QUARTER_PROMILLE_APPRX};
    106 	l_fp actual, actual_ms;
    107 
    108 
    109 	TEST_ASSERT_TRUE(atolfp(str, &actual));
    110 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
    111 
    112 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
    113 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
    114 }
    115 
    116 void test_NegativeMsFraction(void) {
    117 	const char *str = "-199.99975";
    118 	const char *str_ms = "-199999.75";
    119 
    120 	l_fp expected;
    121 	expected.l_i = -200;
    122 	expected.l_uf = QUARTER_PROMILLE_APPRX;
    123 
    124 	l_fp actual, actual_ms;
    125 
    126 	TEST_ASSERT_TRUE(atolfp(str, &actual));
    127 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
    128 
    129 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual), fmtLFP(&expected, &actual));
    130 	TEST_ASSERT_TRUE_MESSAGE(IsEqual(expected, actual_ms), fmtLFP(&expected, &actual_ms));
    131 }
    132 
    133 void test_InvalidChars(void) {
    134 	const char *str = "500.4a2";
    135 	l_fp actual, actual_ms;
    136 
    137 	TEST_ASSERT_FALSE(atolfp(str, &actual));
    138 	TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
    139 }
    140 
    141