First util functions, with corresponding unit test.

release-1.5.4.branch
Vreixo Formoso 15 years ago
parent 60d68df84c
commit 115da82c9e
  1. 9
      Makefile.am
  2. 19
      src/util.c
  3. 17
      src/util.h
  4. 1
      test/test.c
  5. 1
      test/test.h
  6. 36
      test/test_util.c

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

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

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

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

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

@ -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);
}
Loading…
Cancel
Save