|
| 1 | +/* eslint-disable no-new */ |
| 2 | +import { Uint256 } from '../../../src'; |
| 3 | +import { |
| 4 | + CairoUint256, |
| 5 | + UINT_256_HIGH_MAX, |
| 6 | + UINT_256_HIGH_MIN, |
| 7 | + UINT_256_LOW_MAX, |
| 8 | + UINT_256_LOW_MIN, |
| 9 | + UINT_256_MAX, |
| 10 | + UINT_256_MIN, |
| 11 | +} from '../../../src/utils/cairoDataTypes/uint256'; |
| 12 | + |
| 13 | +describe('CairoUint256 class test', () => { |
| 14 | + test('constructor 1 should throw on < UINT_256_MIN', () => { |
| 15 | + expect(() => { |
| 16 | + new CairoUint256(UINT_256_MIN - 1n); |
| 17 | + }).toThrow('bigNumberish is smaller than UINT_256_MIN'); |
| 18 | + }); |
| 19 | + |
| 20 | + test('constructor 1 should throw on > UINT_256_MAX', () => { |
| 21 | + expect(() => { |
| 22 | + new CairoUint256(UINT_256_MAX + 1n); |
| 23 | + }).toThrow('bigNumberish is bigger than UINT_256_MAX'); |
| 24 | + }); |
| 25 | + |
| 26 | + test('constructor 2 (low, high)', () => { |
| 27 | + const u256 = new CairoUint256(1000, 1000); |
| 28 | + expect(u256.toApiRequest()).toEqual(['1000', '1000']); |
| 29 | + }); |
| 30 | + |
| 31 | + test('constructor 2 should throw out of bounds', () => { |
| 32 | + expect(() => { |
| 33 | + new CairoUint256(UINT_256_LOW_MIN - 1n, 1000); |
| 34 | + }).toThrow('low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('constructor 2 should throw out of bounds', () => { |
| 38 | + expect(() => { |
| 39 | + new CairoUint256(UINT_256_LOW_MAX + 1n, 1000); |
| 40 | + }).toThrow('low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX'); |
| 41 | + }); |
| 42 | + |
| 43 | + test('constructor 2 should throw out of bounds', () => { |
| 44 | + expect(() => { |
| 45 | + new CairoUint256(1000, UINT_256_HIGH_MIN - 1n); |
| 46 | + }).toThrow('high is out of range UINT_256_HIGH_MIN - UINT_256_HIGH_MAX'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('constructor 2 should throw out of bounds', () => { |
| 50 | + expect(() => { |
| 51 | + new CairoUint256(1000, UINT_256_HIGH_MAX + 1n); |
| 52 | + }).toThrow('high is out of range UINT_256_HIGH_MIN - UINT_256_HIGH_MAX'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('constructor 3 ({low, high})', () => { |
| 56 | + const u256 = new CairoUint256({ low: 1000, high: 1000 }); |
| 57 | + expect(u256.toApiRequest()).toEqual(['1000', '1000']); |
| 58 | + }); |
| 59 | + |
| 60 | + test('constructor 3 should throw out of bounds', () => { |
| 61 | + expect(() => { |
| 62 | + new CairoUint256({ low: 1000, high: UINT_256_HIGH_MAX + 1n }); |
| 63 | + }).toThrow('high is out of range UINT_256_HIGH_MIN - UINT_256_HIGH_MAX'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('validate should throw on < UINT_256_MIN', () => { |
| 67 | + expect(() => { |
| 68 | + CairoUint256.validate(UINT_256_MIN - 1n); |
| 69 | + }).toThrow('bigNumberish is smaller than UINT_256_MIN'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('validate should throw on > UINT_256_MAX', () => { |
| 73 | + expect(() => { |
| 74 | + CairoUint256.validate(UINT_256_MAX + 1n); |
| 75 | + }).toThrow('bigNumberish is bigger than UINT_256_MAX'); |
| 76 | + }); |
| 77 | + |
| 78 | + test('validate should pass and return bigint', () => { |
| 79 | + const validate = CairoUint256.validate(UINT_256_MAX); |
| 80 | + expect(typeof validate).toBe('bigint'); |
| 81 | + }); |
| 82 | + |
| 83 | + test('is should return true', () => { |
| 84 | + const is = CairoUint256.is(UINT_256_MIN); |
| 85 | + expect(is).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + test('is should return false', () => { |
| 89 | + const is = CairoUint256.is(UINT_256_MAX + 1n); |
| 90 | + expect(is).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + test('constructor 1 should support BigNumberish', () => { |
| 94 | + const case1 = new CairoUint256(10n); |
| 95 | + const case2 = new CairoUint256(10); |
| 96 | + const case3 = new CairoUint256('10'); |
| 97 | + const case4 = new CairoUint256('0xA'); |
| 98 | + |
| 99 | + expect(case1).toEqual(case2); |
| 100 | + expect(case3).toEqual(case4); |
| 101 | + expect(case1).toEqual(case4); |
| 102 | + }); |
| 103 | + |
| 104 | + test('constructor 2 should support Uint256 {low, high}', () => { |
| 105 | + const cases: Uint256[] = []; |
| 106 | + cases[cases.length] = new CairoUint256({ low: 0, high: 0 }); |
| 107 | + cases[cases.length] = new CairoUint256({ low: '0', high: '0' }); |
| 108 | + cases[cases.length] = new CairoUint256({ low: 0n, high: 0n }); |
| 109 | + cases[cases.length] = new CairoUint256({ low: '0x0', high: '0x0' }); |
| 110 | + |
| 111 | + const cases2: Uint256[] = []; |
| 112 | + cases2[cases2.length] = new CairoUint256({ low: 10000, high: 10000 }); |
| 113 | + cases2[cases2.length] = new CairoUint256({ low: '10000', high: '10000' }); |
| 114 | + cases2[cases2.length] = new CairoUint256({ low: 10000n, high: 10000n }); |
| 115 | + cases2[cases2.length] = new CairoUint256({ low: '0x2710', high: '0x2710' }); |
| 116 | + |
| 117 | + expect( |
| 118 | + cases.every((it) => { |
| 119 | + return it.low === 0n && it.high === 0n; |
| 120 | + }) |
| 121 | + ).toEqual(true); |
| 122 | + |
| 123 | + expect( |
| 124 | + cases2.every((it) => { |
| 125 | + return it.low === 10000n && it.high === 10000n; |
| 126 | + }) |
| 127 | + ).toEqual(true); |
| 128 | + }); |
| 129 | + |
| 130 | + test('should convert UINT_256_MAX to Uint256 dec struct', () => { |
| 131 | + const u256 = new CairoUint256(UINT_256_MAX); |
| 132 | + const u256Hex = u256.toUint256DecimalString(); |
| 133 | + expect(u256Hex).toMatchInlineSnapshot(` |
| 134 | + Object { |
| 135 | + "high": "340282366920938463463374607431768211455", |
| 136 | + "low": "340282366920938463463374607431768211455", |
| 137 | + } |
| 138 | + `); |
| 139 | + }); |
| 140 | + |
| 141 | + test('should convert UINT_256_MAX to Uint256 hex struct', () => { |
| 142 | + const u256 = new CairoUint256(UINT_256_MAX); |
| 143 | + const u256Decimal = u256.toUint256HexString(); |
| 144 | + expect(u256Decimal).toMatchInlineSnapshot(` |
| 145 | + Object { |
| 146 | + "high": "0xffffffffffffffffffffffffffffffff", |
| 147 | + "low": "0xffffffffffffffffffffffffffffffff", |
| 148 | + } |
| 149 | + `); |
| 150 | + }); |
| 151 | + |
| 152 | + test('isAbiType should return true', () => { |
| 153 | + const isAbiType = CairoUint256.isAbiType('core::integer::u256'); |
| 154 | + expect(isAbiType).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + test('should convert UINT_256_MAX to BN', () => { |
| 158 | + const u256 = new CairoUint256(UINT_256_MAX); |
| 159 | + expect(u256.toBigInt()).toEqual(UINT_256_MAX); |
| 160 | + }); |
| 161 | + |
| 162 | + test('should convert UINT_256_MAX to API Request', () => { |
| 163 | + const u256 = new CairoUint256(UINT_256_MAX); |
| 164 | + expect(u256.toApiRequest()).toEqual([ |
| 165 | + '340282366920938463463374607431768211455', |
| 166 | + '340282366920938463463374607431768211455', |
| 167 | + ]); |
| 168 | + }); |
| 169 | +}); |
0 commit comments