1if (typeof T === 'undefined') require('../setup'); 2 3T('clone', function () { 4 5 function t(expression) { 6 T.assert(expression); 7 } 8 9 Decimal.config({ 10 precision: 10, 11 rounding: 4, 12 toExpNeg: -7, 13 toExpPos: 21, 14 minE: -9e15, 15 maxE: 9e15 16 }); 17 18 var D1 = Decimal.clone({ precision: 1 }); 19 var D2 = Decimal.clone({ precision: 2 }); 20 var D3 = Decimal.clone({ precision: 3 }); 21 var D4 = Decimal.clone({ precision: 4 }); 22 var D5 = Decimal.clone({ precision: 5 }); 23 var D6 = Decimal.clone({ precision: 6 }); 24 var D7 = Decimal.clone({ precision: 7 }); 25 var D8 = Decimal.clone(); 26 D8.config({ precision: 8 }); 27 var D9 = Decimal.clone({ precision: 9 }); 28 29 t(Decimal.prototype === D9.prototype); 30 t(Decimal !== D9); 31 32 var x = new Decimal(5); 33 var x1 = new D1(5); 34 var x2 = new D2(5); 35 var x3 = new D3(5); 36 var x4 = new D4(5); 37 var x5 = new D5(5); 38 var x6 = new D6(5); 39 var x7 = new D7(5); 40 var x8 = new D8(5); 41 var x9 = new D9(5); 42 43 t(x1.div(3).eq(2)); 44 t(x2.div(3).eq(1.7)); 45 t(x3.div(3).eq(1.67)); 46 t(x4.div(3).eq(1.667)); 47 t(x5.div(3).eq(1.6667)); 48 t(x6.div(3).eq(1.66667)); 49 t(x7.div(3).eq(1.666667)); 50 t(x8.div(3).eq(1.6666667)); 51 t(x9.div(3).eq(1.66666667)); 52 t(x .div(3).eq(1.666666667)); 53 54 var y = new Decimal(3); 55 var y1 = new D1(3); 56 var y2 = new D2(3); 57 var y3 = new D3(3); 58 var y4 = new D4(3); 59 var y5 = new D5(3); 60 var y6 = new D6(3); 61 var y7 = new D7(3); 62 var y8 = new D8(3); 63 var y9 = new D9(3); 64 65 t(x1.div(y1).eq(2)); 66 t(x2.div(y2).eq(1.7)); 67 t(x3.div(y3).eq(1.67)); 68 t(x4.div(y4).eq(1.667)); 69 t(x5.div(y5).eq(1.6667)); 70 t(x6.div(y6).eq(1.66667)); 71 t(x7.div(y7).eq(1.666667)); 72 t(x8.div(y8).eq(1.6666667)); 73 t(x9.div(y9).eq(1.66666667)); 74 t(x .div(y ).eq(1.666666667)); 75 76 t(x1.div(y9).eq(2)); 77 t(x2.div(y8).eq(1.7)); 78 t(x3.div(y7).eq(1.67)); 79 t(x4.div(y6).eq(1.667)); 80 t(x5.div(y5).eq(1.6667)); 81 t(x6.div(y4).eq(1.66667)); 82 t(x7.div(y3).eq(1.666667)); 83 t(x8.div(y2).eq(1.6666667)); 84 t(x9.div(y1).eq(1.66666667)); 85 86 t(Decimal.precision == 10); 87 t(D9.precision === 9); 88 t(D8.precision === 8); 89 t(D7.precision === 7); 90 t(D6.precision === 6); 91 t(D5.precision === 5); 92 t(D4.precision === 4); 93 t(D3.precision === 3); 94 t(D2.precision === 2); 95 t(D1.precision === 1); 96 97 t(new Decimal(9.99).eq(new D5('9.99'))); 98 t(!new Decimal(9.99).eq(new D3('-9.99'))); 99 t(!new Decimal(123.456789).toSD().eq(new D3('123.456789').toSD())); 100 t(new Decimal(123.456789).round().eq(new D3('123.456789').round())); 101 102 t(new Decimal(1).constructor === new Decimal(1).constructor); 103 t(new D9(1).constructor === new D9(1).constructor); 104 t(new Decimal(1).constructor !== new D1(1).constructor); 105 t(new D8(1).constructor !== new D9(1).constructor); 106 107 T.assertException(function () { Decimal.clone(null) }, "Decimal.clone(null)"); 108 109 // defaults: true 110 111 Decimal.config({ 112 precision: 100, 113 rounding: 2, 114 toExpNeg: -100, 115 toExpPos: 200, 116 defaults: true 117 }); 118 119 t(Decimal.precision === 100); 120 t(Decimal.rounding === 2); 121 t(Decimal.toExpNeg === -100); 122 t(Decimal.toExpPos === 200); 123 t(Decimal.defaults === undefined); 124 125 D1 = Decimal.clone({ defaults: true }); 126 127 t(D1.precision === 20); 128 t(D1.rounding === 4); 129 t(D1.toExpNeg === -7); 130 t(D1.toExpPos === 21); 131 t(D1.defaults === undefined); 132 133 D2 = Decimal.clone({ defaults: true, rounding: 5 }); 134 135 t(D2.precision === 20); 136 t(D2.rounding === 5); 137 t(D2.toExpNeg === -7); 138 t(D2.toExpPos === 21); 139 140 D3 = Decimal.clone({ defaults: false }); 141 142 t(D3.rounding === 2); 143}); 144 145