]> pd.if.org Git - pdclib/blob - functions/stdlib/div.c
Added test drivers.
[pdclib] / functions / stdlib / div.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* div( int, int )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdlib.h>
12
13 #ifndef REGTEST
14
15 div_t div( int numer, int denom )
16 {
17     div_t rc;
18     rc.quot = numer / denom;
19     rc.rem  = numer % denom;
20     /* TODO: pre-C99 compilers might require modulus corrections */
21     return rc;
22 }
23
24 #endif
25
26 #ifdef TEST
27 #include <_PDCLIB_test.h>
28
29 #ifndef _PDCLIB_CONFIG_H
30 #include <_PDCLIB_config.h>
31 #endif
32
33 int main()
34 {
35     div_t idiv;
36     BEGIN_TESTS;
37     idiv = div( 5, 2 );
38     TESTCASE( idiv.quot == 2 && idiv.rem == 1 );
39     idiv = div( -5, 2 );
40     TESTCASE( idiv.quot == -2 && idiv.rem == -1 );
41     idiv = div( 5, -2 );
42     TESTCASE( idiv.quot == -2 && idiv.rem == 1 );
43     TESTCASE( sizeof( idiv.quot ) == _PDCLIB_INT_BYTES );
44     TESTCASE( sizeof( idiv.rem )  == _PDCLIB_INT_BYTES );
45     return TEST_RESULTS;
46 }
47
48 #endif