Skip to content

Commit

Permalink
Splitting into two libraries for easier testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Cilibrasi committed Dec 15, 2014
1 parent 20d4df6 commit 2fd493f
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -20,6 +20,7 @@ configure
stamp-h1
tools/ncd
test/unitcheck/lctest
test/unitcheck/lcutiltest
libcomplearn*tar.*z*
lib/libcomplearn.pc
libtool
5 changes: 3 additions & 2 deletions Makefile.am
@@ -1,7 +1,8 @@
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = libcl tools test
SUBDIRS = libcl libclutil tools test

EXTRA_DIST = lib/complearn.map lib/complearn.sym lib/libcomplearn.pc.in
EXTRA_DIST = lib/complearn.map lib/complearn.sym lib/clutil.map lib/clutil.sym \
lib/libcomplearn.pc.in

pkgconfigdir = $(libdir)/pkgconfig
nodist_pkgconfig_DATA = lib/libcomplearn.pc
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -13,6 +13,7 @@ AC_PROG_CC
RRA_LD_VERSION_SCRIPT
AC_CONFIG_FILES([Makefile
libcl/Makefile
libclutil/Makefile
tools/Makefile
test/Makefile
test/unitcheck/Makefile
Expand Down
11 changes: 11 additions & 0 deletions lib/clutil.map
@@ -0,0 +1,11 @@
LIBRARY_1.0 {
global:
newStringHolder;
pushStringHolder;
freeStringHolder;
getStringHolderData;
getStringHolderCount;

local:
*;
};
5 changes: 5 additions & 0 deletions lib/clutil.sym
@@ -0,0 +1,5 @@
newStringHolder
pushStringHolder
freeStringHolder
getStringHolderData
getStringHolderCount
10 changes: 10 additions & 0 deletions libclutil/Makefile.am
@@ -0,0 +1,10 @@
if HAVE_LD_VERSION_SCRIPT
VERSION_LDFLAGS = -Wl,--version-script=$(top_srcdir)/lib/clutil.map
else
VERSION_LDFLAGS = -export-symbols $(top_srcdir)/lib/clutil.sym
endif

lib_LTLIBRARIES = libclutil.la
libclutil_la_SOURCES = stringholder.c stringholder.h
libclutil_la_CPPFLAGS = -I$(top_srcdir)/include -Wall
libclutil_la_LDFLAGS = $(VERSION_LDFLAGS)
51 changes: 51 additions & 0 deletions libclutil/stringholder.c
@@ -0,0 +1,51 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stringholder.h"

#define STARTING_STRING_SIZE 16

void newStringHolder(struct StringHolder *dh) {
dh->alloc = STARTING_STRING_SIZE;
dh->val = malloc(sizeof(char *)*dh->alloc);
dh->count = 0;
}

void freeStringHolder(struct StringHolder *dh) {
if (dh->val) {
int i;
for (i = 0; i < dh->count; ++i) {
free(dh->val[i]);
dh->val[i] = 0;
}
free(dh->val);
dh->val = 0;
dh->alloc = 0;
}
dh->count = 0;
}

void pushStringHolder(struct StringHolder *dh, const char *str) {
if (!dh->val) {
fprintf(stderr, "Cannot push onto freed StringHolder.\n");
exit(1);
}
if (dh->count + 1 >= dh->alloc) {
int nextStep = dh->alloc * 2;
char **nextVal = malloc(sizeof(char *) * nextStep);
memcpy(nextVal, dh->val, sizeof(char *) * dh->count);
free(dh->val);
dh->val = nextVal;
dh->alloc = nextStep;
}
dh->val[dh->count] = strdup(str);
dh->count += 1;
}

char **getStringHolderData(struct StringHolder *dh) {
return dh->val;
}

uint32_t getStringHolderCount(struct StringHolder *dh) {
return dh->count;
}
17 changes: 17 additions & 0 deletions libclutil/stringholder.h
@@ -0,0 +1,17 @@
#ifndef __STRINGHOLDER_H
#define __STRINGHOLDER_H

#include <stdint.h>

struct StringHolder {
uint32_t count, alloc;
char **val;
};

void newStringHolder(struct StringHolder *dh);
void pushStringHolder(struct StringHolder *dh, const char *str);
void freeStringHolder(struct StringHolder *dh);
char **getStringHolderData(struct StringHolder *dh);
uint32_t getStringHolderCount(struct StringHolder *dh);

#endif
6 changes: 3 additions & 3 deletions test/run-unitcheck-test
Expand Up @@ -2,7 +2,7 @@
set -e
test -x tools/ncd
if test -x ./test/unitcheck/lctest; then
exec ./test/unitcheck/lctest
else
exit 0
./test/unitcheck/lctest
./test/unitcheck/lcutiltest
fi
exit 0
14 changes: 10 additions & 4 deletions test/unitcheck/Makefile.am
@@ -1,14 +1,20 @@

if HAVE_CHECKMK

bin_PROGRAMS=lctest
bin_PROGRAMS=lctest lcutiltest

lctest_SOURCES=basic_complete.c
lctest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@
lctest_LDADD =../../libcl/libcomplearn.la -lm @CHECK_LIBS@
lctest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@ -I../../libclutil -g
lctest_LDADD =../../libcl/libcomplearn.la ../../libclutil/libclutil.la -lm @CHECK_LIBS@

lcutiltest_SOURCES=basic_stringholder.c
lcutiltest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@ -I../../libclutil -g
lcutiltest_LDADD =../../libcl/libcomplearn.la ../../libclutil/libclutil.la -lm @CHECK_LIBS@

.ts.c:
checkmk $< >$@

endif

EXTRA_DIST=basic_complete.ts
EXTRA_DIST=basic_complete.ts \
basic_stringholder.ts
22 changes: 22 additions & 0 deletions test/unitcheck/basic_stringholder.ts
@@ -0,0 +1,22 @@
/* A complete test example */

#include <stdio.h>
#include <stringholder.h>

#test stringholder_test
struct StringHolder sh;
newStringHolder(&sh);
ck_assert(0 == getStringHolderCount(&sh));
freeStringHolder(&sh);
newStringHolder(&sh);
ck_assert(0 == getStringHolderCount(&sh));
pushStringHolder(&sh, "cat");
ck_assert(1 == getStringHolderCount(&sh));
ck_assert(0 != getStringHolderData(&sh));
ck_assert(0 == strcmp("cat", getStringHolderData(&sh)[0]));
pushStringHolder(&sh, "dog");
ck_assert(2 == getStringHolderCount(&sh));
ck_assert(0 != getStringHolderData(&sh));
ck_assert(0 == strcmp("dog", getStringHolderData(&sh)[1]));
ck_assert(0 == strcmp("cat", getStringHolderData(&sh)[0]));
freeStringHolder(&sh);

0 comments on commit 2fd493f

Please sign in to comment.