basic.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const test = require('tap').test;
  2. const cephes = require('../index.js');
  3. const almostEqual = require("./almost-equal.js");
  4. test('plain function', function (t) {
  5. almostEqual(t, cephes.exp2(3.4), Math.pow(2, 3.4));
  6. t.end();
  7. });
  8. test('extra function', function (t) {
  9. const [ret, {ai, aip, bi, bip}] = cephes.airy(1.2);
  10. almostEqual(t, ret, 0);
  11. // http://www.wolframalpha.com/input/?i=Ai(1.2),Ai%27(1.2),Bi(1.2),Bi%27(1.2)
  12. almostEqual(t, ai, 0.106126);
  13. almostEqual(t, aip, -0.132785);
  14. almostEqual(t, bi, 1.42113);
  15. almostEqual(t, bip, 1.22123);
  16. t.end();
  17. });
  18. test('array function', function (t) {
  19. almostEqual(t,
  20. cephes.polevl(1.1, new Float64Array([2.2, 3.3, 4.4].reverse()), 2),
  21. 2.2 + 3.3 * 1.1 + 4.4 * 1.1 * 1.1
  22. );
  23. t.end();
  24. });
  25. test('throw error', function (t) {
  26. t.throws(() => cephes.zeta(0, 1), new RangeError('cephes reports "argument domain error" in zeta'));
  27. t.throws(() => cephes.zeta(1.2, -1), new Error('cephes reports "function singularity" in zeta'));
  28. t.end();
  29. });
  30. test('isfinite handling', function (t) {
  31. t.equal(cephes.isfinite(NaN), 0);
  32. t.equal(cephes.isfinite(Infinity), 0);
  33. t.equal(cephes.isfinite(-Infinity), 0);
  34. t.equal(cephes.isfinite(-1), 1);
  35. t.equal(cephes.isfinite(1), 1);
  36. t.equal(cephes.isfinite(0), 1);
  37. t.end();
  38. });
  39. test('isnan handling', function (t) {
  40. t.equal(cephes.isnan(NaN), 1);
  41. t.equal(cephes.isnan(Infinity), 0);
  42. t.equal(cephes.isnan(-Infinity), 0);
  43. t.equal(cephes.isnan(-1), 0);
  44. t.equal(cephes.isnan(1), 0);
  45. t.equal(cephes.isnan(0), 0);
  46. t.end();
  47. });
  48. test('nan returned', function (t) {
  49. t.ok(Number.isNaN(cephes.gamma(-Infinity)));
  50. t.end();
  51. })
  52. test('infinity returned', function (t) {
  53. const MAXL10 = 308.2547155599167;
  54. t.equal(cephes.exp10(MAXL10 + 1), Infinity);
  55. t.end();
  56. })
  57. test("compiled is a promise", async function (t) {
  58. await cephes.compiled;
  59. almostEqual(t, cephes.exp(2), Math.exp(2));
  60. t.end();
  61. });