2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
30 * *****************************************************************************
42 static void err(BclError e) {
43 if (e != BCL_ERROR_NONE) abort();
51 BclNumber n, n2, n3, n4, n5, n6;
61 if (bcl_abortOnFatalError()) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
63 bcl_setAbortOnFatalError(true);
65 if (!bcl_abortOnFatalError()) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
67 ctxt = bcl_ctxt_create();
69 bcl_pushContext(ctxt);
71 ctxt = bcl_ctxt_create();
73 bcl_pushContext(ctxt);
77 bcl_ctxt_setScale(ctxt, scale);
79 scale = bcl_ctxt_scale(ctxt);
80 if (scale != 10) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
84 bcl_ctxt_setIbase(ctxt, scale);
86 scale = bcl_ctxt_ibase(ctxt);
87 if (scale != 16) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
89 bcl_ctxt_setObase(ctxt, scale);
91 scale = bcl_ctxt_obase(ctxt);
92 if (scale != 16) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
94 bcl_ctxt_setIbase(ctxt, 10);
95 bcl_ctxt_setObase(ctxt, 10);
102 n3 = bcl_parse("2938");
105 n4 = bcl_parse("-28390.9108273");
108 if (!bcl_num_neg(n4)) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
110 n3 = bcl_add(n3, n4);
113 res = bcl_string(bcl_dup(n3));
114 if (strcmp(res, "-25452.9108273")) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
118 n4 = bcl_parse("8937458902.2890347");
121 e = bcl_divmod(bcl_dup(n4), n3, &n5, &n6);
124 res = bcl_string(n5);
126 if (strcmp(res, "-351137.0060159482"))
127 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
131 res = bcl_string(n6);
133 if (strcmp(res, ".00000152374405414"))
134 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
141 res = bcl_string(bcl_dup(n4));
143 if (strcmp(res, "94538.1346457028"))
144 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
148 e = bcl_num_setScale(n4, 20);
151 res = bcl_string(bcl_dup(n4));
153 if (strcmp(res, "94538.13464570280000000000"))
154 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
158 e = bcl_num_setScale(n4, 0);
161 res = bcl_string(bcl_dup(n4));
163 if (strcmp(res, "94538"))
164 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
168 e = bcl_bigdig(n4, &b);
171 if (b != 94538) err(BCL_ERROR_FATAL_UNKNOWN_ERR);
173 n4 = bcl_bigdig2num(b);
176 res = bcl_string(bcl_dup(n4));
178 if (strcmp(res, "94538"))
179 err(BCL_ERROR_FATAL_UNKNOWN_ERR);
186 n4 = bcl_lshift(n4, bcl_bigdig2num(10));
192 n2 = bcl_ifrand(bcl_dup(n3), 10);
195 e = bcl_rand_seedWithNum(n3);
198 n4 = bcl_rand_seed2num();
201 n5 = bcl_parse("10");
204 n6 = bcl_modexp(bcl_dup(n5), bcl_dup(n5), bcl_dup(n5));
209 bcl_ctxt_freeNums(ctxt);
217 ctxt = bcl_context();