Home Reference Source Test

test/atomic/float32.spec.js

  1. import { expect } from 'chai'
  2.  
  3. import AtomicFloat32 from '../../src/atomic/float32'
  4.  
  5. /** @test {AtomicFloat32} */
  6. describe('AtomicFloat32', () => {
  7. const bitArray = {
  8. 0: 70, 1: 25, 2: 124, 3: 237,
  9. }
  10.  
  11. let atomic
  12.  
  13. before(() => {
  14. atomic = new AtomicFloat32(9823.2312155)
  15. })
  16.  
  17. /** @test {AtomicFloat32#pack} */
  18. describe('pack', () => {
  19. let result
  20.  
  21. before(() => {
  22. result = atomic.pack()
  23. })
  24.  
  25. it('returns correct bits', () => {
  26. expect(JSON.stringify(result)).to.equal(JSON.stringify(bitArray))
  27. })
  28. })
  29.  
  30. /** @test {AtomicFloat32#unpack} */
  31. describe('unpack', () => {
  32. let returnValue
  33.  
  34. before(() => {
  35. const data = new Uint8Array(8)
  36. const dataView = new DataView(data.buffer)
  37.  
  38. dataView.setFloat32(0, 1.254999123, false)
  39.  
  40. returnValue = atomic.unpack(dataView, 0)
  41. })
  42.  
  43. it('returns a number', () => {
  44. expect(returnValue).to.be.a('number')
  45. })
  46.  
  47. it('sets the offset to 4', () => {
  48. expect(atomic.offset).to.equal(4)
  49. })
  50.  
  51. it('sets the value to a human readable float number', () => {
  52. expect(atomic.value).to.equal(Math.fround(1.254999123))
  53. })
  54. })
  55. })