~registry/gcalctool/trunk

« back to all changes in this revision

Viewing changes to src/test-mp.c

  • Committer: Robert Ancell
  • Date: 2012-10-14 03:31:40 UTC
  • Revision ID: git-v1:12ba2c81b0a81bb3ac776d1034a3c41b3173196a
Port to Vala

https://bugzilla.gnome.org/show_bug.cgi?id=640685

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2008-2011 Robert Ancell.
3
 
 * 
4
 
 * This program is free software: you can redistribute it and/or modify it under
5
 
 * the terms of the GNU General Public License as published by the Free Software
6
 
 * Foundation, either version 2 of the License, or (at your option) any later
7
 
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
8
 
 * license.
9
 
 */
10
 
 
11
 
#include <glib-object.h>
12
 
#include <stdio.h>
13
 
#include <string.h>
14
 
#include <stdarg.h>
15
 
#include <locale.h>
16
 
 
17
 
#include "mp.h"
18
 
#include "mp-private.h"
19
 
 
20
 
static int fails = 0;
21
 
static int passes = 0;
22
 
 
23
 
/* If we're not using GNU C, elide __attribute__ */
24
 
#ifndef __GNUC__
25
 
#  define  __attribute__(x)  /*NOTHING*/
26
 
#endif
27
 
 
28
 
static void pass(const char *format, ...) __attribute__((format(printf, 1, 2)));
29
 
static void fail(const char *format, ...) __attribute__((format(printf, 1, 2)));
30
 
 
31
 
 
32
 
static void pass(const char *format, ...)
33
 
{
34
 
/*    va_list args;
35
 
 
36
 
    printf(" PASS: ");
37
 
    va_start(args, format);
38
 
    vprintf(format, args);
39
 
    va_end(args);
40
 
    printf("\n");
41
 
*/
42
 
    passes += 1;
43
 
}
44
 
 
45
 
static void fail(const char *format, ...)
46
 
{
47
 
    va_list args;
48
 
 
49
 
    printf("*FAIL: ");
50
 
    va_start(args, format);
51
 
    vprintf(format, args);
52
 
    va_end(args);
53
 
    printf("\n");
54
 
    fails++;
55
 
}
56
 
 
57
 
 
58
 
static void
59
 
print_number(MPNumber *x)
60
 
{
61
 
    int i, j;
62
 
 
63
 
    printf("sign=%d exponent=%d fraction=%d", x->sign, x->exponent, x->fraction[0]);
64
 
    for (i = 1; i < MP_SIZE; i++) {
65
 
        for (j = i; j < MP_SIZE && x->fraction[j] == 0; j++);
66
 
        if (j == MP_SIZE) {
67
 
            printf(",...");
68
 
            break;
69
 
        }
70
 
        printf(",%d", x->fraction[i]);
71
 
    }
72
 
}
73
 
 
74
 
static void
75
 
test_string(const char *number)
76
 
{
77
 
    MPNumber t;
78
 
 
79
 
    mp_set_from_string(number, 10, &t);
80
 
 
81
 
    printf("MPNumber(%s) -> {", number);
82
 
    print_number(&t);
83
 
    printf("}\n");
84
 
}
85
 
 
86
 
static void
87
 
test_integer(int number)
88
 
{
89
 
    MPNumber t;
90
 
 
91
 
    mp_set_from_integer(number, &t);
92
 
 
93
 
    printf("MPNumber(%d) -> {", number);
94
 
    print_number(&t);
95
 
    printf("}\n");
96
 
}
97
 
 
98
 
 
99
 
static void
100
 
test_numbers()
101
 
{
102
 
    printf("base=%d\n", MP_BASE);
103
 
    test_integer(0);
104
 
    test_integer(1);
105
 
    test_integer(-1);
106
 
    test_integer(2);
107
 
    test_integer(9999);
108
 
    test_integer(10000);
109
 
    test_integer(10001);
110
 
    test_integer(2147483647);
111
 
 
112
 
    test_string("0");
113
 
    test_string("1");
114
 
    test_string("-1");
115
 
    test_string("16383");
116
 
    test_string("16384");
117
 
    test_string("16385");
118
 
    test_string("268435456");
119
 
 
120
 
    test_string("0.1");
121
 
    test_string("0.5");
122
 
    test_string("0.25");
123
 
    test_string("0.125");
124
 
    test_string("0.0625");
125
 
    test_string("0.00006103515625");
126
 
    test_string("0.000030517578125");
127
 
 
128
 
    test_string("1.00006103515625");
129
 
    test_string("16384.00006103515625");
130
 
}
131
 
 
132
 
 
133
 
static void
134
 
try(const char *string, bool result, bool expected)
135
 
{
136
 
    if ((result && !expected) || (!result && expected))
137
 
        fail("%s -> %s, expected %s", string, expected ? "true" : "false", result ? "true" : "false");
138
 
    else
139
 
        pass("%s -> %s", string, result ? "true" : "false");
140
 
}
141
 
 
142
 
 
143
 
