flags.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { expect } from 'chai'
  2. import {
  3. set,
  4. unset,
  5. toggle,
  6. isset
  7. } from '../src/flags'
  8. describe('flags', function () {
  9. describe('set', function () {
  10. it('should set a single flag', function () {
  11. expect(set(4, 0b10010)).to.eq(0b10110)
  12. })
  13. it('should set the correct bits on empty flags', function () {
  14. const result = set([1, 2, 8, 32])
  15. expect(result).to.eq(0b101011)
  16. })
  17. it('should set the correct bits on exisiting flags', function () {
  18. const settings = 0b1111
  19. const result = set([1, 2, 8], settings)
  20. expect(result).to.eq(0b1111)
  21. })
  22. })
  23. describe('unset', function () {
  24. it('should unset a single flag', function () {
  25. const settings = 0b1111
  26. const result = unset(2, settings)
  27. expect(result).to.eq(0b1101)
  28. })
  29. it('should unset nothing on empty flags', function () {
  30. const settings = 0
  31. const result = unset([1, 2, 4, 8], settings)
  32. expect(result).to.eq(0)
  33. })
  34. it('should unset all bits', function () {
  35. const settings = 0b1111
  36. const result = unset([1, 2, 4, 8], settings)
  37. expect(result).to.eq(0)
  38. })
  39. it('should keep zeros as they are', function () {
  40. const settings = 0b10101
  41. const result = unset([1, 2, 8], settings)
  42. expect(result).to.eq(0b10100)
  43. })
  44. })
  45. describe('toggle', function () {
  46. it('should invert all flags', function () {
  47. const settings = 0b1111
  48. const result = toggle([1, 2, 4, 8], settings)
  49. expect(result).to.eq(0b0000)
  50. })
  51. it('should invert only specific flags', function () {
  52. const settings = 0b1010
  53. const result = toggle([8, 1], settings)
  54. expect(result).to.eq(0b0011)
  55. })
  56. })
  57. describe('isset', function () {
  58. it('should check a single flag', function () {
  59. expect(isset(4, 0b1101101)).to.eq(true)
  60. expect(isset(16, 0b1101101)).to.eq(false)
  61. })
  62. it('should check multiple flags', function () {
  63. expect(isset([4, 16, 2], 0b10010111)).to.eq(true)
  64. expect(isset([8, 32, 64], 0b10010111)).to.eq(false)
  65. expect(isset([1, 8], 0b10010111)).to.eq(false)
  66. expect(isset([1, 4], 0b10010111)).to.eq(true)
  67. })
  68. })
  69. })