[DEV] better interface to ingerprete string number

This commit is contained in:
Edouard DUPIN 2018-07-09 22:47:01 +02:00
parent 9b12492ff9
commit a7df1274ee
4 changed files with 207 additions and 60 deletions

View File

@ -202,6 +202,23 @@ void etest::GenericTest::testResult(bool _result,
m_numberCheckFail++;
}
void etest::GenericTest::testCatchThrow(const etk::Exception& exeption, uint32_t _line) {
ETEST_ERROR("Detect an error: " << m_file << ":" << _line << ": Catch etk::Exception");
ETEST_ERROR(" What='" << exeption.what() << "'");
ETEST_ERROR(" file='" << exeption.file() << "'");
ETEST_ERROR(" Line=" << exeption.line());
ETEST_ERROR(" Function='" << exeption.function() << "'");
m_haveError = true;
m_numberCheckFail++;
}
void etest::GenericTest::testCatchThrow(uint32_t _line) {
ETEST_ERROR("Detect an error: " << m_file << ":" << _line << ": Catch Unknow exception");
m_haveError = true;
m_numberCheckFail++;
}
void etest::GenericTest::clearLocal() {
m_haveError = false;
m_numberCheck = 0;
@ -241,7 +258,15 @@ int32_t etest::runAllTest() {
it->clearLocal();
g_currentTest = it;
echrono::Steady ticTest = echrono::Steady::now();
it->run();
try {
it->addCheck();
it->run();
} catch ( etk::Exception e ) {
it->testCatchThrow(e, __LINE__);
} catch ( ... ) {
it->testCatchThrow(__LINE__);
}
echrono::Steady tocTest = echrono::Steady::now();
g_currentTest = null;
if (it->getError() == true) {

View File

@ -138,6 +138,8 @@ namespace etest {
const etk::String& _test2Value,
const etk::String& _test2,
uint32_t _line);
void testCatchThrow(const etk::Exception& exeption, uint32_t _line);
void testCatchThrow(uint32_t _line);
void clearLocal();
virtual void run() = 0;
};
@ -172,81 +174,105 @@ namespace etest {
#define EXPECT_EQ(element, result) \
do { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_EQ(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) == (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
try { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_EQ(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) == (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} catch ( etk::Exception e ) { \
testCatchThrow(e, __LINE__); \
} catch ( ... ) {\
testCatchThrow(__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} while (false)
#define EXPECT_NE(element, result) \
do { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_NE(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) != (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
try { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_NE(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) != (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} catch ( etk::Exception e ) { \
testCatchThrow(e, __LINE__); \
} catch ( ... ) {\
testCatchThrow(__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} while (false)
#define ASSERT_NE(element, result) \
do { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] ASSERT_NE(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) != (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
if (ETEST_VARIABLE_TMP_res == true) { \
return; \
try { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] ASSERT_NE(" << #element << ", " << #result << ");"); \
bool ETEST_VARIABLE_TMP_res = ((element) != (result)); \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
if (ETEST_VARIABLE_TMP_res == true) { \
return; \
} \
} catch ( etk::Exception e ) { \
testCatchThrow(e, __LINE__); \
} catch ( ... ) {\
testCatchThrow(__LINE__); \
} \
} while (false)
#define EXPECT_FLOAT_EQ_DELTA(element, result, delta) \
do { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_FLOAT_EQ(" << #element << ", " << #result << ");"); \
float ETEST_VARIABLE_TMP_res2 = (element) - (result); \
bool ETEST_VARIABLE_TMP_res = false; \
if (ETEST_VARIABLE_TMP_res2 < delta && ETEST_VARIABLE_TMP_res2 > -delta) { \
ETEST_VARIABLE_TMP_res = true; \
try { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_FLOAT_EQ(" << #element << ", " << #result << ");"); \
float ETEST_VARIABLE_TMP_res2 = (element) - (result); \
bool ETEST_VARIABLE_TMP_res = false; \
if (ETEST_VARIABLE_TMP_res2 < delta && ETEST_VARIABLE_TMP_res2 > -delta) { \
ETEST_VARIABLE_TMP_res = true; \
} \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} catch ( etk::Exception e ) { \
testCatchThrow(e, __LINE__); \
} catch ( ... ) {\
testCatchThrow(__LINE__); \
} \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(ETEST_VARIABLE_TMP_res, \
etest::exportResultToString(element), \
#element, \
etest::exportResultToString(result), \
#result, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} while (false)
#define EXPECT_FLOAT_EQ(element, result) \

View File

@ -7,6 +7,7 @@
#include <etk/String.hpp>
#include <etk/UString.hpp>
#include <etk/Exception.hpp>
#if ETK_ENABLE_INTERNAL_DATA_IN_STRING > 0
@ -1105,6 +1106,98 @@ namespace etk {
uint64_t string_to_uint64_t(const etk::String& _obj) {
return _obj.to<uint64_t>();
}
uint64_t hexaString_to_uint64_t(const etk::String& _obj) {
size_t iii = 0;
uint64_t out = 0;
bool invert = false;
etk::String tmp = _obj.toLower();
if (tmp.startWith("0x") == true) {
iii = 2;
} else if (tmp.startWith("-0x") == true) {
iii = 3;
invert = true;
} else if (tmp.startWith("+0x") == true) {
iii = 3;
}
for (; iii<tmp.size(); ++iii) {
out <<= 4;
if ( tmp[iii] >= '0'
&& tmp[iii] <= '9') {
out += uint64_t(tmp[iii]) - uint64_t('0');
continue;
}
if ( tmp[iii] >= 'a'
&& tmp[iii] <= 'f') {
out += uint64_t(tmp[iii]) - uint64_t('a') + 10;
continue;
}
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument(" not an hexadecimal value: '" + _obj + "'"));
}
if (invert == true) {
int64_t tmp = *((int64_t*)&out);
tmp *= -1;
out = *((uint64_t*)&tmp);
}
return out;
}
uint64_t bitString_to_uint64_t(const etk::String& _obj) {
size_t iii = 0;
uint64_t out = 0;
bool invert = false;
etk::String tmp = _obj.toLower();
if (tmp.startWith("0b") == true) {
iii = 2;
} else if (tmp.startWith("-0b") == true) {
iii = 3;
invert = true;
} else if (tmp.startWith("+0b") == true) {
iii = 3;
}
for (; iii<tmp.size(); ++iii) {
out <<= 1;
if ( tmp[iii] == '0'
|| tmp[iii] == '1') {
out += uint64_t(tmp[iii]) - uint64_t('0');
continue;
}
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument(" not an binary value: '" + _obj + "'"));
}
if (invert == true) {
int64_t tmp = *((int64_t*)&out);
tmp *= -1;
out = *((uint64_t*)&tmp);
}
return out;
}
uint64_t octalString_to_uint64_t(const etk::String& _obj) {
size_t iii = 0;
uint64_t out = 0;
bool invert = false;
etk::String tmp = _obj.toLower();
if (tmp.startWith("0o") == true) {
iii = 2;
} else if (tmp.startWith("-0o") == true) {
iii = 3;
invert = true;
} else if (tmp.startWith("+0o") == true) {
iii = 3;
}
for (; iii<tmp.size(); ++iii) {
out <<= 3;
if ( tmp[iii] >= '0'
&& tmp[iii] <= '7') {
out += uint64_t(tmp[iii]) - uint64_t('0');
continue;
}
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument(" not an octal value: '" + _obj + "'"));
}
if (invert == true) {
int64_t tmp = *((int64_t*)&out);
tmp *= -1;
out = *((uint64_t*)&tmp);
}
return out;
}
bool string_to_bool(const etk::String& _obj) {
return _obj.to<bool>();
}

View File

@ -568,6 +568,9 @@ namespace etk {
uint16_t string_to_uint16_t(const etk::String& _obj);
uint32_t string_to_uint32_t(const etk::String& _obj);
uint64_t string_to_uint64_t(const etk::String& _obj);
uint64_t hexaString_to_uint64_t(const etk::String& _obj);
uint64_t bitString_to_uint64_t(const etk::String& _obj);
uint64_t octalString_to_uint64_t(const etk::String& _obj);
bool string_to_bool(const etk::String& _obj);
etk::String toLower(etk::String _obj);
etk::String toUpper(etk::String _obj);