1 /* $NetBSD: sfptostr.c,v 1.1.1.3 2015/10/23 17:47:45 christos Exp $ */ 2 3 /* 4 * This file contains test for both fptoa and fptoms (which uses dofptoa), 5 * since all these functions are very similar. 6 */ 7 #include "config.h" 8 #include "ntp_fp.h" 9 #include "unity.h" 10 11 #define SFP_MAX_PRECISION 6 12 13 void test_PositiveInteger(void); 14 void test_NegativeInteger(void); 15 void test_PositiveIntegerPositiveFraction(void); 16 void test_NegativeIntegerNegativeFraction(void); 17 void test_PositiveIntegerNegativeFraction(void); 18 void test_NegativeIntegerPositiveFraction(void); 19 void test_SingleDecimalInteger(void); 20 void test_SingleDecimalRounding(void); 21 22 23 void test_PositiveInteger(void) 24 { 25 s_fp test = 300 << 16; // exact 300.000000 26 27 TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION)); 28 TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION)); 29 } 30 31 void test_NegativeInteger(void) 32 { 33 s_fp test = -200 << 16; // exact -200.000000 34 35 TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION)); 36 TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION)); 37 } 38 39 void test_PositiveIntegerPositiveFraction(void) 40 { 41 s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5 42 43 TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION)); 44 TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION)); 45 } 46 47 void test_NegativeIntegerNegativeFraction(void) 48 { 49 s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5 50 51 TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION)); 52 TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION)); 53 } 54 55 void test_PositiveIntegerNegativeFraction(void) 56 { 57 s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25 58 59 TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION)); 60 TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION)); 61 } 62 63 void test_NegativeIntegerPositiveFraction(void) 64 { 65 s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75 66 67 TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION)); 68 TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION)); 69 } 70 71 void test_SingleDecimalInteger(void) 72 { 73 s_fp test = 300 << 16; // 300 74 75 TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1)); 76 TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1)); 77 } 78 79 void test_SingleDecimalRounding(void) 80 { 81 s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75 82 83 TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1)); 84 TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1)); 85 } 86