1- /*!
2- * byte - test/byte.test.js
3- *
4- * Copyright(c) 2013 - 2014
5- * MIT Licensed
6- *
7- * Authors:
8- * fengmk2 <[email protected] > (http://fengmk2.github.com) 9- * dead-horse <[email protected] > (https://github.com/dead-horse) 10- */
11-
12- "use strict" ;
1+ 'use strict' ;
132
143var Long = require ( 'long' ) ;
154var assert = require ( 'assert' ) ;
@@ -494,6 +483,149 @@ describe('byte.test.js', function () {
494483 assert ( bytes . toString ( ) === '<ByteBuffer>' ) ;
495484 } ) ;
496485
486+ it ( 'should 000000000xxxxxxx (0x0000 ~ 0x007f) => 0xxxxxxx (0x00 ~ 0x7f)' , function ( ) {
487+ // UTF-8
488+ var bytes = ByteBuffer . allocate ( 1 ) ;
489+ bytes . putString ( String . fromCharCode ( 0x0000 ) ) ;
490+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 01 00>' ) ;
491+ // CESU-8
492+ bytes = ByteBuffer . allocate ( 1 ) ;
493+ bytes . putRawString ( String . fromCharCode ( 0x0000 ) ) ;
494+ assert ( bytes . toString ( ) === '<ByteBuffer 00>' ) ;
495+
496+ // UTF-8
497+ bytes = ByteBuffer . allocate ( 1 ) ;
498+ bytes . putString ( String . fromCharCode ( 0x0001 ) ) ;
499+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 01 01>' ) ;
500+ // CESU-8
501+ bytes = ByteBuffer . allocate ( 1 ) ;
502+ bytes . putRawString ( String . fromCharCode ( 0x0001 ) ) ;
503+ assert ( bytes . toString ( ) === '<ByteBuffer 01>' ) ;
504+
505+ // UTF-8
506+ bytes = ByteBuffer . allocate ( 1 ) ;
507+ bytes . putString ( 'E' ) ; // 0x45
508+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 01 45>' ) ;
509+ // CESU-8
510+ bytes = ByteBuffer . allocate ( 1 ) ;
511+ bytes . putRawString ( 'E' ) ;
512+ assert ( bytes . toString ( ) === '<ByteBuffer 45>' ) ;
513+
514+ // UTF-8
515+ bytes = ByteBuffer . allocate ( 1 ) ;
516+ bytes . putString ( String . fromCharCode ( 0x7F ) ) ;
517+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 01 7f>' ) ;
518+ // CESU-8
519+ bytes = ByteBuffer . allocate ( 1 ) ;
520+ bytes . putRawString ( String . fromCharCode ( 0x7F ) ) ;
521+ assert ( bytes . toString ( ) === '<ByteBuffer 7f>' ) ;
522+ } ) ;
523+
524+ it ( 'should 00000yyyyyxxxxxx (0x0080 ~ 0x07ff) => 110yyyyy (0xc0 ~ 0xdf) | 10xxxxxx (0x80 ~ 0xbf)' , function ( ) {
525+ // UTF-8
526+ var bytes = ByteBuffer . allocate ( 1 ) ;
527+ bytes = ByteBuffer . allocate ( 1 ) ;
528+ bytes . putString ( String . fromCharCode ( 0x80 ) ) ;
529+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 02 c2 80>' ) ;
530+ // CESU-8
531+ bytes = ByteBuffer . allocate ( 1 ) ;
532+ bytes . putRawString ( String . fromCharCode ( 0x80 ) ) ;
533+ assert ( bytes . toString ( ) === '<ByteBuffer c2 80>' ) ;
534+
535+ // UTF-8
536+ bytes = ByteBuffer . allocate ( 1 ) ;
537+ bytes . putString ( 'ȅ' ) ; // 0x0205: 517
538+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 02 c8 85>' ) ;
539+ // CESU-8
540+ bytes = ByteBuffer . allocate ( 1 ) ;
541+ bytes . putRawString ( 'ȅ' ) ;
542+ assert ( bytes . toString ( ) === '<ByteBuffer c8 85>' ) ;
543+
544+ // UTF-8
545+ bytes = ByteBuffer . allocate ( 1 ) ;
546+ bytes . putString ( String . fromCharCode ( 0x81 ) ) ;
547+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 02 c2 81>' ) ;
548+ // CESU-8
549+ bytes = ByteBuffer . allocate ( 1 ) ;
550+ bytes . putRawString ( String . fromCharCode ( 0x81 ) ) ;
551+ assert ( bytes . toString ( ) === '<ByteBuffer c2 81>' ) ;
552+
553+ // UTF-8
554+ bytes = ByteBuffer . allocate ( 1 ) ;
555+ bytes . putString ( String . fromCharCode ( 0x7FE ) ) ;
556+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 02 df be>' ) ;
557+ // CESU-8
558+ bytes = ByteBuffer . allocate ( 1 ) ;
559+ bytes . putRawString ( String . fromCharCode ( 0x7FE ) ) ;
560+ assert ( bytes . toString ( ) === '<ByteBuffer df be>' ) ;
561+
562+ // UTF-8
563+ bytes = ByteBuffer . allocate ( 1 ) ;
564+ bytes . putString ( String . fromCharCode ( 0x7FF ) ) ;
565+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 02 df bf>' ) ;
566+ // CESU-8
567+ bytes = ByteBuffer . allocate ( 1 ) ;
568+ bytes . putRawString ( String . fromCharCode ( 0x7FF ) ) ;
569+ assert ( bytes . toString ( ) === '<ByteBuffer df bf>' ) ;
570+ } ) ;
571+
572+ it ( 'should zzzzyyyyyyxxxxxx (0x0800 ~ 0xffff) => 1110zzzz (0xe0 ~ 0xef) | 10yyyyyy (0x80 ~ 0xbf) | 10xxxxxx (0x80 ~ 0xbf)' , function ( ) {
573+ // UTF-8
574+ var bytes = ByteBuffer . allocate ( 1 ) ;
575+ bytes = ByteBuffer . allocate ( 1 ) ;
576+ bytes . putString ( String . fromCharCode ( 0x800 ) ) ;
577+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 03 e0 a0 80>' ) ;
578+ // CESU-8
579+ bytes = ByteBuffer . allocate ( 1 ) ;
580+ bytes . putRawString ( String . fromCharCode ( 0x800 ) ) ;
581+ assert ( bytes . toString ( ) === '<ByteBuffer e0 a0 80>' ) ;
582+
583+ // UTF-8
584+ bytes = ByteBuffer . allocate ( 1 ) ;
585+ bytes . putString ( String . fromCharCode ( 0x801 ) ) ;
586+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 03 e0 a0 81>' ) ;
587+ // CESU-8
588+ bytes = ByteBuffer . allocate ( 1 ) ;
589+ bytes . putRawString ( String . fromCharCode ( 0x801 ) ) ;
590+ assert ( bytes . toString ( ) === '<ByteBuffer e0 a0 81>' ) ;
591+
592+ // UTF-8
593+ bytes = ByteBuffer . allocate ( 1 ) ;
594+ bytes . putString ( '𐐀' ) ; // 0xD801 0xDC00
595+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 04 f0 90 90 80>' ) ;
596+ // CESU-8
597+ bytes = ByteBuffer . allocate ( 1 ) ;
598+ bytes . putRawString ( '𐐀' ) ;
599+ assert ( bytes . toString ( ) === '<ByteBuffer ed a0 81 ed b0 80>' ) ;
600+
601+ // UTF-8
602+ bytes = ByteBuffer . allocate ( 1 ) ;
603+ bytes . putString ( '\ud801\udc01' ) ; // 0xD801 0xDC01
604+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 04 f0 90 90 81>' ) ;
605+ // CESU-8
606+ bytes = ByteBuffer . allocate ( 1 ) ;
607+ bytes . putRawString ( '\ud801\udc01' ) ;
608+ assert ( bytes . toString ( ) === '<ByteBuffer ed a0 81 ed b0 81>' ) ;
609+
610+ // UTF-8
611+ bytes = ByteBuffer . allocate ( 1 ) ;
612+ bytes . putString ( String . fromCharCode ( 0xFFFE ) ) ;
613+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 03 ef bf be>' ) ;
614+ // CESU-8
615+ bytes = ByteBuffer . allocate ( 1 ) ;
616+ bytes . putRawString ( String . fromCharCode ( 0xFFFE ) ) ;
617+ assert ( bytes . toString ( ) === '<ByteBuffer ef bf be>' ) ;
618+
619+ // UTF-8
620+ bytes = ByteBuffer . allocate ( 1 ) ;
621+ bytes . putString ( String . fromCharCode ( 0xFFFF ) ) ;
622+ assert ( bytes . toString ( ) === '<ByteBuffer 00 00 00 03 ef bf bf>' ) ;
623+ // CESU-8
624+ bytes = ByteBuffer . allocate ( 1 ) ;
625+ bytes . putRawString ( String . fromCharCode ( 0xFFFF ) ) ;
626+ assert ( bytes . toString ( ) === '<ByteBuffer ef bf bf>' ) ;
627+ } ) ;
628+
497629 it ( 'should put emoji' , function ( ) {
498630 // utf8
499631 var bytes = ByteBuffer . allocate ( 1 ) ;
@@ -514,7 +646,7 @@ describe('byte.test.js', function () {
514646 bytes . putRawString ( str ) ;
515647 assert ( bytes . toString ( ) === '<ByteBuffer ed a0 bd ed b8 80 57 77 77 e9 82 a3>' ) ;
516648 assert . deepEqual ( bytes . getRawString ( 0 , 12 ) , str ) ;
517-
649+
518650 // Construction of a special test case which triggers the bug
519651 // of allocating insufficient space via _checkSize
520652 var bytes = ByteBuffer . allocate ( 4 ) ;
0 commit comments