1284990Scy/* 2284990Scy * This file contains test for both fptoa and fptoms (which uses dofptoa), 3284990Scy * since all these functions are very similar. 4284990Scy */ 5284990Scy#include "config.h" 6284990Scy#include "ntp_fp.h" 7293893Sglebius#include "ntp_stdlib.h" 8284990Scy#include "unity.h" 9284990Scy 10284990Scy#define SFP_MAX_PRECISION 6 11284990Scy 12293893Sglebiusvoid setUp(void); 13289999Sglebiusvoid test_PositiveInteger(void); 14289999Sglebiusvoid test_NegativeInteger(void); 15289999Sglebiusvoid test_PositiveIntegerPositiveFraction(void); 16289999Sglebiusvoid test_NegativeIntegerNegativeFraction(void); 17289999Sglebiusvoid test_PositiveIntegerNegativeFraction(void); 18289999Sglebiusvoid test_NegativeIntegerPositiveFraction(void); 19289999Sglebiusvoid test_SingleDecimalInteger(void); 20289999Sglebiusvoid test_SingleDecimalRounding(void); 21289999Sglebius 22289999Sglebius 23293893Sglebiusvoid 24293893SglebiussetUp(void) 25293893Sglebius{ 26293893Sglebius init_lib(); 27293893Sglebius 28293893Sglebius return; 29293893Sglebius} 30293893Sglebius 31293893Sglebius 32284990Scyvoid test_PositiveInteger(void) 33284990Scy{ 34284990Scy s_fp test = 300 << 16; // exact 300.000000 35284990Scy 36284990Scy TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION)); 37284990Scy TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION)); 38284990Scy} 39284990Scy 40284990Scyvoid test_NegativeInteger(void) 41284990Scy{ 42310419Sdelphij s_fp test = -(200 << 16); // exact -200.000000 43284990Scy 44284990Scy TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION)); 45284990Scy TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION)); 46284990Scy} 47284990Scy 48284990Scyvoid test_PositiveIntegerPositiveFraction(void) 49284990Scy{ 50284990Scy s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5 51284990Scy 52284990Scy TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION)); 53284990Scy TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION)); 54284990Scy} 55284990Scy 56284990Scyvoid test_NegativeIntegerNegativeFraction(void) 57284990Scy{ 58310419Sdelphij s_fp test = -(200 << 16) - (1 << 15); // -200 - 0.5 59284990Scy 60284990Scy TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION)); 61284990Scy TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION)); 62284990Scy} 63284990Scy 64284990Scyvoid test_PositiveIntegerNegativeFraction(void) 65284990Scy{ 66284990Scy s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25 67284990Scy 68284990Scy TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION)); 69284990Scy TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION)); 70284990Scy} 71284990Scy 72284990Scyvoid test_NegativeIntegerPositiveFraction(void) 73284990Scy{ 74310419Sdelphij s_fp test = -(200 << 16) + (1 << 14)*3; // -200 + 0.75 75284990Scy 76284990Scy TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION)); 77284990Scy TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION)); 78284990Scy} 79284990Scy 80284990Scyvoid test_SingleDecimalInteger(void) 81284990Scy{ 82284990Scy s_fp test = 300 << 16; // 300 83284990Scy 84284990Scy TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1)); 85284990Scy TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1)); 86284990Scy} 87284990Scy 88284990Scyvoid test_SingleDecimalRounding(void) 89284990Scy{ 90284990Scy s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75 91284990Scy 92284990Scy TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1)); 93284990Scy TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1)); 94284990Scy} 95