Add testing suites

This commit is contained in:
Pradana AUMARS 2021-06-27 21:50:05 +02:00
parent 0117c62230
commit 6bb86dbb16
9 changed files with 298 additions and 100 deletions

View File

@ -1,2 +1,2 @@
SUBDIRS = . src
SUBDIRS = . src tests
dist_doc_DATA = README

3
README
View File

@ -2,4 +2,5 @@ Easy-to-use CSV parsing library for C language.
Build requirements
------------------
* libtool
* libtool
* Optional: check (for testing)

172
configure vendored
View File

@ -662,6 +662,8 @@ ac_subst_vars='am__EXEEXT_FALSE
am__EXEEXT_TRUE
LTLIBOBJS
LIBOBJS
CHECK_LIBS
CHECK_CFLAGS
LT_SYS_LIBRARY_PATH
OTOOL64
OTOOL
@ -788,6 +790,7 @@ with_aix_soname
with_gnu_ld
with_sysroot
enable_libtool_lock
with_check
'
ac_precious_vars='build_alias
host_alias
@ -1448,6 +1451,7 @@ Optional Packages:
--with-gnu-ld assume the C compiler uses GNU ld [default=no]
--with-sysroot[=DIR] Search for dependent libraries within DIR (or the
compiler's sysroot if not specified).
--with-check=PATH prefix where check is installed default=auto
Some influential environment variables:
CC C compiler command
@ -12572,7 +12576,172 @@ CC=$lt_save_CC
# Only expand once:
ac_config_files="$ac_config_files Makefile src/Makefile"
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: AM_PATH_CHECK() is deprecated" >&5
printf "%s\n" "$as_me: WARNING: AM_PATH_CHECK() is deprecated" >&2;}
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: use PKG_CHECK_MODULES([CHECK], [check >= 0.9.4]) instead" >&5
printf "%s\n" "$as_me: WARNING: use PKG_CHECK_MODULES([CHECK], [check >= 0.9.4]) instead" >&2;}
# Check whether --with-check was given.
if test ${with_check+y}
then :
withval=$with_check;
fi
min_check_version=0.8.2
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for check - version >= $min_check_version" >&5
printf %s "checking for check - version >= $min_check_version... " >&6; }
if test x$with_check = xno; then
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: disabled" >&5
printf "%s\n" "disabled" >&6; }
as_fn_error $? "disabling check is not supported" "$LINENO" 5
else
if test "x$with_check" != x; then
CHECK_CFLAGS="-I$with_check/include"
CHECK_LIBS="-L$with_check/lib -lcheck"
else
CHECK_CFLAGS=""
CHECK_LIBS="-lcheck"
fi
ac_save_CFLAGS="$CFLAGS"
ac_save_LIBS="$LIBS"
CFLAGS="$CFLAGS $CHECK_CFLAGS"
LIBS="$CHECK_LIBS $LIBS"
rm -f conf.check-test
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
$ac_includes_default
#include <check.h>
int main ()
{
int major, minor, micro;
char *tmp_version;
system ("touch conf.check-test");
/* HP/UX 9 (%@#!) writes to sscanf strings */
tmp_version = strdup("$min_check_version");
if (sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) != 3) {
printf("%s, bad version string\n", "$min_check_version");
return 1;
}
if ((CHECK_MAJOR_VERSION != check_major_version) ||
(CHECK_MINOR_VERSION != check_minor_version) ||
(CHECK_MICRO_VERSION != check_micro_version))
{
printf("\n*** The check header file (version %d.%d.%d) does not match\n",
CHECK_MAJOR_VERSION, CHECK_MINOR_VERSION, CHECK_MICRO_VERSION);
printf("*** the check library (version %d.%d.%d).\n",
check_major_version, check_minor_version, check_micro_version);
return 1;
}
if ((check_major_version > major) ||
((check_major_version == major) && (check_minor_version > minor)) ||
((check_major_version == major) && (check_minor_version == minor) && (check_micro_version >= micro)))
{
return 0;
}
else
{
printf("\n*** An old version of check (%d.%d.%d) was found.\n",
check_major_version, check_minor_version, check_micro_version);
printf("*** You need a version of check being at least %d.%d.%d.\n", major, minor, micro);
printf("***\n");
printf("*** If you have already installed a sufficiently new version, this error\n");
printf("*** probably means that the wrong copy of the check library and header\n");
printf("*** file is being found. Rerun configure with the --with-check=PATH option\n");
printf("*** to specify the prefix where the correct version was installed.\n");
}
return 1;
}
_ACEOF
if ac_fn_c_try_compile "$LINENO"
then :
else $as_nop
no_check=yes
fi
rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext
CFLAGS="$ac_save_CFLAGS"
LIBS="$ac_save_LIBS"
if test "x$no_check" = x ; then
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
printf "%s\n" "yes" >&6; }
:
else
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
printf "%s\n" "no" >&6; }
if test -f conf.check-test ; then
:
else
echo "*** Could not run check test program, checking why..."
CFLAGS="$CFLAGS $CHECK_CFLAGS"
LIBS="$CHECK_LIBS $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
int
main (void)
{
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"
then :
echo "*** The test program compiled, but did not run. This usually means"
echo "*** that the run-time linker is not finding check. You'll need to set your"
echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
echo "*** to the installed location Also, make sure you have run ldconfig if that"
echo "*** is required on your system"
echo "***"
echo "*** If you have an old version installed, it is best to remove it, although"
echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"
else $as_nop
echo "*** The test program failed to compile or link. See the file config.log for"
echo "*** the exact error that occured."
fi
rm -f core conftest.err conftest.$ac_objext conftest.beam \
conftest$ac_exeext conftest.$ac_ext
CFLAGS="$ac_save_CFLAGS"
LIBS="$ac_save_LIBS"
fi
CHECK_CFLAGS=""
CHECK_LIBS=""
rm -f conf.check-test
as_fn_error $? "check not found" "$LINENO" 5
fi
rm -f conf.check-test
fi
ac_config_files="$ac_config_files Makefile src/Makefile tests/Makefile"
cat >confcache <<\_ACEOF
@ -13598,6 +13767,7 @@ do
"libtool") CONFIG_COMMANDS="$CONFIG_COMMANDS libtool" ;;
"Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
"src/Makefile") CONFIG_FILES="$CONFIG_FILES src/Makefile" ;;
"tests/Makefile") CONFIG_FILES="$CONFIG_FILES tests/Makefile" ;;
*) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac

