From ae6657a9d645df0ffbbf7ddac8535b794f98b266 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 17 Apr 2016 14:09:08 +0100 Subject: [PATCH] Added HexToInt function and unit tests for it. HexToInt converts a value from a string to a integer. Pointers are used to optimise the memory usage. --- source/common/text.cpp | 71 +++++++++++++++++++ source/common/text.h | 3 + source/tests/xestiacalendar_commonfunctions.h | 61 ++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/source/common/text.cpp b/source/common/text.cpp index df64411..d223993 100644 --- a/source/common/text.cpp +++ b/source/common/text.cpp @@ -185,4 +185,75 @@ PropertyNameValue SplitNameValue(string InputData){ return FinalNameValue; +} + +bool HexToInt(std::string *HexString, int *Number){ + + // Check that each character in the string is a number + // or a letter (a-f/A-F). + + char Char = 0; + int CharNum = 0; + + for (int CharSeek = 0; + CharSeek < HexString->size(); CharSeek++){ + + // Check if character is a number (0-9). + + Char = HexString->at(CharSeek); + CharNum = Char; + + if (CharNum >= 48 && + CharNum <= 57){ + + continue; + + } + + // Check if character is a letter (A-F) + + if (CharNum >= 65 && + CharNum <= 70){ + + continue; + + } + + // Check if character is a letter (a-f). + + if (CharNum >= 97 && + CharNum <= 102){ + + continue; + + } + + // Exit from subroutine as character is invalid. + + return false; + + } + + // Convert a Hex value that is in string to integer. + + try { + + *Number = stoi((*HexString), nullptr, 16); + + } + + catch (const std::invalid_argument &err){ + + return false; + + } + + catch (const std::out_of_range &err){ + + return false; + + } + + return true; + } \ No newline at end of file diff --git a/source/common/text.h b/source/common/text.h index 0cc513a..f50113e 100644 --- a/source/common/text.h +++ b/source/common/text.h @@ -2,9 +2,11 @@ #define __COMMON_TEXT_H__ #include +#include #include #include #include +#include struct PropertyNameValue{ std::string Name; @@ -17,5 +19,6 @@ std::multimap ProcessTextVectors(std::vector SplitValues(std::string InputData); PropertyNameValue SplitNameValue(std::string InputData); +bool HexToInt(std::string *HexString, int *Number); #endif \ No newline at end of file diff --git a/source/tests/xestiacalendar_commonfunctions.h b/source/tests/xestiacalendar_commonfunctions.h index 85ef33e..ce4fc7c 100644 --- a/source/tests/xestiacalendar_commonfunctions.h +++ b/source/tests/xestiacalendar_commonfunctions.h @@ -197,4 +197,65 @@ TEST(CommonFunctions, SplitNameValueTests){ ASSERT_EQ(PropertyName, "TEST"); ASSERT_EQ(PropertyValue, "OK"); +} + +TEST(CommonFunctions, HexToInt){ + + string Value1 = "10"; // 16 + string Value2 = "50"; // 80 + string Value3 = "4F"; // 79 + string Value4 = "FF"; // 255 + string Value5 = "FFF"; // 4095 + string Value6 = "FFFF"; // 65535 + string Value7 = "75AB"; // 30123 + string Value8 = "2AC"; // 684 + string Value9 = "!"; // Fail + string Value10 = "4BZ"; // Fail + string Value11 = "Z?!$"; // Fail + + int OutputValue = 0; + bool Result = false; + + Result = HexToInt(&Value1, &OutputValue); + + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 16); + + Result = HexToInt(&Value2, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 80); + + Result = HexToInt(&Value3, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 79); + + Result = HexToInt(&Value4, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 255); + + Result = HexToInt(&Value5, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 4095); + + Result = HexToInt(&Value6, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 65535); + + Result = HexToInt(&Value7, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 30123); + + Result = HexToInt(&Value8, &OutputValue); + ASSERT_EQ(Result, true); + ASSERT_EQ(OutputValue, 684); + + Result = HexToInt(&Value9, &OutputValue); + ASSERT_EQ(Result, false); + + Result = HexToInt(&Value10, &OutputValue); + ASSERT_EQ(Result, false); + + Result = HexToInt(&Value11, &OutputValue); + ASSERT_EQ(Result, false); + } \ No newline at end of file -- 2.39.5