Skip to content

Commit

Permalink
use Check to unit test libtd
Browse files Browse the repository at this point in the history
22 of 23 tests from td-test.sh can be tested using Check, the last one needs to test stdout. The benchmark would also need another way to perform.

AM_PROG_CC_C_O is added because Automake 1.13 still needs it, it's not required for 1.14+ according to the manual [1], AC_PROG_CC handles that.

[1]: www.gnu.org/software/automake/manual/html_node/Public-Macros.html
  • Loading branch information
livibetter committed Jul 24, 2014
1 parent e012e1e commit 3bc45b9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SUBDIRS = lib src
SUBDIRS = lib src tests
ACLOCAL_AMFLAGS = -I m4
11 changes: 10 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ AM_PROG_AR
LT_INIT
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_TESTDIR([tests])

# Checks for programs.
AC_PROG_CC
AM_PROG_CC_C_O

# Checks for libraries.
PKG_CHECK_MODULES([CHECK], [check >= 0.9.4])

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h])
Expand All @@ -24,5 +27,11 @@ AC_CHECK_HEADER_STDBOOL

# Checks for library functions.

AC_CONFIG_FILES([Makefile lib/Makefile src/Makefile])
AC_CONFIG_FILES([
Makefile
lib/Makefile
src/Makefile
tests/Makefile
])

AC_OUTPUT
23 changes: 0 additions & 23 deletions td-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ test_eq() {
fi
}

test_eq 0 "0 seconds"
test_eq 1 "1 second"
test_eq -1 "1 second"
test_eq 2 "2 seconds"
test_eq 60 "1 minute"
test_eq 61 "1 minute 1 second"
test_eq 3599 "59 minutes 59 seconds"
test_eq 3600 "1 hour"
test_eq 3601 "1 hour 1 second"
test_eq 3660 "1 hour 1 minute"
test_eq 3661 "1 hour 1 minute 1 second"
test_eq 86400 "1 day"
test_eq "$((86400 + 86400 - 1))" "1 day 23 hours 59 minutes 59 seconds"
test_eq "$((2 * 86400 + 86400 - 1))" "2 days 23 hours 59 minutes 59 seconds"
test_eq "$((366 * 86400 + 1))" "366 days 1 second"
# Paddings
test_eq 1 "1 second " -P
test_eq 60 "1 minute " -P
test_eq 60 " 1 minute " -P -p
test_eq 60 "01 minute " -P -p0
test_eq 60 " 0 days 0 hours 1 minute 0 seconds" -p -a
test_eq 60 "0 days 0 hours 1 minute 0 seconds" -P -a
test_eq 60 " 0 days 0 hours 1 minute 0 seconds" -P -p -a
test_eq "1 60" $'1 second \n1 minute ' -P

echo "$test_failures failures of $test_counts tests."
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check_libtd
18 changes: 18 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
AM_CFLAGS = -std=c99 -Wall -Wextra

check_PROGRAMS = check_libtd

check_libtd_SOURCES = check_libtd.c $(top_srcdir)/lib/td.h
check_libtd_CFLAGS = $(AM_CFLAGS) @CHECK_CFLAGS@
check_libtd_LDADD = $(top_builddir)/lib/libtd.la @CHECK_LIBS@

DISTCLEANFILES = atconfig

check-local: run-check-programs

run-check-programs: $(check_PROGRAMS)
for check_program in $^; do \
$(SHELL) "$$check_program" || exit 1; \
done

.PHONY: run-check-programs
96 changes: 96 additions & 0 deletions tests/check_libtd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* libtd uniti test
* Copyright (c) 2014 Yu-Jie Lin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdbool.h>
#include <stdlib.h>

#include <check.h>

#include "../lib/td.h"

#define \
TEST_SPRINT_TD(str, t, all, pad, ch, expect) \
do { \
str[0] = '\0'; \
sprint_td(str, t, all, pad, ch); \
ck_assert_str_eq(str, expect); \
} while (false);

START_TEST (test_libtd)
{
char str[64] = "";

TEST_SPRINT_TD(str, 0, false, false, '\0', "0 seconds");
TEST_SPRINT_TD(str, 1, false, false, '\0', "1 second");
TEST_SPRINT_TD(str, -1, false, false, '\0', "1 second");
TEST_SPRINT_TD(str, 2, false, false, '\0', "2 seconds");
TEST_SPRINT_TD(str, 60, false, false, '\0', "1 minute");
TEST_SPRINT_TD(str, 61, false, false, '\0', "1 minute 1 second");
TEST_SPRINT_TD(str, 3599, false, false, '\0', "59 minutes 59 seconds");
TEST_SPRINT_TD(str, 3600, false, false, '\0', "1 hour");
TEST_SPRINT_TD(str, 3601, false, false, '\0', "1 hour 1 second");
TEST_SPRINT_TD(str, 3660, false, false, '\0', "1 hour 1 minute");
TEST_SPRINT_TD(str, 3661, false, false, '\0', "1 hour 1 minute 1 second");
TEST_SPRINT_TD(str, 86400, false, false, '\0', "1 day");

TEST_SPRINT_TD(str, 86400 + 86400 - 1, false, false, '\0', "1 day 23 hours 59 minutes 59 seconds");
TEST_SPRINT_TD(str, 2 * 86400 + 86400 - 1, false, false, '\0', "2 days 23 hours 59 minutes 59 seconds");
TEST_SPRINT_TD(str, 366 * 86400 + 1, false, false, '\0', "366 days 1 second");

TEST_SPRINT_TD(str, 1, false, true, '\0', "1 second ");
TEST_SPRINT_TD(str, 60, false, true, '\0', "1 minute ");
TEST_SPRINT_TD(str, 60, false, true, ' ', " 1 minute ");
TEST_SPRINT_TD(str, 60, false, true, '0', "01 minute ");

TEST_SPRINT_TD(str, 60, true, false, ' ', " 0 days 0 hours 1 minute 0 seconds");
TEST_SPRINT_TD(str, 60, true, true, '\0', "0 days 0 hours 1 minute 0 seconds");
TEST_SPRINT_TD(str, 60, true, true, ' ', " 0 days 0 hours 1 minute 0 seconds");
}
END_TEST

Suite *
libtd_suite (void)
{
Suite *suite = suite_create("libtd");

TCase *tc_libtd = tcase_create("libtd");
tcase_add_test(tc_libtd, test_libtd);
suite_add_tcase(suite, tc_libtd);

return suite;
}

int
main (void)
{
int number_failed;

Suite *s = libtd_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit 3bc45b9

Please sign in to comment.