aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2019-02-10 21:58:55 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2019-02-10 21:59:02 +0100
commitd04bf489c8bbe190a0648f58682260d5b5cef613 (patch)
tree25e23cc1fd0bceb0a564023d42f5597d13d84ccc
parent74dec7c69ae961f98e0a91da9fc3f6214183467a (diff)
downloadblogc-d04bf489c8bbe190a0648f58682260d5b5cef613.tar.gz
blogc-d04bf489c8bbe190a0648f58682260d5b5cef613.tar.bz2
blogc-d04bf489c8bbe190a0648f58682260d5b5cef613.zip
blogc: rusage: added tests, fixed minor issue
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am28
-rw-r--r--configure.ac6
-rw-r--r--src/blogc/rusage.c2
-rw-r--r--src/blogc/rusage.h4
-rw-r--r--tests/blogc/check_rusage.c104
6 files changed, 141 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 1b816ef..299f37f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -60,6 +60,7 @@ blogc*.html
/tests/blogc/check_funcvars
/tests/blogc/check_loader
/tests/blogc/check_renderer
+/tests/blogc/check_rusage
/tests/blogc/check_source_parser
/tests/blogc/check_sysinfo
/tests/blogc/check_sysinfo2
diff --git a/Makefile.am b/Makefile.am
index c75fc30..f2a8ffa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -597,6 +597,34 @@ tests_common_check_stdin_LDADD = \
$(CMOCKA_LIBS) \
libblogc_common.la \
$(NULL)
+
+if HAVE_SYS_RESOURCE_H
+if HAVE_SYS_TIME_H
+check_PROGRAMS += \
+ tests/blogc/check_rusage \
+ $(NULL)
+
+tests_blogc_check_rusage_SOURCES = \
+ tests/blogc/check_rusage.c \
+ $(NULL)
+
+tests_blogc_check_rusage_CFLAGS = \
+ $(CMOCKA_CFLAGS) \
+ $(NULL)
+
+tests_blogc_check_rusage_LDFLAGS = \
+ -no-install \
+ -Wl,--wrap=getrusage \
+ $(NULL)
+
+tests_blogc_check_rusage_LDADD = \
+ $(CMOCKA_LIBS) \
+ libblogc.la \
+ libblogc_common.la \
+ $(NULL)
+endif
+endif
+
endif
tests_blogc_check_content_parser_SOURCES = \
diff --git a/configure.ac b/configure.ac
index 9239976..a0940c1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -223,10 +223,12 @@ AC_SUBST(BASH)
AC_CHECK_HEADERS([sys/resource.h sys/stat.h sys/time.h sys/wait.h time.h unistd.h pwd.h])
AC_CHECK_FUNCS([gethostname])
-AM_CONDITIONAL([HAVE_UNISTD_H], [test "x$ac_cv_header_unistd_h" = "xyes"])
-AM_CONDITIONAL([HAVE_SYS_TYPES_H], [test "x$ac_cv_header_sys_types_h" = "xyes"])
AM_CONDITIONAL([HAVE_PWD_H], [test "x$ac_cv_header_pwd_h" = "xyes"])
AM_CONDITIONAL([HAVE_TIME_H], [test "x$ac_cv_header_time_h" = "xyes"])
+AM_CONDITIONAL([HAVE_UNISTD_H], [test "x$ac_cv_header_unistd_h" = "xyes"])
+AM_CONDITIONAL([HAVE_SYS_RESOURCE_H], [test "x$ac_cv_header_sys_resource_h" = "xyes"])
+AM_CONDITIONAL([HAVE_SYS_TIME_H], [test "x$ac_cv_header_sys_time_h" = "xyes"])
+AM_CONDITIONAL([HAVE_SYS_TYPES_H], [test "x$ac_cv_header_sys_types_h" = "xyes"])
LT_LIB_M
diff --git a/src/blogc/rusage.c b/src/blogc/rusage.c
index 6d88dc4..a38848d 100644
--- a/src/blogc/rusage.c
+++ b/src/blogc/rusage.c
@@ -48,7 +48,7 @@ char*
blogc_rusage_format_cpu_time(long long time)
{
if (time >= 1000000)
- return bc_strdup_printf("%.3s", ((float) time) / 1000000.0);
+ return bc_strdup_printf("%.3fs", ((float) time) / 1000000.0);
// this is a special case: some systems may report the cpu time rounded up to the
// milisecond. it is useless to show ".000" in this case.
diff --git a/src/blogc/rusage.h b/src/blogc/rusage.h
index 3441630..6344066 100644
--- a/src/blogc/rusage.h
+++ b/src/blogc/rusage.h
@@ -13,9 +13,11 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */
+#ifdef HAVE_SYS_TIME_H
#ifdef HAVE_SYS_RESOURCE_H
#define HAVE_RUSAGE 1
-#endif /* HAVE_SYS_RESOURCE_H */
+#endif
+#endif
#include "../common/utils.h"
diff --git a/tests/blogc/check_rusage.c b/tests/blogc/check_rusage.c
new file mode 100644
index 0000000..e828fb2
--- /dev/null
+++ b/tests/blogc/check_rusage.c
@@ -0,0 +1,104 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include "../../src/common/utils.h"
+#include "../../src/blogc/rusage.h"
+
+
+int
+__wrap_getrusage(int who, struct rusage *usage)
+{
+ assert_int_equal(who, RUSAGE_SELF);
+ int rv = mock_type(int);
+ if (rv == 0) {
+ usage->ru_utime.tv_sec = 1;
+ usage->ru_utime.tv_usec = 2;
+ usage->ru_stime.tv_sec = 3;
+ usage->ru_stime.tv_usec = 4;
+ usage->ru_maxrss = 10250;
+ }
+ return rv;
+}
+
+
+static void
+test_rusage_get(void **state)
+{
+ will_return(__wrap_getrusage, -1);
+ assert_null(blogc_rusage_get());
+
+ will_return(__wrap_getrusage, 0);
+ blogc_rusage_t *r = blogc_rusage_get();
+ assert_non_null(r);
+ assert_int_equal(r->cpu_time, 4000006);
+ assert_int_equal(r->memory, 10250);
+ free(r);
+}
+
+
+static void
+test_rusage_format_cpu_time(void **state)
+{
+ char *f = blogc_rusage_format_cpu_time(0);
+ assert_string_equal(f, "0us");
+ free(f);
+
+ f = blogc_rusage_format_cpu_time(123);
+ assert_string_equal(f, "123us");
+ free(f);
+
+ f = blogc_rusage_format_cpu_time(1234);
+ assert_string_equal(f, "1.234ms");
+ free(f);
+
+ f = blogc_rusage_format_cpu_time(12345678);
+ assert_string_equal(f, "12.346s");
+ free(f);
+}
+
+
+static void
+test_rusage_format_memory(void **state)
+{
+ char *f = blogc_rusage_format_memory(0);
+ assert_string_equal(f, "0KB");
+ free(f);
+
+ f = blogc_rusage_format_memory(123);
+ assert_string_equal(f, "123KB");
+ free(f);
+
+ f = blogc_rusage_format_memory(1234);
+ assert_string_equal(f, "1.205MB");
+ free(f);
+
+ f = blogc_rusage_format_memory(12345678);
+ assert_string_equal(f, "11.774GB");
+ free(f);
+}
+
+
+int
+main(void)
+{
+ const UnitTest tests[] = {
+ unit_test(test_rusage_get),
+ unit_test(test_rusage_format_cpu_time),
+ unit_test(test_rusage_format_memory),
+ };
+ return run_tests(tests);
+}