1284990Scy/* 2284990Scy * This file contains test for both mfptoa and mfptoms (which uses dolfptoa), 3284990Scy * since all these functions are very similar. It also tests ulfptoa, which is 4284990Scy * a macro. 5284990Scy */ 6284990Scy 7284990Scy#include "config.h" 8284990Scy#include "ntp_stdlib.h" 9284990Scy#include "ntp_fp.h" 10284990Scy 11284990Scy#include "unity.h" 12284990Scy 13284990Scystatic const int LFP_MAX_PRECISION = 10; 14284990Scystatic const int LFP_MAX_PRECISION_MS = 7; 15284990Scy 16289999Sglebiusstatic const int ONE_FOURTH = 1073741824; /* (1 << 30) */ 17284990Scystatic const int HALF = (1 << 31); 18284990Scystatic const int THREE_FOURTH = -1073741824; 19289999Sglebiusstatic const int HALF_PROMILLE_UP = 2147484; /* slightly more than 0.0005 */ 20289999Sglebiusstatic const int HALF_PROMILLE_DOWN = 2147483; /* slightly less than 0.0005 */ 21284990Scy 22284990Scy 23293893Sglebiusvoid setUp(void); 24289999Sglebiusvoid test_PositiveInteger(void); 25289999Sglebiusvoid test_NegativeInteger(void); 26289999Sglebiusvoid test_PositiveIntegerWithFraction(void); 27289999Sglebiusvoid test_NegativeIntegerWithFraction(void); 28289999Sglebiusvoid test_RoundingDownToInteger(void); 29289999Sglebiusvoid test_RoundingMiddleToInteger(void); 30289999Sglebiusvoid test_RoundingUpToInteger(void); 31289999Sglebiusvoid test_SingleDecimal(void); 32289999Sglebiusvoid test_MillisecondsRoundingUp(void); 33289999Sglebiusvoid test_MillisecondsRoundingDown(void); 34289999Sglebiusvoid test_UnsignedInteger(void); 35289999Sglebius 36289999Sglebius 37293893Sglebiusvoid 38293893SglebiussetUp(void) 39293893Sglebius{ 40293893Sglebius init_lib(); 41289999Sglebius 42293893Sglebius return; 43293893Sglebius} 44293893Sglebius 45293893Sglebius 46289999Sglebiusvoid 47289999Sglebiustest_PositiveInteger(void) { 48289999Sglebius l_fp test = {{200}, 0}; /* exact 200.0000000000 */ 49289999Sglebius 50284990Scy TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION)); 51284990Scy TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS)); 52284990Scy} 53284990Scy 54289999Sglebiusvoid 55289999Sglebiustest_NegativeInteger(void) { 56289999Sglebius l_fp test = {{-100}, 0}; /* -100 */ 57284990Scy 58284990Scy TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION)); 59284990Scy TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 60284990Scy} 61284990Scy 62289999Sglebiusvoid 63289999Sglebiustest_PositiveIntegerWithFraction(void) { 64289999Sglebius l_fp test = {{200}, ONE_FOURTH}; /* 200.25 */ 65284990Scy 66284990Scy TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION)); 67284990Scy TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 68284990Scy} 69284990Scy 70289999Sglebiusvoid 71289999Sglebiustest_NegativeIntegerWithFraction(void) { 72289999Sglebius l_fp test = {{-100}, ONE_FOURTH}; /* -99.75 */ 73284990Scy 74284990Scy TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION)); 75284990Scy TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 76284990Scy} 77284990Scy 78289999Sglebiusvoid 79289999Sglebiustest_RoundingDownToInteger(void) { 80289999Sglebius l_fp test = {{10}, ONE_FOURTH}; /* 10.25 */ 81284990Scy 82284990Scy TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0)); 83284990Scy TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0)); 84284990Scy} 85284990Scy 86289999Sglebiusvoid 87289999Sglebiustest_RoundingMiddleToInteger(void) { 88289999Sglebius l_fp test = {{10}, HALF}; /* 10.5 */ 89284990Scy 90284990Scy TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0)); 91284990Scy TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0)); 92284990Scy} 93284990Scy 94289999Sglebiusvoid 95289999Sglebiustest_RoundingUpToInteger(void) { 96289999Sglebius l_fp test = {{5}, THREE_FOURTH}; /* 5.75 */ 97284990Scy 98284990Scy TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0)); 99284990Scy TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0)); 100284990Scy} 101284990Scy 102289999Sglebiusvoid 103289999Sglebiustest_SingleDecimal(void) { 104289999Sglebius l_fp test = {{8}, ONE_FOURTH}; /* 8.25 */ 105284990Scy 106284990Scy TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1)); 107284990Scy TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1)); 108284990Scy} 109284990Scy 110289999Sglebiusvoid 111289999Sglebiustest_MillisecondsRoundingUp(void) { 112289999Sglebius l_fp test = {{1}, HALF_PROMILLE_UP}; /* slightly more than 1.0005 */ 113284990Scy 114284990Scy TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1)); 115284990Scy 116284990Scy TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1)); 117284990Scy TEST_ASSERT_EQUAL_STRING("1001", lfptoms(&test, 0)); 118284990Scy} 119284990Scy 120289999Sglebiusvoid 121289999Sglebiustest_MillisecondsRoundingDown(void) { 122289999Sglebius l_fp test = {{1}, HALF_PROMILLE_DOWN}; /* slightly less than 1.0005 */ 123284990Scy 124284990Scy TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1)); 125284990Scy 126284990Scy TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1)); 127284990Scy TEST_ASSERT_EQUAL_STRING("1000", lfptoms(&test, 0)); 128284990Scy} 129284990Scy 130284990Scyvoid test_UnsignedInteger(void) { 131289999Sglebius l_fp test = {{3000000000UL}, 0}; 132284990Scy 133284990Scy TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1)); 134284990Scy} 135