@@ -3,11 +3,12 @@ import { Uint32, Uint53, Uint64 } from "./integers";
33
44describe ( "Decimal" , ( ) => {
55 describe ( "fromAtomics" , ( ) => {
6- it ( "leads to correct atomics value" , ( ) => {
6+ it ( "leads to correct atomics value (string) " , ( ) => {
77 expect ( Decimal . fromAtomics ( "1" , 0 ) . atomics ) . toEqual ( "1" ) ;
88 expect ( Decimal . fromAtomics ( "1" , 1 ) . atomics ) . toEqual ( "1" ) ;
99 expect ( Decimal . fromAtomics ( "1" , 2 ) . atomics ) . toEqual ( "1" ) ;
1010
11+ expect ( Decimal . fromAtomics ( "0" , 5 ) . atomics ) . toEqual ( "0" ) ;
1112 expect ( Decimal . fromAtomics ( "1" , 5 ) . atomics ) . toEqual ( "1" ) ;
1213 expect ( Decimal . fromAtomics ( "2" , 5 ) . atomics ) . toEqual ( "2" ) ;
1314 expect ( Decimal . fromAtomics ( "3" , 5 ) . atomics ) . toEqual ( "3" ) ;
@@ -24,24 +25,51 @@ describe("Decimal", () => {
2425 expect ( Decimal . fromAtomics ( "00044" , 5 ) . atomics ) . toEqual ( "44" ) ;
2526 } ) ;
2627
28+ it ( "leads to correct atomics value (bigint)" , ( ) => {
29+ expect ( Decimal . fromAtomics ( 1n , 0 ) . atomics ) . toEqual ( "1" ) ;
30+ expect ( Decimal . fromAtomics ( 1n , 1 ) . atomics ) . toEqual ( "1" ) ;
31+ expect ( Decimal . fromAtomics ( 1n , 2 ) . atomics ) . toEqual ( "1" ) ;
32+
33+ expect ( Decimal . fromAtomics ( 0n , 5 ) . atomics ) . toEqual ( "0" ) ;
34+ expect ( Decimal . fromAtomics ( 1n , 5 ) . atomics ) . toEqual ( "1" ) ;
35+ expect ( Decimal . fromAtomics ( 2n , 5 ) . atomics ) . toEqual ( "2" ) ;
36+ expect ( Decimal . fromAtomics ( 3n , 5 ) . atomics ) . toEqual ( "3" ) ;
37+ expect ( Decimal . fromAtomics ( 10n , 5 ) . atomics ) . toEqual ( "10" ) ;
38+ expect ( Decimal . fromAtomics ( 20n , 5 ) . atomics ) . toEqual ( "20" ) ;
39+ expect ( Decimal . fromAtomics ( 30n , 5 ) . atomics ) . toEqual ( "30" ) ;
40+ expect ( Decimal . fromAtomics ( 100000000000000000000000n , 5 ) . atomics ) . toEqual ( "100000000000000000000000" ) ;
41+ expect ( Decimal . fromAtomics ( 200000000000000000000000n , 5 ) . atomics ) . toEqual ( "200000000000000000000000" ) ;
42+ expect ( Decimal . fromAtomics ( 300000000000000000000000n , 5 ) . atomics ) . toEqual ( "300000000000000000000000" ) ;
43+ } ) ;
44+
2745 it ( "reads fractional digits correctly" , ( ) => {
2846 expect ( Decimal . fromAtomics ( "44" , 0 ) . toString ( ) ) . toEqual ( "44" ) ;
2947 expect ( Decimal . fromAtomics ( "44" , 1 ) . toString ( ) ) . toEqual ( "4.4" ) ;
3048 expect ( Decimal . fromAtomics ( "44" , 2 ) . toString ( ) ) . toEqual ( "0.44" ) ;
3149 expect ( Decimal . fromAtomics ( "44" , 3 ) . toString ( ) ) . toEqual ( "0.044" ) ;
3250 expect ( Decimal . fromAtomics ( "44" , 4 ) . toString ( ) ) . toEqual ( "0.0044" ) ;
51+ expect ( Decimal . fromAtomics ( 44n , 0 ) . toString ( ) ) . toEqual ( "44" ) ;
52+ expect ( Decimal . fromAtomics ( 44n , 1 ) . toString ( ) ) . toEqual ( "4.4" ) ;
53+ expect ( Decimal . fromAtomics ( 44n , 2 ) . toString ( ) ) . toEqual ( "0.44" ) ;
54+ expect ( Decimal . fromAtomics ( 44n , 3 ) . toString ( ) ) . toEqual ( "0.044" ) ;
55+ expect ( Decimal . fromAtomics ( 44n , 4 ) . toString ( ) ) . toEqual ( "0.0044" ) ;
3356 } ) ;
3457
3558 it ( "throws for atomics that are not non-negative integers" , ( ) => {
3659 expect ( ( ) => Decimal . fromAtomics ( "0xAA" , 0 ) ) . toThrowError (
37- "Invalid string format. Only non-negative integers in decimal representation supported." ,
60+ "Invalid string format. Only integers in decimal representation supported." ,
3861 ) ;
3962 expect ( ( ) => Decimal . fromAtomics ( "" , 0 ) ) . toThrowError (
40- "Invalid string format. Only non-negative integers in decimal representation supported." ,
63+ "Invalid string format. Only integers in decimal representation supported." ,
4164 ) ;
42- expect ( ( ) => Decimal . fromAtomics ( "-1 " , 0 ) ) . toThrowError (
43- "Invalid string format. Only non-negative integers in decimal representation supported." ,
65+ expect ( ( ) => Decimal . fromAtomics ( "--2 " , 0 ) ) . toThrowError (
66+ "Invalid string format. Only integers in decimal representation supported." ,
4467 ) ;
68+ expect ( ( ) => Decimal . fromAtomics ( "0.7" , 0 ) ) . toThrowError (
69+ "Invalid string format. Only integers in decimal representation supported." ,
70+ ) ;
71+
72+ expect ( ( ) => Decimal . fromAtomics ( "-1" , 0 ) ) . toThrowError ( "Only non-negative values supported." ) ;
4573 } ) ;
4674 } ) ;
4775
0 commit comments