actual.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const fs = require('fs');
  2. const path = require('path');
  3. const test = require('tap').test;
  4. const cephes = require('../index.js');
  5. const almostEqual = require("./almost-equal.js");
  6. const expectedList = fs.readFileSync(
  7. path.resolve(__dirname, 'expected.ndjson')
  8. )
  9. .toString()
  10. .split('\n')
  11. .slice(0, -1)
  12. .map((line) => JSON.parse(line));
  13. const mtherr_codemsg = new Map([
  14. [0, 'unknown error'],
  15. [1, 'argument domain error'],
  16. [2, 'function singularity'],
  17. [3, 'overflow range error'],
  18. [4, 'underflow range error'],
  19. [5, 'total loss of precision'],
  20. [6, 'partial loss of precision'],
  21. [33, 'Unix domain error code'],
  22. [34, 'Unix range error code']
  23. ]);
  24. for (const { fn, ret, args, extra, error_code } of expectedList) {
  25. const hasExtra = Object.keys(extra).length > 0;
  26. const parsedArgs = args.map(function (arg) {
  27. if (Array.isArray(arg)) return new Float64Array(arg);
  28. else return arg;
  29. });
  30. test(`random samples from ${fn}`, function (t) {
  31. if (error_code !== -1) {
  32. const codemsg = mtherr_codemsg.get(error_code);
  33. const message = new RegExp('^cephes reports "' + codemsg + '" in [a-z0-9]+$');
  34. t.throws(() => cephes[fn].apply(null, parsedArgs), message);
  35. t.end();
  36. return;
  37. }
  38. if (hasExtra) {
  39. let [actualRet, actualExtra] = cephes[fn].apply(null, parsedArgs);
  40. almostEqual(t, actualRet, ret);
  41. for (const extraKey of Object.keys(extra)) {
  42. almostEqual(t, actualExtra[extraKey], extra[extraKey]);
  43. }
  44. } else {
  45. let actualRet = cephes[fn].apply(null, parsedArgs);
  46. almostEqual(t, actualRet, ret);
  47. }
  48. t.end();
  49. });
  50. }