static void
144
 
test_mp()
145
 
{
146
 
    MPNumber zero, one, minus_one;
147
 
  
148
 
    mp_set_from_integer(0, &zero);
149
 
    mp_set_from_integer(1, &one);
150
 
    mp_set_from_integer(-1, &minus_one);
151
 
  
152
 
    try("mp_is_zero(-1)", mp_is_zero(&minus_one), false);
153
 
    try("mp_is_zero(0)", mp_is_zero(&zero), true);
154
 
    try("mp_is_zero(1)", mp_is_zero(&one), false);
155
 
 
156
 
    try("mp_is_negative(-1)", mp_is_negative(&minus_one), true);
157
 
    try("mp_is_negative(0)", mp_is_negative(&zero), false);
158
 
    try("mp_is_negative(1)", mp_is_negative(&one), false);
159
 
 
160
 
    try("mp_is_integer(-1)", mp_is_integer(&minus_one), true);
161
 
    try("mp_is_integer(0)", mp_is_integer(&zero), true);
162
 
    try("mp_is_integer(1)", mp_is_integer(&one), true);
163
 
 
164
 
    try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one), false);
165
 
    try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero), true);
166
 
    try("mp_is_positive_integer(1)", mp_is_positive_integer(&one), true);
167
 
 
168
 
    try("mp_is_natural(-1)", mp_is_natural(&minus_one), false);
169
 
    try("mp_is_natural(0)", mp_is_natural(&zero), false);
170
 
    try("mp_is_natural(1)", mp_is_natural(&one), true);
171
 
 
172
 
    try("mp_is_complex(-1)", mp_is_complex(&minus_one), false);
173
 
    try("mp_is_complex(0)", mp_is_complex(&zero), false);
174
 
    try("mp_is_complex(1)", mp_is_complex(&one), false);
175
 
 
176
 
    try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one, &minus_one), true);
177
 
    try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one, &zero), false);
178
 
    try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one, &one), false);
179
 
    try("mp_is_equal(0, -1)", mp_is_equal(&zero, &minus_one), false);
180
 
    try("mp_is_equal(0, 0)", mp_is_equal(&zero, &zero), true);
181
 
    try("mp_is_equal(0, 1)", mp_is_equal(&zero, &one), false);
182
 
    try("mp_is_equal(1, -1)", mp_is_equal(&one, &minus_one), false);
183
 
    try("mp_is_equal(1, 0)", mp_is_equal(&one, &zero), false);
184
 
    try("mp_is_equal(1, 1)", mp_is_equal(&one, &one), true);
185
 
 
186
 
    try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero, &minus_one), true);  
187
 
    try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero, &zero), false);
188
 
    try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero, &one), false);
189
 
    try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one, &minus_one), true);  
190
 
    try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one, &zero), true);
191
 
    try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one, &one), false);
192
 
    try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one, &minus_one), false);  
193
 
    try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one, &zero), false);
194
 
    try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one, &one), false);
195
 
 
196
 
    try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero, &minus_one), true);  
197
 
    try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero, &zero), true);
198
 
    try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero, &one), false);
199
 
    try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one, &minus_one), true);
200
 
    try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one, &zero), true);
201
 
    try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one, &one), true);
202
 
    try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one, &minus_one), true);
203
 
    try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one, &zero), false);
204
 
    try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one, &one), false);
205
 
 
206
 
    try("mp_is_less_than(0, -1)", mp_is_less_than (&zero, &minus_one), false);  
207
 
    try("mp_is_less_than(0, 0)", mp_is_less_than (&zero, &zero), false);
208
 
    try("mp_is_less_than(0, 1)", mp_is_less_than (&zero, &one), true);
209
 
    try("mp_is_less_than(1, -1)", mp_is_less_than (&one, &minus_one), false);  
210
 
    try("mp_is_less_than(1, 0)", mp_is_less_than (&one, &zero), false);
211
 
    try("mp_is_less_than(1, 1)", mp_is_less_than (&one, &one), false);
212
 
    try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one, &minus_one), false);  
213
 
    try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one, &zero), true);
214
 
    try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one, &one), true);
215
 
 
216
 
    try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero, &minus_one), false);  
217
 
    try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero, &zero), true);
218
 
    try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero, &one), true);
219
 
    try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one, &minus_one), false);  
220
 
    try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one, &zero), false);
221
 
    try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one, &one), true);
222
 
    try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one, &minus_one), true);  
223
 
    try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one, &zero), true);
224
 
    try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one, &one), true);
225
 
}
226
 
 
227
 
 
228
 
int
229
 
main (int argc, char **argv)
230
 
{
231
 
    setlocale(LC_ALL, "C");
232
 
    g_type_init ();
233
 
 
234
 
    test_mp();
235
 
    test_numbers();
236
 
    if (fails == 0)
237
 
        printf("Passed all %i tests\n", passes);
238
 
 
239
 
    return fails;
240
 
}