diff --git a/src/iniparser.c b/src/iniparser.c index a92a30455689a996ff3b9d90e6ce441331580156..8c98fbc9448aaf75f11e0ba4c305de52caa24f6f 100644 --- a/src/iniparser.c +++ b/src/iniparser.c @@ -449,7 +449,7 @@ int iniparser_getint(const dictionary * d, const char * key, int notfound) handling. */ /*--------------------------------------------------------------------------*/ -int64_t iniparser_getint64(const dictionary * d, const char * key, int64_t notfound) +int64_t iniparser_getlongint(const dictionary * d, const char * key, int64_t notfound) { const char * str ; diff --git a/src/iniparser.h b/src/iniparser.h index b1560b51c24cdd09694308ef0524c289f93140ff..b78f4923493fc2d881d9f5f12dbdd79c3b883d17 100644 --- a/src/iniparser.h +++ b/src/iniparser.h @@ -216,7 +216,7 @@ int iniparser_getint(const dictionary * d, const char * key, int notfound); handling. */ /*--------------------------------------------------------------------------*/ -int64_t iniparser_getint64(const dictionary * d, const char * key, int64_t notfound); +int64_t iniparser_getlongint(const dictionary * d, const char * key, int64_t notfound); /*-------------------------------------------------------------------------*/ diff --git a/test/CuTest.c b/test/CuTest.c index 144f8c30613b88b303d28b57434ff403be67d44e..a5a496d7fcdb094bfd99dcfdc80dd692f6f72b52 100644 --- a/test/CuTest.c +++ b/test/CuTest.c @@ -211,6 +211,15 @@ void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const cha CuFail_Line(tc, file, line, message, buf); } +void CuAssertLongIntEquals_LineMsg(CuTest *tc, const char *file, int line, const char *message, + int64_t expected, int64_t actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected <%ld> but was <%ld>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta) { diff --git a/test/CuTest.h b/test/CuTest.h index 2f7ef5422b1aa0ea3b0c5f69e0e726f1d6a14525..9845fef4128eef741e62551b31e6ab606dcdd3d8 100644 --- a/test/CuTest.h +++ b/test/CuTest.h @@ -3,6 +3,7 @@ #include #include +#include #define CUTEST_VERSION "CuTest 1.5" @@ -64,6 +65,9 @@ void CuAssertStrEquals_LineMsg(CuTest* tc, void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual); +void CuAssertLongIntEquals_LineMsg(CuTest *tc, + const char *file, int line, const char *message, + int64_t expected, int64_t actual); void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta); @@ -81,6 +85,8 @@ void CuAssertPtrEquals_LineMsg(CuTest* tc, #define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) #define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) #define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertLongIntEquals(tc,ex,ac) CuAssertLongIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertLongIntEquals_Msg(tc,ms,ex,ac) CuAssertLongIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) #define CuAssertDblEquals(tc,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl)) #define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl)) #define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) diff --git a/test/test_iniparser.c b/test/test_iniparser.c index bf7a2e2e9a9c33993f1a5c4e842f90228289ca50..3119f5ba842cd4eceb767f425b5482095679d942 100644 --- a/test/test_iniparser.c +++ b/test/test_iniparser.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -337,6 +338,69 @@ void Test_iniparser_getint(CuTest *tc) dictionary_del(dic); } +void Test_iniparser_getlongint(CuTest *tc) +{ + unsigned i; + char key_name[64]; + dictionary *dic; + const struct { int64_t num; const char *value; } good_val[] = { + { 0, "0" }, + { 1, "1" }, + { -1, "-1" }, + { 1000, "1000" }, + { 077, "077" }, + { -01000, "-01000" }, + { 0x7FFFFFFFFFFFFFFF, "0x7FFFFFFFFFFFFFFF" }, + { -0x7FFFFFFFFFFFFFFF, "-0x7FFFFFFFFFFFFFFF" }, + { 0x4242, "0x4242" }, + { 0, NULL} /* must be last */ + }; + const char *bad_val[] = { + "", + "notanumber", + "0x", + "k2000", + " ", + "0xG1" + }; + /* NULL test */ + CuAssertLongIntEquals(tc, -42, iniparser_getlongint(NULL, NULL, -42)); + CuAssertLongIntEquals(tc, -42, iniparser_getlongint(NULL, "dummy", -42)); + + /* Check the def return element */ + dic = dictionary_new(10); + CuAssertLongIntEquals(tc, 42, iniparser_getlongint(dic, "dummy", 42)); + CuAssertLongIntEquals(tc, 0x7FFFFFFFFFFFFFFF, iniparser_getlongint(dic, NULL, 0x7FFFFFFFFFFFFFFF)); + CuAssertLongIntEquals(tc, -0x7FFFFFFFFFFFFFFF, iniparser_getlongint(dic, "dummy", -0x7FFFFFFFFFFFFFFF)); + dictionary_del(dic); + + /* Generic dictionary */ + dic = dictionary_new(10); + for (i = 0; good_val[i].value != NULL; ++i) { + sprintf(key_name, "longint:value%d", i); + dictionary_set(dic, key_name, good_val[i].value); + } + for (i = 0; good_val[i].value != NULL; ++i) { + sprintf(key_name, "longint:value%d", i); + CuAssertLongIntEquals(tc, good_val[i].num, + iniparser_getlongint(dic, key_name, 0)); + } + dictionary_del(dic); + + /* Test bad names */ + dic = dictionary_new(10); + for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) { + sprintf(key_name, "longint:bad%d", i); + dictionary_set(dic, key_name, bad_val[i]); + } + for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) { + sprintf(key_name, "longint:bad%d", i); + CuAssertLongIntEquals(tc, 0, + iniparser_getlongint(dic, key_name, 0)); + } + dictionary_del(dic); +} + void Test_iniparser_getdouble(CuTest *tc) { dictionary *dic;