1+ import { NumericValue } from "@smithy/core/serde" ;
12import * as fs from "fs" ;
23// @ts -ignore
34import JSONbig from "json-bigint" ;
@@ -88,12 +89,12 @@ describe("cbor", () => {
8889 {
8990 name : "negative float" ,
9091 data : - 3015135.135135135 ,
91- cbor : allocByteArray ( [ 0b111_11011 , + 193 , + 71 , + 0 , + 239 , + 145 , + 76 , + 27 , + 173 ] ) ,
92+ cbor : allocByteArray ( [ 0b111_11011 , 193 , 71 , 0 , 239 , 145 , 76 , 27 , 173 ] ) ,
9293 } ,
9394 {
9495 name : "positive float" ,
9596 data : 3015135.135135135 ,
96- cbor : allocByteArray ( [ 0b111_11011 , + 65 , + 71 , + 0 , + 239 , + 145 , + 76 , + 27 , + 173 ] ) ,
97+ cbor : allocByteArray ( [ 0b111_11011 , 65 , 71 , 0 , 239 , 145 , 76 , 27 , 173 ] ) ,
9798 } ,
9899 {
99100 name : "various numbers" ,
@@ -214,6 +215,18 @@ describe("cbor", () => {
214215 65 , 109 , 110 , 101 , 115 , 116 , 101 , 100 , 32 , 105 , 116 , 101 , 109 , 32 , 66 ,
215216 ] ) ,
216217 } ,
218+ {
219+ name : "object containing big numbers" ,
220+ data : {
221+ map : {
222+ items : [ BigInt ( 1e80 ) ] ,
223+ } ,
224+ } ,
225+ cbor : allocByteArray ( [
226+ 161 , 99 , 109 , 97 , 112 , 161 , 101 , 105 , 116 , 101 , 109 , 115 , 129 , 194 , 88 , 34 , 3 , 95 , 157 , 234 , 62 , 31 , 107 , 224 ,
227+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
228+ ] ) ,
229+ } ,
217230 ] ;
218231
219232 const toBytes = ( hex : string ) => {
@@ -226,6 +239,72 @@ describe("cbor", () => {
226239 } ;
227240
228241 describe ( "locally curated scenarios" , ( ) => {
242+ it ( "should round-trip bigInteger to major 6 with tag 2" , ( ) => {
243+ const bigInt = BigInt ( "1267650600228229401496703205376" ) ;
244+ const serialized = cbor . serialize ( bigInt ) ;
245+
246+ const major = serialized [ 0 ] >> 5 ;
247+ expect ( major ) . toEqual ( 0b110 ) ; // 6
248+
249+ const tag = serialized [ 0 ] & 0b11111 ;
250+ expect ( tag ) . toEqual ( 0b010 ) ; // 2
251+
252+ const byteStringCount = serialized [ 1 ] ;
253+ expect ( byteStringCount ) . toEqual ( 0b010_01101 ) ; // major 2, 13 bytes
254+
255+ const byteString = serialized . slice ( 2 ) ;
256+ expect ( byteString ) . toEqual ( allocByteArray ( [ 0b000_10000 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
257+
258+ const deserialized = cbor . deserialize ( serialized ) ;
259+ expect ( deserialized ) . toEqual ( bigInt ) ;
260+ } ) ;
261+
262+ it ( "should round-trip negative bigInteger to major 6 with tag 3" , ( ) => {
263+ const bigInt = BigInt ( "-1267650600228229401496703205377" ) ;
264+ const serialized = cbor . serialize ( bigInt ) ;
265+
266+ const major = serialized [ 0 ] >> 5 ;
267+ expect ( major ) . toEqual ( 0b110 ) ; // 6
268+
269+ const tag = serialized [ 0 ] & 0b11111 ;
270+ expect ( tag ) . toEqual ( 0b011 ) ; // 3
271+
272+ const byteStringCount = serialized [ 1 ] ;
273+ expect ( byteStringCount ) . toEqual ( 0b010_01101 ) ; // major 2, 13 bytes
274+
275+ const byteString = serialized . slice ( 2 ) ;
276+ expect ( byteString ) . toEqual ( allocByteArray ( [ 0b000_10000 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
277+
278+ const deserialized = cbor . deserialize ( serialized ) ;
279+ expect ( deserialized ) . toEqual ( bigInt ) ;
280+ } ) ;
281+
282+ it ( "should round-trip NumericValue to major 6 with tag 4" , ( ) => {
283+ for ( const bigDecimal of [
284+ "10000000000000000000000054.321" ,
285+ "1000000000000000000000000000000000054.134134321" ,
286+ "100000000000000000000000000000000000054.0000000000000001" ,
287+ "100000000000000000000000000000000000054.00510351095130000" ,
288+ "-10000000000000000000000054.321" ,
289+ "-1000000000000000000000000000000000054.134134321" ,
290+ "-100000000000000000000000000000000000054.0000000000000001" ,
291+ "-100000000000000000000000000000000000054.00510351095130000" ,
292+ ] ) {
293+ const nv = new NumericValue ( bigDecimal , "bigDecimal" ) ;
294+ const serialized = cbor . serialize ( nv ) ;
295+
296+ const major = serialized [ 0 ] >> 5 ;
297+ expect ( major ) . toEqual ( 0b110 ) ; // 6
298+
299+ const tag = serialized [ 0 ] & 0b11111 ;
300+ expect ( tag ) . toEqual ( 0b0100 ) ; // 4
301+
302+ const deserialized = cbor . deserialize ( serialized ) ;
303+ expect ( deserialized ) . toEqual ( nv ) ;
304+ expect ( deserialized . string ) . toEqual ( nv . string ) ;
305+ }
306+ } ) ;
307+
229308 it ( "should throw an error if serializing a tag with missing properties" , ( ) => {
230309 expect ( ( ) =>
231310 cbor . serialize ( {
0 commit comments