diff --git a/Makefile.am b/Makefile.am index f5fcac9..6b8684c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,8 +25,10 @@ src_libisofs_la_SOURCES = \ src/messages.c \ src/libiso_msgs.h \ src/libiso_msgs.c \ - src/stream.h \ - src/stream.c + src/stream.h \ + src/stream.c \ + src/util.h \ + src/util.c libinclude_HEADERS = \ src/libisofs.h @@ -65,7 +67,8 @@ test_test_SOURCES = \ test/test.c \ test/test_node.c \ test/test_image.c \ - test/test_tree.c \ + test/test_tree.c \ + test/test_util.c \ test/mocked_fsrc.h \ test/mocked_fsrc.c diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..7bf4d63 --- /dev/null +++ b/src/util.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2007 Vreixo Formoso + * + * This file is part of the libisofs project; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. See COPYING file for details. + */ + +#include "util.h" + +int div_up(int n, int div) +{ + return (n + div - 1) / div; +} + +int round_up(int n, int mul) +{ + return div_up(n, mul) * mul; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..9a1ebe2 --- /dev/null +++ b/src/util.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2007 Vreixo Formoso + * + * This file is part of the libisofs project; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. See COPYING file for details. + */ + +#ifndef LIBISO_UTIL_H_ +#define LIBISO_UTIL_H_ + +extern inline int div_up(int n, int div); + +extern inline int round_up(int n, int mul); + + +#endif /*LIBISO_UTIL_H_*/ diff --git a/test/test.c b/test/test.c index 9e1e04a..87f2e75 100644 --- a/test/test.c +++ b/test/test.c @@ -5,6 +5,7 @@ static void create_test_suite() add_node_suite(); add_image_suite(); add_tree_suite(); + add_util_suite(); } int main(int argc, char **argv) diff --git a/test/test.h b/test/test.h index 54ae9af..cdc062e 100644 --- a/test/test.h +++ b/test/test.h @@ -7,5 +7,6 @@ void add_node_suite(); void add_image_suite(); void add_tree_suite(); +void add_util_suite(); #endif /*TEST_H_*/ diff --git a/test/test_util.c b/test/test_util.c new file mode 100644 index 0000000..a22f55b --- /dev/null +++ b/test/test_util.c @@ -0,0 +1,36 @@ +/* + * Unit test for util.h + * + * This test utiliy functions + */ +#include "test.h" +#include "util.h" + +static void test_div_up() +{ + CU_ASSERT_EQUAL( div_up(1, 2), 1 ); + CU_ASSERT_EQUAL( div_up(2, 2), 1 ); + CU_ASSERT_EQUAL( div_up(0, 2), 0 ); + CU_ASSERT_EQUAL( div_up(-1, 2), 0 ); + CU_ASSERT_EQUAL( div_up(3, 2), 2 ); +} + +static void test_round_up() +{ + CU_ASSERT_EQUAL( round_up(1, 2), 2 ); + CU_ASSERT_EQUAL( round_up(2, 2), 2 ); + CU_ASSERT_EQUAL( round_up(0, 2), 0 ); + CU_ASSERT_EQUAL( round_up(-1, 2), 0 ); + CU_ASSERT_EQUAL( round_up(3, 2), 4 ); + CU_ASSERT_EQUAL( round_up(15, 7), 21 ); + CU_ASSERT_EQUAL( round_up(13, 7), 14 ); + CU_ASSERT_EQUAL( round_up(14, 7), 14 ); +} + +void add_util_suite() +{ + CU_pSuite pSuite = CU_add_suite("UtilSuite", NULL, NULL); + + CU_add_test(pSuite, "div_up()", test_div_up); + CU_add_test(pSuite, "round_up()", test_round_up); +}