github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/test_uint.js (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 'use strict'; 20 21 var Uint64 = Uint.Uint64; 22 var Uint128 = Uint.Uint128; 23 var Uint256 = Uint.Uint256; 24 var Uint512 = Uint.Uint512; 25 26 var err1 = "[Uint64 Error] overflow"; 27 var err2 = '[Uint128 Error] incompatible type'; 28 var err3 = "[Uint64 Error] underflow"; 29 var err4 = '[Uint512 Error] incompatible type'; 30 var err5 = '[Uint256 Error] overflow'; 31 var err6 = '[Uint64 Error] NaN'; 32 var err7 = '[Uint64 Error] not an integer'; 33 34 var a = new Uint64(100000000000000); 35 var a1 = new Uint64(100000000000001); 36 var b = new Uint128(100000000000000); 37 var c = new Uint256(100000000000000); 38 var d = new Uint512(100000000000000); 39 40 var f = new Uint64(0); 41 if (f.toString(10) !== "0") { 42 throw new Error("not eq"); 43 } 44 45 if (!Uint.isUint(a)) { 46 throw new Error("uint64 should be uint"); 47 } 48 49 if (Uint.isUint(new BigNumber("123"))) { 50 throw new Error("bignumber should not be uint"); 51 } 52 53 if (Uint.isUint(123)) { 54 throw new Error("number should not be uint"); 55 } 56 57 if (Uint.isUint("123")) { 58 throw new Error("string should not be uint"); 59 } 60 61 // overflow 62 try { 63 a.pow(new Uint64(2)); 64 } catch (e) { 65 if (e.message !== err1) { 66 throw e; 67 } 68 } 69 a.pow(new Uint64(1)); 70 71 try { 72 c.mul(c).mul(c).mul(c).mul(c).mul(c).mul(c); 73 } catch (e) { 74 if (e.message !== err5) { 75 throw e; 76 } 77 } 78 79 var bpow2 = b.pow(new Uint128(2)); 80 if (bpow2.toString(10) !== "10000000000000000000000000000") { 81 throw new Error("b.pow(2) not equal"); 82 } 83 84 // incompatible 85 try { 86 b.plus(c); 87 } catch (e) { 88 if (e.message !== err2) { 89 throw e; 90 } 91 } 92 b.plus(b); 93 94 try { 95 d.minus(1); 96 } catch (e) { 97 if (e.message !== err4) { 98 throw e; 99 } 100 } 101 102 // underflow 103 try { 104 a.minus(a1); 105 } catch (e) { 106 if (e.message !== err3) { 107 throw e; 108 } 109 } 110 if (a.minus(a).toString(10) !== "0") { 111 throw new Error("a.minus(a) not 0"); 112 } 113 114 // NaN 115 try { 116 a.div(null); 117 } catch (e) { 118 if (e.message !== err6) { 119 throw e; 120 } 121 } 122 123 if (a.div(a).toString(10) !== "1") { 124 throw new Error("a.div(a) not 1"); 125 } 126 127 if (a.mod(a).toString(10) !== "0") { 128 throw new Error("a.mod(a) not 0"); 129 } 130 131 // not an integer 132 try { 133 new Uint64(1.2); 134 } catch (e) { 135 if (e.message !== err7) { 136 throw e; 137 } 138 } 139 try { 140 new Uint64("1.2"); 141 } catch (e) { 142 if (e.message !== err7) { 143 throw e; 144 } 145 } 146 try { 147 a.div(new Uint64(0)); 148 } catch (e) { 149 if (e.message !== err7) { 150 throw e; 151 } 152 }