From 31830bdc55bd603e90a13b54b81b919526cf03a5 Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 20 Mar 2018 16:08:52 -0400 Subject: [PATCH] Full branch coverage for all common/type modules. --- src/common/type/variant.c | 21 +++++++++++++++++---- test/src/module/common/typeBufferTest.c | 1 + test/src/module/common/typeKeyValueTest.c | 2 ++ test/src/module/common/typeListTest.c | 3 ++- test/src/module/common/typeStringListTest.c | 4 +++- test/src/module/common/typeStringTest.c | 7 ++++++- test/src/module/common/typeVariantTest.c | 11 ++++++++--- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/common/type/variant.c b/src/common/type/variant.c index fccf5caca..f5d2ec04a 100644 --- a/src/common/type/variant.c +++ b/src/common/type/variant.c @@ -6,6 +6,7 @@ Variant Data Type #include #include +#include "common/debug.h" #include "common/memContext.h" #include "common/type/variant.h" @@ -38,14 +39,17 @@ New variant of any supported type static Variant * varNewInternal(VariantType type, void *data, size_t dataSize) { + // Make sure there is some data to store + ASSERT_DEBUG(dataSize > 0); + ASSERT_DEBUG(data != NULL); + // Allocate memory for the variant and set the type Variant *this = memNew(sizeof(Variant) + dataSize); this->memContext = memContextCurrent(); this->type = type; - // Copy data if there is any - if (dataSize > 0) - memcpy((unsigned char *)this + sizeof(Variant), data, dataSize); + // Copy data + memcpy((unsigned char *)this + sizeof(Variant), data, dataSize); return this; } @@ -597,14 +601,23 @@ varStrForce(const Variant *this) case varTypeDouble: { + // Convert to a string String *working = strNewFmt("%f", varDbl(this)); + // Any formatted double should be at least 8 characters, i.e. 0.000000 + ASSERT_DEBUG(strSize(working) >= 8); + // Any formatted double should have a decimal point + ASSERT_DEBUG(strchr(strPtr(working), '.') != NULL); + // Strip off any final 0s and the decimal point if there are no non-zero digits after it const char *begin = strPtr(working); const char *end = begin + strSize(working) - 1; - while (end > strPtr(working) && (*end == '0' || *end == '.')) + while (*end == '0' || *end == '.') { + // It should not be possible to go past the beginning because format "%f" will always write a decimal point + ASSERT_DEBUG(end > begin); + end--; if (*(end + 1) == '.') diff --git a/test/src/module/common/typeBufferTest.c b/test/src/module/common/typeBufferTest.c index 7a441a039..e30d13672 100644 --- a/test/src/module/common/typeBufferTest.c +++ b/test/src/module/common/typeBufferTest.c @@ -21,6 +21,7 @@ testRun() TEST_RESULT_BOOL(memcmp(bufPtr(buffer), "TEST-STR", 8) == 0, true, "check buffer"); TEST_RESULT_VOID(bufFree(buffer), "free buffer"); + TEST_RESULT_VOID(bufFree(bufNew(0)), "free empty buffer"); } // ***************************************************************************************************************************** diff --git a/test/src/module/common/typeKeyValueTest.c b/test/src/module/common/typeKeyValueTest.c index b89306124..a8bc73553 100644 --- a/test/src/module/common/typeKeyValueTest.c +++ b/test/src/module/common/typeKeyValueTest.c @@ -42,6 +42,7 @@ testRun() TEST_RESULT_INT(varInt(varLstGet(kvGetList(store, varNewInt(42)), 0)), 57, "get int/int"); TEST_RESULT_INT(varInt(kvGet(store, varNewStr(strNew("str-key-int")))), 99, "get string/int"); TEST_RESULT_PTR(kvGet(store, varNewInt(78)), NULL, "get int/null"); + TEST_RESULT_PTR(kvGet(store, varNewInt(777)), NULL, "get missing key"); // Check that a null value can be changed to non-null // ------------------------------------------------------------------------------------------------------------------------- @@ -65,6 +66,7 @@ testRun() TEST_RESULT_INT(varInt(varLstGet(varVarLst(kvGet(store, varNewInt(99))), 2)), 3, "get int/int"); TEST_RESULT_INT(varInt(varLstGet(kvGetList(store, varNewInt(99)), 2)), 3, "get int/int"); + TEST_RESULT_PTR(varLstGet(kvGetList(store, varNewInt(777)), 0), NULL, "get NULL list"); // Check item in key list // ------------------------------------------------------------------------------------------------------------------------- diff --git a/test/src/module/common/typeListTest.c b/test/src/module/common/typeListTest.c index 037fe2ed9..bb00a4db8 100644 --- a/test/src/module/common/typeListTest.c +++ b/test/src/module/common/typeListTest.c @@ -38,7 +38,8 @@ testRun() void *ptr = NULL; TEST_RESULT_PTR(lstAdd(list, &ptr), list, "add item"); - lstFree(list); + TEST_RESULT_VOID(lstFree(list), "free list"); + TEST_RESULT_VOID(lstFree(lstNew(1)), "free empty list"); } // ***************************************************************************************************************************** diff --git a/test/src/module/common/typeStringListTest.c b/test/src/module/common/typeStringListTest.c index dc8d085d2..cc0fe80cb 100644 --- a/test/src/module/common/typeStringListTest.c +++ b/test/src/module/common/typeStringListTest.c @@ -39,7 +39,8 @@ testRun() TEST_RESULT_STR(strPtr(strLstGet(list, listIdx)), strPtr(strNewFmt("STR%02u", listIdx)), "check item %u", listIdx); } - strLstFree(list); + TEST_RESULT_VOID(strLstFree(list), "free string list"); + TEST_RESULT_VOID(strLstFree(NULL), "free null string list"); } // ***************************************************************************************************************************** @@ -59,6 +60,7 @@ testRun() TEST_RESULT_STR(strPtr(strLstJoin(strLstNewSplitSizeZ(strNew("abc def"), " ", 4), "-")), "abc-def", "one items"); TEST_RESULT_STR(strPtr(strLstJoin(strLstNewSplitSizeZ(strNew("abc def ghi"), " ", 4), "-")), "abc-def-ghi", "three items"); TEST_RESULT_STR(strPtr(strLstJoin(strLstNewSplitSizeZ(strNew("abc def ghi"), " ", 8), "-")), "abc def-ghi", "three items"); + TEST_RESULT_STR(strPtr(strLstJoin(strLstNewSplitSizeZ(strNew("abc def "), " ", 4), "-")), "abc-def ", "two items"); TEST_RESULT_STR( strPtr(strLstJoin(strLstNewSplitSize(strNew("this is a short sentence"), strNew(" "), 10), "\n")), diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index 97d33b9b5..8f7a05adb 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -10,7 +10,7 @@ void testRun() { // ***************************************************************************************************************************** - if (testBegin("strNew(), strNewBuf(), strNewN(), and strFree()")) + if (testBegin("strNew(), strNewBuf(), strNewN(), strPtr(), and strFree()")) { String *string = strNew("static string"); TEST_RESULT_STR(strPtr(string), "static string", "new with static string"); @@ -32,8 +32,10 @@ testRun() // ------------------------------------------------------------------------------------------------------------------------- string = strNewFmt("formatted %s %04d", "string", 1); TEST_RESULT_STR(strPtr(string), "formatted string 0001", "new with formatted string"); + TEST_RESULT_PTR(strPtr(NULL), NULL, "null string pointer"); TEST_RESULT_VOID(strFree(string), "free string"); + TEST_RESULT_VOID(strFree(NULL), "free null string"); } // ***************************************************************************************************************************** @@ -69,6 +71,8 @@ testRun() String *string = strNew("duplicated string"); String *stringDup = strDup(string); TEST_RESULT_STR(strPtr(stringDup), strPtr(string), "duplicated strings match"); + + TEST_RESULT_PTR(strDup(NULL), NULL, "duplicate null string"); } // ***************************************************************************************************************************** @@ -127,6 +131,7 @@ testRun() TEST_RESULT_STR(strPtr(strTrim(strNew(""))), "", "trim empty"); TEST_RESULT_STR(strPtr(strTrim(strNew("X"))), "X", "no trim (one char)"); TEST_RESULT_STR(strPtr(strTrim(strNew("no-trim"))), "no-trim", "no trim (string)"); + TEST_RESULT_STR(strPtr(strTrim(strNew(" \t\r\n"))), "", "all whitespace"); TEST_RESULT_STR(strPtr(strTrim(strNew(" \tbegin-only"))), "begin-only", "trim begin"); TEST_RESULT_STR(strPtr(strTrim(strNew("end-only\t "))), "end-only", "trim end"); TEST_RESULT_STR(strPtr(strTrim(strNew("\n\rboth\r\n"))), "both", "trim both"); diff --git a/test/src/module/common/typeVariantTest.c b/test/src/module/common/typeVariantTest.c index 69546b2cd..505ed282d 100644 --- a/test/src/module/common/typeVariantTest.c +++ b/test/src/module/common/typeVariantTest.c @@ -65,6 +65,7 @@ testRun() TEST_RESULT_DOUBLE(varDblForce(varNewInt(123)), 123, "force int to double"); TEST_RESULT_DOUBLE(varDblForce(varNewInt64(999999999999)), 999999999999, "force int64 to double"); TEST_RESULT_DOUBLE(varDblForce(varNewStr(strNew("879.01"))), 879.01, "force String to double"); + TEST_RESULT_DOUBLE(varDblForce(varNewStr(strNew("0"))), 0, "force String to double"); TEST_ERROR(varDblForce(varNewStr(strNew("AAA"))), FormatError, "unable to force String 'AAA' to double"); TEST_ERROR(varDblForce(varNewVarLstEmpty()), FormatError, "unable to force VariantList to double"); @@ -107,6 +108,7 @@ testRun() // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_BOOL(varEq(NULL, NULL), true, "null, null eq"); TEST_RESULT_BOOL(varEq(NULL, varNewInt(123)), false, "null, int not eq"); + TEST_RESULT_BOOL(varEq(varNewInt(123), NULL), false, "int, null not eq"); TEST_RESULT_BOOL(varEq(varNewInt(123), varNewInt(123)), true, "int, int eq"); TEST_RESULT_BOOL(varEq(varNewInt(444), varNewInt(123)), false, "int, int not eq"); @@ -161,6 +163,7 @@ testRun() TEST_ASSIGN(keyValue, varNewKv(), "new"); TEST_RESULT_PTR(kvPut(varKv(keyValue), varNewInt(44), varNewInt(55)), varKv(keyValue), " put int/int"); TEST_RESULT_INT(varInt(kvGet(varKv(keyValue), varNewInt(44))), 55, " get int/int"); + TEST_RESULT_PTR(varKv(NULL), NULL, "get null kv"); // ------------------------------------------------------------------------------------------------------------------------- Variant *keyValueDup = NULL; @@ -185,6 +188,8 @@ testRun() TEST_RESULT_STR(strPtr(varStr(string)), "test-str", "string pointer"); varFree(string); + TEST_RESULT_PTR(varStr(NULL), NULL, "get null string variant"); + // ------------------------------------------------------------------------------------------------------------------------- string = varNewStr(strNew("not-a-string")); string->type = varTypeInt; @@ -192,9 +197,8 @@ testRun() varFree(string); // ------------------------------------------------------------------------------------------------------------------------- - string = varNewStr(strNew("777")); - TEST_RESULT_INT(varIntForce(string), 777, "int from string"); - varFree(string); + TEST_RESULT_INT(varIntForce(varNewStrZ("777")), 777, "int from string"); + TEST_RESULT_INT(varIntForce(varNewStrZ("0")), 0, "int from string"); // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_INT(varBoolForce(varNewStr(strNew("y"))), true, "bool from string"); @@ -249,6 +253,7 @@ testRun() TEST_ASSIGN(listVar, varNewVarLstEmpty(), "new empty"); TEST_RESULT_INT(varLstSize(varVarLst(listVar)), 0, " empty size"); + TEST_RESULT_PTR(varVarLst(NULL), NULL, "get null var list"); TEST_RESULT_PTR(varLstAdd(varVarLst(listVar), varNewBool(true)), varVarLst(listVar), " add bool"); TEST_RESULT_PTR(varLstAdd(varVarLst(listVar), varNewInt(55)), varVarLst(listVar), " add int");