1 /* $NetBSD: lfptostr.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */ 2 3 /* 4 * This file contains test for both mfptoa and mfptoms (which uses dolfptoa), 5 * since all these functions are very similar. It also tests ulfptoa, which is 6 * a macro. 7 */ 8 9 #include "config.h" 10 #include "ntp_stdlib.h" 11 #include "ntp_fp.h" 12 13 #include "unity.h" 14 15 static const int LFP_MAX_PRECISION = 10; 16 static const int LFP_MAX_PRECISION_MS = 7; 17 18 static const int ONE_FOURTH = 1073741824; /* (1 << 30) */ 19 static const int HALF = (1 << 31); 20 static const int THREE_FOURTH = -1073741824; 21 static const int HALF_PROMILLE_UP = 2147484; /* slightly more than 0.0005 */ 22 static const int HALF_PROMILLE_DOWN = 2147483; /* slightly less than 0.0005 */ 23 24 25 void setUp(void); 26 void test_PositiveInteger(void); 27 void test_NegativeInteger(void); 28 void test_PositiveIntegerWithFraction(void); 29 void test_NegativeIntegerWithFraction(void); 30 void test_RoundingDownToInteger(void); 31 void test_RoundingMiddleToInteger(void); 32 void test_RoundingUpToInteger(void); 33 void test_SingleDecimal(void); 34 void test_MillisecondsRoundingUp(void); 35 void test_MillisecondsRoundingDown(void); 36 void test_UnsignedInteger(void); 37 38 39 void 40 setUp(void) 41 { 42 init_lib(); 43 44 return; 45 } 46 47 48 void 49 test_PositiveInteger(void) { 50 l_fp test = {{200}, 0}; /* exact 200.0000000000 */ 51 52 TEST_ASSERT_EQUAL_STRING("+200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION)); 53 TEST_ASSERT_EQUAL_STRING("+200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS)); 54 } 55 56 void 57 test_NegativeInteger(void) { 58 l_fp test = {{-100}, 0}; /* -100 */ 59 60 TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION)); 61 TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 62 } 63 64 void 65 test_PositiveIntegerWithFraction(void) { 66 l_fp test = {{200}, ONE_FOURTH}; /* 200.25 */ 67 68 TEST_ASSERT_EQUAL_STRING("+200.2500000000", lfptoa(&test, LFP_MAX_PRECISION)); 69 TEST_ASSERT_EQUAL_STRING("+200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 70 } 71 72 void 73 test_NegativeIntegerWithFraction(void) { 74 l_fp test = {{-100}, ONE_FOURTH}; /* -99.75 */ 75 76 TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION)); 77 TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); 78 } 79 80 void 81 test_RoundingDownToInteger(void) { 82 l_fp test = {{10}, ONE_FOURTH}; /* 10.25 */ 83 84 TEST_ASSERT_EQUAL_STRING("+10", lfptoa(&test, 0)); 85 TEST_ASSERT_EQUAL_STRING("+10250", lfptoms(&test, 0)); 86 } 87 88 void 89 test_RoundingMiddleToInteger(void) { 90 l_fp test = {{10}, HALF}; /* 10.5 */ 91 92 TEST_ASSERT_EQUAL_STRING("+11", lfptoa(&test, 0)); 93 TEST_ASSERT_EQUAL_STRING("+10500", lfptoms(&test, 0)); 94 } 95 96 void 97 test_RoundingUpToInteger(void) { 98 l_fp test = {{5}, THREE_FOURTH}; /* 5.75 */ 99 100 TEST_ASSERT_EQUAL_STRING("+6", lfptoa(&test, 0)); 101 TEST_ASSERT_EQUAL_STRING("+5750", lfptoms(&test, 0)); 102 } 103 104 void 105 test_SingleDecimal(void) { 106 l_fp test = {{8}, ONE_FOURTH}; /* 8.25 */ 107 108 TEST_ASSERT_EQUAL_STRING("+8.3", lfptoa(&test, 1)); 109 TEST_ASSERT_EQUAL_STRING("+8250.0", lfptoms(&test, 1)); 110 } 111 112 void 113 test_MillisecondsRoundingUp(void) { 114 l_fp test = {{1}, HALF_PROMILLE_UP}; /* slightly more than 1.0005 */ 115 116 TEST_ASSERT_EQUAL_STRING("+1.0", lfptoa(&test, 1)); 117 118 TEST_ASSERT_EQUAL_STRING("+1000.5", lfptoms(&test, 1)); 119 TEST_ASSERT_EQUAL_STRING("+1001", lfptoms(&test, 0)); 120 } 121 122 void 123 test_MillisecondsRoundingDown(void) { 124 l_fp test = {{1}, HALF_PROMILLE_DOWN}; /* slightly less than 1.0005 */ 125 126 TEST_ASSERT_EQUAL_STRING("+1.0", lfptoa(&test, 1)); 127 128 TEST_ASSERT_EQUAL_STRING("+1000.5", lfptoms(&test, 1)); 129 TEST_ASSERT_EQUAL_STRING("+1000", lfptoms(&test, 0)); 130 } 131 132 void test_UnsignedInteger(void) { 133 l_fp test = {{3000000000UL}, 0}; 134 135 TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1)); 136 } 137