aboutsummaryrefslogtreecommitdiffstats
path: root/tests
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 /tests
parent74dec7c69ae961f98e0a91da9fc3f6214183467a (diff)
downloadblogc-d04bf489c8bbe190a0648f58682260d5b5cef613.tar.gz
blogc-d04bf489c8bbe190a0648f58682260d5b5cef613.tar.bz2
blogc-d04bf489c8bbe190a0648f58682260d5b5cef613.zip
blogc: rusage: added tests, fixed minor issue
Diffstat (limited to 'tests')
-rw-r--r--tests/blogc/check_rusage.c104
1 files changed, 104 insertions, 0 deletions
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);
+}