@@ -371,3 +371,234 @@ suite('BitBuffer', function () {
371371 assert . equal ( 0xFFFF , buffer . readUInt16LE ( 0 ) ) ;
372372 } ) ;
373373} ) ;
374+
375+ suite ( 'Reading big/little endian' , function ( ) {
376+ var array , u8 , bv , bsw , bsr ;
377+
378+ setup ( function ( ) {
379+ array = new ArrayBuffer ( 64 ) ;
380+ u8 = new Uint8Array ( array ) ;
381+ u8 [ 0 ] = 0x01 ;
382+ u8 [ 1 ] = 0x02 ;
383+ // Test initializing straight from the array.
384+ bsr = new BitStream ( array ) ;
385+ } ) ;
386+
387+ test ( '4b, little-endian' , function ( ) {
388+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
389+
390+ var result = [ ] ;
391+ result . push ( bsr . readBits ( 4 ) ) ;
392+ result . push ( bsr . readBits ( 4 ) ) ;
393+ result . push ( bsr . readBits ( 4 ) ) ;
394+ result . push ( bsr . readBits ( 4 ) ) ;
395+
396+ // 0000 0001 0000 0010 [01 02]
397+ // [#2] [#1] [#4] [#3]
398+ assert . deepEqual ( result , [ 1 , 0 , 2 , 0 ] ) ;
399+ } ) ;
400+
401+ test ( '8b, little-endian' , function ( ) {
402+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
403+
404+ var result = [ ] ;
405+ result . push ( bsr . readBits ( 8 ) ) ;
406+ result . push ( bsr . readBits ( 8 ) ) ;
407+
408+ // 0000 0001 0000 0010 [01 02]
409+ // [ #1] [ #2]
410+ assert . deepEqual ( result , [ 1 , 2 ] ) ;
411+ } ) ;
412+
413+ test ( '10b, little-endian' , function ( ) {
414+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
415+
416+ var result = [ ] ;
417+ result . push ( bsr . readBits ( 10 ) ) ;
418+
419+ // 0000 0001 0000 0010 [01 02]
420+ // ... #1] [ #2][#1...
421+ assert . deepEqual ( result , [ 513 ] ) ;
422+ } ) ;
423+
424+ test ( '16b, little-endian' , function ( ) {
425+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
426+
427+ var result = [ ] ;
428+ result . push ( bsr . readBits ( 16 ) ) ;
429+
430+ // 0000 0001 0000 0010 [01 02]
431+ // [ #1]
432+ assert . deepEqual ( result , [ 0x201 ] ) ;
433+ } ) ;
434+
435+ test ( '24b, little-endian' , function ( ) {
436+ u8 [ 2 ] = 0x03 ;
437+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
438+
439+ var result = [ ] ;
440+ result . push ( bsr . readBits ( 24 ) ) ;
441+
442+ // 0000 0001 0000 0010 0000 0011 [01 02 03]
443+ // [ #1]
444+ assert . deepEqual ( result , [ 0x30201 ] ) ;
445+ } ) ;
446+
447+ test ( '4b, big-endian' , function ( ) {
448+ bsr . bigEndian = true ;
449+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
450+
451+ var result = [ ] ;
452+ result . push ( bsr . readBits ( 4 ) ) ;
453+ result . push ( bsr . readBits ( 4 ) ) ;
454+ result . push ( bsr . readBits ( 4 ) ) ;
455+ result . push ( bsr . readBits ( 4 ) ) ;
456+
457+ // 0000 0001 0000 0010 [01 02]
458+ // [#1] [#2] [#3] [#4]
459+ assert . deepEqual ( result , [ 0 , 1 , 0 , 2 ] ) ;
460+ } ) ;
461+
462+ test ( '8b, big-endian' , function ( ) {
463+ bsr . bigEndian = true ;
464+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
465+
466+ var result = [ ] ;
467+ result . push ( bsr . readBits ( 8 ) ) ;
468+ result . push ( bsr . readBits ( 8 ) ) ;
469+
470+ // 0000 0001 0000 0010 [01 02]
471+ // [ #1] [ #2]
472+ assert . deepEqual ( result , [ 1 , 2 ] ) ;
473+ } ) ;
474+
475+ test ( '10b, big-endian' , function ( ) {
476+ bsr . bigEndian = true ;
477+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
478+
479+ var result = [ ] ;
480+ result . push ( bsr . readBits ( 10 ) ) ;
481+ result . push ( bsr . readBits ( 6 ) ) ;
482+
483+ // 0000 0001 0000 0010 [01 02]
484+ // [ #1][ #2]
485+ assert . deepEqual ( result , [ 4 , 2 ] ) ;
486+ } ) ;
487+
488+ test ( '16b, big-endian' , function ( ) {
489+ bsr . bigEndian = true ;
490+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
491+
492+ var result = [ ] ;
493+ result . push ( bsr . readBits ( 16 ) ) ;
494+
495+ // 0000 0001 0000 0010 [01 02]
496+ // [ #1]
497+ assert . deepEqual ( result , [ 0x102 ] ) ;
498+ } ) ;
499+
500+ test ( '24b, big-endian' , function ( ) {
501+ u8 [ 2 ] = 0x03 ;
502+ bsr . bigEndian = true ;
503+ assert . equal ( bsr . index , 0 , 'BitStream didn\'t init at offset 0' ) ;
504+
505+ var result = [ ] ;
506+ result . push ( bsr . readBits ( 24 ) ) ;
507+
508+ // 0000 0001 0000 0010 0000 0011 [01 02 03]
509+ // [ #1]
510+ assert . deepEqual ( result , [ 0x10203 ] ) ;
511+ } ) ;
512+ } ) ;
513+
514+ suite ( 'Writing big/little endian' , function ( ) {
515+ var array , u8 , bv , bsw , bsr ;
516+
517+ setup ( function ( ) {
518+ array = new ArrayBuffer ( 2 ) ;
519+ u8 = new Uint8Array ( array ) ;
520+ bv = new BitView ( array ) ;
521+ bsw = new BitStream ( bv ) ;
522+ } ) ;
523+
524+ test ( '4b, little-endian' , function ( ) {
525+ // 0000 0001 0000 0010 [01 02]
526+ // [#2] [#1] [#4] [#3]
527+ bsw . writeBits ( 1 , 4 ) ;
528+ bsw . writeBits ( 0 , 4 ) ;
529+ bsw . writeBits ( 2 , 4 ) ;
530+ bsw . writeBits ( 0 , 4 ) ;
531+
532+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
533+ } ) ;
534+
535+ test ( '8b, little-endian' , function ( ) {
536+ // 0000 0001 0000 0010 [01 02]
537+ // [ #1] [ #2]
538+ bsw . writeBits ( 1 , 8 ) ;
539+ bsw . writeBits ( 2 , 8 ) ;
540+
541+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
542+ } ) ;
543+
544+ test ( '10b, little-endian' , function ( ) {
545+ // 0000 0001 0000 0010 [01 02]
546+ // ... #1] [ #2][#1...
547+ bsw . writeBits ( 513 , 10 ) ;
548+
549+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
550+ } ) ;
551+
552+ test ( '16b, little-endian' , function ( ) {
553+ // 0000 0001 0000 0010 [01 02]
554+ // [ #1]
555+ bsw . writeBits ( 0x201 , 16 ) ;
556+
557+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
558+ } ) ;
559+
560+ test ( '4b, big-endian' , function ( ) {
561+ bsw . bigEndian = true ;
562+
563+ // 0000 0001 0000 0010 [01 02]
564+ // [#1] [#2] [#3] [#4]
565+ bsw . writeBits ( 0 , 4 ) ;
566+ bsw . writeBits ( 1 , 4 ) ;
567+ bsw . writeBits ( 0 , 4 ) ;
568+ bsw . writeBits ( 2 , 4 ) ;
569+
570+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
571+ } ) ;
572+
573+ test ( '8b, big-endian' , function ( ) {
574+ bsw . bigEndian = true ;
575+
576+ // 0000 0001 0000 0010 [01 02]
577+ // [ #1] [ #2]
578+ bsw . writeBits ( 1 , 8 ) ;
579+ bsw . writeBits ( 2 , 8 ) ;
580+
581+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
582+ } ) ;
583+
584+ test ( '10b, big-endian' , function ( ) {
585+ bsw . bigEndian = true ;
586+
587+ // 0000 0001 0000 0010 [01 02]
588+ // [ #1][ #2]
589+ bsw . writeBits ( 4 , 10 ) ;
590+ bsw . writeBits ( 2 , 6 ) ;
591+
592+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
593+ } ) ;
594+
595+ test ( '16b, big-endian' , function ( ) {
596+ bsw . bigEndian = true ;
597+
598+ // 0000 0001 0000 0010 [01 02]
599+ // [ #1]
600+ bsw . writeBits ( 0x102 , 16 ) ;
601+
602+ assert . deepEqual ( u8 , [ 0x01 , 0x02 ] ) ;
603+ } ) ;
604+ } ) ;
0 commit comments