Jens Rohler: prevent negative numbers
[infodrom/dtaus] / test_bigint.c
1 /*
2     test_bigint.c - Testcase for big positive integer numbers
3     Copyright (c) 2005  Martin Schulze <joey@infodrom.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20  */
21
22 #include "bigint.h"
23 #include <stdio.h>
24 #include <string.h>
25
26 int nrtest = 0;
27
28 int compare (bigint is, char *should)
29 {
30   char s[30];
31
32   nrtest++;
33   bigint_sprintf (s, "%s", is);
34
35   if (!strcmp(s, should)) {
36     printf ("Test %d passed, %s == %s\n", nrtest, s, should);
37     return 1;
38   } else {
39     fprintf (stderr, "Test %d failed, %s != %s\n", nrtest, s, should);
40     return 0;
41   }
42 }
43
44 int main()
45 {
46   bigint foo, bar, baz;
47   char s[20];
48   int passed = 0;
49   int i;
50
51   /* Test 1: Representing zero */
52   foo = bigint_int (0);
53   passed += compare (foo, "0");
54
55   /* Test 2: Representing nonzero */
56   foo = bigint_int (654321);
57   passed += compare (foo, "654321");
58
59   /* Test 3: Representing BIGINT_MAX */
60   foo = bigint_int (BIGINT_MAX);
61   sprintf (s, "%d", BIGINT_MAX);
62   passed += compare (foo, s);
63
64   /* Test 4: BIGINT_MAX + 1 */
65   foo = bigint_int (BIGINT_MAX);
66   bar = bigint_int (1);
67   baz = bigint_add (foo, bar);
68   sprintf (s, "%d", BIGINT_MAX);
69   s[strlen(s)-1] = '1';
70   passed += compare (baz, s);
71
72   /* Test 5: BIGINT_MAX - 1 */
73   foo = bigint_int (BIGINT_MAX);
74   bar = bigint_int (1);
75   baz = bigint_sub (foo, bar);
76   sprintf (s, "%d", BIGINT_MAX);
77   for (i=0;s[i];i++) s[i] = '9';
78   s[strlen(s)-1] = '\0';
79   passed += compare (baz, s);
80
81   /* Test 6: BIGINT_MAX + 1 -1 */
82   foo = bigint_int (BIGINT_MAX);
83   bar = bigint_int (1);
84   baz = bigint_add (foo, bar);
85   foo = bigint_sub (baz, bar);
86   sprintf (s, "%d", BIGINT_MAX);
87   passed += compare (foo, s);
88
89   /* Test 7: BIGINT_MAX + 1234 */
90   foo = bigint_int (BIGINT_MAX);
91   bar = bigint_int (1234);
92   baz = bigint_add (foo, bar);
93   sprintf (s, "%d", BIGINT_MAX);
94   sprintf (s+6, "%d", 1234);
95   passed += compare (baz, s);
96
97   /* Test 8: BIGINT_MAX + 1234 - 1234 */
98   foo = bigint_sub (baz, bar);
99   sprintf (s, "%d", BIGINT_MAX);
100   passed += compare (foo, s);
101
102   /* Test 9: BIGINT_MAX - 100000000 */
103   foo = bigint_int (BIGINT_MAX);
104   bar = bigint_int (100000000);
105   baz = bigint_sub (foo, bar);
106   sprintf (s, "%d", 900000000);
107   passed += compare (baz, s);
108
109   /* Test 10: BIGINT_MAX + BIGINT_MAX */
110   foo = bigint_int (BIGINT_MAX);
111   baz = bigint_add (foo, foo);
112   sprintf (s, "%d", BIGINT_MAX);
113   s[0] = '2';
114   passed += compare (baz, s);
115
116   /* Test 11: BIGINT_MAX - BIGINT_MAX */
117   foo = bigint_int (BIGINT_MAX);
118   baz = bigint_sub (foo, foo);
119   passed += compare (baz, "0");
120
121   /* Test 12: BIGINT_MAX + ( BIGINT_MAX -1) */
122   foo = bigint_int (BIGINT_MAX);
123   baz = bigint_sub (foo, foo);
124   passed += compare (baz, "0");
125
126   /* Test 13: BIGINT_MAX-1 + 1 */
127   foo = bigint_int (BIGINT_MAX-1);
128   bar = bigint_int (1);
129   baz = bigint_add (foo, bar);
130   sprintf (s, "%d", BIGINT_MAX);
131   passed += compare (baz, s);
132
133   /* Test 14: BIGINT_MAX-1 + 2 */
134   foo = bigint_int (BIGINT_MAX-1);
135   bar = bigint_int (2);
136   baz = bigint_add (foo, bar);
137   sprintf (s, "%d", BIGINT_MAX);
138   s[strlen(s)-1] = '1';
139   passed += compare (baz, s);
140
141   /* Test 15: BIGINT_MAX + BIGINT_MAX-1 */
142   foo = bigint_int (BIGINT_MAX);
143   bar = bigint_int (BIGINT_MAX-1);
144   baz = bigint_add (foo, bar);
145   sprintf (s, "%d", BIGINT_MAX);
146   for (i=1;s[i];i++) s[i]='9';
147   passed += compare (baz, s);
148
149
150   printf ("%d tests passed\n", passed);
151   if (passed < nrtest) printf ("%d tests failed\n", nrtest-passed);
152   return 0;
153 }