]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/tests/bcl.c
Update to version 3.2.0
[FreeBSD/FreeBSD.git] / contrib / bc / tests / bcl.c
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * *****************************************************************************
31  *
32  * Tests for bcl(3).
33  *
34  */
35
36 #include <stdlib.h>
37 #include <stdbool.h>
38 #include <string.h>
39
40 #include <bcl.h>
41
42 static void err(BclError e) {
43         if (e != BCL_ERROR_NONE) abort();
44 }
45
46 int main(void) {
47
48         BclError e;
49         BclContext ctxt;
50         size_t scale;
51         BclNumber n, n2, n3, n4, n5, n6;
52         char* res;
53         BclBigDig b = 0;
54
55         e = bcl_init();
56         err(e);
57
58         e = bcl_init();
59         err(e);
60
61         if (bcl_abortOnFatalError()) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
62
63         bcl_setAbortOnFatalError(true);
64
65         if (!bcl_abortOnFatalError()) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
66
67         ctxt = bcl_ctxt_create();
68
69         bcl_pushContext(ctxt);
70
71         ctxt = bcl_ctxt_create();
72
73         bcl_pushContext(ctxt);
74
75         scale = 10;
76
77         bcl_ctxt_setScale(ctxt, scale);
78
79         scale = bcl_ctxt_scale(ctxt);
80         if (scale != 10) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
81
82         scale = 16;
83
84         bcl_ctxt_setIbase(ctxt, scale);
85
86         scale = bcl_ctxt_ibase(ctxt);
87         if (scale != 16) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
88
89         bcl_ctxt_setObase(ctxt, scale);
90
91         scale = bcl_ctxt_obase(ctxt);
92         if (scale != 16) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
93
94         bcl_ctxt_setIbase(ctxt, 10);
95         bcl_ctxt_setObase(ctxt, 10);
96
97         n = bcl_num_create();
98
99         n2 = bcl_dup(n);
100         bcl_copy(n, n2);
101
102         n3 = bcl_parse("2938");
103         err(bcl_err(n3));
104
105         n4 = bcl_parse("-28390.9108273");
106         err(bcl_err(n4));
107
108         if (!bcl_num_neg(n4)) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
109
110         n3 = bcl_add(n3, n4);
111         err(bcl_err(n3));
112
113         res = bcl_string(bcl_dup(n3));
114         if (strcmp(res, "-25452.9108273")) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
115
116         free(res);
117
118         n4 = bcl_parse("8937458902.2890347");
119         err(bcl_err(n4));
120
121         e = bcl_divmod(bcl_dup(n4), n3, &n5, &n6);
122         err(e);
123
124         res = bcl_string(n5);
125
126         if (strcmp(res, "-351137.0060159482"))
127                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
128
129         free(res);
130
131         res = bcl_string(n6);
132
133         if (strcmp(res, ".00000152374405414"))
134                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
135
136         free(res);
137
138         n4 = bcl_sqrt(n4);
139         err(bcl_err(n4));
140
141         res = bcl_string(bcl_dup(n4));
142
143         if (strcmp(res, "94538.1346457028"))
144                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
145
146         free(res);
147
148         e = bcl_num_setScale(n4, 20);
149         err(e);
150
151         res = bcl_string(bcl_dup(n4));
152
153         if (strcmp(res, "94538.13464570280000000000"))
154                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
155
156         free(res);
157
158         e = bcl_num_setScale(n4, 0);
159         err(e);
160
161         res = bcl_string(bcl_dup(n4));
162
163         if (strcmp(res, "94538"))
164                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
165
166         free(res);
167
168         e = bcl_bigdig(n4, &b);
169         err(e);
170
171         if (b != 94538) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
172
173         n4 = bcl_bigdig2num(b);
174         err(bcl_err(n4));
175
176         res = bcl_string(bcl_dup(n4));
177
178         if (strcmp(res, "94538"))
179                 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
180
181         free(res);
182
183         n4 = bcl_frand(10);
184         err(bcl_err(n4));
185
186         n4 = bcl_lshift(n4, bcl_bigdig2num(10));
187         err(bcl_err(n4));
188
189         n3 = bcl_irand(n4);
190         err(bcl_err(n3));
191
192         n2 = bcl_ifrand(bcl_dup(n3), 10);
193         err(bcl_err(n2));
194
195         e = bcl_rand_seedWithNum(n3);
196         err(e);
197
198         n4 = bcl_rand_seed2num();
199         err(bcl_err(n4));
200
201         n5 = bcl_parse("10");
202         err(bcl_err(n5));
203
204         n6 = bcl_modexp(bcl_dup(n5), bcl_dup(n5), bcl_dup(n5));
205         err(bcl_err(n6));
206
207         bcl_num_free(n);
208
209         bcl_ctxt_freeNums(ctxt);
210
211         bcl_gc();
212
213         bcl_popContext();
214
215         bcl_ctxt_free(ctxt);
216
217         ctxt = bcl_context();
218
219         bcl_popContext();
220
221         bcl_ctxt_free(ctxt);
222
223         bcl_free();
224
225         bcl_free();
226
227         return 0;
228 }