|
| 1 | +import { Int32 } from '../src/types' |
| 2 | +import { encode, decode } from '../src' |
| 3 | + |
| 4 | +describe('Int32', () => { |
| 5 | + describe('from()', () => { |
| 6 | + it('should create Int32 from positive number', () => { |
| 7 | + const int32 = Int32.from(12345) |
| 8 | + expect(int32.valueOf()).toBe(12345) |
| 9 | + }) |
| 10 | + |
| 11 | + it('should create Int32 from negative number', () => { |
| 12 | + const int32 = Int32.from(-12345) |
| 13 | + expect(int32.valueOf()).toBe(-12345) |
| 14 | + }) |
| 15 | + |
| 16 | + it('should create Int32 from zero', () => { |
| 17 | + const int32 = Int32.from(0) |
| 18 | + expect(int32.valueOf()).toBe(0) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should create Int32 from positive string', () => { |
| 22 | + const int32 = Int32.from('67890') |
| 23 | + expect(int32.valueOf()).toBe(67890) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should create Int32 from negative string', () => { |
| 27 | + const int32 = Int32.from('-67890') |
| 28 | + expect(int32.valueOf()).toBe(-67890) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should create Int32 from another Int32', () => { |
| 32 | + const original = Int32.from(999) |
| 33 | + const copy = Int32.from(original) |
| 34 | + expect(copy.valueOf()).toBe(999) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should handle MIN_VALUE', () => { |
| 38 | + const int32 = Int32.from(-2147483648) |
| 39 | + expect(int32.valueOf()).toBe(-2147483648) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should handle MAX_VALUE', () => { |
| 43 | + const int32 = Int32.from(2147483647) |
| 44 | + expect(int32.valueOf()).toBe(2147483647) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + describe('range validation', () => { |
| 49 | + it('should throw error when value exceeds MAX_VALUE', () => { |
| 50 | + expect(() => Int32.from(2147483648)).toThrow( |
| 51 | + new Error( |
| 52 | + 'Invalid Int32: 2147483648 must be >= -2147483648 and <= 2147483647', |
| 53 | + ), |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should throw error when value is below MIN_VALUE', () => { |
| 58 | + expect(() => Int32.from(-2147483649)).toThrow( |
| 59 | + new Error( |
| 60 | + 'Invalid Int32: -2147483649 must be >= -2147483648 and <= 2147483647', |
| 61 | + ), |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should throw error for string exceeding MAX_VALUE', () => { |
| 66 | + expect(() => Int32.from('2147483648')).toThrow( |
| 67 | + new Error( |
| 68 | + 'Invalid Int32: 2147483648 must be >= -2147483648 and <= 2147483647', |
| 69 | + ), |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should throw error for string below MIN_VALUE', () => { |
| 74 | + expect(() => Int32.from('-2147483649')).toThrow( |
| 75 | + new Error( |
| 76 | + 'Invalid Int32: -2147483649 must be >= -2147483648 and <= 2147483647', |
| 77 | + ), |
| 78 | + ) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('decimal validation', () => { |
| 83 | + it('should throw error when passed a decimal number', () => { |
| 84 | + expect(() => Int32.from(100.5)).toThrow( |
| 85 | + new Error('Cannot construct Int32 from given value'), |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should throw error when passed a negative decimal', () => { |
| 90 | + expect(() => Int32.from(-100.5)).toThrow( |
| 91 | + new Error('Cannot construct Int32 from given value'), |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should throw error when passed a small decimal', () => { |
| 96 | + expect(() => Int32.from(0.001)).toThrow( |
| 97 | + new Error('Cannot construct Int32 from given value'), |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + it('should throw error for decimal string', () => { |
| 102 | + expect(() => Int32.from('1.23')).toThrow( |
| 103 | + new Error('Cannot construct Int32 from string: 1.23'), |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should throw error for non-numeric string', () => { |
| 108 | + expect(() => Int32.from('abc')).toThrow( |
| 109 | + new Error('Cannot construct Int32 from string: abc'), |
| 110 | + ) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + describe('compareTo()', () => { |
| 115 | + it('should return 0 for equal values', () => { |
| 116 | + expect(Int32.from(100).compareTo(Int32.from(100))).toBe(0) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should return 1 when first value is greater', () => { |
| 120 | + expect(Int32.from(100).compareTo(Int32.from(50))).toBe(1) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should return -1 when first value is smaller', () => { |
| 124 | + expect(Int32.from(50).compareTo(Int32.from(100))).toBe(-1) |
| 125 | + }) |
| 126 | + |
| 127 | + it('should compare with raw number', () => { |
| 128 | + expect(Int32.from(100).compareTo(100)).toBe(0) |
| 129 | + expect(Int32.from(100).compareTo(50)).toBe(1) |
| 130 | + expect(Int32.from(50).compareTo(100)).toBe(-1) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle negative values', () => { |
| 134 | + expect(Int32.from(-100).compareTo(Int32.from(-50))).toBe(-1) |
| 135 | + expect(Int32.from(-50).compareTo(Int32.from(-100))).toBe(1) |
| 136 | + expect(Int32.from(-100).compareTo(Int32.from(-100))).toBe(0) |
| 137 | + }) |
| 138 | + |
| 139 | + it('should compare negative and positive values', () => { |
| 140 | + expect(Int32.from(-100).compareTo(Int32.from(100))).toBe(-1) |
| 141 | + expect(Int32.from(100).compareTo(Int32.from(-100))).toBe(1) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + describe('toJSON()', () => { |
| 146 | + it('should return number for positive value', () => { |
| 147 | + expect(Int32.from(12345).toJSON()).toBe(12345) |
| 148 | + }) |
| 149 | + |
| 150 | + it('should return number for negative value', () => { |
| 151 | + expect(Int32.from(-12345).toJSON()).toBe(-12345) |
| 152 | + }) |
| 153 | + |
| 154 | + it('should return 0 for zero', () => { |
| 155 | + expect(Int32.from(0).toJSON()).toBe(0) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe('valueOf()', () => { |
| 160 | + it('should allow bitwise operations', () => { |
| 161 | + const val = Int32.from(5) |
| 162 | + expect(val.valueOf() | 0x2).toBe(7) |
| 163 | + }) |
| 164 | + |
| 165 | + it('should handle negative values in bitwise operations', () => { |
| 166 | + const val = Int32.from(-1) |
| 167 | + expect(val.valueOf() & 0xff).toBe(255) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('encode/decode with Loan ledger entry', () => { |
| 172 | + // Loan object from Devnet with LoanScale field |
| 173 | + const loanWithScale = { |
| 174 | + Borrower: 'rs5fUokF7Y5bxNkstM4p4JYHgqzYkFamCg', |
| 175 | + GracePeriod: 60, |
| 176 | + LoanBrokerID: |
| 177 | + '18F91BD8009DAF09B5E4663BE7A395F5F193D0657B12F8D1E781EB3D449E8151', |
| 178 | + LoanScale: -11, |
| 179 | + LoanSequence: 1, |
| 180 | + NextPaymentDueDate: 822779431, |
| 181 | + PaymentInterval: 400, |
| 182 | + PaymentRemaining: 1, |
| 183 | + PeriodicPayment: '10000', |
| 184 | + PrincipalOutstanding: '10000', |
| 185 | + StartDate: 822779031, |
| 186 | + TotalValueOutstanding: '10000', |
| 187 | + LedgerEntryType: 'Loan', |
| 188 | + } |
| 189 | + |
| 190 | + it('can encode and decode Loan with negative LoanScale', () => { |
| 191 | + const encoded = encode(loanWithScale) |
| 192 | + const decoded = decode(encoded) |
| 193 | + expect(decoded).toEqual(loanWithScale) |
| 194 | + }) |
| 195 | + |
| 196 | + it('can encode and decode Loan with positive LoanScale', () => { |
| 197 | + const loan = { ...loanWithScale, LoanScale: 5 } |
| 198 | + const encoded = encode(loan) |
| 199 | + const decoded = decode(encoded) |
| 200 | + expect(decoded).toEqual(loan) |
| 201 | + }) |
| 202 | + }) |
| 203 | +}) |
0 commit comments