pax_global_header00006660000000000000000000000064146164733610014525gustar00rootroot0000000000000052 comment=c9e16dd8aed26d4a1d01a7e47f3cb0b340b8ece8 zarith_stubs_js-0.17.0/000077500000000000000000000000001461647336100150275ustar00rootroot00000000000000zarith_stubs_js-0.17.0/.gitignore000066400000000000000000000000411461647336100170120ustar00rootroot00000000000000_build *.install *.merlin _opam zarith_stubs_js-0.17.0/.ocamlformat000066400000000000000000000000231461647336100173270ustar00rootroot00000000000000profile=janestreet zarith_stubs_js-0.17.0/CONTRIBUTING.md000066400000000000000000000044101461647336100172570ustar00rootroot00000000000000This repository contains open source software that is developed and maintained by [Jane Street][js]. Contributions to this project are welcome and should be submitted via GitHub pull requests. Signing contributions --------------------- We require that you sign your contributions. Your signature certifies that you wrote the patch or otherwise have the right to pass it on as an open-source patch. The rules are pretty simple: if you can certify the below (from [developercertificate.org][dco]): ``` Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 1 Letterman Drive Suite D4700 San Francisco, CA, 94129 Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ``` Then you just add a line to every git commit message: ``` Signed-off-by: Joe Smith ``` Use your real name (sorry, no pseudonyms or anonymous contributions.) If you set your `user.name` and `user.email` git configs, you can sign your commit automatically with git commit -s. [dco]: http://developercertificate.org/ [js]: https://opensource.janestreet.com/ zarith_stubs_js-0.17.0/LICENSE.md000066400000000000000000000021461461647336100164360ustar00rootroot00000000000000The MIT License Copyright (c) 2019--2024 Jane Street Group, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. zarith_stubs_js-0.17.0/Makefile000066400000000000000000000004031461647336100164640ustar00rootroot00000000000000INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),) default: dune build install: dune install $(INSTALL_ARGS) uninstall: dune uninstall $(INSTALL_ARGS) reinstall: uninstall install clean: dune clean .PHONY: default install uninstall reinstall clean zarith_stubs_js-0.17.0/README.md000066400000000000000000000022541461647336100163110ustar00rootroot00000000000000# Zarith Stubs JS [Zarith](https://github.com/ocaml/Zarith) is an OCaml library that implements common operations over arbitrary-precision integers and rationals. It is implemented via a C api that primarily calls out to [Gnu Multiple Precision Arithmetic Library](https://gmplib.org). Because of that C API, Zarith could not be compiled via Js_of_ocaml until now. [Zarith Stubs JS](https://github.com/janestreet/zarith_stubs_js) is a reimplementation of the native C functions in JavaScript. It makes extensive use of [peterolson/BigInteger.js](https://github.com/peterolson/BigInteger.js) as a shim for browser BigInt functionality and also for the implementations of many numerical algorithms. ## How to use it ? In order to use `zarith` with `js_of_ocaml`, just provide the javascript runtime files to the js_of_ocaml compiler and use `zarith` as you would normally do. In practice, - if you use [dune](https://github.com/ocaml/dune) to build your project, just add `zarith_stubs_js` as a library dependency. - if you don't use dune, you need to find a way to pass both [biginteger.js](biginteger.js) and [runtime.js](runtime.js) files to the js_of_ocaml compiler command line. zarith_stubs_js-0.17.0/dune000066400000000000000000000002671461647336100157120ustar00rootroot00000000000000(library (name zarith_stubs_js) (public_name zarith_stubs_js) (js_of_ocaml (javascript_files ./runtime.js) (flags --no-sourcemap)) (libraries) (preprocess no_preprocessing)) zarith_stubs_js-0.17.0/dune-project000066400000000000000000000000211461647336100173420ustar00rootroot00000000000000(lang dune 3.11) zarith_stubs_js-0.17.0/runtime.js000066400000000000000000000777201461647336100170650ustar00rootroot00000000000000/* eslint-disable no-unused-vars */ // We represent a [Z.t] as a javascript 32bit integers or as a BigInt. // Like in ZArith, we guarantee that: // - If the number fits in a 32bit integer, it is stored in a 32bit integer, // not a BigInt. // - Conversely, if the number does not fit in a 32bit integer, it is stored in // a BigInt. We almost could get away with using primitive integers up to 53 // bits, but this would require giving up marshaling (because the js_of_ocaml // runtime assumes that numbers are either 32bit integers or floats, and // refuses to marshal floats that are not exactly 32bit integers), and so we // don't. I am not sure this would bring much performance benefits due to the // additional checks required anyways (the checks for 32bit integers happen // in ZArith's OCaml code, so we can't replace them with checks for 53 bit // integers). // // Note that some functions in this module call other functions as utility in // a way that doesn't respect these invariants, and so some functions can see // non-normalized numbers as input. //Provides: ml_z_normalize function ml_z_normalize(x) { if (typeof x === "number") { if (x === (x | 0)) return x; return BigInt(x); } if (-2147483648 <= x && x <= 2147483647) return Number(x) | 0; return x; } //external mul_overflows: int -> int -> bool //Provides: ml_z_mul_overflows function ml_z_mul_overflows(x, y) { let z = x * y; return +(z !== (z | 0)); } //external init: unit -> unit //Provides: ml_z_init //Requires: caml_zarith_marshal, caml_zarith_unmarshal, caml_custom_ops //Requires: ml_z_hash, ml_z_compare function ml_z_init(unit) { caml_custom_ops['_z'] = { serialize: caml_zarith_marshal, deserialize: caml_zarith_unmarshal, hash: ml_z_hash, compare: ml_z_compare, }; Object.defineProperty(BigInt.prototype, 'caml_custom', { value: '_z' }); return 0 } //external neg: t -> t //Provides: ml_z_neg const //Requires: ml_z_normalize function ml_z_neg(z1) { return ml_z_normalize(-z1); } //external add: t -> t -> t //Provides: ml_z_add const //Requires: ml_z_normalize function ml_z_add(z1, z2) { return ml_z_normalize(BigInt(z1) + BigInt(z2)); } //external sub: t -> t -> t //Provides: ml_z_sub const //Requires: ml_z_normalize function ml_z_sub(z1, z2) { return ml_z_normalize(BigInt(z1) - BigInt(z2)); } //external mul: t -> t -> t //Provides: ml_z_mul const //Requires: ml_z_normalize function ml_z_mul(z1, z2) { return ml_z_normalize(BigInt(z1) * BigInt(z2)); } //external div: t -> t -> t //Provides: ml_z_div //Requires: caml_raise_zero_divide, ml_z_normalize function ml_z_div(z1, z2) { if (z2 == 0) caml_raise_zero_divide(); return ml_z_normalize(BigInt(z1) / BigInt(z2)); } //external cdiv: t -> t -> t //Provides: ml_z_cdiv //Requires: ml_z_div, ml_z_sign, ml_z_normalize function ml_z_cdiv(z1, z2) { let z1_pos = ml_z_sign(z1); let z2_pos = ml_z_sign(z2); if (z1_pos * z2_pos > 0) /* Multiplication is like a signwise xor */ { if (BigInt(z1) % BigInt(z2) !== 0n) { return ml_z_normalize(BigInt(z1) / BigInt(z2) + 1n); } } return ml_z_div(z1, z2); } //external fdiv: t -> t -> t //Provides: ml_z_fdiv //Requires: ml_z_div, ml_z_sign, ml_z_normalize function ml_z_fdiv(z1, z2) { let z1_pos = ml_z_sign(z1); let z2_pos = ml_z_sign(z2); if (z1_pos * z2_pos < 0) /* Multiplication is like a signwise xor */ { if (BigInt(z1) % BigInt(z2) !== 0n) { return ml_z_normalize(BigInt(z1) / BigInt(z2) - 1n); } } return ml_z_div(z1, z2); } //external rem: t -> t -> t //Provides: ml_z_rem //Requires: caml_raise_zero_divide, ml_z_normalize function ml_z_rem(z1, z2) { if (z2 == 0) caml_raise_zero_divide(); return ml_z_normalize(BigInt(z1) % BigInt(z2)); } //external div_rem: t -> t -> (t * t) //Provides: ml_z_div_rem //Requires: ml_z_div, ml_z_rem function ml_z_div_rem(z1, z2) { return [0, ml_z_div(z1, z2), ml_z_rem(z1, z2)] } //external succ: t -> t //Provides: ml_z_succ const //Requires: ml_z_normalize function ml_z_succ(z1) { return ml_z_normalize(BigInt(z1) + 1n); } //external pred: t -> t //Provides: ml_z_pred const //Requires: ml_z_normalize function ml_z_pred(z1) { return ml_z_normalize(BigInt(z1) - 1n); } //external abs: t -> t //Provides: ml_z_abs const //Requires: ml_z_normalize function ml_z_abs(z1) { if (z1 < 0) return ml_z_normalize(-z1); return z1; } //external logand: t -> t -> t //Provides: ml_z_logand const //Requires: ml_z_normalize function ml_z_logand(z1, z2) { return ml_z_normalize(BigInt(z1) & BigInt(z2)); } //external logor: t -> t -> t //Provides: ml_z_logor const //Requires: ml_z_normalize function ml_z_logor(z1, z2) { return ml_z_normalize(BigInt(z1) | BigInt(z2)); } //external logxor: t -> t -> t //Provides: ml_z_logxor const //Requires: ml_z_normalize function ml_z_logxor(z1, z2) { return ml_z_normalize(BigInt(z1) ^ BigInt(z2)); } //external lognot: t -> t //Provides: ml_z_lognot const //Requires: ml_z_normalize function ml_z_lognot(z1) { return ml_z_normalize(~z1); } //external shift_left: t -> int -> t //Provides: ml_z_shift_left const //Requires: ml_z_normalize function ml_z_shift_left(z1, amt) { return ml_z_normalize(BigInt(z1) << BigInt(amt)); } //external shift_right: t -> int -> t //Provides: ml_z_shift_right const //Requires: ml_z_normalize function ml_z_shift_right(z1, amt) { return ml_z_normalize(BigInt(z1) >> BigInt(amt)); } //external shift_right_trunc: t -> int -> t //Provides: ml_z_shift_right_trunc const //Requires: ml_z_normalize function ml_z_shift_right_trunc(z1, z2) { return ml_z_normalize(BigInt(z1) / (1n << BigInt(z2))); } //external of_int32: int32 -> t //Provides: ml_z_of_int32 const function ml_z_of_int32(i) { return i | 0; } //external of_nativeint: nativeint -> t //Provides: ml_z_of_nativeint const function ml_z_of_nativeint(i) { return i | 0; } //external of_int64: int64 -> t //Provides: ml_z_of_int64 const //Requires: caml_int64_compare, caml_int64_neg, ml_z_normalize //Requires: caml_int64_create_lo_hi,caml_int64_hi32,caml_int64_lo32 function ml_z_of_int64(i64) { let neg = false; if (caml_int64_compare(i64, caml_int64_create_lo_hi(0, 0)) < 0) { neg = true; i64 = caml_int64_neg(i64) } let lo = caml_int64_lo32(i64) >>> 0; let hi = caml_int64_hi32(i64) >>> 0; let x = BigInt(lo) + (BigInt(hi) << 32n); if (neg) { x = -x } return ml_z_normalize(x) } //external of_float: float -> t //Provides: ml_z_of_float //Requires: caml_raise_constant, caml_named_value, ml_z_normalize function ml_z_of_float(f1) { if (f1 == Infinity || f1 == -Infinity || f1 != f1) caml_raise_constant(caml_named_value("ml_z_overflow")); return ml_z_normalize(f1 < 0 ? Math.ceil(f1) : Math.floor(f1)); } //external to_int: t -> int //Provides: ml_z_to_int //Requires: caml_raise_constant, caml_named_value function ml_z_to_int(z1) { if (typeof z1 === "number" && z1 === (z1 | 0)) return z1; caml_raise_constant(caml_named_value("ml_z_overflow")); } //external to_int32: t -> int32 //Provides: ml_z_to_int32 //Requires: ml_z_to_int function ml_z_to_int32(z1) { return ml_z_to_int(z1) } //external to_int32_unsigned: t -> int32 //Provides: ml_z_to_int32_unsigned //Requires: ml_z_fits_int32_unsigned, ml_z_normalize, caml_raise_constant //Requires: caml_named_value function ml_z_to_int32_unsigned(z1) { if (ml_z_fits_int32_unsigned(z1)) { return Number(BigInt.asIntN(32, BigInt(z1))) | 0; } caml_raise_constant(caml_named_value("ml_z_overflow")); } //external to_nativeint_unsigned: t -> nativeint //Provides: ml_z_to_nativeint_unsigned //Requires: ml_z_to_int32_unsigned function ml_z_to_nativeint_unsigned(z1) { return ml_z_to_int32_unsigned(z1); } //external to_int64: t -> int64 //Provides: ml_z_to_int64 //Requires: ml_z_fits_int64, caml_raise_constant, caml_named_value //Requires: caml_int64_create_lo_hi function ml_z_to_int64(z1) { if (!ml_z_fits_int64(z1)) { caml_raise_constant(caml_named_value("ml_z_overflow")); } let z1n = BigInt(z1) let mask = 0xffffffffn let lo = Number(z1n & mask); let hi = Number((z1n >> 32n) & mask); let x = caml_int64_create_lo_hi(lo, hi); return x; } //external to_int64_unsigned: t -> int64 //Provides: ml_z_to_int64_unsigned //Requires: ml_z_fits_int64_unsigned, caml_raise_constant, caml_named_value //Requires: caml_int64_create_lo_hi function ml_z_to_int64_unsigned(z1) { if (!ml_z_fits_int64_unsigned(z1)) { caml_raise_constant(caml_named_value("ml_z_overflow")); } let z1n = BigInt.asIntN(64, BigInt(z1)) let mask = 0xffffffffn let lo = Number(z1n & mask); let hi = Number((z1n >> 32n) & mask); let x = caml_int64_create_lo_hi(lo, hi); return x; } //external testbit: t -> int -> bool //Provides: ml_z_testbit const function ml_z_testbit(z, pos) { return Number((BigInt(z) >> BigInt(pos)) & 1n); } //external to_nativeint: t -> nativeint //Provides: ml_z_to_nativeint //Requires: ml_z_to_int function ml_z_to_nativeint(z1) { return ml_z_to_int(z1) } //external format: string -> t -> string //Provides: ml_z_format //Requires: caml_jsbytes_of_string, caml_failwith, caml_string_of_jsbytes //Requires: ml_z_normalize function ml_z_format(fmt, z1) { let z1n = BigInt(z1); fmt = caml_jsbytes_of_string(fmt); // https://github.com/ocaml/Zarith/blob/d0555d451ce295c4497f24a8d9993f8dd23097df/z.mlip#L297 let base = 10; let cas = 0; let width = 0; let alt = 0; let dir = 0; let sign = ''; let pad = ' '; let idx = 0; let prefix = ""; while (fmt[idx] == '%') idx++; for (; ; idx++) { if (fmt[idx] == '#') alt = 1; else if (fmt[idx] == '0') pad = '0'; else if (fmt[idx] == '-') dir = 1; else if (fmt[idx] == ' ' || fmt[idx] == '+') sign = fmt[idx]; else break; } if (z1n < 0) { sign = '-'; z1n = -z1n } for (; fmt[idx] >= '0' && fmt[idx] <= '9'; idx++) width = 10 * width + (+fmt[idx]); switch (fmt[idx]) { case 'i': case 'd': case 'u': break; case 'b': base = 2; if (alt) prefix = "0b"; break; case 'o': base = 8; if (alt) prefix = "0o"; break; case 'x': base = 16; if (alt) prefix = "0x"; break; case 'X': base = 16; if (alt) prefix = "0X"; cas = 1; break; default: caml_failwith("Unsupported format '" + fmt + "'"); } if (dir) pad = ' '; let res = z1n.toString(base); if (cas === 1) { res = res.toUpperCase(); } let size = res.length; if (pad == ' ') { if (dir) { res = sign + prefix + res; for (; res.length < width;) res = res + pad; } else { res = sign + prefix + res; for (; res.length < width;) res = pad + res; } } else { let pre = sign + prefix; for (; res.length + pre.length < width;) res = pad + res; res = pre + res; } return caml_string_of_jsbytes(res); } //Provides: jsoo_z_of_js_string_base //Requires: caml_invalid_argument, ml_z_normalize function jsoo_z_of_js_string_base(base, s) { if (base == 0) { // https://github.com/ocaml/Zarith/blob/b8dbaf48a7927061df699ad7ce642bb4f1fe5308/caml_z.c#L598 base = 10; let p = 0; let sign = 1; if (s[p] == '-') { sign = -1; p++ } else if (s[p] == '+') { p++ } if (s[p] == '0') { p++; if (s.length == p) { return 0; } else { let bc = s[p]; if (bc == 'o' || bc == 'O') { base = 8; } else if (bc == 'x' || bc == 'X') { base = 16; } else if (bc == 'b' || bc == 'B') { base = 2; } if (base != 10) { s = s.substring(p + 1); if (sign == -1) s = "-" + s; } } } } function digit(code) { if (code >= 48 && code <= 57) return code - 48; if (code >= 97 && code <= 102) return code - 97 + 10; if (code >= 65 && code <= 70) return code - 65 + 10; } let i = 0; if (s[i] == '+') { //remove leading '+' s = s.substring(1); } else if (s[i] == '-') i++; if (s[i] == '_') caml_invalid_argument("Z.of_substring_base: invalid digit"); s = s.replace(/_/g, ''); //normalize "empty" numbers if (s == '-' || s == '') s = '0'; for (; i < s.length; i++) { let c = digit(s.charCodeAt(i)); if (c == undefined || c >= base) caml_invalid_argument("Z.of_substring_base: invalid digit"); } if (base === 10) return ml_z_normalize(BigInt(s)); let neg = false; i = 0; if (s[i] == '-') { neg = true; i++; } let n = 0n; for (; i < s.length; i++) { n *= BigInt(base); n += BigInt(digit(s.charCodeAt(i))); } if (neg) n = -n; return ml_z_normalize(n); } //external of_substring_base: int -> string -> pos:int -> len:int -> t //Provides: ml_z_of_substring_base //Requires: jsoo_z_of_js_string_base, caml_jsbytes_of_string //Requires: caml_invalid_argument, caml_ml_string_length function ml_z_of_substring_base(base, s, pos, len) { s = caml_jsbytes_of_string(s); if (pos != 0 || len != s.length) { if (s.length - pos < len) { caml_invalid_argument("Z.of_substring_base: invalid offset or length"); } s = s.slice(pos, pos + len); } return jsoo_z_of_js_string_base(base, s); } //external compare: t -> t -> int //Provides: ml_z_compare const function ml_z_compare(z1, z2) { return z1 == z2 ? 0 : z1 > z2 ? 1 : -1; } //external equal: t -> t -> bool //Provides: ml_z_equal const function ml_z_equal(z1, z2) { return z1 == z2 ? 1 : 0; } //external sign: t -> int //Provides: ml_z_sign const function ml_z_sign(z1) { return z1 == 0 ? 0 : z1 > 0 ? 1 : -1; } //external gcd: t -> t -> t //Provides: ml_z_gcd //Requires: ml_z_normalize function ml_z_gcd(z1, z2) { let a = BigInt(z1); if (a < 0) a = -a; let b = BigInt(z2); if (b < 0) b = -b; if (a === b) return ml_z_normalize(a); if (a === 0n) return ml_z_normalize(b); if (b === 0n) return ml_z_normalize(a); let c = 1n, d, t; function min(a, b) { return a < b ? a : b } while ((a & 1n) === 0n && (b & 1n) === 0n) { d = min(a & -a, b & -b); a /= d; b /= d; c *= d; } while ((a & 1n) === 0n) { a /= a & -a; } do { while ((b & 1n) === 0n) { b /= b & -b; } if (a > b) { t = b; b = a; a = t; } b -= a; } while (b !== 0n); return ml_z_normalize(c === 1n ? a : a * c); } //external numbits: t -> int //Provides: ml_z_numbits const function ml_z_numbits(z1) { if (z1 < 0) z1 = -z1; let n = 0; let upperBound = 1n; while (upperBound <= z1) { n += 1; upperBound = upperBound << 1n; } return n; // 2^{n-1} <= |x| < 2^n } //external fits_int: t -> bool //Provides: ml_z_fits_int const function ml_z_fits_int(z1) { return typeof z1 === "number" ? +(z1 === (z1 | 0)) : 0; } //external fits_int32: t -> bool //Provides: ml_z_fits_int32 //Requires: ml_z_fits_int function ml_z_fits_int32(z1) { return ml_z_fits_int(z1); } //external fits_int32_unsigned: t -> bool //Provides: ml_z_fits_int32_unsigned function ml_z_fits_int32_unsigned(z1) { return 0 <= z1 && z1 <= 4294967295; } //external fits_int64_unsigned: t -> bool //Provides: ml_z_fits_int64_unsigned function ml_z_fits_int64_unsigned(z1) { return 0 <= z1 && z1 <= 18446744073709551615n; } //external fits_nativeint_unsigned: t -> bool //Provides: ml_z_fits_nativeint_unsigned //Requires: ml_z_fits_int32_unsigned function ml_z_fits_nativeint_unsigned(z1) { return ml_z_fits_int32_unsigned(z1); } //external fits_int64: t -> bool //Provides: ml_z_fits_int64 function ml_z_fits_int64(z1) { if (z1 <= 9223372036854775807n && z1 >= -9223372036854775808n) return 1 else return 0 } //external fits_nativeint: t -> bool //Provides: ml_z_fits_nativeint //Requires: ml_z_fits_int function ml_z_fits_nativeint(z1) { return ml_z_fits_int(z1); } //external powm: t -> t -> t -> t //Provides: ml_z_powm //Requires: ml_z_normalize, ml_z_invert, caml_raise_zero_divide //Requires: jsoo_bigint_mod_pow function ml_z_powm(z1, z2, z3) { if (z3 == 0) caml_raise_zero_divide(); if (z3 == 1 || z3 == -1) return 0; if (z2 == 0) return 1; let z3n = BigInt(z3); if (z2 < 0) { let inv = BigInt(ml_z_invert(z1, z3)); let r = jsoo_bigint_mod_pow(inv, BigInt(-z2), z3n); if (r < 0) r = r + (z3n < 0 ? -z3n : z3n); return ml_z_normalize(r); } else { let r = jsoo_bigint_mod_pow(BigInt(z1), BigInt(z2), z3n); if (r < 0) r = r + (z3n < 0 ? -z3n : z3n); return ml_z_normalize(r); } } //external pown: t -> t -> t //Provides: ml_z_pow //Requires: caml_failwith, ml_z_normalize, caml_invalid_argument function ml_z_pow(z1, i1) { if (i1 < 0) { caml_invalid_argument("Z.pow: exponent must be nonnegative"); } return ml_z_normalize(BigInt(z1) ** BigInt(i1)); } //external hash: t -> int //Provides: ml_z_hash const //Requires: caml_hash_mix_int function ml_z_hash(z1) { let z1n = BigInt(z1); let neg = z1n < 0; if (neg) z1n = -z1n; let acc = 0, len = 1; let left = z1n; while (left >= 2 ** 32) { acc = caml_hash_mix_int(acc, Number(left & 0xffffffffn)); left >>= 32n; len += 1; } acc = caml_hash_mix_int(acc, Number(left) | 0); if (len % 2 !== 0) { acc = caml_hash_mix_int(acc, 0); } if (neg) { acc = acc + 1 } return acc | 0 } //external to_bits: t -> string //Provides: ml_z_to_bits const //Requires: caml_string_of_jsbytes, caml_str_repeat function ml_z_to_bits(z1) { let z1n = BigInt(z1); if (z1n < 0) z1n = -z1n; let res = ""; while (z1n !== 0n) { res += String.fromCharCode(Number(z1n % 256n) | 0); z1n /= 256n; } while (res.length % 4 != 0) { res += String.fromCharCode(0); } return caml_string_of_jsbytes(res); } //external of_bits: string -> t //Provides: ml_z_of_bits const //Requires: caml_string_unsafe_get, caml_ml_string_length, ml_z_normalize function ml_z_of_bits(z1) { let r = 0n; let base = 1n; for (let i = 0; i < caml_ml_string_length(z1); i++) { let d = caml_string_unsafe_get(z1, i); r += base * BigInt(d); base *= 256n; } return ml_z_normalize(r); } //external powm_sec: t -> t -> t -> t //Provides: ml_z_powm_sec //Requires: caml_failwith, ml_z_powm, caml_invalid_argument function ml_z_powm_sec(z1, z2, z3) { if (z1 < 0) z3 = -z3; // powm_sec requires that the exponent be positive if (z2 < 1) { caml_invalid_argument("Z.powm_sec: exponent must be positive"); } if ((BigInt(z3) & 1n) !== 1n) { caml_invalid_argument("Z.powm_sec: modulus must be odd"); } return ml_z_powm(z1, z2, z3) } //external root: t -> int -> t //Provides: ml_z_root //Requires: ml_z_pow, ml_z_normalize, caml_invalid_argument function ml_z_root(z, i) { if (i % 2 === 0 && z < 0) { caml_invalid_argument("Z.root: even root of a negative number"); } if (z == 0 || z == 1) { return Number(z) | 0; } let start = 0n; let end = BigInt(z); let ans = 0n; let bigi = BigInt(i); while (start <= end) { let mid = (start + end) / 2n; let po = mid ** bigi; if (po == z) { return ml_z_normalize(mid); } else if (po < z) { start = mid + 1n; ans = mid; } else { end = mid - 1n; } } return ml_z_normalize(ans); } //external rootrem: t -> int -> t * t //Provides: ml_z_rootrem //Requires: ml_z_pow, ml_z_normalize, caml_invalid_argument function ml_z_rootrem(z, i) { if (i % 2 === 0 && z < 0) { caml_invalid_argument("Z.rootrem: even root of a negative number"); } if (z == 0 || z === 1) { return [0, Number(z) | 0, 0]; } let start = 0n; let end = BigInt(z); let ans = 0n; let bigi = BigInt(i); while (start <= end) { let mid = (start + end) / 2n; let po = mid ** bigi; if (po == z) { return [0, ml_z_normalize(mid), 0]; } else if (po < z) { start = mid + 1n; ans = mid; } else { end = mid - 1n; } } return [0, ml_z_normalize(ans), ml_z_normalize(BigInt(z) - (ans ** bigi))]; } //external invert: t -> t -> t //Provides: ml_z_invert //Requires: caml_raise_zero_divide, ml_z_gcdext_intern, ml_z_normalize function ml_z_invert(a, n) { // Because [a.modInv(n)] produces different results for edge cases, // we wrote our own implementation based on gcdext_intern. if (n == 1 || n == -1) return 0; if (n == 0 && (a == 1 || a == -1)) { return a; } if (n == 0 || a == 0) { caml_raise_zero_divide(); } let x = ml_z_gcdext_intern(a, n); let r = BigInt(x[2]); a = BigInt(a); n = BigInt(n); if (n < 0) n = -n; let tmp = (a * r) % n; if (tmp < 0) tmp += n; if (r < 0) r += n; if (tmp == 1) { return ml_z_normalize(r); } caml_raise_zero_divide(); } //external perfect_power: t -> bool //Provides: ml_z_perfect_power //Requires: caml_failwith, ml_z_numbits, ml_z_root, ml_z_pow function ml_z_perfect_power(z) { // Return true if op is a perfect power, i.e., if there exist integers a and // b, with b > 1, such that op = a^b. // Otherwise false. if (z == 0 || z == 1 || z == -1) return 1; let zp = z < 0 ? -z : z; let log2z = ml_z_numbits(zp); for (let b = 2; b <= log2z; b++) { if (z < 0 && b % 2 == 0) continue; let p = ml_z_root(zp, b); if (z < 0) p = -p; let r = ml_z_pow(p, b); if (z == r) { return 1; } } return 0; } //external perfect_square: t -> bool //Provides: ml_z_perfect_square //Requires: ml_z_root function ml_z_perfect_square(z) { if (z < 0) return 0; let root = BigInt(ml_z_root(z, 2)); if (root * root == z) { return 1; } else { return 0 } } //Must have 0 <= low <= high //Provides: jsoo_bigint_rand_between function jsoo_bigint_rand_between(low, high) { let range = high - low + 1n; let base = 1e7, bigbase = BigInt(base); let result = 0n, restricted = true; let digits = []; while (range >= bigbase) { digits.push(Number(range % bigbase)); range /= bigbase; } digits.push(Number(range)); digits.reverse(); for (let i = 0; i < digits.length; i++) { let top = restricted ? digits[i] | 0 : base; range /= bigbase; let digit = Math.floor(Math.random() * top); result *= bigbase; result += BigInt(digit); if (digit < top) restricted = false; } return low + result; } //Provides: jsoo_bigint_mod_pow function jsoo_bigint_mod_pow(self, exp, mod) { if (mod === 0n) throw new Error("Cannot take modPow with modulo 0"); if (exp < 0) throw new Error("Cannot take modPow with negative exponent"); let r = 1n, base = self % mod; while (exp > 0) { if (base === 0n) return 0n; if ((exp & 1n) === 1n) r = (r * base) % mod; exp >>= 1n; base = (base * base) % mod; } return r; } //external probab_prime: t -> int -> int //Provides: ml_z_probab_prime const //Requires: jsoo_bigint_mod_pow, jsoo_bigint_rand_between //Note: called with [1n] from [ml_z_primorial] function ml_z_probab_prime(z, i) { if (z < 0) z = -z; // Test for basic primes (multiples of 2, 3, 5) if (z == 1) return 0; if (z == 2 || z == 3 || z == 5) return 1; let n = BigInt(z); if (n % 2n === 0n || n % 3n === 0n || n % 5n === 0n) return 0; if (z < 49) return 1; // Miller-Rabin test let nPrev = n - 1n, b = nPrev, r = 0, a, d, j, x; while (b % 2n === 0n) b /= 2n, r++; next: for (j = 0; j < i; j++) { a = jsoo_bigint_rand_between(2n, n - 2n); x = jsoo_bigint_mod_pow(a, b, n); if (x === 1n || x === nPrev) continue; for (d = r - 1; d != 0; d--) { x = (x * x) % n; if (x === 1n) return 0; if (x === nPrev) continue next; } return 0; } return 1; } //external nextprime: t -> t //Provides: ml_z_nextprime const //Requires: ml_z_normalize, ml_z_probab_prime function ml_z_nextprime(z1) { // Interestingly, the zarith next_prime only returns // probabalistic primes. We do the same, with the // same probablistic parameter of 25. // https://fossies.org/dox/gmp-6.1.2/mpz_2nextprime_8c_source.html if (z1 < 1 || z1 == 1) { return 2; } let z1n = BigInt(z1) if ((z1n & 1n) === 1n) { z1n += 2n; } else { z1n += 1n; } for (; ;) { if (ml_z_probab_prime(z1n, 25)) { return ml_z_normalize(z1n); } else { z1n += 2n; } } } //external c_extract: t -> int -> int -> t //Provides: ml_z_extract //Requires: caml_failwith, ml_z_normalize function ml_z_extract(z1, pos, len) { return ml_z_normalize((BigInt(z1) >> BigInt(pos)) & ((1n << BigInt(len)) - 1n)); } //external c_extract_small: t -> int -> int -> t //Provides: ml_z_extract_small function ml_z_extract_small(z1, pos, len) { return Number(BigInt.asIntN(32, z1 >> BigInt(pos))) & ((1 << len) - 1); } //external gcdext_intern: t -> t -> (t * t * bool) //Provides: ml_z_gcdext_intern //Requires: caml_raise_zero_divide, ml_z_normalize function ml_z_gcdext_intern(z1, z2) { if (z1 == 0) caml_raise_zero_divide(); let a = BigInt(z1); let b = BigInt(z2); let x = 0n; let lastx = 1n; let y = 1n; let lasty = 1n; let q, t, r; while (b !== 0n) { q = a / b; r = a - q * b; t = x; x = lastx - q * x; lastx = t; t = y; y = lasty - q * y; lasty = t; a = b; b = r; } if (a < 0) return [0, ml_z_normalize(-a), ml_z_normalize(-lastx), 1] else return [0, ml_z_normalize(a), ml_z_normalize(lastx), 1] } //external sqrt: t -> t //Provides: ml_z_sqrt //Requires: ml_z_root, caml_invalid_argument function ml_z_sqrt(z1) { if (z1 < 0) { caml_invalid_argument("Z.sqrt: square root of a negative number"); } return ml_z_root(z1, 2); } //external sqrt_rem: t -> (t * t) //Provides: ml_z_sqrt_rem //Requires: ml_z_root, caml_invalid_argument, ml_z_normalize function ml_z_sqrt_rem(z) { if (z < 0) { caml_invalid_argument("Z.sqrt_rem: square root of a negative number"); } let root = ml_z_root(z, 2); let rootn = BigInt(root); let diff = BigInt(z) - rootn * rootn; return [0, root, ml_z_normalize(diff)] } //external trailing_zeros: t -> int //Provides: ml_z_trailing_zeros const function ml_z_trailing_zeros(z) { if (z == 0) { // max_int in 32bit return 0x7fffffff; } if (z < 0) z = -z; let zn = BigInt(z); let i = 0; zn = (zn ^ (zn - 1n)) >> 1n; for (i = 0; zn !== 0n; i++) zn >>= 1n; return i; } //external popcount: t -> int //Provides: ml_z_popcount //Requires: caml_raise_constant, caml_named_value function ml_z_popcount(z) { if (z < 0) { caml_raise_constant(caml_named_value("ml_z_overflow")); } z = BigInt(z); let i; for (i = 0; z !== 0n; i++) { z &= z - 1n; } if (i !== (i | 0)) caml_raise_constant(caml_named_value("ml_z_overflow")); return i | 0; } //external hamdist: t -> t -> int //Provides: ml_z_hamdist //Requires: ml_z_popcount, caml_invalid_argument, caml_raise_constant //Requires: caml_named_value, ml_z_normalize function ml_z_hamdist(z1, z2) { if ((z1 < 0) !== (z2 < 0)) { caml_raise_constant(caml_named_value("ml_z_overflow")); } if ((typeof z1 == 'bignum' || typeof z2 == 'bignum') && (z1 < 0 || z2 < 0)) { caml_invalid_argument("Z.hamdist: negative arguments"); } return ml_z_popcount(BigInt(z1) ^ BigInt(z2)); } //external size: t -> int //Provides: ml_z_size const function ml_z_size(z1) { // Claim to be a 32-bit architecture. if (z1 < 0) z1 = -z1; let z1n = BigInt(z1); let len = 1; while (z1n >= 2 ** 32) { len += 1; z1n >>= 32n; } return len | 0; } //external divexact: t -> t -> t //Provides: ml_z_divexact //Requires: ml_z_div function ml_z_divexact(z1, z2) { return ml_z_div(z1, z2); } //Provides: caml_zarith_marshal function caml_zarith_marshal(writer, v, sz) { let vn = BigInt(v); let neg = vn < 0; if (neg) vn = -vn; let bits = []; while (vn >= 2 ** 32) { bits.push(Number(vn & 0xffffffffn)); vn >>= 32n; } bits.push(Number(vn) | 0); writer.write(8, neg ? 1 : 0); let block = bits.length; let len = block * 4; writer.write(32, len); for (let i = 0; i < block; i++) { writer.write(8, (bits[i] >>> 0) & 0xff); writer.write(8, (bits[i] >>> 8) & 0xff); writer.write(8, (bits[i] >>> 16) & 0xff); writer.write(8, (bits[i] >>> 24) & 0xff); } sz[0] = 4 * (1 + (((len + 3) / 4) | 0)); sz[1] = 8 * (1 + (((len + 7) / 8) | 0)); } //Provides: caml_zarith_unmarshal //Requires: caml_failwith, ml_z_normalize function caml_zarith_unmarshal(reader, sz) { let negate; switch (reader.read8u()) { case 1: negate = true; break; case 0: negate = false; break; default: caml_failwith("input_value: z (malformed input)"); } let len = reader.read32u(); let x = 0n; for (let i = 0; i < len / 4; i++) { let y = reader.read8u(); y |= reader.read8u() << 8 y |= reader.read8u() << 16 y |= reader.read8u() << 24 x |= BigInt.asUintN(32, BigInt(y)) << (BigInt(i) * 32n); } if (negate) x = -x; sz[0] = len + 4; return ml_z_normalize(x) } //Provides: ml_z_divisible function ml_z_divisible(a, b) { if (a == 0 && b == 0) return 1; if (b == 0) return 0; if (b == 1) return 1; if (b == 2 || b == -2) return +((BigInt(a) & 1n) === 0n); return +((BigInt(a) % BigInt(b)) === 0n); } //Provides: ml_z_congruent //Requires: ml_z_divisible function ml_z_congruent(a, b, c) { return ml_z_divisible(BigInt(a) - BigInt(b), c); } //external remove : t -> t -> t * int //Provides: ml_z_remove //Requires: ml_z_normalize, caml_raise_zero_divide function ml_z_remove(a, b) { if (b == 0) caml_raise_zero_divide(); if (a == 0 || b == 1 || b == -1) return [0, a, 0]; let i = 0; a = BigInt(a); b = BigInt(b); while ((a % b) === 0n) { a /= b; i++; } return [0, ml_z_normalize(a), i]; } //external fac : int -> t //Provides: ml_z_fac //Requires: ml_z_facM, caml_invalid_argument function ml_z_fac(i) { if (i < 0) caml_invalid_argument("Z.fac: non-positive argument"); return ml_z_facM(i, 1); } //external fac2 : int -> t //Provides: ml_z_fac2 //Requires: ml_z_facM, caml_invalid_argument function ml_z_fac2(i) { if (i < 0) caml_invalid_argument("Z.fac2: non-positive argument"); return ml_z_facM(i, 2); } //external facM : int -> int -> t //Provides: ml_z_facM //Requires: caml_invalid_argument, ml_z_normalize function ml_z_facM(i, m) { if (i < 0 || m < 0) caml_invalid_argument("Z.facM: non-positive argument"); if (m == 0) return i; let mn = BigInt(m); let current = BigInt(i); let res = 1n; while (current > 0) { res *= current; current -= mn; } return ml_z_normalize(res); } //external fib : int -> t //Provides: ml_z_fib //Requires: caml_invalid_argument, ml_z_normalize function ml_z_fib(i) { if (i < 0) caml_invalid_argument("Z.fib: negative arguments"); if (i == 0 || i == 1) return i; let a = 0n, b = 1n; for (let k = 1; k < i; k++) { let b2 = b; b += a; a = b2; } return ml_z_normalize(b); } //external lucnum : int -> t //Provides: ml_z_lucnum //Requires: caml_invalid_argument, ml_z_normalize function ml_z_lucnum(i) { if (i < 0) caml_invalid_argument("Z.lucnum: negative arguments"); if (i == 0) return 2; if (i == 1) return 1; let a = 2n, b = 1n; for (let k = 1; k < i; k++) { let b2 = b; b += a; a = b2; } return ml_z_normalize(b); } //Provides: ml_z_jacobi //Requires: caml_invalid_argument function ml_z_jacobi(n, k) { let nn = BigInt(n); let kn = BigInt(k); //assert(k > 0 and k % 2 == 1) if (kn <= 0 || kn % 2n !== 1n) caml_invalid_argument("Z.jacobi: second argument is negative or even"); nn = nn % kn; if (nn < 0) nn += kn; let t = 1; while (nn !== 0n) { while (nn % 2n === 0n) { nn /= 2n; let r = kn % 8n if (r === 3n || r === 5n) { t = -t } } let n1 = nn, k1 = kn; nn = k1; kn = n1; if (nn % 4n === 3n && kn % 4n === 3n) { t = -t } nn = nn % kn } if (kn === 1n) return t else return 0 } //Provides: ml_z_legendre //Requires: ml_z_jacobi function ml_z_legendre(a, b) { return ml_z_jacobi(a, b); } //Provides: ml_z_kronecker //Requires: caml_failwith function ml_z_kronecker(n, k) { caml_failwith("ml_z_kronecker is not implemented"); } //Provides: ml_z_primorial //Requires: ml_z_probab_prime, ml_z_normalize, caml_invalid_argument function ml_z_primorial(a) { if (a < 0) caml_invalid_argument("Z.primorial: non-positive argument"); let z1 = 1n; let res = 1n; while (z1 <= a) { if (ml_z_probab_prime(z1, 25)) { res *= z1; } if (z1 === 1n || z1 === 2n) z1 += 1n; else z1 += 2n; } return ml_z_normalize(res); } //Provides: ml_z_bin //Requires: ml_z_normalize, caml_invalid_argument function ml_z_bin(n, k) { if (k < 0) caml_invalid_argument("Z.bin: non-positive argument"); n = BigInt(n); k = BigInt(k); let coeff = 1n; for (let x = n - k + 1n; x <= n; x++) coeff *= x; for (let x = 1n; x <= k; x++) coeff /= x; return ml_z_normalize(coeff); } zarith_stubs_js-0.17.0/test/000077500000000000000000000000001461647336100160065ustar00rootroot00000000000000zarith_stubs_js-0.17.0/test/arithmetic.ml000066400000000000000000000142531461647336100204760ustar00rootroot00000000000000open! Core open! Import module Ml_z_neg = struct let%test_unit "neg neg x = x" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] x (neg (neg x))) ;; let%test_unit "neg x = 0 - x" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (zero - x) (neg x)) ;; let%expect_test "print (x, neg x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (neg x : t)]) (); [%expect "((hash 7b0a8e898b48b6e4ef3a08b8879fc3a9) (uniqueness_rate 85.742188))"] ;; end module Ml_z_add = struct let add_test_helper a b = of_string a + of_string b |> print let%expect_test "small_integer_addition" = add_test_helper "1" "2"; [%expect "3"] ;; let%expect_test "addition_with_a_negative" = add_test_helper "1" "-2"; [%expect "-1"] ;; let%expect_test "addition_with_0" = add_test_helper "1" "0"; [%expect "1"]; add_test_helper "0" "1"; [%expect "1"] ;; let%test_unit "a + 0 = a" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a + zero) a) ;; let%test_unit "a + b = b + a" = Dynamic.quickcheck_pair () ~f:(fun a b -> [%test_eq: t] (a + b) (b + a)) ;; let%test_unit "(a + b) + c = a + (b + c)" = Dynamic.quickcheck_tripple () ~f:(fun a b c -> [%test_eq: t] (a + b + c) (a + (b + c))) ;; let%test_unit "a + -a = 0" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a + neg a) zero) ;; let%expect_test "print (x, y, x + y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x + y : t)]) (); [%expect "((hash 9763ccb282897020828d44e81572fd92) (uniqueness_rate 96.529814))"] ;; end module Ml_z_sub = struct let%test_unit "a - b = - (b - a)" = Dynamic.quickcheck_pair () ~f:(fun a b -> [%test_eq: t] (a - b) (neg (b - a))) ;; let%test_unit "a - a = 0" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a - a) zero) ;; let%test_unit "a - (neg a) = a + a" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a - neg a) (a + a)) ;; let%expect_test "print (x, y, x - y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x - y : t)]) (); [%expect "((hash d2c310975ededc4cc2125e5f2f04f269) (uniqueness_rate 96.529814))"] ;; end module Ml_z_mul = struct let%test_unit "a * 0 = 0" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a * zero) zero) ;; let%test_unit "a * 2 = a + a" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a * of_int 2) (a + a)) ;; let%test_unit "a * b = b * a" = Dynamic.quickcheck_pair () ~f:(fun a b -> [%test_eq: t] (a * b) (b * a)) ;; let%test_unit "(a * b) * c = a * (b * c)" = Dynamic.quickcheck_tripple () ~f:(fun a b c -> [%test_eq: t] (a * b * c) (a * (b * c))) ;; let%test_unit "a * -a = neg (a * a)" = Dynamic.quickcheck () ~f:(fun a -> [%test_eq: t] (a * neg a) (neg (a * a))) ;; let%expect_test "print (x, y, x * y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x * y : t)]) (); [%expect "((hash 39c63570aefd9c9c0285c15dc3d5970e) (uniqueness_rate 96.529814))"] ;; end module Ml_z_succ = struct let%expect_test "succ 1 = 2" = print (succ (of_string "1")); [%expect "2"] ;; let%expect_test "succ 0 = 1" = print (succ (of_string "0")); [%expect "1"] ;; let%expect_test "succ 23092345681102982356239087190819801239109812287639021 = \ 23092345681102982356239087190819801239109812287639022" = print (succ (of_string "23092345681102982356239087190819801239109812287639021")); [%expect "23092345681102982356239087190819801239109812287639022"] ;; let%expect_test "succ -1 = 0" = print (succ (of_string "-1")); [%expect "0"] ;; let%test_unit "succ x = x + 1" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (succ x) (x + of_string "1")) ;; let%test_unit "succ x > x" = Dynamic.quickcheck () ~f:(fun x -> assert (succ x > x)) let%expect_test "print (x, succ x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (succ x : t)]) (); [%expect "((hash 3b86d6cc93b6c79a143979a4fa00a7da) (uniqueness_rate 85.742188))"] ;; end module Ml_z_pred = struct let%expect_test "pred 2 = 1" = print (pred (of_string "2")); [%expect "1"] ;; let%expect_test "pred 1 = 0" = print (pred (of_string "1")); [%expect "0"] ;; let%expect_test "succ 23092345681102982356239087190819801239109812287639021 = \ 23092345681102982356239087190819801239109812287639020" = print (pred (of_string "23092345681102982356239087190819801239109812287639021")); [%expect "23092345681102982356239087190819801239109812287639020"] ;; let%expect_test "pred -1 = -2" = print (pred (of_string "-1")); [%expect "-2"] ;; let%test_unit "pred x = x - 1" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (pred x) (x - of_string "1")) ;; let%test_unit "succ x < x" = Dynamic.quickcheck () ~f:(fun x -> assert (pred x < x)) let%test_unit "x |> succ |> pred = x" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (x |> succ |> pred) x) ;; let%test_unit "x |> pred |> succ = x" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (x |> pred |> succ) x) ;; let%expect_test "print (x, pred x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (pred x : t)]) (); [%expect "((hash 9f99db74aa88e98daa9e3a8c3d2bf45b) (uniqueness_rate 85.742188))"] ;; end module Ml_z_abs = struct let%expect_test "abs 0 = 0" = print (abs (of_string "0")); [%expect "0"] ;; let%expect_test "abs 1 = 1" = print (abs (of_string "1")); [%expect "1"] ;; let%expect_test "abs -1 = 1" = print (abs (of_string "-1")); [%expect "1"] ;; let%expect_test "abs -2 = 2" = print (abs (of_string "-2")); [%expect "2"] ;; let%test_unit "abs x >= 0" = Dynamic.quickcheck () ~f:(fun x -> assert (geq (abs x) zero)) ;; let%test_unit "abs x = -1 * x when x < 0" = Dynamic.quickcheck () ~f:(fun x -> if x < of_string "0" then [%test_eq: t] (abs x) (x * of_string "-1") else [%test_eq: t] (abs x) x) ;; let%expect_test "print (x, abs x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (abs x : t)]) (); [%expect "((hash fd1abbf43e359f12fcd47315f3167c8d) (uniqueness_rate 85.742188))"] ;; end zarith_stubs_js-0.17.0/test/arithmetic.mli000066400000000000000000000000511461647336100206360ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/base.ml000066400000000000000000000027531461647336100172610ustar00rootroot00000000000000open! Core open! Import module Ml_z_compare = struct let%expect_test "print (a, b, compare a b)" = Static.quickcheck_pair ~f:(fun a b -> [%message (a : t) (b : t) (compare a b : int)]) (); [%expect "((hash 1f5ae6577a1ebc7c342fdcf1dde524af) (uniqueness_rate 96.529814))"] ;; end module Ml_z_equal = struct let%test_unit "a = a" = Dynamic.quickcheck () ~f:(fun x -> assert (Z.equal x x)) let%test_unit "succ a != a" = Dynamic.quickcheck () ~f:(fun x -> assert (not (Z.equal x (succ x)))) ;; let%test _ = Bool.equal (Z.equal Z.one Z.one) (Int.equal 1 1) let%test _ = Bool.equal (Z.equal Z.one Z.zero) (Int.equal 1 0) end module Ml_z_hash = struct (* We don't expect hash to be the same in javascript and native 64bit *) let ignore_the_actual_hash_value output = match Sexp.of_string output with | Sexp.List (Sexp.List (Sexp.Atom "hash" :: _) :: rest) -> print_s (Sexp.List rest) | output -> raise_s [%message "unexpected output" (output : Sexp.t)] ;; let%expect_test ("print x, hash x (no-js)" [@tags "64-bits-only"]) = Static.quickcheck ~f:(fun x -> [%message (x : t) (hash x : int)]) (); ignore_the_actual_hash_value [%expect.output]; [%expect {| ((uniqueness_rate 85.742188)) |}] ;; let%expect_test ("print x, hash x (js-only)" [@tags "js-only"]) = Static.quickcheck ~f:(fun x -> [%message (x : t) (hash x : int)]) (); ignore_the_actual_hash_value [%expect.output]; [%expect {| ((uniqueness_rate 85.742188)) |}] ;; end zarith_stubs_js-0.17.0/test/base.mli000066400000000000000000000000511461647336100174170ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/bitwise.ml000066400000000000000000000101551461647336100200100ustar00rootroot00000000000000open! Core open! Import module Ml_z_logand = struct let%expect_test "print (x, y, x logand y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (Z.logand x y : t)]) (); [%expect "((hash edb3907ae89e9a054a3a3468ec4fa41e) (uniqueness_rate 96.529814))"] ;; end module Ml_z_logor = struct let%expect_test "print (x, y, x logor y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (Z.logor x y : t)]) (); [%expect "((hash 924dc08ed08d44cc6daa895e9cb15c1c) (uniqueness_rate 96.529814))"] ;; end module Ml_z_logxor = struct let%expect_test "print (x, y, x logxor y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (Z.logxor x y : t)]) (); [%expect "((hash b11b218de81f69b7985e08f02df67b1f) (uniqueness_rate 96.529814))"] ;; end module Ml_z_lognot = struct let%expect_test "print (x, lognot x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (Z.lognot x : t)]) (); [%expect "((hash b131739e5dd70cb57dd7a7250e4e4b1f) (uniqueness_rate 85.742188))"] ;; end module Ml_z_shift_left = struct let%expect_test "print x << y" = Static.quickcheck ~f:(fun x -> List.range 0 100 |> List.map ~f:(fun y -> [%message (x : t) (y : int) (Z.shift_left x y : t)]) |> Sexp.List) (); [%expect {| ((hash ec0fcbe27e42ce2098a5644515678069) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_shift_right = struct let%expect_test "print x >> y" = Static.quickcheck ~f:(fun x -> List.range 0 100 |> List.map ~f:(fun y -> [%message (x : t) (y : int) (Z.shift_right x y : t)]) |> Sexp.List) (); [%expect {| ((hash f7642afef5845a19faa1e2a1ff320b84) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_shift_right_trunc = struct let%expect_test "print x >> y" = Static.quickcheck ~f:(fun x -> List.range 0 100 |> List.map ~f:(fun y -> [%message (x : t) (y : int) (Z.shift_right_trunc x y : t)]) |> Sexp.List) (); [%expect {| ((hash 798c8f514381c4521966e3dd3ea93c1d) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_testbit = struct let%expect_test "testbit" = Static.quickcheck ~f:(fun x -> Sexp.List (List.init 100 ~f:(fun i -> [%message (x : t) (i : int) (Z.testbit x i : bool)]))) (); [%expect {| ((hash a0f6bad9471e135b64764ab691a06b02) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_popcount = struct let%expect_test "print x, popcnt x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (popcount x : int)]) (); (* Compression rate is low because our quickcheck implementation generates integers with a bounded bitcount. *) [%expect {| ((hash 1e429706c701b111d98b6e6e858bbea4) (uniqueness_rate 42.96875)) |}] ;; end module Ml_z_hamdist = struct let%expect_test "print x, y, hamdist x y" = let fits_int31 x = fits_int32 (Z.shift_left x 1) in Static.quickcheck_pair ~f:(fun x y -> (* hamdist handle negative argument weirdly *) if geq x zero || geq y zero || (fits_int31 x && fits_int31 y) then [%message (x : t) (y : t) (hamdist x y : int)] else [%message "special"]) (); (* Compression rate is low because our quickcheck implementation generates integers with a bounded bitcount. *) [%expect {| ((hash 0a270232628736ee7d47c8b403250989) (uniqueness_rate 33.284457)) |}] ;; end module Ml_z_extract = struct let%expect_test "extract" = let test a o l = let result = extract (of_int a) o l in print_s [%message (a : int) (o : int) (l : int) (result : t)] in test 0b0101 1 1; [%expect {| ((a 5) (o 1) (l 1) (result 0)) |}]; test 0b0101 1 2; [%expect {| ((a 5) (o 1) (l 2) (result 2)) |}]; test 0b1101 1 3; [%expect {| ((a 13) (o 1) (l 3) (result 6)) |}]; test (-4) 1 3; [%expect {| ((a -4) (o 1) (l 3) (result 6)) |}] ;; let%expect_test "print x, extract x 2 8" = Static.quickcheck ~f:(fun x -> [%message (x : t) (extract x 2 8 : t)]) (); [%expect {| ((hash 20b6f322ff762ded3d569d57b7f18135) (uniqueness_rate 85.742188)) |}] ;; end zarith_stubs_js-0.17.0/test/bitwise.mli000066400000000000000000000000511461647336100201530ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/conversions.ml000066400000000000000000000163411461647336100207150ustar00rootroot00000000000000open! Core open! Import module Ml__z_of_int = struct let%test "a = a |> z_of_int |> int_of_z" = Core.Quickcheck.test Int.quickcheck_generator ~f:(fun i -> assert (Int.equal i (i |> Z.of_int_exn |> Z.to_int_exn))); true ;; end module Ml_z_of_nativeint = struct let%test "a = a |> z_of_nativeint |> int_of_z" = Core.Quickcheck.test Nativeint.quickcheck_generator ~f:(fun i -> assert (Nativeint.equal i (i |> Z.of_nativeint_exn |> Z.to_nativeint_exn))); true ;; end module Ml_z_of_int32 = struct let%test "a = a |> z_of_int32 |> int32_of_z" = Core.Quickcheck.test Int32.quickcheck_generator ~f:(fun i -> assert (Int32.equal i (i |> Z.of_int32_exn |> Z.to_int32_exn))); true ;; end module Ml_z_of_int64 = struct let max = Z.of_string "9223372036854775807" let min = Z.of_string "-9223372036854775808" let%expect_test _ = Z.to_int64 max |> [%sexp_of: Int64.t option] |> print_s; [%expect {| (9223372036854775807) |}]; Z.to_int64 (Z.add max Z.one) |> [%sexp_of: Int64.t option] |> print_s; [%expect {| () |}]; Z.to_int64 min |> [%sexp_of: Int64.t option] |> print_s; [%expect {| (-9223372036854775808) |}]; Z.to_int64 (Z.sub min Z.one) |> [%sexp_of: Int64.t option] |> print_s; [%expect {| () |}] ;; let%test "a = a |> z_of_int_64 |> int64_of_z" = Core.Quickcheck.test Int64.quickcheck_generator ~f:(fun i -> assert (Int64.equal i (i |> Z.of_int64_exn |> Z.to_int64_exn))); true ;; end module Ml_z_to_int = struct let%test "i = i |> z_of_int |> z_to_int" = Core.Quickcheck.test Int.quickcheck_generator ~f:(fun x -> assert (Int.equal x (x |> Z.of_int |> Z.to_int_exn))); true ;; let%test "if fitsint i then i = i |> to_z |> of_z" = Static.quickcheck ~quiet:true ~f:(fun x -> if Z.fits_int x then assert (Z.equal x (x |> Z.to_int_exn |> Z.of_int)) else (); Sexp.Atom "") (); true ;; end module Ml_z_to_int32 = struct let%expect_test "print (Z.to_int_32 x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (Z.to_int32 x : int32 option)]) (); [%expect {| ((hash 2b8919d2f03f90fd1928acdea3fe9049) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_to_int64 = struct let%expect_test "print (Z.to_int_64 x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (Z.to_int64 x : int64 option)]) (); [%expect {| ((hash 2d4c79adf4965bbbcd2bda4c83610b41) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_to_nativeint = struct let%test "i = i |> z_of_int |> z_to_int" = Core.Quickcheck.test Nativeint.quickcheck_generator ~f:(fun x -> assert (Nativeint.equal x (x |> Z.of_nativeint |> Z.to_nativeint_exn))); true ;; let%test "if fitsint i then i = i |> to_z |> of_z" = Static.quickcheck ~quiet:true ~f:(fun x -> if Z.fits_nativeint x then assert (Z.equal x (x |> Z.to_nativeint_exn |> Z.of_nativeint)) else (); Sexp.Atom "") (); true ;; end module To_float = struct let%expect_test "print (Z.to_float x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (Z.to_float x : float)]) (); [%expect {| ((hash f9142dbb58637f3d8a7f34d5164e2fef) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_fits_int = struct let fits_test_helper a = Z.fits_int (Z.of_string a) |> printf "%b" let%expect_test "fits_int" = fits_test_helper "1"; [%expect "true"] ;; (* 'int' in js_of_ocaml is 32 bits, so the fits_int32 tests are sufficient. *) end module Ml_z_fits_int32 = struct let fits_test_helper a = Z.fits_int32 (Z.of_string a) |> printf "%b" let%expect_test "fits_int" = fits_test_helper "1"; [%expect "true"] ;; let%expect_test "print x, fits_int32 x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (fits_int32 x : bool)]) (); [%expect "((hash db765bf400e9c6d1e2b418f6891e51a8) (uniqueness_rate 85.742188))"] ;; end module Ml_z_fits_int64 = struct let fits_test_helper a = Z.fits_int64 (Z.of_string a) |> printf "%b" let%expect_test "fits_int" = fits_test_helper "1"; [%expect "true"]; fits_test_helper "9223372036854775807"; [%expect "true"]; fits_test_helper "9223372036854775808"; [%expect "false"]; fits_test_helper "-9223372036854775808"; [%expect "true"]; fits_test_helper "-9223372036854775809"; [%expect "false"] ;; let%expect_test "print x, fits_int64 x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (fits_int64 x : bool)]) (); [%expect "((hash 6e86cc7603c6284295d7ac5111d575d7) (uniqueness_rate 85.742188))"] ;; end module Ml_z_fits_nativeint = struct let fits_test_helper a = Z.fits_nativeint (Z.of_string a) |> printf "%b" let%expect_test "fits_int" = fits_test_helper "1"; [%expect "true"] ;; (* 'nativeint' in js_of_ocaml is 32 bits, so the fits_int32 tests are sufficient. *) end module Ml_z_of_float = struct let%expect_test "of_float" = let of_float_test_helper a = Z.of_float a |> Z.print in of_float_test_helper 3.3; [%expect "3"]; of_float_test_helper (-3.3); [%expect "-3"] ;; let%expect_test "float conversions" = Core.Quickcheck.test Core.Float.quickcheck_generator ~f:(fun f -> if Float.is_finite f then [%test_eq: float] (f |> Float.round_towards_zero) (f |> Z.of_float |> Z.to_float) else ( try let (_ : t) = Z.of_float f in failwith "failed to fail" with | Overflow -> () | _ -> failwith "failed unexpectedly")) ;; let print_or_print_exn x = try Z.of_float x |> Z.to_string |> print_endline with | Overflow -> print_endline "overflow" ;; let%expect_test "of_float nan" = print_or_print_exn Float.nan; [%expect {| overflow |}] ;; let%expect_test "of_float infinity" = print_or_print_exn Float.infinity; [%expect {| overflow |}]; print_or_print_exn Float.neg_infinity; [%expect {| overflow |}] ;; let%expect_test "of_float x" = print_or_print_exn 1.0; [%expect {| 1 |}]; print_or_print_exn 1.01; [%expect {| 1 |}]; print_or_print_exn 1.99; [%expect {| 1 |}]; print_or_print_exn (-1.0); [%expect {| -1 |}]; print_or_print_exn (-1.01); [%expect {| -1 |}]; print_or_print_exn (-1.99); [%expect {| -1 |}]; print_or_print_exn 1e30; [%expect {| 1000000000000000019884624838656 |}]; print_or_print_exn 1e308; [%expect {| 100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336 |}]; print_or_print_exn 1e309; [%expect {| overflow |}]; print_or_print_exn (-1e30); [%expect {| -1000000000000000019884624838656 |}]; print_or_print_exn (-1e308); [%expect {| -100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336 |}]; print_or_print_exn (-1e309); [%expect {| overflow |}] ;; end zarith_stubs_js-0.17.0/test/conversions.mli000066400000000000000000000000511461647336100210550ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/division.ml000066400000000000000000000162511461647336100201710ustar00rootroot00000000000000open! Core open! Import module Ml_z_div = struct let%test_unit "a / a = 1" = Dynamic.quickcheck () ~include_zero:false ~f:(fun a -> [%test_eq: t] (a / a) one) ;; let%test_unit "a / 0 = throws" = Dynamic.quickcheck () ~f:(fun a -> try let (_ : Z.t) = a / zero in failwith "did not throw" with | Division_by_zero -> ()) ;; let%expect_test "-844387025997697699501 / -588" = Z.print (Z.of_string "-844387025997697699501" / Z.of_string "-588"); [%expect "1436032357138941665"] ;; let%test_unit "a / b = 1 / (b / a)" = Dynamic.quickcheck_pair () ~include_zero:false ~f:(fun a b -> let div1 = a / b in let div2 = b / a in if equal div1 zero || equal div2 zero then () else [%test_eq: t] div1 (one / div2)) ;; let%test_unit "(a * b) / b = a" = Dynamic.quickcheck_pair () ~include_zero:false ~f:(fun a b -> [%test_eq: t] (a * b / b) a) ;; let%expect_test "print (x, y, x / y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x / y : t)]) (); [%expect "((hash 9d89273bc7184f22fd83e7308beb398b) (uniqueness_rate 88.416422))"] ;; end module Ml_z_cdiv = struct (* cdiv rounds towards positive infinity. *) let%expect_test "5 /> 3" = Z.print (Z.of_string "5" /> Z.of_string "3"); [%expect "2"] ;; let%expect_test "-5 /> 3" = Z.print (Z.of_string "-5" /> Z.of_string "3"); [%expect "-1"] ;; let%expect_test "5 /> -3" = Z.print (Z.of_string "5" /> Z.of_string "-3"); [%expect "-1"] ;; let%expect_test "4 /> 4" = Z.print (Z.of_string "4" /> Z.of_string "4"); [%expect "1"] ;; let%expect_test "1 /> 1" = Z.print (Z.of_string "1" /> Z.of_string "1"); [%expect "1"] ;; let%expect_test "-1 /> 1" = Z.print (Z.of_string "-1" /> Z.of_string "1"); [%expect "-1"] ;; let%expect_test "-844387025997697699501 /> -588" = Z.print (Z.of_string "-844387025997697699501" /> Z.of_string "-588"); [%expect "1436032357138941666"] ;; let%expect_test "1 /> -1" = Z.print (Z.of_string "1" /> Z.of_string "-1"); [%expect "-1"] ;; let%test_unit "(a * b) /> b = a" = Dynamic.quickcheck_pair () ~include_zero:false ~f:(fun a b -> assert (a /> b >= a / b)) ;; let%expect_test "print (x, y, x /> y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x /> y : t)]) (); [%expect "((hash 98fa01b1831c668a6a76933f11ceb54b) (uniqueness_rate 88.416422))"] ;; end module Ml_z_fdiv = struct (* Fdiv rounds towards negative infinity *) let%expect_test "5 /< 3" = Z.print (Z.of_string "5" /< Z.of_string "3"); [%expect "1"] ;; let%expect_test "-5 /< 3" = Z.print (Z.of_string "-5" /< Z.of_string "3"); [%expect "-2"] ;; let%expect_test "5 /< -3" = Z.print (Z.of_string "5" /< Z.of_string "-3"); [%expect "-2"] ;; let%expect_test "4 /< 4" = Z.print (Z.of_string "4" /< Z.of_string "4"); [%expect "1"] ;; let%expect_test "1 /< 1" = Z.print (Z.of_string "1" /< Z.of_string "1"); [%expect "1"] ;; let%expect_test "-1 /< 1" = Z.print (Z.of_string "-1" /< Z.of_string "1"); [%expect "-1"] ;; let%expect_test "-844387025997697699501 /< -588" = Z.print (Z.of_string "-844387025997697699501" /< Z.of_string "-588"); [%expect "1436032357138941665"] ;; let%expect_test "1 /< -1" = Z.print (Z.of_string "1" /< Z.of_string "-1"); [%expect "-1"] ;; let%expect_test "print (x, y, x /< y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (x /< y : t)]) (); [%expect "((hash d77a9337f3926e4659a3d7e4e9e3f196) (uniqueness_rate 88.416422))"] ;; end module Ml_z_rem = struct let%expect_test "print (x, y, rem x y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (Z.rem x y : t)]) (); [%expect "((hash a6d99e4b3b3bd7f59e7954dd820f5b04) (uniqueness_rate 88.416422))"] ;; end module Ml_z_div_rem = struct let%expect_test "print (x, y, divrem x y)" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (Z.div_rem x y : t * t)]) (); [%expect "((hash 237975ffcd477be34a47b0a26c28593e) (uniqueness_rate 88.416422))"] ;; end module Ml_z_divexact = struct (* divexact divides the integers correctly iff the division is exact. *) let%expect_test "print x, y, divexact x y" = Static.quickcheck_pair ~f:(fun x y -> let prod = x * y in Sexp.List [ [%message (x : t) (y : t) (prod : t) (divexact prod x : t)] ; [%message (x : t) (y : t) (prod : t) (divexact prod y : t)] ]) (); [%expect {| ((hash 01098278b1918be5cc873b9061bd3bd5) (uniqueness_rate 80.30303)) |}] ;; end module Ml_z_divisible = struct let%expect_test "print x, y, divisible x y" = Static.quickcheck_pair ~f:(fun x y -> Sexp.List [ [%message (x : t) (y : t) (divisible x y : bool)] ; [%message (x : t) (y : t) (divisible y x : bool)] ]) (); [%expect {| ((hash 6505140a50f6be8c19d753b5bc9d0078) (uniqueness_rate 96.529814)) |}] ;; end module Ml_z_congruent = struct let%expect_test "congruent(_,_,0)" = printf "%b\n" Zarith.(Z.congruent Z.zero Z.zero Z.zero); printf "%b\n" Zarith.(Z.congruent (Z.of_int 1) (Z.of_int 1) Z.zero); printf "%b\n" Zarith.(Z.congruent (Z.of_int 2) (Z.of_int 4) Z.zero); [%expect {| true true false |}] ;; let%expect_test "print x, y, z, congruent x y z" = Static.quickcheck_tripple ~f:(fun x y z -> Sexp.List [ [%message (x : t) (y : t) (z : t) (congruent x y z : bool)] ; [%message (x : t) (y : t) (z : t) (congruent x z y : bool)] ; [%message (x : t) (y : t) (z : t) (congruent z y x : bool)] ]) (); [%expect {| ((hash f7b829847eeeab5c6bada69e8ec6905d) (uniqueness_rate 96.934116)) |}] ;; end module Ml_z_remove = struct let%expect_test "remove" = let test a b = let open Zarith.Z in print_s [%message (a : t) (b : t) (remove a b : t * int)] in test (Z.of_int 0) (Z.of_int 1); test (Z.of_int 0) (Z.of_int 10); test (Z.of_int 1) (Z.of_int 1); test (Z.of_int 10) (Z.of_int 1); test (Z.of_int 1) (Z.of_int (-1)); test (Z.of_int 10) (Z.of_int (-1)); test (Z.of_int 2) (Z.of_int 4); test (Z.of_int 4) (Z.of_int 2); [%expect {| ((a 0) (b 1) ("remove a b" (0 0))) ((a 0) (b 10) ("remove a b" (0 0))) ((a 1) (b 1) ("remove a b" (1 0))) ((a 10) (b 1) ("remove a b" (10 0))) ((a 1) (b -1) ("remove a b" (1 0))) ((a 10) (b -1) ("remove a b" (10 0))) ((a 2) (b 4) ("remove a b" (2 0))) ((a 4) (b 2) ("remove a b" (1 2))) |}] ;; let%expect_test "print x, y, remove x y" = Static.quickcheck_pair ~f:(fun x y -> Sexp.List (List.filter_opt [ (if Z.equal Z.zero y then None else Some [%message (x : t) (y : t) (remove x y : t * int)]) ; (if Z.equal Z.zero x then None else Some [%message (x : t) (y : t) (remove y x : t * int)]) ])) (); [%expect {| ((hash 31785be785de93844f221901a25a0b5f) (uniqueness_rate 96.529814)) |}] ;; end zarith_stubs_js-0.17.0/test/division.mli000066400000000000000000000000511461647336100203310ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/dune000066400000000000000000000023121461647336100166620ustar00rootroot00000000000000(library (name zarith_stubs_js_test) (libraries zarith core base.md5 zarith_stubs_js) (flags :standard -w -60) (preprocess (pps ppx_jane))) (rule (targets zarith_externals.txt) (deps ../../../external/zarith/src/z.ml) (action (bash "(cat %{deps} | grep -Eo '^external[^=]*= *\"[^\"]*' | grep -v \"identity\" | grep -v \"obj_is_int\" | sed 's/.*\"ml_z_//' | sort -u) > %{targets}"))) (rule (targets implemented_externals.txt) (deps ../src/runtime.js) (action (bash "(cat ../src/runtime.js | grep -o '//Provides: ml_z[^ ]*' | sed 's/\\/\\/Provides: ml_z_//' | grep -v 'fits_.*_unsigned' | grep -v 'to.*int.*unsigned' | grep -v normalize | grep -v extract_small | sort) > %{targets}"))) (rule (targets tested_externals.txt) (deps (glob_files *.ml)) (action (bash "(grep -E 'module Ml_z_[^ ]+' -o *.ml | cut -f 2 -d ' ' | sed 's/Ml_z_//' | sort) > %{targets}"))) (alias (name DEFAULT) (deps tested_externals.txt implemented_externals.txt zarith_externals.txt)) (rule (alias runtest) (deps implemented_externals.txt tested_externals.txt) (action (bash "diff %{deps}"))) (rule (alias runtest) (deps implemented_externals.txt zarith_externals.txt) (action (bash "diff %{deps}"))) zarith_stubs_js-0.17.0/test/exponential.ml000066400000000000000000000221631461647336100206720ustar00rootroot00000000000000open! Core open! Import module Ml_z_powm = struct let%expect_test "print x y z, powm x y z" = Static.quickcheck_tripple ~f:(fun x y z -> [%message (x : t) (y : t) (z : t) (powm x y z : t)]) (); [%expect {| ((hash 9d5d2749d3ebde755be8b18a7420c365) (uniqueness_rate 71.705806)) |}] ;; end module Ml_z_root = struct let root_helper n s = Z.root (Z.of_string s) n |> Z.print let%expect_test "root 2 -1 = invalid_argument" = (try root_helper 2 "-1" with | exn -> Exn.sexp_of_t exn |> print_s); [%expect {| (Invalid_argument "Z.root: even root of a negative number") |}] ;; let%expect_test "root 2 0 = 0" = root_helper 2 "0"; [%expect "0"] ;; let%expect_test "root 2 1 = 1" = root_helper 2 "1"; [%expect "1"] ;; let%expect_test "root 2 2 = 1" = root_helper 2 "2"; [%expect "1"] ;; let%expect_test "root 2 4 = 2" = root_helper 2 "4"; [%expect "2"] ;; let%expect_test "print i, x, (root x i)" = Static.quickcheck ~f:(fun x -> List.range 2 16 |> List.map ~f:(fun i -> [%message (x : t) (root x i : t)]) |> Sexp.List) (); [%expect "((hash b2af96bdd453c921ceb81ae36c8d8dcd) (uniqueness_rate 42.96875))"] ;; end module Ml_z_rootrem = struct let rootrem_helper n s = Z.rootrem (Z.of_string s) n |> fun (a, b) -> Z.print a; print_string " - "; Z.print b ;; let%expect_test "rootrem 2 -1 = invalid_argument" = (try rootrem_helper 2 "-1" with | exn -> Exn.sexp_of_t exn |> print_s); [%expect {| (Invalid_argument "Z.rootrem: even root of a negative number") |}] ;; let%expect_test "rootrem 2 0 = 0" = rootrem_helper 2 "0"; [%expect "0 - 0"] ;; let%expect_test "rootrem 2 1 = 1" = rootrem_helper 2 "1"; [%expect "1 - 0"] ;; let%expect_test "rootrem 2 2 = 1" = rootrem_helper 2 "2"; [%expect "1 - 1"] ;; let%expect_test "rootrem 2 4 = 2" = rootrem_helper 2 "4"; [%expect "2 - 0"] ;; let%expect_test "rootrem 2 4 = 2" = rootrem_helper 2 "8"; [%expect "2 - 4"] ;; let%expect_test "print i, x, (rootrem x i)" = Static.quickcheck ~f:(fun x -> List.range 2 16 |> List.map ~f:(fun i -> [%message (x : t) (rootrem x i : t * t)]) |> Sexp.List) (); [%expect {| ((hash 202e15549663f9b0e3305f9c6af33961) (uniqueness_rate 42.96875)) |}] ;; end module Ml_z_perfect_square = struct let%expect_test "print x, perfect_square x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (perfect_square x : bool)]) (); [%expect "((hash 8a842a620afa024c8415a5c82a2b3652) (uniqueness_rate 85.742188))"] ;; end module Ml_z_sqrt = struct let%expect_test "print x, sqrt x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (sqrt x : t)]) (); (* Low comppression rate because there are so many "no sqrt of negative number" errors. *) [%expect {| ((hash 9cd6d29511aaaa02fb96ce90dc2bb0d8) (uniqueness_rate 42.96875)) |}] ;; end module Ml_z_sqrt_rem = struct let%expect_test "print x, sqrt x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (sqrt_rem x : t * t)]) (); (* Low comppression rate because there are so many "no sqrt of negative number" errors. *) [%expect {| ((hash 9c54b9ca1f1401e7eb5b8dca2d80e8fd) (uniqueness_rate 42.96875)) |}] ;; end module Ml_z_powm_sec = struct let%expect_test "print x y z, powm_sec x y z" = Static.quickcheck_tripple ~f:(fun x y z -> [%message (x : t) (y : t) (z : t) (powm_sec x y z : t)]) (); (* Low comppression rate because there are so many "modulus must be odd" and "exponent must be positive" errors. *) [%expect {| ((hash a3fab8ed7394c3c05d62041aaae68b0f) (uniqueness_rate 20.890411)) |}] ;; let%expect_test "FILTERED print x y z, powm_sec x y z" = Static.quickcheck_tripple ~filter:Filter.(combine [ odd; positive ]) (* Filter to get a better uniqueness rate*) ~f:(fun x y z -> [%message (x : t) (y : t) (z : t) (powm_sec x y z : t)]) (); [%expect {| ((hash 571382f34276e42f2a65e217aee6e69e) (uniqueness_rate 99.790795)) |}] ;; end module Ml_z_perfect_power = struct (* These are the tests that gmp have for their perfect_power implementation *) let test_perfect_power_helper s v = let res = if Z.(perfect_power (of_string s)) then 1 else 0 in [%test_eq: int] res v; true ;; let%test "0" = test_perfect_power_helper "0" 1 let%test "1" = test_perfect_power_helper "1" 1 let%test "-1" = test_perfect_power_helper "-1" 1 let%test "2" = test_perfect_power_helper "2" 0 let%test "-2" = test_perfect_power_helper "-2" 0 let%test "3" = test_perfect_power_helper "3" 0 let%test "-3" = test_perfect_power_helper "-3" 0 let%test "4" = test_perfect_power_helper "4" 1 let%test "-4" = test_perfect_power_helper "-4" 0 let%test "64" = test_perfect_power_helper "64" 1 let%test "-64" = test_perfect_power_helper "-64" 1 let%test "128" = test_perfect_power_helper "128" 1 let%test "-128" = test_perfect_power_helper "-128" 1 let%test "256" = test_perfect_power_helper "256" 1 let%test "-256" = test_perfect_power_helper "-256" 0 let%test "512" = test_perfect_power_helper "512" 1 let%test "-512" = test_perfect_power_helper "-512" 1 let%test "0x4000000" = test_perfect_power_helper "0x4000000" 1 let%test "-0x4000000" = test_perfect_power_helper "-0x4000000" 1 let%test "0x3cab640" = test_perfect_power_helper "0x3cab640" 1 let%test "-0x3cab640" = test_perfect_power_helper "-0x3cab640" 0 let%test "0x3e23840" = test_perfect_power_helper "0x3e23840" 1 let%test "-0x3e23840" = test_perfect_power_helper "-0x3e23840" 0 let%test "0x3d3a7ed1" = test_perfect_power_helper "0x3d3a7ed1" 1 let%test "-0x3d3a7ed1" = test_perfect_power_helper "-0x3d3a7ed1" 1 let%test "0x30a7a6000" = test_perfect_power_helper "0x30a7a6000" 1 let%test "-0x30a7a6000" = test_perfect_power_helper "-0x30a7a6000" 1 let%test "0xf33e5a5a59" = test_perfect_power_helper "0xf33e5a5a59" 1 let%test "-0xf33e5a5a59" = test_perfect_power_helper "-0xf33e5a5a59" 0 let%test "0xed1b1182118135d" = test_perfect_power_helper "0xed1b1182118135d" 1 let%test "-0xed1b1182118135d" = test_perfect_power_helper "-0xed1b1182118135d" 1 let%test "0xe71f6eb7689cc276b2f1" = test_perfect_power_helper "0xe71f6eb7689cc276b2f1" 1 let%test "-0xe71f6eb7689cc276b2f1" = test_perfect_power_helper "-0xe71f6eb7689cc276b2f1" 0 ;; let%test "0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01" = test_perfect_power_helper "0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01" 1 ;; let%test "-0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01" = test_perfect_power_helper "-0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01" 0 ;; let%test "0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000" = test_perfect_power_helper "0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000" 1 ;; let%test "-0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000" = test_perfect_power_helper "-0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000" 1 ;; let%test "0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000" = test_perfect_power_helper "0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000" 1 ;; let%test "-0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000" = test_perfect_power_helper "-0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000" 1 ;; let%test "0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81" = test_perfect_power_helper "0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81" 1 ;; let%test "-0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81" = test_perfect_power_helper "-0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81" 1 ;; (* Disable this test in javascript as it takes more than 2 minutes to complete. *) let%expect_test ("print (x, perfect_power x)" [@tags "no-js"]) = Static.quickcheck ~f:(fun x -> [%message (x : t) (perfect_power x : bool)]) (); [%expect "((hash ec1b6a78eea1217d5ec7ea0e85eafaef) (uniqueness_rate 85.742188))"] ;; end module Ml_z_pow = struct let%expect_test "print x y , pow x y" = Static.quickcheck_pair ~f:(fun x y -> let y = y mod of_int 1024 in [%message (x : t) (y : t) (pow x y : t)]) (); (* Low uniqueness rate because there are so many "no pow of a negative number" errors. *) [%expect "((hash 39a530e2309b08c3b598d4a8525d110c) (uniqueness_rate 52.297165))"] ;; let%expect_test "FILTERED print x y , pow x y" = Static.quickcheck_pair ~filter:Filter.(combine [ positive ]) (* Filter for better uniqueness compared to the above. *) ~f:(fun x y -> let y = y mod of_int 1024 in [%message (x : t) (y : t) (pow x y : t)]) (); (* Low uniqueness rate because there are so many "no pow of a negative number" errors. *) [%expect "((hash e298785b52497454a125dd95c691dec3) (uniqueness_rate 99.778761))"] ;; end zarith_stubs_js-0.17.0/test/exponential.mli000066400000000000000000000001641461647336100210400ustar00rootroot00000000000000(* These functions aren't exp-time, they're just dealing with exponents *) (* This file intentionally left empty *) zarith_stubs_js-0.17.0/test/fac.ml000066400000000000000000000062071461647336100170760ustar00rootroot00000000000000open! Core open! Import module Ml_z_fac = struct let%expect_test "print x, fact x" = let open Zarith.Z in Sexp.List (List.init 10 ~f:(fun i -> let i = Int.succ i in [%message (i : int) (fac i : t)])) |> print_s; [%expect {| (((i 1) ("fac i" 1)) ((i 2) ("fac i" 2)) ((i 3) ("fac i" 6)) ((i 4) ("fac i" 24)) ((i 5) ("fac i" 120)) ((i 6) ("fac i" 720)) ((i 7) ("fac i" 5040)) ((i 8) ("fac i" 40320)) ((i 9) ("fac i" 362880)) ((i 10) ("fac i" 3628800))) |}] ;; end module Ml_z_fac2 = struct let%expect_test "print x, fact2 x" = let open Zarith.Z in Sexp.List (List.init 10 ~f:(fun i -> let i = Int.succ i in [%message (i : int) (fac2 i : t)])) |> print_s; [%expect {| (((i 1) ("fac2 i" 1)) ((i 2) ("fac2 i" 2)) ((i 3) ("fac2 i" 3)) ((i 4) ("fac2 i" 8)) ((i 5) ("fac2 i" 15)) ((i 6) ("fac2 i" 48)) ((i 7) ("fac2 i" 105)) ((i 8) ("fac2 i" 384)) ((i 9) ("fac2 i" 945)) ((i 10) ("fac2 i" 3840))) |}] ;; end module Ml_z_facM = struct let%expect_test "print x, fact2 x" = let open Zarith.Z in Sexp.List (List.init 10 ~f:(fun i -> let i = Int.succ i in Sexp.List (List.init 5 ~f:(fun m -> let m = Int.succ m in [%message (i : int) (m : int) (facM i m : t)])))) |> print_s; [%expect {| ((((i 1) (m 1) ("facM i m" 1)) ((i 1) (m 2) ("facM i m" 1)) ((i 1) (m 3) ("facM i m" 1)) ((i 1) (m 4) ("facM i m" 1)) ((i 1) (m 5) ("facM i m" 1))) (((i 2) (m 1) ("facM i m" 2)) ((i 2) (m 2) ("facM i m" 2)) ((i 2) (m 3) ("facM i m" 2)) ((i 2) (m 4) ("facM i m" 2)) ((i 2) (m 5) ("facM i m" 2))) (((i 3) (m 1) ("facM i m" 6)) ((i 3) (m 2) ("facM i m" 3)) ((i 3) (m 3) ("facM i m" 3)) ((i 3) (m 4) ("facM i m" 3)) ((i 3) (m 5) ("facM i m" 3))) (((i 4) (m 1) ("facM i m" 24)) ((i 4) (m 2) ("facM i m" 8)) ((i 4) (m 3) ("facM i m" 4)) ((i 4) (m 4) ("facM i m" 4)) ((i 4) (m 5) ("facM i m" 4))) (((i 5) (m 1) ("facM i m" 120)) ((i 5) (m 2) ("facM i m" 15)) ((i 5) (m 3) ("facM i m" 10)) ((i 5) (m 4) ("facM i m" 5)) ((i 5) (m 5) ("facM i m" 5))) (((i 6) (m 1) ("facM i m" 720)) ((i 6) (m 2) ("facM i m" 48)) ((i 6) (m 3) ("facM i m" 18)) ((i 6) (m 4) ("facM i m" 12)) ((i 6) (m 5) ("facM i m" 6))) (((i 7) (m 1) ("facM i m" 5040)) ((i 7) (m 2) ("facM i m" 105)) ((i 7) (m 3) ("facM i m" 28)) ((i 7) (m 4) ("facM i m" 21)) ((i 7) (m 5) ("facM i m" 14))) (((i 8) (m 1) ("facM i m" 40320)) ((i 8) (m 2) ("facM i m" 384)) ((i 8) (m 3) ("facM i m" 80)) ((i 8) (m 4) ("facM i m" 32)) ((i 8) (m 5) ("facM i m" 24))) (((i 9) (m 1) ("facM i m" 362880)) ((i 9) (m 2) ("facM i m" 945)) ((i 9) (m 3) ("facM i m" 162)) ((i 9) (m 4) ("facM i m" 45)) ((i 9) (m 5) ("facM i m" 36))) (((i 10) (m 1) ("facM i m" 3628800)) ((i 10) (m 2) ("facM i m" 3840)) ((i 10) (m 3) ("facM i m" 280)) ((i 10) (m 4) ("facM i m" 120)) ((i 10) (m 5) ("facM i m" 50)))) |}] ;; end zarith_stubs_js-0.17.0/test/fac.mli000066400000000000000000000000511461647336100172360ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/gcd.ml000066400000000000000000000037111461647336100170770ustar00rootroot00000000000000open! Core open! Import module Ml_z_gcd = struct let%expect_test "print x, y, gcd x y" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (gcd x y : t)]) (); [%expect "((hash 92f8df73c82a7f926d5783455913a5fb) (uniqueness_rate 96.529814))"] ;; end module Ml_z_gcdext_intern = struct let%expect_test "gcdext 12 17" = let test a b = let a = of_int a and b = of_int b in print_s [%message (a : t) (b : t) (gcdext a b : t * t * t)] in test 12 27; [%expect {| ((a 12) (b 27) ("gcdext a b" (3 -2 1))) |}]; test 27 12; [%expect {| ((a 27) (b 12) ("gcdext a b" (3 1 -2))) |}]; test (-12) 27; [%expect {| ((a -12) (b 27) ("gcdext a b" (3 2 1))) |}]; test 27 (-12); [%expect {| ((a 27) (b -12) ("gcdext a b" (3 1 2))) |}]; test 12 (-27); [%expect {| ((a 12) (b -27) ("gcdext a b" (3 -2 -1))) |}]; test (-27) 12; [%expect {| ((a -27) (b 12) ("gcdext a b" (3 -1 -2))) |}]; test (-12) (-27); [%expect {| ((a -12) (b -27) ("gcdext a b" (3 2 -1))) |}]; test (-27) (-12); [%expect {| ((a -27) (b -12) ("gcdext a b" (3 -1 2))) |}] ;; let%expect_test "print x, y, gcdext x y" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (gcdext x y : t * t * t)]) (); [%expect {| ((hash 053b7e844ff37e7111c238d84e8f5c42) (uniqueness_rate 96.529814)) |}] ;; end module Ml_z_invert = struct let%expect_test "" = format "%d" (invert (of_int (-437423)) (of_int (-50))) |> print_endline; [%expect {| 13 |}] ;; let%expect_test "" = format "%d" (invert (of_int (-437423)) (of_int 48)) |> print_endline; [%expect {| 1 |}] ;; let%expect_test "print x, y, invert x y" = Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (invert x y : t)]) (); (* Low uniqueness rate because not many pairs of numbers are invertable. *) [%expect {| ((hash c792a46ebce6ab1538e70bbf2d463465) (uniqueness_rate 54.203324)) |}] ;; end zarith_stubs_js-0.17.0/test/gcd.mli000066400000000000000000000000511461647336100172420ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/import.ml000066400000000000000000000000441461647336100176500ustar00rootroot00000000000000include Core include Z include Util zarith_stubs_js-0.17.0/test/of_string_to_string.ml000066400000000000000000000224371461647336100224320ustar00rootroot00000000000000open! Core open! Import module Ml_z_of_substring_base = struct let add_underscore s = List.init (Int.succ (String.length s)) ~f:(fun i -> String.prefix s i ^ "_" ^ String.suffix s (Int.( - ) (String.length s) i)) ;; let test ~of_string ~sexp_of_t s = add_underscore s |> List.map ~f:(fun s -> s, Result.try_with (fun () -> of_string s)) |> [%sexp_of: (string * (t, Exn.t) Result.t) list] |> print_s ;; let%expect_test "Z.of_string" = let test = test ~of_string:Z.of_string ~sexp_of_t in test "123"; [%expect {| ((_123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (1_23 (Ok 123)) (12_3 (Ok 123)) (123_ (Ok 123))) |}]; test "0x123"; [%expect {| ((_0x123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (0_x123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (0x_123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (0x1_23 (Ok 291)) (0x12_3 (Ok 291)) (0x123_ (Ok 291))) |}]; test "-0o123"; [%expect {| ((_-0o123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (-_0o123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (-0_o123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (-0o_123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (-0o1_23 (Ok -83)) (-0o12_3 (Ok -83)) (-0o123_ (Ok -83))) |}] ;; let%expect_test "Q.of_string" = let test = test ~of_string:Zarith.Q.of_string ~sexp_of_t:(fun q -> Sexp.Atom (Zarith.Q.to_string q)) in test "123"; [%expect {| ((_123 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (1_23 (Ok 123)) (12_3 (Ok 123)) (123_ (Ok 123))) |}]; test "12.3e12"; [%expect {| ((_12.3e12 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (1_2.3e12 (Ok 12300000000000)) (12_.3e12 (Ok 12300000000000)) (12._3e12 (Ok 12300000000000)) (12.3_e12 (Ok 12300000000000)) (12.3e_12 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (12.3e1_2 (Ok 12300000000000)) (12.3e12_ (Ok 12300000000000))) |}]; test "0x1.23p12"; [%expect {| ((_0x1.23p12 (Error (Invalid_argument "Q.of_string: invalid digit"))) (0_x1.23p12 (Error (Invalid_argument "Q.of_string: invalid digit"))) (0x_1.23p12 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (0x1_.23p12 (Ok 4656)) (0x1._23p12 (Ok 4656)) (0x1.2_3p12 (Ok 4656)) (0x1.23_p12 (Ok 4656)) (0x1.23p_12 (Error (Invalid_argument "Z.of_substring_base: invalid digit"))) (0x1.23p1_2 (Ok 4656)) (0x1.23p12_ (Ok 4656))) |}] ;; let%expect_test "print of_base (format x)" = Static.quickcheck ~f:(fun x -> [ 2, "b"; 8, "o"; 10, "d"; 16, "x" ] |> List.map ~f:(fun (i, f) -> let string = Z.format f x in let string_dropped n = let n = Int.min n (String.length string) in String.sub string ~pos:n ~len:(Int.( - ) (String.length string) n) in try [%message (i : int) (string : string) (string_dropped 2 : string) (Z.of_substring_base i (string_dropped 2) ~pos:1 ~len:4 : t) (Z.of_substring_base 0 string ~pos:0 ~len:4 : t)] with | exn -> Sexp.Atom (Exn.to_string exn)) |> Sexp.List) (); [%expect {| ((hash 6e19ad54e13932775b7dcb252f2460c6) (uniqueness_rate 81.25)) |}] ;; let%expect_test "print of_base (format x)" = Static.quickcheck ~f:(fun x -> [ 2, "b"; 8, "o"; 10, "d"; 16, "x" ] |> List.map ~f:(fun (i, f) -> let formatted = Z.format f x in let parsed = Z.of_string_base i formatted in [%message (x : t) (i : int) (f : string) (formatted : string) (parsed : t)]) |> Sexp.List) (); [%expect {| ((hash 5accf29c4669d527bd5779447b54dab1) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_to_bits = struct let to_bits a = let r = Z.to_bits a in match Sys.word_size_in_bits with | 64 -> Option.value ~default:r (String.chop_suffix r ~suffix:"\000\000\000\000") | _ -> r ;; let to_bits_test_helper x = print_string (to_bits (Z.of_string x)) let%expect_test "to_bits" = to_bits_test_helper "1234567"; [%expect "\135\214\018\000"] ;; let%expect_test "to_bits" = to_bits_test_helper "-1234567"; [%expect "\135\214\018\000"] ;; let%expect_test "to_bits" = to_bits_test_helper "316049152"; [%expect "\000\135\214\018"] ;; let%expect_test "print x, (to_bits x)" = Static.quickcheck ~f:(fun x -> [%message (x : t) (to_bits x : string)]) (); [%expect {| ((hash 8941f3eca65e7d039f6d987683e3803b) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_of_bits = struct let of_bits_test_helper a = Z.of_bits a |> Z.print let%expect_test "of_bits" = of_bits_test_helper "\135\214\018\000\000\000\000\000"; [%expect "1234567"] ;; let%test "assert (abs x) = (of_bits (to_bits x))" = Dynamic.quickcheck () ~f:(fun x -> [%test_eq: t] (abs x) (of_bits (to_bits x))); true ;; end module Ml_z_format = struct let%expect_test "format '%d' x" = Static.quickcheck ~f:(fun x -> let str = Z.format "%d" x in [%message str]) (); [%expect {| ((hash cf890da293ccebc1e11c9ff4eec88058) (uniqueness_rate 85.742188)) |}] ;; let%expect_test "format '%x' x" = Static.quickcheck ~f:(fun x -> let str = Z.format "%x" x in [%message str]) (); [%expect {| ((hash 0db4620a008e5958554fff85f7bc950a) (uniqueness_rate 85.742188)) |}] ;; let%expect_test "permute formats" = let combinations = let open List.Let_syntax in let%bind flag = [ ""; "+"; " "; "0"; "#" ] in let%bind width = [ ""; "0"; "10"; "100" ] in let%bind typ = [ "i"; "d"; "u"; "b"; "o"; "x"; "X" ] in return (sprintf "%%%s%s%s" flag width typ) in Static.quickcheck ~f:(fun x -> combinations |> List.map ~f:(fun f -> f, Z.format f x) |> List.map ~f:[%sexp_of: string * string] |> Sexp.List) (); [%expect "((hash d8e5286d828a22da8141249fc86185be) (uniqueness_rate 85.742188))"] ;; end module Marshal = struct let m x = let y = of_string x in let marshal = Marshal.to_string y [] in print_endline (String.to_list marshal |> List.map ~f:(fun x -> sprintf "%02X" (Char.to_int x)) |> List.chunks_of ~length:8 |> List.map ~f:(String.concat ~sep:",") |> String.concat ~sep:"\n"); let y' : Z.t = Marshal.from_string marshal 0 in if not (equal y y') then print_endline "(roundtrip failed)" ;; let%expect_test "marshal large" = m "8432103214690897934401335661952849215774309719238"; [%expect {| 84,95,A6,BE,00,00,00,2D 00,00,00,01,00,00,00,09 00,00,00,06,18,5F,7A,00 00,00,00,1C,00,00,00,00 00,00,00,20,00,00,00,00 18,C6,9C,63,3B,6A,F2,2A E0,41,A2,BE,0F,2B,5F,8B 0C,CC,95,FC,C4,05,00,00 00 |}]; m "-107549090292258971570605440155"; [%expect {| 84,95,A6,BE,00,00,00,25 00,00,00,01,00,00,00,07 00,00,00,05,18,5F,7A,00 00,00,00,14,00,00,00,00 00,00,00,18,01,00,00,00 10,9B,BC,2C,E8,CF,9C,72 2F,BB,85,82,5B,01,00,00 00 |}]; m "107549090292258971570605440155"; [%expect {| 84,95,A6,BE,00,00,00,25 00,00,00,01,00,00,00,07 00,00,00,05,18,5F,7A,00 00,00,00,14,00,00,00,00 00,00,00,18,00,00,00,00 10,9B,BC,2C,E8,CF,9C,72 2F,BB,85,82,5B,01,00,00 00 |}] ;; let%expect_test ("marshal mix len" [@tags "64-bits-only"]) = m "-549389047489539543158"; [%expect {| 84,95,A6,BE,00,00,00,25 00,00,00,01,00,00,00,07 00,00,00,05,18,5F,7A,00 00,00,00,14,00,00,00,00 00,00,00,18,01,00,00,00 10,76,10,37,60,E7,FB,4D C8,1D,00,00,00,00,00,00 00 |}] ;; let%expect_test ("marshal mix len" [@tags "32-bits-only"]) = m "-549389047489539543158"; [%expect {| 84,95,A6,BE,00,00,00,21 00,00,00,01,00,00,00,06 00,00,00,05,18,5F,7A,00 00,00,00,10,00,00,00,00 00,00,00,18,01,00,00,00 0C,76,10,37,60,E7,FB,4D C8,1D,00,00,00 |}] ;; let%expect_test ("marshal mix len" [@tags "js-only"]) = m "-549389047489539543158"; [%expect {| 84,95,A6,BE,00,00,00,21 00,00,00,01,00,00,00,06 00,00,00,05,18,5F,7A,00 00,00,00,10,00,00,00,00 00,00,00,18,01,00,00,00 0C,76,10,37,60,E7,FB,4D C8,1D,00,00,00 |}] ;; let%expect_test "marshal small" = m "0"; [%expect {| 84,95,A6,BE,00,00,00,01 00,00,00,00,00,00,00,00 00,00,00,00,40 |}] ;; let%expect_test "marshal roundtrip" = Static.quickcheck ~verbose:false ~f:(fun x -> let str = Marshal.to_string x [] in let y = Marshal.from_string str 0 in [%message (x : t) (y : t) (equal x y : bool)]) (); [%expect {| ((hash c9b9d5441e3470cb51b8114f77ea91d8) (uniqueness_rate 85.742188)) |}] ;; end zarith_stubs_js-0.17.0/test/of_string_to_string.mli000066400000000000000000000000511461647336100225670ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/prime.ml000066400000000000000000000135101461647336100174540ustar00rootroot00000000000000open! Core open! Import module Ml_z_probab_prime = struct let%expect_test "print x, probab_prime x" = Static.quickcheck ~f:(fun x -> List.range 22 27 |> List.map ~f:(fun i -> let probab_prime x i = not (Int.equal (Z.probab_prime x i) 0) in [%message (x : t) (i : int) (probab_prime x i : bool)]) |> Sexp.List) (); [%expect {| ((hash 841bf3c0441e2a8f411a38399b51b11e) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_nextprime = struct let%expect_test "print x, nextprime x" = Static.quickcheck ~f:(fun x -> [%message (x : t) (nextprime x : t)]) (); [%expect "((hash e4324c97c8ef012678595c7c8203b8c8) (uniqueness_rate 85.742188))"] ;; end module Ml_z_primorial = struct let%expect_test "primorial x" = let open Zarith.Z in Sexp.List (List.init 100 ~f:(fun i -> [%message (i : int) (primorial i : t)])) |> print_s; [%expect {| (((i 0) ("primorial i" 1)) ((i 1) ("primorial i" 1)) ((i 2) ("primorial i" 2)) ((i 3) ("primorial i" 6)) ((i 4) ("primorial i" 6)) ((i 5) ("primorial i" 30)) ((i 6) ("primorial i" 30)) ((i 7) ("primorial i" 210)) ((i 8) ("primorial i" 210)) ((i 9) ("primorial i" 210)) ((i 10) ("primorial i" 210)) ((i 11) ("primorial i" 2310)) ((i 12) ("primorial i" 2310)) ((i 13) ("primorial i" 30030)) ((i 14) ("primorial i" 30030)) ((i 15) ("primorial i" 30030)) ((i 16) ("primorial i" 30030)) ((i 17) ("primorial i" 510510)) ((i 18) ("primorial i" 510510)) ((i 19) ("primorial i" 9699690)) ((i 20) ("primorial i" 9699690)) ((i 21) ("primorial i" 9699690)) ((i 22) ("primorial i" 9699690)) ((i 23) ("primorial i" 223092870)) ((i 24) ("primorial i" 223092870)) ((i 25) ("primorial i" 223092870)) ((i 26) ("primorial i" 223092870)) ((i 27) ("primorial i" 223092870)) ((i 28) ("primorial i" 223092870)) ((i 29) ("primorial i" 6469693230)) ((i 30) ("primorial i" 6469693230)) ((i 31) ("primorial i" 200560490130)) ((i 32) ("primorial i" 200560490130)) ((i 33) ("primorial i" 200560490130)) ((i 34) ("primorial i" 200560490130)) ((i 35) ("primorial i" 200560490130)) ((i 36) ("primorial i" 200560490130)) ((i 37) ("primorial i" 7420738134810)) ((i 38) ("primorial i" 7420738134810)) ((i 39) ("primorial i" 7420738134810)) ((i 40) ("primorial i" 7420738134810)) ((i 41) ("primorial i" 304250263527210)) ((i 42) ("primorial i" 304250263527210)) ((i 43) ("primorial i" 13082761331670030)) ((i 44) ("primorial i" 13082761331670030)) ((i 45) ("primorial i" 13082761331670030)) ((i 46) ("primorial i" 13082761331670030)) ((i 47) ("primorial i" 614889782588491410)) ((i 48) ("primorial i" 614889782588491410)) ((i 49) ("primorial i" 614889782588491410)) ((i 50) ("primorial i" 614889782588491410)) ((i 51) ("primorial i" 614889782588491410)) ((i 52) ("primorial i" 614889782588491410)) ((i 53) ("primorial i" 32589158477190044730)) ((i 54) ("primorial i" 32589158477190044730)) ((i 55) ("primorial i" 32589158477190044730)) ((i 56) ("primorial i" 32589158477190044730)) ((i 57) ("primorial i" 32589158477190044730)) ((i 58) ("primorial i" 32589158477190044730)) ((i 59) ("primorial i" 1922760350154212639070)) ((i 60) ("primorial i" 1922760350154212639070)) ((i 61) ("primorial i" 117288381359406970983270)) ((i 62) ("primorial i" 117288381359406970983270)) ((i 63) ("primorial i" 117288381359406970983270)) ((i 64) ("primorial i" 117288381359406970983270)) ((i 65) ("primorial i" 117288381359406970983270)) ((i 66) ("primorial i" 117288381359406970983270)) ((i 67) ("primorial i" 7858321551080267055879090)) ((i 68) ("primorial i" 7858321551080267055879090)) ((i 69) ("primorial i" 7858321551080267055879090)) ((i 70) ("primorial i" 7858321551080267055879090)) ((i 71) ("primorial i" 557940830126698960967415390)) ((i 72) ("primorial i" 557940830126698960967415390)) ((i 73) ("primorial i" 40729680599249024150621323470)) ((i 74) ("primorial i" 40729680599249024150621323470)) ((i 75) ("primorial i" 40729680599249024150621323470)) ((i 76) ("primorial i" 40729680599249024150621323470)) ((i 77) ("primorial i" 40729680599249024150621323470)) ((i 78) ("primorial i" 40729680599249024150621323470)) ((i 79) ("primorial i" 3217644767340672907899084554130)) ((i 80) ("primorial i" 3217644767340672907899084554130)) ((i 81) ("primorial i" 3217644767340672907899084554130)) ((i 82) ("primorial i" 3217644767340672907899084554130)) ((i 83) ("primorial i" 267064515689275851355624017992790)) ((i 84) ("primorial i" 267064515689275851355624017992790)) ((i 85) ("primorial i" 267064515689275851355624017992790)) ((i 86) ("primorial i" 267064515689275851355624017992790)) ((i 87) ("primorial i" 267064515689275851355624017992790)) ((i 88) ("primorial i" 267064515689275851355624017992790)) ((i 89) ("primorial i" 23768741896345550770650537601358310)) ((i 90) ("primorial i" 23768741896345550770650537601358310)) ((i 91) ("primorial i" 23768741896345550770650537601358310)) ((i 92) ("primorial i" 23768741896345550770650537601358310)) ((i 93) ("primorial i" 23768741896345550770650537601358310)) ((i 94) ("primorial i" 23768741896345550770650537601358310)) ((i 95) ("primorial i" 23768741896345550770650537601358310)) ((i 96) ("primorial i" 23768741896345550770650537601358310)) ((i 97) ("primorial i" 2305567963945518424753102147331756070)) ((i 98) ("primorial i" 2305567963945518424753102147331756070)) ((i 99) ("primorial i" 2305567963945518424753102147331756070))) |}] ;; end zarith_stubs_js-0.17.0/test/prime.mli000066400000000000000000000000511461647336100176210ustar00rootroot00000000000000(* This file intentionally left empty *) zarith_stubs_js-0.17.0/test/special.ml000066400000000000000000000045131461647336100177630ustar00rootroot00000000000000open! Core open! Import module Ml_z_fib = struct let%expect_test "print x, fib x" = let open Zarith.Z in Sexp.List (List.init 10 ~f:(fun i -> [%message (i : int) (fib i : t)])) |> print_s; [%expect {| (((i 0) ("fib i" 0)) ((i 1) ("fib i" 1)) ((i 2) ("fib i" 1)) ((i 3) ("fib i" 2)) ((i 4) ("fib i" 3)) ((i 5) ("fib i" 5)) ((i 6) ("fib i" 8)) ((i 7) ("fib i" 13)) ((i 8) ("fib i" 21)) ((i 9) ("fib i" 34))) |}] ;; end module Ml_z_lucnum = struct let%expect_test "print x, lucnum x" = let open Zarith.Z in Sexp.List (List.init 10 ~f:(fun i -> [%message (i : int) (lucnum i : t)])) |> print_s; [%expect {| (((i 0) ("lucnum i" 2)) ((i 1) ("lucnum i" 1)) ((i 2) ("lucnum i" 3)) ((i 3) ("lucnum i" 4)) ((i 4) ("lucnum i" 7)) ((i 5) ("lucnum i" 11)) ((i 6) ("lucnum i" 18)) ((i 7) ("lucnum i" 29)) ((i 8) ("lucnum i" 47)) ((i 9) ("lucnum i" 76))) |}] ;; end module Ml_z_jacobi = struct let%expect_test "print x, y, jacobi x y" = let open Zarith.Z in Static.quickcheck_pair ~f:(fun x y -> let y = Z.abs (Z.add (Z.mul y (Z.of_int 2)) Z.one) in [%message (x : t) (y : t) (jacobi x y : int)]) (); [%expect {| ((hash cd1cadf3e1014c37df027be2d6cfdc37) (uniqueness_rate 96.089932)) |}] ;; end module Ml_z_legendre = struct let%expect_test "print x, y, legendre x y" = let open Zarith.Z in Static.quickcheck_pair ~f:(fun x y -> let y = Z.abs (Z.add (Z.mul y (Z.of_int 2)) Z.one) in [%message (x : t) (y : t) (legendre x y : int)]) (); [%expect {| ((hash 00f3ba8c7b418e70c4311a972509d7d5) (uniqueness_rate 96.089932)) |}] ;; end module Ml_z_kronecker = struct let%expect_test ("print x, y, kronecker x y" [@tags "no-js"]) = let open Zarith.Z in Static.quickcheck_pair ~f:(fun x y -> [%message (x : t) (y : t) (kronecker x y : int)]) (); [%expect {| ((hash 5e20979fff7d0cdacebdaf949ad2a9cf) (uniqueness_rate 96.529814)) |}] ;; end module Ml_z_bin = struct let%expect_test "print x, y, bin x y" = let open Zarith.Z in Static.quickcheck ~f:(fun n -> Sexp.List (List.init 10 ~f:(fun i -> [%message (n : t) (i : int) (bin n i : t)]))) (); [%expect {| ((hash e9550b72915b41a5e8e84150d82c3cda) (uniqueness_rate 85.742188)) |}] ;; end zarith_stubs_js-0.17.0/test/special.mli000066400000000000000000000000511461647336100201250ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/stable_bigints.ml000066400000000000000000000642201461647336100213350ustar00rootroot00000000000000let all = [| "8" ; "-50" ; "-437423" ; "-55" ; "0" ; "-27" ; "0" ; "-336" ; "-3383997640736" ; "48" ; "277813463068683339" ; "239957969022" ; "-588" ; "-844387025997697699501" ; "59028683922313" ; "138977538398" ; "3189772952" ; "27120" ; "14" ; "-346677414235816687659581081796638469468218553179" ; "2843412491601089781597635012" ; "-431" ; "1067299085357" ; "-494979609893511946158886" ; "895322596518294" ; "138395307643388" ; "10224195565745627946169632831085633" ; "0" ; "-1531906813008657567" ; "-670316294463519193322281017716842421239660931643187539780123220547484452" ; "844661916302136987529514182018720897327463" ; "-233" ; "-4" ; "240" ; "3" ; "21705487" ; "3629802647875" ; "1620161797322" ; "140608794" ; "3693857032053557" ; "-8" ; "-328" ; "-2480427" ; "1058125119569363310743832371" ; "-528184449726422101" ; "-179764732" ; "4" ; "1900664367024278253695746455269985367" ; "63896625038230640031081167879532" ; "-4703730618541680" ; "0" ; "1091100230858450947222924005" ; "-4316208300828377275632" ; "-4720768029333375649765471008084146927244846" ; "-172681485321870925190189268309200803534299257614181211" ; "-93029741438856968616386118237960128168854760469556959208055" ; "0" ; "-24260240266455133113749822147386832123142165061378581737570987" ; "11272369846948577423376062456190827929" ; "0" ; "-53381523536217360088446554" ; "-588369486" ; "-73" ; "-266" ; "-2748" ; "344668" ; "-180414568" ; "303632" ; "1625390" ; "30619425267585535" ; "116964126163915067419" ; "-267978887386691872279" ; "3892536127561188" ; "-64193317" ; "0" ; "-2730855825" ; "-15182719531832" ; "1160352384" ; "43434993631486" ; "0" ; "-135316828446942148255987897844839065" ; "32066921084828502715723146541371845436787" ; "246063130735973029055328" ; "-42399549354593667143445329219221129382299657" ; "145204107077395310578041506208326069347677" ; "0" ; "0" ; "-20450214038114032095913203252103233267339867" ; "167895708212258800656094145966" ; "-29208700801765349848608" ; "-19517637290403104334310931896628509835175896322451783" ; "-1808373051345201" ; "133484398693043438458579350491" ; "1" ; "-32" ; "2" ; "4297" ; "-231" ; "-307510221" ; "-658733" ; "0" ; "6151661836257" ; "3892777393356336" ; "615739314807" ; "103070944665205224" ; "-2445" ; "49922454054676519" ; "63806973527147522037807432236" ; "365644066051804428986011410502" ; "-919200326000781" ; "12517183113920456133263518427" ; "73001498057399709283920" ; "-253174999409" ; "-186681384481242988685600915656315759039805970928694" ; "-282176948987750898026267772267979030312" ; "0" ; "-439299127233511335" ; "11200388671002008553453888073057698778680403" ; "357767644073688041" ; "-21823193817034956310917090971349626816000987583043209713" ; "-20788922" ; "90382955164535620595055278381055986777175158821109410130085735337" ; "22689801357040579" ; "6393883191712582890511998699192373974089431993571120966284051" ; "-2" ; "-1751" ; "7" ; "123394825" ; "118130" ; "-17542941854" ; "-1300008180322250" ; "-599788292" ; "-7222495910" ; "-1612062438020978775525" ; "19605443878499521" ; "0" ; "73682256085" ; "-110053199827722691027885670" ; "-162422941953331778" ; "-431549151988792267392" ; "58433579865" ; "29595" ; "284264749859814065197" ; "-33651663416039186292344646493082655739581" ; "0" ; "0" ; "0" ; "-788805743417251976889813501" ; "-209019926020031" ; "-716304274373113512694626807194046" ; "89151550281015240365299530446367296314363735795481654562" ; "0" ; "-225983" ; "10450553550083008398841119772549266840624422696122978" ; "-309086373462974435525221472784415705276355226901425019346100105" ; "-2" ; "-1" ; "-136" ; "0" ; "-617196" ; "0" ; "0" ; "13716279931071501" ; "343311068881323325764" ; "-1306535436" ; "56402555118322" ; "7607" ; "3626820511195966866" ; "0" ; "0" ; "-5" ; "-4610183352" ; "10407726613792429248" ; "-182736902616066323" ; "-8740472534540819747734773749913706658893499" ; "386566729529296946000583278051" ; "-169646919959026" ; "71979259011011671" ; "-515064760" ; "-350854590609916240021621339209265605473999635689" ; "0" ; "0" ; "1460" ; "441003349606328036308562044663323503358690600280142142935" ; "1392416853008918764288426448156165198390856182553506" ; "0" ; "-190" ; "-12" ; "-4" ; "60474" ; "-7" ; "180178" ; "431" ; "-6196937934710936970" ; "-3277200294770412" ; "-7" ; "3259346939422291" ; "135854431554467" ; "-118229647200990" ; "16165069206692054229795982768493" ; "0" ; "20084723013643723912532957" ; "113557" ; "858" ; "0" ; "-5" ; "-4" ; "31915869" ; "861224052884570525221070975370146" ; "27263128483240209921208877496649" ; "-788110981418673268605520508478456979670532409869652337" ; "-111695503704323388469102772876316813801666303043" ; "-115035722398660831776289580343093239649525293" ; "-1852567375" ; "51548119467237576776048537545" ; "-103726209055732" ; "541967" ; "1" ; "-1" ; "1614803" ; "1" ; "-117897297" ; "-212" ; "0" ; "-1000859734100590" ; "2140258182908663" ; "-18051925" ; "2908" ; "1113910462725347" ; "60" ; "-2754040822063139407498717119665" ; "-137272" ; "1880812762303726" ; "-598665921951977359131771091640502436" ; "-3940615676426844759" ; "-32823435300474224906503144" ; "-1185765700" ; "-5823718" ; "-23034154479221063735" ; "0" ; "-213329565046871682" ; "1686729305821152544" ; "-639968" ; "-43" ; "21259577516103646296325725504541027210997984371835" ; "0" ; "-135898976936720452922799393385703354653159537801572985733903605621" ; "39596609008949899949339596326120" ; "13" ; "1481" ; "123" ; "-1304360872" ; "-59502113" ; "-40451196914397" ; "192406880447" ; "12068774826" ; "418945213955665411" ; "0" ; "-541603496123089" ; "132793434009017449" ; "629128238105400149925454" ; "35200923990935212530" ; "0" ; "-29754993742" ; "564667075129590345076515215426" ; "109343798958792126" ; "-961618258182704363397053142092917345" ; "15692" ; "-28831478488075628423507857979147667850795" ; "527317632680605742752581758256954785" ; "-1755581218337925894460079483166198558425771878124" ; "0" ; "0" ; "4131487205861414376698947317849732929637" ; "16821747897714703990875551744" ; "6250485169504384573106893166559120312655267305295893334027" ; "9499354664352236162804445" ; "94617376187" ; "-199831934725429761877960919188993646261953454096232329689276" ; "0" ; "1" ; "93" ; "27878" ; "-2243" ; "-24" ; "-11820" ; "-7243443936" ; "0" ; "685696609" ; "-3267" ; "4878161557930" ; "-1219045702572374694781172198096" ; "974083192341148744303" ; "-10556165248931670490334611" ; "-17" ; "8353518945248625615519012098524819253" ; "19837224492333457898727341019253841582642858" ; "63382234088043779642760173968398776341778" ; "1905488012732621631513423727" ; "-213495704478" ; "3807455148428293514882270453137523531040805164" ; "-38203236689012328932510073491138721933169874634759051" ; "216377018370077831983240363012220855597" ; "17969167955665965909636986969613895" ; "-92775893401113409680229545536505931" ; "-787066" ; "-936819284" ; "6979079380821070075096132693803994731545879711616700" ; "0" ; "14439702993821764651088292731895577536952582450854600491326861346" ; "207" ; "3994" ; "-19596" ; "1317" ; "0" ; "2301666" ; "-1312410244918703" ; "1001484" ; "-2272170275904542418" ; "-3607087429312316367454" ; "31791641436593663312687847" ; "-3988623384588369402949398" ; "-193897956539" ; "-915523086485687211660994049066" ; "-1456" ; "7953368212507658290908861281321705" ; "-45075" ; "0" ; "1050595663028403259081551998511059091" ; "51710238340166029788318106" ; "30715275" ; "-24604758025191419" ; "-472851204267590820946903" ; "-1124948488159443121546" ; "0" ; "-6676014497429899963523148674258859341207160712" ; "112000000083063" ; "-14214621085792" ; "493" ; "15240822670826642210840177" ; "-3391865667684" ; "12" ; "0" ; "-38379" ; "-4550" ; "-1" ; "0" ; "0" ; "-2" ; "0" ; "6913917397015026559278" ; "-38021774" ; "728556" ; "139219758111861790" ; "3054186699408748" ; "517383227087047050453219887934" ; "8380824277442" ; "1783" ; "-13578" ; "0" ; "328374693383004777991827361953430649" ; "-11474106768269293" ; "16535578097156296990588019458857080600406" ; "-211921516155591373123253350895829900557115845" ; "24" ; "5196998972608940013167859702170277549765" ; "-836747911700" ; "283735479126557722071069883820715281738586824602301625" ; "-113549914214042090897" ; "-2954330449750" ; "80594" ; "317838649715448102087141548555245164423243" ; "1" ; "15" ; "-1132" ; "405055313" ; "-94641760256" ; "-89" ; "0" ; "10921" ; "-3353" ; "901938840952353430830" ; "21532135998991295378507" ; "9266052421368463388035890" ; "361" ; "-125163402286577615751464" ; "-137245949331719659175109" ; "-6975920088565" ; "512773713943948082677683192213594" ; "1705551948805" ; "0" ; "61642497719" ; "-1707357770312645109208511219263136571697717" ; "-27" ; "0" ; "-71873118891466239156457557400888" ; "-2250672432753901146757188172" ; "0" ; "0" ; "13883670993107341866396367241701240945909934" ; "1896411070669926096342646888826646378287105010198679878842705" ; "695613170077014638944974833294905518073612" ; "3948242048614076888936922940064" ; "-3" ; "2" ; "30743" ; "0" ; "-329056639" ; "0" ; "0" ; "0" ; "9" ; "88706159253149733038" ; "8838892597408284844294" ; "49656177" ; "21414434921056274806" ; "664327" ; "5863190977205" ; "15334481557" ; "-7314110391385" ; "44" ; "20591247969788673781462564" ; "-338040554905996897" ; "-2837044356822800" ; "86488140183779869358719054685565033969267590085892267" ; "-42686663" ; "-973826" ; "327322373092578647567865485133566142250008486967" ; "-54623879282448194947738991" ; "-1576657303733438198459055709" ; "3287272860" ; "651797278874228248649552936853083763542138" ; "-512354234514145" ; "-24483521493844810777416836510472350991251143737981716019892477" ; "33" ; "-141" ; "0" ; "65306489" ; "6" ; "-35824" ; "-79395954" ; "13489310670516" ; "5" ; "3592070142" ; "227699030848504367" ; "4216528476962448445458043116" ; "0" ; "3160170101067175721172177169153" ; "-48474694774458979" ; "-3781488555430725297740215" ; "-1423888555376812716018683575208" ; "670475527314292893" ; "446685498655553468066496520322" ; "98152" ; "-134271024709694009765469713912" ; "-51770312718735672774" ; "-2026688448132102738588774361" ; "19854997856834113454212975610043696747526164069050516809" ; "-16840635825259528096553284576990" ; "1684601392858111893056983884479041498548822174986217723783931" ; "334860486016614440995460937853179002631980" ; "0" ; "21515560840724730180759516192462081805498056" ; "-307836233467021875701002574882311773473806127734830630449029" ; "-174882756944923999721092426824526922509333965175" ; "-4" ; "30938" ; "242" ; "99" ; "-987" ; "0" ; "-23493688306984" ; "-10244817193080" ; "223386964" ; "10877267" ; "-7357" ; "-1" ; "-18601371" ; "-13069817802433641769647457654" ; "18279173746806255693288" ; "310063730992712773607" ; "9631444022382685" ; "-10716" ; "-158932920044639600456473230997140" ; "-1352559992083028361617" ; "-538004430663516617493306546640640318" ; "35903441394966610452863416" ; "-2257827967193479" ; "-548813" ; "-811076753360116216" ; "723049407979694486623049949157692618055578328121657381880414" ; "21975858406600619863671672098634897567206379371352909497659041461" ; "-55131454196211" ; "146100140800683063364695125065525373636" ; "-589077529519616932958738278953" ; "779190962320572186413634655451109941666870031099233" ; "57" ; "-7586" ; "22261" ; "-5500" ; "0" ; "11047" ; "-550957702" ; "-86" ; "-142586511361622997272" ; "-20445" ; "0" ; "-6627124" ; "0" ; "-19612500640663078385651075" ; "47298531092629406386929" ; "105666808268979" ; "20987721684379187361061637806350163" ; "-2173109224206526353216" ; "11163536180082993074997213735832932110" ; "3" ; "-6382459294638633402043" ; "-1012888969218" ; "-1479053251" ; "-178061703911712675912442442" ; "30024277495307642150971673900297" ; "30843535630754879433294418608613585538359482571353610871260" ; "-6860363559756892841806905231271960161123" ; "0" ; "-5696780959891441001683700075237031168555799930953" ; "0" ; "28011048" ; "1" ; "11" ; "-12149" ; "134" ; "187569447602" ; "-1028581471" ; "-354424989013" ; "0" ; "-1571947" ; "940121473390906573" ; "-24402181" ; "-1963323994147113" ; "-1180021562040" ; "0" ; "0" ; "-3846461641977163883496290875" ; "-720855047580964364285608044342395828435" ; "2687782180135165325530327625117973" ; "-627003287429665856773976252" ; "-437776600951529" ; "203535716379793483681691" ; "357259804749537922271779" ; "-206" ; "679921865457231957926256920288775325431300419" ; "-1" ; "19441307747998544206458994949301007924617775018787" ; "-677418969762419989397429733951445925052220850776060780093932228" ; "-9164389234151973286702686815582651018282950" ; "0" ; "415781133122129510652615" ; "1023481090916133545341392603410202356" ; "-108" ; "73" ; "50598" ; "-511966" ; "-3797595" ; "-250" ; "0" ; "0" ; "0" ; "3" ; "-29754401838806777" ; "-69420248908754914" ; "2698690639694154" ; "-263384932523113" ; "10154378" ; "-56297543711071" ; "247841469544281696" ; "32180601429116471077147" ; "298788099503581930237186408" ; "17240743859816485476712" ; "-2480494" ; "105115336224453746407510615418148816235209549" ; "-536471702832129171074947669073787704975546865524748" ; "0" ; "-3726088121882851805814434" ; "100517095999314675142" ; "340586381311105204034659102555" ; "333884127035516264505061843946428874866274940742768" ; "6" ; "4346522448387558102697429816415" ; "79645715607949364190212619583893924448469198127044063193356" ; "187" ; "107" ; "30952" ; "-6025" ; "-227691" ; "205311764" ; "6666177" ; "-8645714244024862" ; "219735508689153" ; "832233558873248082233" ; "1688859462059" ; "-1739322174" ; "1571" ; "63429788172229" ; "1808716328653469105103675580402862" ; "-2373210193092364846383588718749" ; "16572472724521682765723992395944525" ; "2495663671140254842114492667001" ; "-4148966002784" ; "108490311885859562990701557617964813" ; "43956866208345245147094241408497929518456928631" ; "16595091515" ; "-243974746" ; "71233441119492606340280064922328084580349770" ; "256544424" ; "-1624739112298933471912188159" ; "4796771051264193130639818952894960" ; "113783838147945" ; "-47" ; "-28206440792526517677141571228274825171" ; "0" ; "0" ; "7680" ; "-4756082" ; "418255" ; "74236840386" ; "-18264771807" ; "229761547970" ; "-2602845086670" ; "178596265658953" ; "14587610564" ; "0" ; "252092699670" ; "-26" ; "-591633942944553306704945" ; "-24861288086753143596776144009832365" ; "110827901379193323287976362836103" ; "-416929083684650148779932527" ; "1753468832986073137579106892189350" ; "-20" ; "11018441962007" ; "30313991861341954532690756634" ; "0" ; "254106625146757" ; "-23350617361641034630973649594470178244017864566078" ; "30546297958618253554161490543059330" ; "1346629175977805852932748" ; "986517299238" ; "-21" ; "-254159695983748984" ; "-5533013177481898515343368530236580382990604264" ; "-270214090979306436657629504" ; "112" ; "-988" ; "6146" ; "1020" ; "-1040655" ; "3031" ; "0" ; "-135202" ; "-1640876477611" ; "-556234988737" ; "10792625990928489183587088" ; "57578099" ; "-22014" ; "-37535155466529541588696915473" ; "3115068575635" ; "29189216224323374318885496" ; "1218540002220200344819296" ; "0" ; "-28501907231867150059" ; "-5018542992702702400227" ; "-53976513458613215224928360878" ; "67300563555437035631514244905291788408423462250" ; "-14992610155158094734274606162770877" ; "-572791388816949559292411" ; "116293161123575870" ; "33851760264183140669976436150703759475216859226223664450890721" ; "-5343460726735341526504901550671524772940" ; "11501407500589858695910693957201898335505672670628158" ; "-25320616173886280411054849659185555620592603" ; "44527831827341355927113462994" ; "3494901359" ; "0" ; "-313" ; "-5" ; "-664194" ; "-48388626" ; "-87042785110" ; "-20906496766641" ; "-4227155389427848182" ; "-26337317137955660577" ; "-34278662440310" ; "-275505087898031148" ; "22" ; "-27387088155450818" ; "-12469805266855665196" ; "0" ; "-425793326367266642260468362526100" ; "0" ; "24639039891329933179011" ; "-7455997170105317654034715" ; "4" ; "-77145178514948140734277639556176014430886941793" ; "86720662871453138861973876491596522232327402" ; "-2289354620667670116643841656137426949834662520970" ; "-8641991137401820545188278828138168782122787496105358821" ; "1576873882793023261807" ; "944059053604649" ; "7180578155638724839380463416062" ; "-1807140908321063258" ; "0" ; "0" ; "-1038938" ; "-92" ; "59500" ; "-902" ; "-3625997" ; "-33066216159" ; "0" ; "-24667869765963995" ; "2852" ; "26419751" ; "5" ; "-26718762749335752463" ; "-59015959680619928710" ; "-43781" ; "-22551594583561614207177437" ; "1285442702181759557794992473" ; "-672542562100412964390" ; "-74706" ; "-3230348822876416431995980432470425392530497" ; "263873641072327306408" ; "-3554962831952049352259168540584691608175622" ; "500656905064395270374900930936495" ; "100828138659669" ; "2048165659" ; "-78937588016844282482430623390409114262867284388774040285" ; "9995503521549883435195903121323882126156949162" ; "-4149666995435244947" ; "-174287209705" ; "-1770234572628635641" ; "0" ; "2100799893642" ; "29138130826191219805730713150481761735757023344912075111572" ; "-11" ; "83" ; "-3241692" ; "2" ; "-35372306" ; "-22" ; "16182378175" ; "-944128881639905838" ; "-315187446805415367" ; "7323443" ; "-1599944612621471399112" ; "928876181687152172402867959" ; "8378308263804417109866521" ; "4596869983650591747966527616" ; "3147036703789862" ; "-59039469" ; "190427697813747896792810775860719" ; "13783037490590313260619471679965497466637874" ; "7399298462891" ; "12850767507058512844" ; "-2197704352321532693000" ; "-116087243380929784310441232986687432098" ; "208184585875499335498080326526094" ; "-3503130295731442" ; "-12105316801394744112314028423399" ; "-31069394635329052822" ; "40283122914733740327198552252" ; "-97507204438448721382736362010" ; "0" ; "-127516784244102511913991516534" ; "-702" ; "-23" ; "-1" ; "0" ; "-221856713" ; "-12265995298" ; "13642496654" ; "1" ; "-1" ; "2683267" ; "103" ; "17" ; "-2848109328233" ; "-259911292492908949479300336" ; "17442378384937" ; "5816" ; "-969" ; "664788288581993" ; "39" ; "-5184955804790432029262" ; "-157902379783408148597980007210671" ; "-272285415" ; "-217618905423494543022138106560" ; "-1856130911" ; "-126543913398910770002233622219" ; "6921404007373674583159791494101908401274399262990970495" ; "-406999776595290760128197746461999080232761" ; "-1210987642833445033959548276303298119705531211185594039323" ; "16521421877064008237150936931" ; "-579724622645165953633" ; "-2171897767238084415363570636854389070955972" ; "-8131545885008924121593776789305678" ; "-127" ; "-8" ; "-203768" ; "-1" ; "350" ; "124600660781714" ; "-51654568794" ; "-693" ; "0" ; "466" ; "19199875055846" ; "77" ; "-108282830381472554236" ; "-21605906" ; "-1203737207322143168065" ; "9625443002519041312824146494940" ; "152163993652968618393" ; "-2377259722455" ; "0" ; "-4689718794931456142113" ; "82017887146849430443599587" ; "-33126129261852531731175625809849676004" ; "0" ; "-2862799782" ; "-60368859528732832357692935282206921315121" ; "11088226744719361926064124587272690269428009736952" ; "-2934606476524910977047120364596015571959645635377715451592819" ; "-1782893765427217472419614363100045987092783014307784583091841" ; "173" ; "-145039251576110707591401" ; "0" ; "254" ; "-401" ; "-5862960" ; "-234477" ; "0" ; "-4514947640" ; "-11092866052263" ; "-3534" ; "0" ; "4955798" ; "-3161781099390944076264830" ; "819043441771037530" ; "14516732251337572" ; "350288522064448658" ; "101513397920673871320867703523912" ; "-2094767" ; "-6230" ; "-4214130576" ; "8792628052310827077676562" ; "112501825191927195173326146" ; "206695086935645373644894" ; "11151659572955141938030746082115835687784878316556303" ; "-4371225735478337643213265712" ; "-380768798831349928968795790638631" ; "0" ; "42854" ; "0" ; "1419907991191699285683250399843281593829328021349178" ; "-9916" ; "-36217680385030618039507701773128564348473655416503" ; "-30445326430738993952391311467764" ; "-251" ; "1" ; "-225471" ; "-121371" ; "71823" ; "16543" ; "55260708460177" ; "310" ; "0" ; "232960845464367" ; "83495" ; "1645361450385966551833" ; "-1752430331168586752" ; "-5818422578761790739504842227" ; "1412288844300546057765445501" ; "0" ; "0" ; "0" ; "482479050914711581660671843663476" ; "29581776624417033480355" ; "51226181707071527928834608" ; "-2431110764362272641" ; "11696389994839776550666231541" ; "96272662248757260817" ; "20842156466238584586883972123755447096" ; "0" ; "-1461653006252202649019118699342526849524171895848643" ; "11433231792144953510994565206104416207820200271184924965" ; "-1404942563157146439419201562594659258954142946684" ; "-2722124975596429839617351715242632930549823184309397598620144713790" ; "-66047842426521302655545006" ; "-1" ; "12" ; "0" ; "0" ; "-99759796685" ; "13152675845" ; "11566590306897" ; "53143265029675267" ; "-1369158141530243625648" ; "-8847699003156153584329" ; "-74" ; "-1537615468" ; "-31863016257008400580930183737" ; "-842796218822401041639046" ; "114583008" ; "32142383103644" ; "13687561273029426989" ; "71582419924281850053" ; "-88074754621641844" ; "281867823508953127965949531425599578093054636571" ; "21256005119207311333243028230892676116836617196" ; "35" ; "197664161723274766165153" ; "-40224840969560589810917347" ; "14262108830454178554389228" ; "87144921090023893885663103699622958887204867281386782947989" ; "-17257523278626534207860341753879364449799346824197494457" ; "-13" ; "0" ; "-1948155117220716414302217966729085804117747408621" ; "368589683594434076" ; "143" ; "41832" ; "182" ; "5350" ; "7086" ; "17647" ; "3967" ; "5490875" ; "0" ; "116603605218372020894096" ; "-68511369497" ; "-516807151129557" ; "5773881681543697521" ; "-260527844" ; "175244618862324995" ; "36782725503573559759707432188451064" ; "-170022910052878025649900174673" ; "-8602608" ; "-60144302202161296487296134400047473721466867" ; "15659435907714322" ; "-630882228158519793197572341936289897810800682732" ; "275490251033145969099056095703604490673111147389536" ; "-33689457812259069541485933623" ; "-2406927222542526106009216789825493618372362740535343949970" ; "-4245240513242117808361149240635016472627" ; "-822753" ; "10974811200131381965440819913083882466427" ; "1105625530683733874352728960575079334039245159670216765648" ; "325328299602637049569505236464025208294515184782" ; "-17992099644390503600776150405130428190463369494227819510514090674917697" ; "6012300320171316574397113233571644681233" ; "-2" ; "63" ; "-50" ; "2535092913" ; "-105620530886" ; "6402781" ; "-1574097036869" ; "2470556798" ; "45" ; "-1799716770" ; "8824744670" ; "0" ; "0" ; "137002521500034779107778" ; "0" ; "0" ; "1242" ; "-2269596920692366572124528340" ; "-217351005088508" ; "0" ; "-167574949992942" ; "-460919091055996341" ; "482747" ; "-10585754736289268" ; "-28992878660852475241226202422" ; "138868167152468298835980134042940092582093043320281" ; "37847196962122940873487137639" ; "-294742633768365193012766805231762" ; "-176766094438454601836301496231858877297850071" ; "35631744504382697713695672814534652491659251413133599048764387470721739" ; "0" ; "-9" ; "0" ; "7100245" ; "89" ; "-997626046" ; "-7784880220006" ; "1981863" ; "-417248815610920" ; "-212458431796274358" ; "-57" ; "2564670749047252960" ; "51721267295" ; "103721006402529511534576321" ; "-6" ; "-3412747755867184079502" ; "-249525825769430257710670059984629841707" ; "-124139765915" ; "78244179328935704244" ; "415918766" ; "-170563964806949" ; "-327177269417898586190378912186618377706309110" ; "595777007891781669715478783859618193448000160735166" ; "-360038656587214330585530534" ; "-246247572083515574" ; "1820482421827997886410168256255077632754333" ; "-107549090292258971570605440155" ; "0" ; "4590579623660977323176806412347590" ; "-682757150569102630886575194205139747345488" ; "-549389047489539543158" ; "8432103214690897934401335661952849215774309719238" ; "-6" |] ;; zarith_stubs_js-0.17.0/test/stable_bigints.mli000066400000000000000000000000271461647336100215010ustar00rootroot00000000000000val all : string array zarith_stubs_js-0.17.0/test/stats.ml000066400000000000000000000026631461647336100175050ustar00rootroot00000000000000open! Core open! Import module Ml_z_sign = struct let%expect_test "print (a, sign a)" = Static.quickcheck ~f:(fun a -> [%message (a : t) (sign a : int)]) (); [%expect "((hash 6a50fc55f309b06e235a58279d95ab7c) (uniqueness_rate 85.742188))"] ;; end module Ml_z_numbits = struct let%expect_test "print (a, numbits a)" = Static.quickcheck ~f:(fun a -> [%message (a : t) (numbits a : int)]) (); [%expect "((hash 8b57321da6ff80f190ac44f5b0ccd514) (uniqueness_rate 85.742188))"] ;; end module Ml_z_size = struct let%expect_test "print x, size x" = let size64 = match Sys.word_size_in_bits with | 64 -> fun x -> size x | 32 -> fun x -> Int.((size x + 1) / 2) | _ -> assert false in Static.quickcheck ~f:(fun x -> [%message (x : t) (size64 x : int)]) (); [%expect {| ((hash 0ce1a8f087b3d87b3d852b046f0f3c6d) (uniqueness_rate 85.742188)) |}] ;; end module Ml_z_trailing_zeros = struct let%expect_test "print x, trailing_zeros x" = Static.quickcheck (* The "zero" behavior for zarith is really weird, it returns Int.max_value. *) ~f:(fun x -> let trailing_zeros = trailing_zeros x in if equal zero x && Int.equal trailing_zeros Int.max_value then [%message (x : t) ("Int.max_value" : string)] else [%message (x : t) (trailing_zeros : int)]) (); [%expect {| ((hash d14ebff09189ec485d47fdb192b84435) (uniqueness_rate 85.742188)) |}] ;; end zarith_stubs_js-0.17.0/test/stats.mli000066400000000000000000000000511461647336100176430ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/untested.ml000066400000000000000000000003511461647336100201720ustar00rootroot00000000000000module Ml_z_init = struct (* Not necessary to test. The marshalling tests cover initialization *) end module Ml_z_mul_overflows = struct (* This function isn't exposed, and it's covered by being used in other tests *) end zarith_stubs_js-0.17.0/test/untested.mli000066400000000000000000000000511461647336100203400ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/test/util.ml000066400000000000000000000133701461647336100173210ustar00rootroot00000000000000open! Core open! Z let sexp_of_t t = Sexp.Atom (Z.to_string t) let is_zero a = equal zero a module Dynamic = struct let sexp_of = sexp_of_t let trials = 100 let get_generator_and_shrinker ~include_zero = let generator = Z.For_quickcheck.quickcheck_generator in let shrinker = Z.For_quickcheck.quickcheck_shrinker in if include_zero then generator, shrinker else ( let generator = Quickcheck.Generator.filter generator ~f:(fun a -> not (is_zero a)) in generator, shrinker) ;; let quickcheck ?(include_zero = true) ?(trials = trials) ~f () = let generator, shrinker = get_generator_and_shrinker ~include_zero in Quickcheck.test generator ~trials ~sexp_of ~shrinker ~f ;; let quickcheck_pair ?(include_zero = false) ~f () = let generator, shrinker = get_generator_and_shrinker ~include_zero in let generator = Quickcheck.Generator.tuple2 generator generator in let shrinker = Quickcheck.Shrinker.tuple2 shrinker shrinker in let sexp_of = Tuple2.sexp_of_t sexp_of sexp_of in let f (a, b) = f a b in Quickcheck.test generator ~trials ~sexp_of ~shrinker ~f ;; let quickcheck_tripple ?(include_zero = false) ~f () = let generator, shrinker = get_generator_and_shrinker ~include_zero in let generator = Quickcheck.Generator.tuple3 generator generator generator in let shrinker = Quickcheck.Shrinker.tuple3 shrinker shrinker shrinker in let sexp_of = Tuple3.sexp_of_t sexp_of sexp_of sexp_of in let f (a, b, c) = f a b c in Quickcheck.test generator ~trials ~sexp_of ~shrinker ~f ;; end module Static = struct (** For non-random input, the functions in this module read from the "stable-bigints.txt" file, run the requested computation, and then sexpify the results and print the hash of that result. This is here solely to verify that the Javascript and C implementations produce the same (hashed) output. If a test fails, toggle the [?verbose] argument to [true] and diff the output. *) let () = let regenerate_stable_quickcheck_file = match Sys.getenv "zarith_test_generate_sample" with | Some "true" -> true | Some _ -> failwith "zarith_test_generate_sample should be set to true or not set at all" | None -> false in if regenerate_stable_quickcheck_file then ( let q = Queue.create ~capacity:1024 () in while Int.( < ) (Queue.length q) 1024 do Dynamic.quickcheck ~trials:1024 () ~f:(fun x -> Queue.enqueue q x) done; Out_channel.with_file "stable_bigints.ml" ~f:(fun oc -> Out_channel.output_string oc "let all =\n"; Queue.iteri q ~f:(fun i z -> if Int.equal i 0 then Out_channel.output_string oc (sprintf " [| %S\n" (Z.to_string z)) else Out_channel.output_string oc (sprintf " ; %S\n" (Z.to_string z))); Out_channel.output_string oc " |]\n;;\n")) else () ;; let static_bigints = Stable_bigints.all |> Array.to_list |> List.map ~f:Z.of_string let bigint_seq_1 ~filter = List.filter static_bigints ~f:filter let bigint_seq_2 ~filter = let bigints = bigint_seq_1 ~filter in let a = List.drop bigints 0 in let b = List.drop bigints 1 in let zipped, _ = List.zip_with_remainder b a in zipped ;; let bigint_seq_3 ~filter = let bigints = bigint_seq_1 ~filter in let a = List.drop bigints 0 in let b = List.drop bigints 1 in let c = List.drop bigints 2 in let zipped, _ = List.zip_with_remainder a b in let zipped, _ = List.zip_with_remainder zipped c in List.map zipped ~f:(fun ((a, b), c) -> a, b, c) ;; let[@warning "-16"] qc ?(quiet = false) ~target ~verbose ~f = let q = Queue.create () in let target = target in List.iter target ~f:(fun a -> let result = List.map f ~f:(fun f -> try f a with | Division_by_zero -> Sexp.Atom "division-by-zero" | ex -> Sexp.Atom (Core.Exn.to_string ex)) in Queue.enqueue_all q result); let all_sexps = Queue.to_list q in let big_sexp = all_sexps |> List.map ~f:Sexp.to_string_hum |> String.concat ~sep:"\n" in let set_of_sexps = Sexp.Set.of_list all_sexps in let uniqueness_rate = (set_of_sexps |> Set.length |> Float.of_int) /. (all_sexps |> List.length |> Float.of_int) *. 100.0 in if verbose then print_endline big_sexp else (); let hash = big_sexp |> Md5_lib.string |> Md5_lib.to_hex in if not quiet then print_s [%message (hash : string) (uniqueness_rate : Float.Terse.t)] ;; let quickcheck ?quiet ?(verbose = false) ?(filter = Fn.const true) ~f () = qc ?quiet ~target:(bigint_seq_1 ~filter) ~verbose ~f:[ (fun a -> f a) ] ;; let quickcheck_pair ?quiet ?(verbose = false) ?(filter = Fn.const true) ~f () = let g m x = let a, b = m x in f a b in let permutation = [ Fn.id; Tuple2.swap ] in qc ?quiet ~target:(bigint_seq_2 ~filter) ~verbose ~f:(List.map permutation ~f:(fun p -> g p)) ;; let quickcheck_tripple ?quiet ?(verbose = false) ?(filter = Fn.const true) ~f () = let g m x = let a, b, c = m x in f a b c in let permutation = [ (fun (a, b, c) -> a, b, c) ; (fun (a, b, c) -> a, c, b) ; (fun (a, b, c) -> b, a, c) ; (fun (a, b, c) -> b, c, a) ; (fun (a, b, c) -> c, a, b) ; (fun (a, b, c) -> c, b, a) ] in qc ?quiet ~target:(bigint_seq_3 ~filter) ~verbose ~f:(List.map permutation ~f:(fun p -> g p)) ;; end module Filter = struct type nonrec t = t -> bool let not_zero = Fn.non is_zero let positive x = x > zero let small x = abs x < of_string "1024" let combine fs a = List.for_all fs ~f:(fun f -> f a) let odd x = equal one (bit_and x one) end zarith_stubs_js-0.17.0/test/util.mli000066400000000000000000000016041461647336100174670ustar00rootroot00000000000000open! Z open! Core module Dynamic : sig val quickcheck : ?include_zero:bool -> ?trials:int -> f:(t -> unit) -> unit -> unit val quickcheck_pair : ?include_zero:bool -> f:(t -> t -> unit) -> unit -> unit val quickcheck_tripple : ?include_zero:bool -> f:(t -> t -> t -> unit) -> unit -> unit end module Filter : sig type t val not_zero : t val positive : t val small : t val odd : t val combine : t list -> t end module Static : sig val quickcheck : ?quiet:bool -> ?verbose:bool -> ?filter:Filter.t -> f:(t -> Sexp.t) -> unit -> unit val quickcheck_pair : ?quiet:bool -> ?verbose:bool -> ?filter:Filter.t -> f:(t -> t -> Sexp.t) -> unit -> unit val quickcheck_tripple : ?quiet:bool -> ?verbose:bool -> ?filter:Filter.t -> f:(t -> t -> t -> Sexp.t) -> unit -> unit end val sexp_of_t : t -> Sexp.t zarith_stubs_js-0.17.0/test/z.ml000066400000000000000000000172701461647336100166200ustar00rootroot00000000000000open Core open Zarith include Zarith.Z module Redefinitions = struct let ( - ) = Z.( - ) let ( + ) = Z.( + ) let ( * ) = Z.( * ) let ( / ) = Z.( / ) let rem = Z.rem let ( ~- ) = Z.( ~- ) let neg = Z.neg let abs = Z.abs let succ = Z.succ let pred = Z.pred let equal = Z.equal let ( = ) = Z.equal let ( < ) = Z.lt let ( > ) = Z.gt let ( <= ) = Z.leq let ( >= ) = Z.geq let max = Z.max let min = Z.min let ascending = compare let shift_right = Z.shift_right let shift_left = Z.shift_left let bit_not = Z.lognot let bit_xor = Z.logxor let bit_or = Z.logor let bit_and = Z.logand let ( land ) = bit_and let ( lor ) = bit_or let ( lxor ) = bit_xor let lnot = bit_not let ( lsl ) = shift_left let ( asr ) = shift_right let of_int = Z.of_int let of_int32 = Z.of_int32 let of_int64 = Z.of_int64 let of_nativeint = Z.of_nativeint let of_float_unchecked = Z.of_float let of_float = Z.of_float let of_int_exn = of_int let of_int32_exn = of_int32 let of_int64_exn = of_int64 let of_nativeint_exn = of_nativeint let to_int_exn = Z.to_int let to_int32_exn = Z.to_int32 let to_int64_exn = Z.to_int64 let to_nativeint_exn = Z.to_nativeint let to_float = Z.to_float let zero = Z.zero let one = Z.one let minus_one = Z.minus_one let to_int t = if Z.fits_int t then Some (Z.to_int t) else None let to_int32 t = if Z.fits_int32 t then Some (Z.to_int32 t) else None let to_int64 t = if Z.fits_int64 t then Some (Z.to_int64 t) else None let to_nativeint t = if Z.fits_nativeint t then Some (Z.to_nativeint t) else None let ( <> ) x y = not (equal x y) let incr cell = cell := succ !cell let decr cell = cell := pred !cell let pow x y = Z.pow x (to_int_exn y) let ( ** ) x y = pow x y let popcount x = Z.popcount x let to_string = Z.to_string let ( /% ) x y = if Int.( >= ) (Z.sign y) 0 then Z.ediv x y else failwithf "%s.(%s /%% %s) : divisor must be positive" "Zarith_kernel" (to_string x) (to_string y) () ;; let ( % ) x y = if Int.( >= ) (Z.sign y) 0 then Z.erem x y else failwithf "%s.(%s %% %s) : divisor must be positive" "Zarith_kernel" (to_string x) (to_string y) () ;; let hash_fold_t state t = Int.hash_fold_t state (Z.hash t) end include Redefinitions module Make_random (State : sig type t val bits : t -> int val int : t -> int -> int end) : sig val random : state:State.t -> t -> t end = struct (* Uniform random generation of Bigint values. [random ~state range] chooses a [depth] and generates random values using [Random.State.bits state], called [1 lsl depth] times and concatenated. The preliminary result [n] therefore satisfies [0 <= n < 1 lsl (30 lsl depth)]. In order for the random choice to be uniform between [0] and [range-1], there must exist [k > 0] such that [n < k * range <= 1 lsl (30 lsl depth)]. If so, [n % range] is returned. Otherwise the random choice process is repeated from scratch. The [depth] value is chosen so that repeating is uncommon (1 in 1,000 or less). *) let bits_at_depth ~depth = Int.shift_left 30 depth let range_at_depth ~depth = shift_left one (bits_at_depth ~depth) let rec choose_bit_depth_for_range_from ~range ~depth = if range_at_depth ~depth >= range then depth else choose_bit_depth_for_range_from ~range ~depth:(Int.succ depth) ;; let choose_bit_depth_for_range ~range = choose_bit_depth_for_range_from ~range ~depth:0 let rec random_bigint_at_depth ~state ~depth = if Int.equal depth 0 then of_int (State.bits state) else ( let prev_depth = Int.pred depth in let prefix = random_bigint_at_depth ~state ~depth:prev_depth in let suffix = random_bigint_at_depth ~state ~depth:prev_depth in bit_or (shift_left prefix (bits_at_depth ~depth:prev_depth)) suffix) ;; let random_value_is_uniform_in_range ~range ~depth n = let k = range_at_depth ~depth / range in n < k * range ;; let rec large_random_at_depth ~state ~range ~depth = let result = random_bigint_at_depth ~state ~depth in if random_value_is_uniform_in_range ~range ~depth result then result % range else large_random_at_depth ~state ~range ~depth ;; let large_random ~state ~range = let tolerance_factor = of_int 1_000 in let depth = choose_bit_depth_for_range ~range:(range * tolerance_factor) in large_random_at_depth ~state ~range ~depth ;; let random ~state range = if range <= zero then failwithf "Bigint.random: argument %s <= 0" (to_string range) () (* Note that it's not safe to do [1 lsl 30] on a 32-bit machine (with 31-bit signed integers) *) else if range < shift_left one 30 then of_int (State.int state (to_int_exn range)) else large_random ~state ~range ;; end module Random_internal = Make_random (Random.State) module For_quickcheck : sig include Quickcheckable.S_int with type t := t val gen_negative : t Quickcheck.Generator.t val gen_positive : t Quickcheck.Generator.t end = struct module Generator = Quickcheck.Generator open Generator.Let_syntax module Uniform = Make_random (struct type t = Splittable_random.t let int t range = Splittable_random.int t ~lo:0 ~hi:(Int.pred range) let bits t = int t (Int.shift_left 1 30) end) let random_uniform ~state lo hi = lo + Uniform.random ~state (succ (hi - lo)) let gen_uniform_incl lower_bound upper_bound = if lower_bound > upper_bound then ( let lower_bound = to_string lower_bound in let upper_bound = to_string upper_bound in raise_s [%message "Bigint.gen_uniform_incl: bounds are crossed" (lower_bound : string) (upper_bound : string)]); Generator.create (fun ~size:_ ~random:state -> random_uniform ~state lower_bound upper_bound) ;; let gen_incl lower_bound upper_bound = Generator.weighted_union [ 0.05, Generator.return lower_bound ; 0.05, Generator.return upper_bound ; 0.9, gen_uniform_incl lower_bound upper_bound ] ;; let min_represented_by_n_bits n = if Int.equal n 0 then zero else shift_left one (Int.pred n) ;; let max_represented_by_n_bits n = pred (shift_left one n) let gen_log_uniform_incl lower_bound upper_bound = if lower_bound < zero || lower_bound > upper_bound then ( let lower_bound = to_string lower_bound in let upper_bound = to_string upper_bound in raise_s [%message "Bigint.gen_log_incl: invalid bounds" (lower_bound : string) (upper_bound : string)]); let min_bits = Z.numbits lower_bound in let max_bits = Z.numbits upper_bound in let%bind bits = Int.gen_uniform_incl min_bits max_bits in gen_uniform_incl (max lower_bound (min_represented_by_n_bits bits)) (min upper_bound (max_represented_by_n_bits bits)) ;; let gen_log_incl lower_bound upper_bound = Generator.weighted_union [ 0.05, Generator.return lower_bound ; 0.05, Generator.return upper_bound ; 0.9, gen_log_uniform_incl lower_bound upper_bound ] ;; let gen_positive = let%bind extra_bytes = Generator.size in let num_bytes = Int.succ extra_bytes in let num_bits = Int.( * ) num_bytes 8 in gen_log_uniform_incl one (pred (shift_left one num_bits)) ;; let gen_negative = Generator.map gen_positive ~f:neg let quickcheck_generator = Generator.weighted_union [ 0.45, gen_positive; 0.1, Generator.return zero; 0.45, gen_negative ] ;; let quickcheck_observer = Quickcheck.Observer.create (fun t ~size:_ ~hash -> hash_fold_t hash t) ;; let quickcheck_shrinker = Quickcheck.Shrinker.empty () end zarith_stubs_js-0.17.0/test/zarith.ml000066400000000000000000000001361461647336100176410ustar00rootroot00000000000000module Big_int_Z = Big_int_Z module Q = Q module Z = Z module Zarith_version = Zarith_version zarith_stubs_js-0.17.0/test/zarith_stubs_js_test.ml000066400000000000000000000000511461647336100226100ustar00rootroot00000000000000(* This file intentionally left blank *) zarith_stubs_js-0.17.0/zarith_stubs_js.opam000066400000000000000000000013611461647336100211230ustar00rootroot00000000000000opam-version: "2.0" version: "v0.17.0" maintainer: "Jane Street developers" authors: ["Jane Street Group, LLC"] homepage: "https://github.com/janestreet/zarith_stubs_js" bug-reports: "https://github.com/janestreet/zarith_stubs_js/issues" dev-repo: "git+https://github.com/janestreet/zarith_stubs_js.git" doc: "https://ocaml.janestreet.com/ocaml-core/latest/doc/zarith_stubs_js/index.html" license: "MIT" build: [ ["dune" "build" "-p" name "-j" jobs] ] depends: [ "ocaml" {>= "5.1.0"} "dune" {>= "3.11.0"} ] available: arch != "arm32" & arch != "x86_32" synopsis: "Javascripts stubs for the Zarith library" description: " This library contains no ocaml code, but instead implements all of the Zarith C stubs in Javascript for use in Js_of_ocaml "