2 test_bigint.c - Testcase for big positive integer numbers
3 Copyright (c) 2005 Martin Schulze <joey@infodrom.org>
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.
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.
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.
28 int compare (bigint is, char *should)
33 bigint_sprintf (s, "%s", is);
35 if (!strcmp(s, should)) {
36 printf ("Test %d passed, %s == %s\n", nrtest, s, should);
39 fprintf (stderr, "Test %d failed, %s != %s\n", nrtest, s, should);
51 /* Test 1: Representing zero */
53 passed += compare (foo, "0");
55 /* Test 2: Representing nonzero */
56 foo = bigint_int (654321);
57 passed += compare (foo, "654321");
59 /* Test 3: Representing BIGINT_MAX */
60 foo = bigint_int (BIGINT_MAX);
61 sprintf (s, "%d", BIGINT_MAX);
62 passed += compare (foo, s);
64 /* Test 4: BIGINT_MAX + 1 */
65 foo = bigint_int (BIGINT_MAX);
67 baz = bigint_add (foo, bar);
68 sprintf (s, "%d", BIGINT_MAX);
70 passed += compare (baz, s);
72 /* Test 5: BIGINT_MAX - 1 */
73 foo = bigint_int (BIGINT_MAX);
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);
81 /* Test 6: BIGINT_MAX + 1 -1 */
82 foo = bigint_int (BIGINT_MAX);
84 baz = bigint_add (foo, bar);
85 foo = bigint_sub (baz, bar);
86 sprintf (s, "%d", BIGINT_MAX);
87 passed += compare (foo, s);
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);
97 /* Test 8: BIGINT_MAX + 1234 - 1234 */
98 foo = bigint_sub (baz, bar);
99 sprintf (s, "%d", BIGINT_MAX);
100 passed += compare (foo, s);
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);
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);
114 passed += compare (baz, s);
116 /* Test 11: BIGINT_MAX - BIGINT_MAX */
117 foo = bigint_int (BIGINT_MAX);
118 baz = bigint_sub (foo, foo);
119 passed += compare (baz, "0");
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");
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);
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);
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);
150 printf ("%d tests passed\n", passed);
151 if (passed < nrtest) printf ("%d tests failed\n", nrtest-passed);