equal.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function absoluteEqual(s) {
  2. for (var i = 0, l = s.map.length; i < l; i++) {
  3. var args = s.map[i].slice(0, -1);
  4. var last = s.map[i][ s.map[i].length - 1 ];
  5. var ret = s.fn.apply(null, args);
  6. if (Number.isFinite(last)) {
  7. s.test.ok(
  8. Math.abs(ret - last) < s.limit,
  9. `is ${ret} almost absolute equal ${last}`
  10. );
  11. } else {
  12. s.test.ok(
  13. Object.is(ret, last),
  14. `is ${ret} equal ${last}`
  15. );
  16. }
  17. }
  18. }
  19. exports.absoluteEqual = absoluteEqual;
  20. function relativeEqual(s) {
  21. for (var i = 0, l = s.map.length; i < l; i++) {
  22. var args = s.map[i].slice(0, -1);
  23. var last = s.map[i][ s.map[i].length - 1 ];
  24. if (Number.isFinite(last)) {
  25. if (last === 0) {
  26. s.test.ok(
  27. Math.abs( s.fn.apply(null, args) - last) < s.limit,
  28. 'almost absolute equal'
  29. );
  30. } else {
  31. s.test.ok(
  32. Math.abs( s.fn.apply(null, args) - last ) / last < s.limit,
  33. 'almost relative equal'
  34. );
  35. }
  36. } else {
  37. s.test.ok(
  38. Object.is(s.fn.apply(null, args), last),
  39. 'exact equal'
  40. );
  41. }
  42. }
  43. }
  44. exports.relativeEqual = relativeEqual;