1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Full branch coverage for all common/type modules.

This commit is contained in:
David Steele 2018-03-20 16:08:52 -04:00
parent a2030da200
commit 31830bdc55
7 changed files with 39 additions and 10 deletions

View File

@ -6,6 +6,7 @@ Variant Data Type
#include <string.h>
#include <strings.h>
#include "common/debug.h"
#include "common/memContext.h"
#include "common/type/variant.h"
@ -38,13 +39,16 @@ 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)
// 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) == '.')

View File

@ -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");
}
// *****************************************************************************************************************************

View File

@ -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
// -------------------------------------------------------------------------------------------------------------------------

View File

@ -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");
}
// *****************************************************************************************************************************

View File

@ -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")),

View File

@ -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");

View File

@ -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");