22/* global it */
33
44import assert from "assert" ;
5- import { buffer2address , address2buffer } from "../lib/index.ts" ;
5+ import { pinBuffer , address2buffer } from "../lib/index.ts" ;
66
77describe ( "buffer2address" , ( ) => {
88 it ( "should provide address of buffer as BigInt" , ( ) => {
99 const buffer = Buffer . alloc ( 32 ) ;
10- const addr = buffer2address ( { buffer } ) ;
11- assert . equal ( typeof addr , "bigint" ) ;
10+
11+ const pinnedBuffer = pinBuffer ( { buffer } ) ;
12+ assert . equal ( typeof pinnedBuffer . address , "bigint" ) ;
13+ pinnedBuffer . unpin ( ) ;
1214 } ) ;
1315
1416 // it("should throw an error when no arguments are given", () => {
@@ -25,22 +27,38 @@ describe("buffer2address", () => {
2527 // // @ts -expect-error testing wrong argument type
2628 // assert.throws(() => buffer2address("abc"));
2729 // });
30+
31+ it ( "should throw an error when unpinning twice" , ( ) => {
32+ const buffer = Buffer . alloc ( 16 ) ;
33+ const pinnedBuffer = pinBuffer ( { buffer } ) ;
34+
35+ pinnedBuffer . unpin ( ) ;
36+
37+ assert . throws ( ( ) => {
38+ pinnedBuffer . unpin ( ) ;
39+ } , ( err : Error ) => {
40+ assert . equal ( err . message , "buffer already unpinned" ) ;
41+ return true ;
42+ } ) ;
43+ } ) ;
2844} ) ;
2945
3046describe ( "address2buffer" , ( ) => {
3147 it ( "should create buffer from address correctly" , ( ) => {
3248 const size = 32 ;
3349 const buf = new Uint8Array ( size ) ;
34- const address = buffer2address ( { buffer : buf } ) ;
3550
36- const newBuf = address2buffer ( { address, size } ) ;
51+ const pinnedBuffer = pinBuffer ( { buffer : buf } ) ;
52+ const newBuf = address2buffer ( { address : pinnedBuffer . address , size } ) ;
3753
3854 const testValues = [ 0x22 , 0x33 , 0x44 ] ;
3955
4056 testValues . forEach ( ( value ) => {
4157 buf [ 0 ] = value ;
4258 assert . equal ( newBuf [ 0 ] , value ) ;
4359 } ) ;
60+
61+ pinnedBuffer . unpin ( ) ;
4462 } ) ;
4563
4664 // it("should fail on missing address parameter", () => {
0 commit comments