Fix a bug in stralloc_starts() and add a test.
strcalloc_starts() should have tested the return value of `memcmp` against 0 for equality. Fixed and added a test case. As an aside, one might wonder how bugs like that are creeping into well-tested code imported from other projects? The answer, specific to stralloc, is that the original code was very specific to qmail, and used a number of additional functions specific to qmail. Rather than import half of qmail, the version imported into Magicka has been reworked to, instead, use standard C functions. The process of modifying the code gave rise to the opportunity for bugs to creep in. Now that a unit testing framework is in place, we can test things in isolation more easily and hopefully catch such things BEFORE they are published to the master repository. Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
This commit is contained in:
parent
33beceadd3
commit
1c7849b724
@ -11,6 +11,7 @@ ODOORS:= ${DEPSDIR}/odoors/libs-${OS}/libODoors.a
|
||||
JSMN:= ${DEPSDIR}/jsmn/libjsmn.a
|
||||
CDK:= ${DEPSDIR}/cdk-5-20161210/libcdk.a
|
||||
UUID:= ${DEPSDIR}/libuuid/.libs/libuuid.a
|
||||
CUTEST:= ${DEPSDIR}/cutest-1.5/libcutest.a
|
||||
|
||||
all: magicka magimail magiedit ticproc mgpost magichat \
|
||||
filecenter dosbox_shim magiftpd reset_pass
|
||||
@ -46,6 +47,9 @@ ${UUID}:
|
||||
cd ${DEPSDIR}/libuuid/ && ./configure
|
||||
cd ${DEPSDIR}/libuuid/ && ${MAKE} CC=${CC}
|
||||
|
||||
${CUTEST}:
|
||||
cd ${DEPSDIR}/cutest-1.5 && make
|
||||
|
||||
HDRS:= bbs.h
|
||||
OBJS:= inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o \
|
||||
doors.o bbs_list.o chat_system.o email.o files.o settings.o \
|
||||
@ -116,3 +120,5 @@ clean:
|
||||
cd ${UTILSDIR}/magiftpd && ${MAKE} clean
|
||||
cd ${UTILSDIR}/reset_pass && ${MAKE} clean
|
||||
cd ${UTILSDIR}/dosbox_shim && ${MAKE} clean
|
||||
cd ${DEPSDIR}/libuuid && ${MAKE} clean
|
||||
cd ${DEPSDIR}/cutest-1.5 && ${MAKE} clean
|
||||
|
@ -33,7 +33,7 @@ int stralloc_starts(stralloc *sa, const char *s) {
|
||||
assert(sa != NULL);
|
||||
assert(s != NULL);
|
||||
len = strlen(s);
|
||||
return (sa->len >= len) && memcmp(sa->s, s, len);
|
||||
return (sa->len >= len) && memcmp(sa->s, s, len) == 0;
|
||||
}
|
||||
|
||||
int stralloc_copyb(stralloc *sa, const char *s, size_t n) {
|
||||
|
58
src/stralloc/test_stralloc.c
Normal file
58
src/stralloc/test_stralloc.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "CuTest.h"
|
||||
|
||||
#include "stralloc.h"
|
||||
|
||||
void test_stralloc_starts(CuTest* tc) {
|
||||
stralloc sa = EMPTY_STRALLOC;
|
||||
stralloc_copys(&sa, "This is a test");
|
||||
CuAssertTrue(tc, stralloc_starts(&sa, "This"));
|
||||
}
|
||||
|
||||
void test_stralloc_starts_equal(CuTest* tc) {
|
||||
stralloc sa = EMPTY_STRALLOC;
|
||||
stralloc_copys(&sa, "This is a test");
|
||||
CuAssertTrue(tc, stralloc_starts(&sa, "This is a test"));
|
||||
}
|
||||
|
||||
void test_stralloc_starts_notequal(CuTest* tc) {
|
||||
stralloc sa = EMPTY_STRALLOC;
|
||||
stralloc_copys(&sa, "This is a test");
|
||||
CuAssertTrue(tc, !stralloc_starts(&sa, "this"));
|
||||
}
|
||||
|
||||
void test_stralloc_starts_toolong(CuTest* tc) {
|
||||
stralloc sa = EMPTY_STRALLOC;
|
||||
stralloc_copys(&sa, "This is a test");
|
||||
CuAssertTrue(tc, !stralloc_starts(&sa, "This is a test!"));
|
||||
}
|
||||
|
||||
CuSuite* stralloc_suite(void) {
|
||||
CuSuite* suite = CuSuiteNew();
|
||||
|
||||
SUITE_ADD_TEST(suite, test_stralloc_starts);
|
||||
SUITE_ADD_TEST(suite, test_stralloc_starts_equal);
|
||||
SUITE_ADD_TEST(suite, test_stralloc_starts_notequal);
|
||||
SUITE_ADD_TEST(suite, test_stralloc_starts_toolong);
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
void RunAllTests(void) {
|
||||
CuString *output = CuStringNew();
|
||||
CuSuite* suite = CuSuiteNew();
|
||||
|
||||
CuSuiteAddSuite(suite, stralloc_suite());
|
||||
|
||||
CuSuiteRun(suite);
|
||||
CuSuiteSummary(suite, output);
|
||||
CuSuiteDetails(suite, output);
|
||||
printf("%s\n", output->buffer);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
RunAllTests();
|
||||
}
|
Reference in New Issue
Block a user