First util functions, with corresponding unit test.
parent
60d68df84c
commit
115da82c9e
@ -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_*/
|
@ -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…
Reference in New Issue