1 /* $NetBSD: hextoint.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */ 2 3 #include "config.h" 4 5 #include "ntp_stdlib.h" 6 #include "ntp_calendar.h" 7 #include "ntp_fp.h" 8 9 #include "unity.h" 10 11 void test_SingleDigit(void); 12 void test_MultipleDigits(void); 13 void test_MaxUnsigned(void); 14 void test_Overflow(void); 15 void test_IllegalChar(void); 16 17 test_SingleDigit(void)18void test_SingleDigit(void) { 19 const char *str = "a"; // 10 decimal 20 u_long actual; 21 22 TEST_ASSERT_TRUE(hextoint(str, &actual)); 23 TEST_ASSERT_EQUAL(10, actual); 24 } 25 test_MultipleDigits(void)26void test_MultipleDigits(void) { 27 const char *str = "8F3"; // 2291 decimal 28 u_long actual; 29 30 TEST_ASSERT_TRUE(hextoint(str, &actual)); 31 TEST_ASSERT_EQUAL(2291, actual); 32 } 33 test_MaxUnsigned(void)34void test_MaxUnsigned(void) { 35 const char *str = "ffffffff"; // 4294967295 decimal 36 u_long actual; 37 38 TEST_ASSERT_TRUE(hextoint(str, &actual)); 39 TEST_ASSERT_EQUAL(4294967295UL, actual); 40 } 41 test_Overflow(void)42void test_Overflow(void) { 43 const char *str = "100000000"; // Overflow by 1 44 u_long actual; 45 46 TEST_ASSERT_FALSE(hextoint(str, &actual)); 47 } 48 test_IllegalChar(void)49void test_IllegalChar(void) { 50 const char *str = "5gb"; // Illegal character g 51 u_long actual; 52 53 TEST_ASSERT_FALSE(hextoint(str, &actual)); 54 } 55 56