GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

2 months ago
  1. var tape = require("tape");
  2. var float = require("..");
  3. tape.test("float", function(test) {
  4. // default
  5. test.test(test.name + " - typed array", function(test) {
  6. runTest(float, test);
  7. });
  8. // ieee754
  9. test.test(test.name + " - fallback", function(test) {
  10. var F32 = global.Float32Array,
  11. F64 = global.Float64Array;
  12. delete global.Float32Array;
  13. delete global.Float64Array;
  14. runTest(float({}), test);
  15. global.Float32Array = F32;
  16. global.Float64Array = F64;
  17. });
  18. });
  19. function runTest(float, test) {
  20. var common = [
  21. 0,
  22. -0,
  23. Infinity,
  24. -Infinity,
  25. 0.125,
  26. 1024.5,
  27. -4096.5,
  28. NaN
  29. ];
  30. test.test(test.name + " - using 32 bits", function(test) {
  31. common.concat([
  32. 3.4028234663852886e+38,
  33. 1.1754943508222875e-38,
  34. 1.1754946310819804e-39
  35. ])
  36. .forEach(function(value) {
  37. var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
  38. test.ok(
  39. checkValue(value, 4, float.readFloatLE, float.writeFloatLE, Buffer.prototype.writeFloatLE),
  40. "should write and read back " + strval + " (32 bit LE)"
  41. );
  42. test.ok(
  43. checkValue(value, 4, float.readFloatBE, float.writeFloatBE, Buffer.prototype.writeFloatBE),
  44. "should write and read back " + strval + " (32 bit BE)"
  45. );
  46. });
  47. test.end();
  48. });
  49. test.test(test.name + " - using 64 bits", function(test) {
  50. common.concat([
  51. 1.7976931348623157e+308,
  52. 2.2250738585072014e-308,
  53. 2.2250738585072014e-309
  54. ])
  55. .forEach(function(value) {
  56. var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
  57. test.ok(
  58. checkValue(value, 8, float.readDoubleLE, float.writeDoubleLE, Buffer.prototype.writeDoubleLE),
  59. "should write and read back " + strval + " (64 bit LE)"
  60. );
  61. test.ok(
  62. checkValue(value, 8, float.readDoubleBE, float.writeDoubleBE, Buffer.prototype.writeDoubleBE),
  63. "should write and read back " + strval + " (64 bit BE)"
  64. );
  65. });
  66. test.end();
  67. });
  68. test.end();
  69. }
  70. function checkValue(value, size, read, write, write_comp) {
  71. var buffer = new Buffer(size);
  72. write(value, buffer, 0);
  73. var value_comp = read(buffer, 0);
  74. var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
  75. if (value !== value) {
  76. if (value_comp === value_comp)
  77. return false;
  78. } else if (value_comp !== value)
  79. return false;
  80. var buffer_comp = new Buffer(size);
  81. write_comp.call(buffer_comp, value, 0);
  82. for (var i = 0; i < size; ++i)
  83. if (buffer[i] !== buffer_comp[i]) {
  84. console.error(">", buffer, buffer_comp);
  85. return false;
  86. }
  87. return true;
  88. }