View File

@ -5,9 +5,11 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AM_PROG_AR
LT_INIT
AM_PATH_CHECK
AC_CONFIG_FILES([
Makefile
src/Makefile
tests/Makefile
])
AC_OUTPUT

View File

@ -1,44 +0,0 @@
#include "../include/easycsv/easycsv.h"
#define CSV_FP "01.csv"
#define CSV_MODE "r"
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "No arguments!\n");
return -1;
}
struct easycsv *csv = easycsv_init(CSV_FP, CSV_MODE);
if (access(CSV_FP, F_OK) < 0) {
fprintf(stderr, "%s cannot be found!\n", CSV_FP);
return -1;
}
if (csv == NULL) {
fprintf(stderr, "Failed to initialise %s on mode %s\n", CSV_FP, CSV_MODE);
return -1;
}
printf("COLUMNS: %i\nROWS: %i\n", easycsv_rows(csv), easycsv_columns(csv));
// char *str = malloc(EASYCSV_BUFFER_SIZE);
printf("Searching column values of %s...\n", argv[1]);
for (int i = 2;;i++) {
char *null = (char*) easycsv_readcolumnvalue(csv, argv[1], i);
if (null == NULL) {
free(null);
puts("End of file!\n");
break;
}
printf("ROW %i: %s\n", i, null);
}
easycsv_free(csv);
csv = NULL;
return 0;
}

View File

@ -1,53 +0,0 @@
#include "../include/easycsv/easycsv.h"
int main(void)
{
const char CSV_FP[] = "02.csv";
struct easycsv *csv = easycsv_init(CSV_FP, EASYCSV_W);
if (csv == NULL) {
fprintf(stderr, "Failed to load %s\n", CSV_FP);
return -1;
}
const char mod_one[] = "first-mod-deluxe-edition";
const char *paths_one[5] = {
"FILEPATH1ONE",
"FILEPATH2ONE",
"FILEPATH3ONE",
"FILEPATH4ONE",
"FILEPATH5ONE"
};
const char mod_two[] = "second-mod-gold-edition";
const char *paths_two[4] = {
"FILEPATH1TWO",
"FILEPATH2TWO",
"FILEPATH3TWO",
"FILEPATH4TWO"
};
if (easycsv_pushcolumn(csv, mod_one) < 0) {
return -1;
}
for (size_t st = 0; st < 5; st++) {
if (easycsv_pushcolumnvalue(csv, mod_one, paths_one[st]) < 0) return -1;
}
if (easycsv_pushcolumn(csv, mod_two) < 0) {
return -1;
}
for (size_t st = 0; st < 4; st++) {
if (easycsv_pushcolumnvalue(csv, mod_two, paths_two[st]) < 0) return -1;
}
easycsv_free(csv);
csv = NULL;
return 0;
}

