float_test_pairs.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @fileoverview Test data for float encoding and decoding.
  3. */
  4. goog.module('protobuf.binary.floatTestPairs');
  5. const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
  6. const {createBufferDecoder} = goog.require('protobuf.binary.bufferDecoderHelper');
  7. /**
  8. * An array of Pairs of float values and their bit representation.
  9. * This is used to test encoding and decoding from/to the protobuf wire format.
  10. * @return {!Array<{name: string, floatValue:number, bufferDecoder:
  11. * !BufferDecoder}>}
  12. */
  13. function getFloatPairs() {
  14. const floatPairs = [
  15. {
  16. name: 'zero',
  17. floatValue: 0,
  18. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x00, 0x00),
  19. },
  20. {
  21. name: 'minus zero',
  22. floatValue: -0,
  23. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x00, 0x80)
  24. },
  25. {
  26. name: 'one ',
  27. floatValue: 1,
  28. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x80, 0x3F)
  29. },
  30. {
  31. name: 'minus one',
  32. floatValue: -1,
  33. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x80, 0xBF)
  34. },
  35. {
  36. name: 'two',
  37. floatValue: 2,
  38. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x00, 0x40)
  39. },
  40. {
  41. name: 'max float32',
  42. floatValue: Math.pow(2, 127) * (2 - 1 / Math.pow(2, 23)),
  43. bufferDecoder: createBufferDecoder(0xFF, 0xFF, 0x7F, 0x7F)
  44. },
  45. {
  46. name: 'min float32',
  47. floatValue: 1 / Math.pow(2, 127 - 1),
  48. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x80, 0x00)
  49. },
  50. {
  51. name: 'Infinity',
  52. floatValue: Infinity,
  53. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x80, 0x7F)
  54. },
  55. {
  56. name: 'minus Infinity',
  57. floatValue: -Infinity,
  58. bufferDecoder: createBufferDecoder(0x00, 0x00, 0x80, 0xFF)
  59. },
  60. {
  61. name: '1.5',
  62. floatValue: 1.5,
  63. bufferDecoder: createBufferDecoder(0x00, 0x00, 0xC0, 0x3F)
  64. },
  65. {
  66. name: '1.6',
  67. floatValue: 1.6,
  68. bufferDecoder: createBufferDecoder(0xCD, 0xCC, 0xCC, 0x3F)
  69. },
  70. ];
  71. return [...floatPairs];
  72. }
  73. exports = {getFloatPairs};