mightyplow 5 роки тому
батько
коміт
2600ba15ec
1 змінених файлів з 80 додано та 0 видалено
  1. 80 0
      tests/flags.test.ts

+ 80 - 0
tests/flags.test.ts

@@ -0,0 +1,80 @@
+import { expect } from 'chai'
+import {
+    set,
+    unset,
+    toggle,
+    isset
+} from '../src/flags'
+
+describe('flags', function () {
+    describe('set', function () {
+        it('should set a single flag', function () {
+            expect(set(4, 0b10010)).to.eq(0b10110)
+        })
+
+        it('should set the correct bits on empty flags', function () {
+            const result = set([1, 2, 8, 32])
+            expect(result).to.eq(0b101011)
+        })
+    
+        it('should set the correct bits on exisiting flags', function () {
+            const settings = 0b1111
+            const result = set([1, 2, 8], settings)
+            expect(result).to.eq(0b1111)
+        })
+    })
+
+    describe('unset', function () {
+        it('should unset a single flag', function () {
+            const settings = 0b1111
+            const result = unset(2, settings)
+            expect(result).to.eq(0b1101)
+        })
+
+        it('should unset nothing on empty flags', function () {
+            const settings = 0
+            const result = unset([1, 2, 4, 8], settings)
+            expect(result).to.eq(0)
+        })
+
+        it('should unset all bits', function () {
+            const settings = 0b1111
+            const result = unset([1, 2, 4, 8], settings)
+            expect(result).to.eq(0)
+        })
+
+        it('should keep zeros as they are', function () {
+            const settings = 0b10101
+            const result = unset([1, 2, 8], settings)
+            expect(result).to.eq(0b10100)
+        })
+    })
+
+    describe('toggle', function () {
+        it('should invert all flags', function () {
+            const settings = 0b1111
+            const result = toggle([1, 2, 4, 8], settings)
+            expect(result).to.eq(0b0000)
+        })
+
+        it('should invert only specific flags', function () {
+            const settings = 0b1010
+            const result = toggle([8, 1], settings)
+            expect(result).to.eq(0b0011)
+        })
+    })
+
+    describe('isset', function () {
+        it('should check a single flag', function () {
+            expect(isset(4, 0b1101101)).to.eq(true)
+            expect(isset(16, 0b1101101)).to.eq(false)
+        })
+
+        it('should check multiple flags', function () {
+            expect(isset([4, 16, 2], 0b10010111)).to.eq(true)
+            expect(isset([8, 32, 64], 0b10010111)).to.eq(false)
+            expect(isset([1, 8], 0b10010111)).to.eq(false)
+            expect(isset([1, 4], 0b10010111)).to.eq(true)
+        })
+    })
+})