import test from "ava" ; import { PolusBuffer } from "../../lib/util/polusBuffer" ; test ( "reads a boolean" , ( t ) => { const buf = new PolusBuffer ( "0100" , true ) ; t . true ( buf . readBoolean ( ) ) ; t . false ( buf . readBoolean ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a boolean" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeBoolean ( true ) ; buf . writeBoolean ( false ) ; t . is ( "0100" , buf . buf . toString ( "hex" ) ) ; t . is ( 2 , buf . length ) ; } ) ; test ( "reads an int8" , ( t ) => { const buf = new PolusBuffer ( "7ff05680" , true ) ; t . is ( 127 , buf . read8 ( ) ) ; t . is ( - 16 , buf . read8 ( ) ) ; t . is ( 86 , buf . read8 ( ) ) ; t . is ( - 128 , buf . read8 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes an int8" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . write8 ( 127 ) ; buf . write8 ( - 16 ) ; buf . write8 ( 86 ) ; buf . write8 ( - 128 ) ; t . is ( "7ff05680" , buf . buf . toString ( "hex" ) ) ; t . is ( 4 , buf . length ) ; } ) ; test ( "reads a uint8" , ( t ) => { const buf = new PolusBuffer ( "fff056" , true ) ; t . is ( 255 , buf . readU8 ( ) ) ; t . is ( 240 , buf . readU8 ( ) ) ; t . is ( 86 , buf . readU8 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a uint8" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeU8 ( 255 ) ; buf . writeU8 ( 240 ) ; buf . writeU8 ( 86 ) ; t . is ( "fff056" , buf . buf . toString ( "hex" ) ) ; t . is ( 3 , buf . length ) ; } ) ; test ( "reads an int16" , ( t ) => { const buf = new PolusBuffer ( "ff7ff0f056560080" , true ) ; t . is ( 32767 , buf . read16 ( ) ) ; t . is ( - 3856 , buf . read16 ( ) ) ; t . is ( 22102 , buf . read16 ( ) ) ; t . is ( - 32768 , buf . read16 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes an int16" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . write16 ( 32767 ) ; buf . write16 ( - 3856 ) ; buf . write16 ( 22102 ) ; buf . write16 ( - 32768 ) ; t . is ( "ff7ff0f056560080" , buf . buf . toString ( "hex" ) ) ; t . is ( 8 , buf . length ) ; } ) ; test ( "reads a uint16" , ( t ) => { const buf = new PolusBuffer ( "fffff0f05656" , true ) ; t . is ( 65535 , buf . readU16 ( ) ) ; t . is ( 61680 , buf . readU16 ( ) ) ; t . is ( 22102 , buf . readU16 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a uint16" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeU16 ( 65535 ) ; buf . writeU16 ( 61680 ) ; buf . writeU16 ( 22102 ) ; t . is ( "fffff0f05656" , buf . buf . toString ( "hex" ) ) ; t . is ( 6 , buf . length ) ; } ) ; test ( "reads an int32" , ( t ) => { const buf = new PolusBuffer ( "ffffff7ff0f0f0f05656565600000080" , true ) ; t . is ( 2147483647 , buf . read32 ( ) ) ; t . is ( - 252645136 , buf . read32 ( ) ) ; t . is ( 1448498774 , buf . read32 ( ) ) ; t . is ( - 2147483648 , buf . read32 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes an int32" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . write32 ( 2147483647 ) ; buf . write32 ( - 252645136 ) ; buf . write32 ( 1448498774 ) ; buf . write32 ( - 2147483648 ) ; t . is ( "ffffff7ff0f0f0f05656565600000080" , buf . buf . toString ( "hex" ) ) ; t . is ( 16 , buf . length ) ; } ) ; test ( "reads a uint32" , ( t ) => { const buf = new PolusBuffer ( "fffffffff0f0f0f056565656" , true ) ; t . is ( 4294967295 , buf . readU32 ( ) ) ; t . is ( 4042322160 , buf . readU32 ( ) ) ; t . is ( 1448498774 , buf . readU32 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a uint32" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeU32 ( 4294967295 ) ; buf . writeU32 ( 4042322160 ) ; buf . writeU32 ( 1448498774 ) ; t . is ( "fffffffff0f0f0f056565656" , buf . buf . toString ( "hex" ) ) ; t . is ( 12 , buf . length ) ; } ) ; test ( "reads a float32" , ( t ) => { const buf = new PolusBuffer ( "00401cc600401c460000803f" , true ) ; t . is ( - 10000 , buf . readFloat32 ( ) ) ; t . is ( 10000 , buf . readFloat32 ( ) ) ; t . is ( 1 , buf . readFloat32 ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a float32" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeFloat32 ( - 10000 ) ; buf . writeFloat32 ( 10000 ) ; buf . writeFloat32 ( 1 ) ; t . is ( "00401cc600401c460000803f" , buf . buf . toString ( "hex" ) ) ; t . is ( 12 , buf . length ) ; } ) ; test ( "reads a packed int32" , ( t ) => { const buf = new PolusBuffer ( "dcfcffff0f45a08a20" , true ) ; t . is ( - 420 , buf . readVarInt ( ) ) ; t . is ( 69 , buf . readVarInt ( ) ) ; t . is ( 525600 , buf . readVarInt ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a packed int32" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeVarInt ( - 420 ) ; buf . writeVarInt ( 69 ) ; buf . writeVarInt ( 525600 ) ; t . is ( "dcfcffff0f45a08a20" , buf . buf . toString ( "hex" ) ) ; t . is ( 9 , buf . length ) ; } ) ; test ( "reads a packed uint32" , ( t ) => { const buf = new PolusBuffer ( "dcfcffff0f45a08a20" , true ) ; t . is ( 4294966876 , buf . readVarUInt ( ) ) ; t . is ( 69 , buf . readVarUInt ( ) ) ; t . is ( 525600 , buf . readVarUInt ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a packed uint32" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeVarUInt ( 4294966876 ) ; buf . writeVarUInt ( 69 ) ; buf . writeVarUInt ( 525600 ) ; t . is ( "dcfcffff0f45a08a20" , buf . buf . toString ( "hex" ) ) ; t . is ( 9 , buf . length ) ; } ) ; test ( "reads a string" , ( t ) => { const buf = new PolusBuffer ( "0548656c6c6f8008202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202cd094d0bcd0b8d182d180d0b8d0b920d098d0b2d0b0d0bdd0bed0b2d0b8d18720d09fd0b5d182d180d0bed0b200" , true ) ; t . is ( "Hello" , buf . readString ( ) ) ; t . is ( " " . repeat ( 1024 ) , buf . readString ( ) ) ; t . is ( "Дмитрий Иванович Петров" , buf . readString ( ) ) ; t . is ( "" , buf . readString ( ) ) ; t . true ( buf . dataRemaining . length == 0 ) ; } ) ; test ( "writes a string" , ( t ) => { const buf = new PolusBuffer ( ) ; buf . writeString ( "Hello" ) ; buf . writeString ( " " . repeat ( 1024 ) ) ; buf . writeString ( "Дмитрий Иванович Петров" ) ; buf . writeString ( "" ) ; t . is ( "0548656c6c6f8008202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202cd094d0bcd0b8d182d180d0b8d0b920d098d0b2d0b0d0bdd0bed0b2d0b8d18720d09fd0b5d182d180d0bed0b200" , buf . buf . toString ( "hex" ) ) ; t . is ( 1078 , buf . length ) ; } ) ;