test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var alloc = require('buffer-alloc')
  2. var tape = require('tape')
  3. var bitfield = require('./')
  4. tape('set and get', function (t) {
  5. var bits = bitfield()
  6. t.same(bits.get(0), false, 'first bit is false')
  7. bits.set(0, true)
  8. t.same(bits.get(0), true, 'first bit is true')
  9. t.same(bits.get(1), false, 'second bit is false')
  10. bits.set(0, false)
  11. t.same(bits.get(0), false, 'first bit is reset')
  12. t.end()
  13. })
  14. tape('set large and get', function (t) {
  15. var bits = bitfield()
  16. t.same(bits.get(9999999999999), false, 'large bit is false')
  17. bits.set(9999999999999, true)
  18. t.same(bits.get(9999999999999), true, 'large bit is true')
  19. t.same(bits.get(9999999999999 + 1), false, 'large bit + 1 is false')
  20. bits.set(9999999999999, false)
  21. t.same(bits.get(9999999999999), false, 'large bit is reset')
  22. t.end()
  23. })
  24. tape('get and set buffer', function (t) {
  25. var bits = bitfield({trackUpdates: true})
  26. t.same(bits.pages.get(0, true), undefined)
  27. t.same(bits.pages.get(Math.floor(9999999999999 / 8 / 1024), true), undefined)
  28. bits.set(9999999999999, true)
  29. var bits2 = bitfield()
  30. var upd = bits.pages.lastUpdate()
  31. bits2.pages.set(Math.floor(upd.offset / 1024), upd.buffer)
  32. t.same(bits2.get(9999999999999), true, 'bit is set')
  33. t.end()
  34. })
  35. tape('toBuffer', function (t) {
  36. var bits = bitfield()
  37. t.same(bits.toBuffer(), alloc(0))
  38. bits.set(0, true)
  39. t.same(bits.toBuffer(), bits.pages.get(0).buffer)
  40. bits.set(9000, true)
  41. t.same(bits.toBuffer(), Buffer.concat([bits.pages.get(0).buffer, bits.pages.get(1).buffer]))
  42. t.end()
  43. })
  44. tape('pass in buffer', function (t) {
  45. var bits = bitfield()
  46. bits.set(0, true)
  47. bits.set(9000, true)
  48. var clone = bitfield(bits.toBuffer())
  49. t.same(clone.get(0), true)
  50. t.same(clone.get(9000), true)
  51. t.end()
  52. })
  53. tape('set small buffer', function (t) {
  54. var buf = alloc(1)
  55. buf[0] = 255
  56. var bits = bitfield(buf)
  57. t.same(bits.get(0), true)
  58. t.same(bits.pages.get(0).buffer.length, bits.pageSize)
  59. t.end()
  60. })