First util functions, with corresponding unit test.

This commit is contained in:
Vreixo Formoso 2007-12-13 21:02:36 +01:00
parent 60d68df84c
commit 115da82c9e
6 changed files with 80 additions and 3 deletions

View File

@ -25,8 +25,10 @@ src_libisofs_la_SOURCES = \
src/messages.c \ src/messages.c \
src/libiso_msgs.h \ src/libiso_msgs.h \
src/libiso_msgs.c \ src/libiso_msgs.c \
src/stream.h \ src/stream.h \
src/stream.c src/stream.c \
src/util.h \
src/util.c
libinclude_HEADERS = \ libinclude_HEADERS = \
src/libisofs.h src/libisofs.h
@ -65,7 +67,8 @@ test_test_SOURCES = \
test/test.c \ test/test.c \
test/test_node.c \ test/test_node.c \
test/test_image.c \ test/test_image.c \
test/test_tree.c \ test/test_tree.c \
test/test_util.c \
test/mocked_fsrc.h \ test/mocked_fsrc.h \
test/mocked_fsrc.c test/mocked_fsrc.c

19
src/util.c Normal file
View File

@ -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;
}

17
src/util.h Normal file
View File

@ -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_*/

View File

@ -5,6 +5,7 @@ static void create_test_suite()
add_node_suite(); add_node_suite();
add_image_suite(); add_image_suite();
add_tree_suite(); add_tree_suite();
add_util_suite();
} }
int main(int argc, char **argv) int main(int argc, char **argv)

View File

@ -7,5 +7,6 @@
void add_node_suite(); void add_node_suite();
void add_image_suite(); void add_image_suite();
void add_tree_suite(); void add_tree_suite();
void add_util_suite();
#endif /*TEST_H_*/ #endif /*TEST_H_*/

36
test/test_util.c Normal file
View File

@ -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);
}