hextolfp.c revision 1.1.1.5
1/* $NetBSD: hextolfp.c,v 1.1.1.5 2016/01/08 21:21:33 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 11void test_PositiveInteger(void); 12void test_NegativeInteger(void); 13void test_PositiveFraction(void); 14void test_NegativeFraction(void); 15void test_IllegalNumberOfInteger(void); 16void test_IllegalChar(void); 17 18 19void 20test_PositiveInteger(void) { 21 const char *str = "00001000.00000000"; 22 l_fp actual; 23 24 l_fp expected = {{4096}, 0}; /* 16^3, no fraction part. */ 25 26 TEST_ASSERT_TRUE(hextolfp(str, &actual)); 27 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 28} 29 30void 31test_NegativeInteger(void) { 32 const char *str = "ffffffff.00000000"; /* -1 decimal */ 33 l_fp actual; 34 35 l_fp expected = {{-1}, 0}; 36 37 TEST_ASSERT_TRUE(hextolfp(str, &actual)); 38 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 39} 40 41void 42test_PositiveFraction(void) { 43 const char *str = "00002000.80000000"; /* 8196.5 decimal */ 44 l_fp actual; 45 46 l_fp expected = {{8192}, HALF}; 47 48 TEST_ASSERT_TRUE(hextolfp(str, &actual)); 49 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 50} 51 52void 53test_NegativeFraction(void) { 54 const char *str = "ffffffff.40000000"; /* -1 + 0.25 decimal */ 55 l_fp actual; 56 57 l_fp expected = {{-1}, QUARTER}; /* -1 + 0.25 */ 58 59 TEST_ASSERT_TRUE(hextolfp(str, &actual)); 60 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 61} 62 63void 64test_IllegalNumberOfInteger(void) { 65 const char *str = "1000000.00000000"; /* Missing one digit in integral part. */ 66 l_fp actual; 67 68 TEST_ASSERT_FALSE(hextolfp(str, &actual)); 69} 70 71void 72test_IllegalChar(void) { 73 const char *str = "10000000.0000h000"; /* Illegal character h. */ 74 l_fp actual; 75 76 TEST_ASSERT_FALSE(hextolfp(str, &actual)); 77} 78