11
tests/Makefile.am Normal file
View File

@ -0,0 +1,11 @@
TESTS = \
check_easycsv
noinst_PROGRAMS = \
check_easycsv
check_easycsv_SOURCES = \
check_easycsv.c
check_easycsv_CFLAGS = $(CHECK_CFLAGS)
check_easycsv_LDFLAGS = $(CHECK_LIBS)
check_easycsv_LDADD = $(top_builddir)/src/libeasycsv.la

111
tests/check_easycsv.c Normal file
View File

@ -0,0 +1,111 @@
#include <check.h>
#include "../src/easycsv.h"
static const char SAMPLE1_PATH[] = "samples/1.csv";
static const char SAMPLE2_PATH[] = "samples/2.csv";
START_TEST(test_easycsv_init_read)
{
easycsv *csv = NULL;
csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
ck_assert_ptr_nonnull(csv);
easycsv_free(csv);
csv = easycsv_init(SAMPLE2_PATH, EASYCSV_R);
ck_assert_ptr_nonnull(csv);
easycsv_free(csv);
}
START_TEST(test_easycsv_init_write)
{
easycsv *csv = NULL;
csv = easycsv_init(SAMPLE1_PATH, EASYCSV_W);
ck_assert_ptr_nonnull(csv);
easycsv_free(csv);
csv = easycsv_init(SAMPLE2_PATH, EASYCSV_W);
ck_assert_ptr_nonnull(csv);
easycsv_free(csv);
}
START_TEST(test_easycsv_printcolumns)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
ck_assert_int_eq(easycsv_printcolumns(csv), 3);
easycsv_free(csv);
}
START_TEST(test_easycsv_printrows)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
ck_assert_int_eq(easycsv_printrows(csv), 5);
easycsv_free(csv);
}
START_TEST(test_easycsv_readcolumnvalue)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
ck_assert_str_eq(easycsv_readcolumnvalue(csv, "TEST1", 1), "FILEPATHA1");
ck_assert_str_eq(easycsv_readcolumnvalue(csv, "TEST1", 2), "FILEPATHB1");
ck_assert_str_eq(easycsv_readcolumnvalue(csv, "TEST1", 3), NULL);
ck_assert_str_eq(easycsv_readcolumnvalue(csv, "TEST2", 1), "FILEPATHA2");
easycsv_free(csv);
}
Suite*
easycsv_constructor_suite(void)
{
Suite *s;
TCase *tc_init;
s = suite_create ("constructor");
tc_init = tcase_create ("basic constructor");
tcase_add_test(tc_init, test_easycsv_init_read);
tcase_add_test(tc_init, test_easycsv_init_write);
suite_add_tcase(s, tc_init);
return s;
}
Suite*
easycsv_print_suite(void)
{
Suite *s;
TCase *tc_col, *tc_row;
s = suite_create ("print");
tc_col = tcase_create ("columns");
tc_row = tcase_create ("rows");
tcase_add_test(tc_col, test_easycsv_printcolumns);
tcase_add_test(tc_row, test_easycsv_printrows);
suite_add_tcase(s, tc_col);
suite_add_tcase(s, tc_row);
return s;
}
Suite*
easycsv_read_suite(void)
{
Suite *s;
TCase *tc_readcolumnvalue;
s = suite_create ("read");
tc_readcolumnvalue = tcase_create ("readcolumnvalue");
tcase_add_test(tc_readcolumnvalue, test_easycsv_readcolumnvalue);
return s;
}
int
main(void)
{
int number_failed;
SRunner *sr;
sr = srunner_create(easycsv_constructor_suite());
srunner_add_suite(sr, easycsv_print_suite());
srunner_add_suite(sr, easycsv_read_suite());
srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return number_failed;
}