@@ -11,18 +11,35 @@ import 'mocha';
1111import { expect } from 'chai' ;
1212
1313describe ( 'bitjs.io.BitBuffer' , ( ) => {
14+ /** @type {BitBuffer } */
1415 let buffer ;
1516
17+ it ( 'throws when invalid numBytes' , ( ) => {
18+ expect ( ( ) => new BitBuffer ( ) ) . throws ( ) ;
19+ } ) ;
20+
1621 describe ( 'least-to-most-significant bit-packing' , ( ) => {
1722 beforeEach ( ( ) => {
1823 buffer = new BitBuffer ( 4 ) ;
1924 } ) ;
2025
2126 it ( 'bit/byte pointers initialized properly' , ( ) => {
27+ expect ( buffer . getPackingDirection ( ) ) . equals ( false ) ;
2228 expect ( buffer . bytePtr ) . equals ( 0 ) ;
2329 expect ( buffer . bitPtr ) . equals ( 0 ) ;
24- } )
30+ } ) ;
2531
32+ it ( 'throws when writing invalid values' , ( ) => {
33+ expect ( ( ) => buffer . writeBits ( - 3 , 2 ) ) . throws ( ) ;
34+ expect ( ( ) => buffer . writeBits ( 3 , - 2 ) ) . throws ( ) ;
35+ expect ( ( ) => buffer . writeBits ( 0 , 54 ) ) . throws ( ) ;
36+ } ) ;
37+
38+ it ( 'throws when writing too many bits into the buffer' , ( ) => {
39+ buffer . writeBits ( 0 , 31 ) ; // thirty-one zeroes.
40+ expect ( ( ) => buffer . writeBits ( 1 , 2 ) ) . throws ( ) ;
41+ } ) ;
42+
2643 it ( 'write multiple bits' , ( ) => {
2744 buffer . writeBits ( 0b01011 , 5 ) ; // Should result in: 0b00001011.
2845 expect ( buffer . bytePtr ) . equals ( 0 ) ;
@@ -47,6 +64,26 @@ describe('bitjs.io.BitBuffer', () => {
4764 expect ( Array . from ( buffer . data ) ) . to . have . ordered . members (
4865 [ 0xfe , 0xff , 0x03 , 0x00 ] ) ;
4966 } ) ;
67+
68+ it ( 'properly changes bit-packing direction' , ( ) => {
69+ buffer . writeBits ( 3 , 2 ) ;
70+ expect ( buffer . data [ 0 ] ) . equals ( 3 ) ;
71+ expect ( buffer . bytePtr ) . equals ( 0 ) ;
72+ expect ( buffer . bitPtr ) . equals ( 2 ) ;
73+
74+ buffer . setPackingDirection ( true /** most to least significant */ ) ;
75+ expect ( buffer . bytePtr ) . equals ( 1 ) ;
76+ expect ( buffer . bitPtr ) . equals ( 7 ) ;
77+
78+ buffer . writeBits ( 7 , 3 ) ;
79+ expect ( buffer . data [ 0 ] ) . equals ( 3 ) ;
80+ expect ( buffer . data [ 1 ] ) . equals ( 224 ) ;
81+ } ) ;
82+
83+ it ( 'throws when switching packing direction and no more bytes left' , ( ) => {
84+ buffer . writeBits ( 0 , 25 ) ;
85+ expect ( ( ) => buffer . setPackingDirection ( true ) ) . throws ( ) ;
86+ } ) ;
5087 } ) ;
5188
5289 describe ( 'most-to-least-significant bit-packing' , ( ) => {
@@ -55,6 +92,7 @@ describe('bitjs.io.BitBuffer', () => {
5592 } ) ;
5693
5794 it ( 'bit/byte pointers initialized properly' , ( ) => {
95+ expect ( buffer . getPackingDirection ( ) ) . equals ( true ) ;
5896 expect ( buffer . bytePtr ) . equals ( 0 ) ;
5997 expect ( buffer . bitPtr ) . equals ( 7 ) ;
6098 } )
@@ -84,5 +122,25 @@ describe('bitjs.io.BitBuffer', () => {
84122 expect ( Array . from ( buffer . data ) ) . to . have . ordered . members (
85123 [ 0x7f , 0xff , 0xc0 , 0x00 ] ) ;
86124 } ) ;
125+
126+ it ( 'properly changes bit-packing direction' , ( ) => {
127+ buffer . writeBits ( 3 , 2 ) ;
128+ expect ( buffer . bytePtr ) . equals ( 0 ) ;
129+ expect ( buffer . bitPtr ) . equals ( 5 ) ;
130+ expect ( buffer . data [ 0 ] ) . equals ( 192 ) ;
131+
132+ buffer . setPackingDirection ( false /** least to most significant */ ) ;
133+ expect ( buffer . bytePtr ) . equals ( 1 ) ;
134+ expect ( buffer . bitPtr ) . equals ( 0 ) ;
135+
136+ buffer . writeBits ( 7 , 3 ) ;
137+ expect ( buffer . data [ 0 ] ) . equals ( 192 ) ;
138+ expect ( buffer . data [ 1 ] ) . equals ( 7 ) ;
139+ } ) ;
140+
141+ it ( 'throws when switching packing direction and no more bytes left' , ( ) => {
142+ buffer . writeBits ( 0 , 25 ) ;
143+ expect ( ( ) => buffer . setPackingDirection ( false ) ) . throws ( ) ;
144+ } ) ;
87145 } ) ;
88146} ) ;
0 commit comments