Permalink
Browse files

Add UnsignedModulus

In contrast to the standard C modulus operator (%), this gives
you an unsigned modulus. So where -1 % 3 => -1, UnsignedModulus(-1, 3) => 2.
  • Loading branch information...
1 parent 154a362 commit fff3481bf66f8155ffcdf14b5d77d048dc30501f @sigurdteigen sigurdteigen committed Apr 17, 2012
Showing with 75 additions and 1 deletion.
  1. +1 −0 src/Makefile.am
  2. +6 −0 src/misc_lib.c
  3. +35 −0 src/misc_lib.h
  4. +2 −1 tests/unit/Makefile.am
  5. +31 −0 tests/unit/misc_lib_test.c
View
@@ -91,6 +91,7 @@ libpromises_la_SOURCES = \
logging.c \
logic_expressions.c logic_expressions.h \
matching.c \
+ misc_lib.c misc_lib.h \
mod_access.c \
mod_common.c \
mod_databases.c \
View
@@ -0,0 +1,6 @@
+#include "misc_lib.h"
+
+unsigned long UnsignedModulus(long dividend, long divisor)
+{
+ return ((dividend % divisor) + divisor) % divisor;
+}
View
@@ -0,0 +1,35 @@
+/*
+ Copyright (C) Cfengine AS
+
+ This file is part of Cfengine 3 - written and maintained by Cfengine AS.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 3.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ To the extent this program is licensed as part of the Enterprise
+ versions of Cfengine, the applicable Commerical Open Source License
+ (COSL) may apply to this file if you as a licensee so wish it. See
+ included file COSL.txt.
+*/
+
+#ifndef CFENGINE_MISC_LIB_H
+#define CFENGINE_MISC_LIB_H
+
+/*
+ In contrast to the standard C modulus operator (%), this gives
+ you an unsigned modulus. So where -1 % 3 => -1,
+ UnsignedModulus(-1, 3) => 2.
+*/
+unsigned long UnsignedModulus(long dividend, long divisor);
+
+#endif
View
@@ -32,7 +32,8 @@ check_PROGRAMS = \
sequence_test \
db_test \
lastseen_test \
- lastseen_migration_test
+ lastseen_migration_test \
+ misc_lib_test
TESTS = $(check_PROGRAMS)
View
@@ -0,0 +1,31 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmockery.h>
+
+#include "misc_lib.h"
+
+static void test_unsigned_modulus(void **state)
+{
+ assert_int_equal(UnsignedModulus(0, 3), 0);
+ assert_int_equal(UnsignedModulus(1, 3), 1);
+ assert_int_equal(UnsignedModulus(2, 3), 2);
+ assert_int_equal(UnsignedModulus(3, 3), 0);
+ assert_int_equal(UnsignedModulus(4, 3), 1);
+
+ assert_int_equal(UnsignedModulus(-1, 3), 2);
+ assert_int_equal(UnsignedModulus(-2, 3), 1);
+ assert_int_equal(UnsignedModulus(-3, 3), 0);
+ assert_int_equal(UnsignedModulus(-4, 3), 2);
+}
+
+
+int main()
+{
+ const UnitTest tests[] =
+ {
+ unit_test(test_unsigned_modulus),
+ };
+
+ return run_tests(tests);
+}

0 comments on commit fff3481

Please sign in to comment.