Skip to content

Commit

Permalink
feat: used utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabián Valverde authored and Fabián Valverde committed Oct 22, 2024
1 parent a400361 commit 088a398
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 156 deletions.
42 changes: 21 additions & 21 deletions __tests__/utils/CairoTypes/CairoInt128.test.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
/* eslint-disable no-new */
import { Cairoint128, INT_128_MAX, INT_128_MIN } from '../../../src/utils/cairoDataTypes/int128';
import { CairoInt128, INT_128_MAX, INT_128_MIN } from '../../../src/utils/cairoDataTypes/int128';

describe('Cairoint128 class test', () => {
describe('CairoInt128 class test', () => {
test('constructor 1 should throw on < INT_128_MIN', () => {
expect(() => {
new Cairoint128(INT_128_MIN - 1n);
new CairoInt128(INT_128_MIN - 1n);
}).toThrow('bigNumberish is smaller than the int minimum');
});

test('constructor should throw on > INT_128_MAX', () => {
expect(() => {
new Cairoint128(INT_128_MAX + 1n);
new CairoInt128(INT_128_MAX + 1n);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_128_MAX to API Request', () => {
const i128 = new Cairoint128(INT_128_MAX);
const i128 = new CairoInt128(INT_128_MAX);
expect(i128.toApiRequest()).toEqual('170141183460469231731687303715884105727');
});

test('should serialize negative number to felt252', () => {
const i128 = new Cairoint128(INT_128_MIN);
const i128 = new CairoInt128(INT_128_MIN);
expect(i128.toApiRequest()).toEqual(
'3618502788666131213697322783095070105452966031871127468241404752419987914754'
);
});

test('should convert negative serialized number to BigInt', () => {
const i128 = new Cairoint128(INT_128_MIN);
const i128 = new CairoInt128(INT_128_MIN);
expect(i128.negativeFelt252ToBigInt()).toEqual(-170141183460469231731687303715884105727n);
});

test('validate should throw on < INT_128_MIN', () => {
expect(() => {
Cairoint128.validate(INT_128_MIN - 1n);
CairoInt128.validate(INT_128_MIN - 1n);
}).toThrow('bigNumberish is smaller than INT_128_MIN');
});

test('validate should throw on > INT_128_MAX', () => {
expect(() => {
Cairoint128.validate(INT_128_MAX + 1n);
CairoInt128.validate(INT_128_MAX + 1n);
}).toThrow('bigNumberish is bigger than INT_128_MAX');
});

test('validate should pass and return bigint', () => {
const validate = Cairoint128.validate(INT_128_MAX);
const validate = CairoInt128.validate(INT_128_MAX);
expect(typeof validate).toBe('bigint');
});

test('is should return true', () => {
const is = Cairoint128.is(INT_128_MAX);
const is = CairoInt128.is(INT_128_MAX);
expect(is).toBe(true);
});

test('is should return false', () => {
const is = Cairoint128.is(INT_128_MAX + 1n);
const is = CairoInt128.is(INT_128_MAX + 1n);
expect(is).toBe(false);
});

test('constructor should support BigNumberish', () => {
const case1 = new Cairoint128(10n);
const case2 = new Cairoint128(10);
const case3 = new Cairoint128('10');
const case4 = new Cairoint128('0xA');
const case1 = new CairoInt128(10n);
const case2 = new CairoInt128(10);
const case3 = new CairoInt128('10');
const case4 = new CairoInt128('0xA');

expect(case1).toEqual(case2);
expect(case3).toEqual(case4);
expect(case1).toEqual(case4);
});

test('should convert INT_128_MAX to Int128 dec struct', () => {
const i128 = new Cairoint128(INT_128_MAX);
const i128Decimal = i128.toIntDecimalString();
const i128 = new CairoInt128(INT_128_MAX);
const i128Decimal = i128.toIntDecimalString().int;
expect(i128Decimal).toEqual('170141183460469231731687303715884105727');
});

test('should convert INT_128_MAX to Int128 hex struct', () => {
const i128 = new Cairoint128(INT_128_MAX);
const i128 = new CairoInt128(INT_128_MAX);
const i128Hex = i128.toIntHexString();
expect(i128Hex).toEqual('0x7fffffffffffffffffffffffffffffff');
});

test('isAbiType should return true', () => {
const isAbiType = Cairoint128.isAbiType('core::integer::i128');
const isAbiType = CairoInt128.isAbiType('core::integer::i128');
expect(isAbiType).toBe(true);
});

test('should convert INT_128_MAX to BigInt', () => {
const i128 = new Cairoint128(INT_128_MAX);
const i128 = new CairoInt128(INT_128_MAX);
expect(i128.toBigInt()).toEqual(INT_128_MAX);
});
});
42 changes: 21 additions & 21 deletions __tests__/utils/CairoTypes/CairoInt16.test.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
/* eslint-disable no-new */
import { Cairoint16, INT_16_MAX, INT_16_MIN } from '../../../src/utils/cairoDataTypes/int16';
import { CairoInt16, INT_16_MAX, INT_16_MIN } from '../../../src/utils/cairoDataTypes/int16';

describe('Cairoint16 class test', () => {
describe('CairoInt16 class test', () => {
test('constructor 1 should throw on < INT_16_MIN', () => {
expect(() => {
new Cairoint16(INT_16_MIN - 1n);
new CairoInt16(INT_16_MIN - 1n);
}).toThrow('bigNumberish is smaller than the int minimum');
});

test('constructor should throw on > INT_16_MAX', () => {
expect(() => {
new Cairoint16(INT_16_MAX + 1n);
new CairoInt16(INT_16_MAX + 1n);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_16_MAX to API Request', () => {
const i16 = new Cairoint16(INT_16_MAX);
const i16 = new CairoInt16(INT_16_MAX);
expect(i16.toApiRequest()).toEqual('32767');
});

test('should serialize negative number to felt252', () => {
const i16 = new Cairoint16(INT_16_MIN);
const i16 = new CairoInt16(INT_16_MIN);
expect(i16.toApiRequest()).toEqual(
'3618502788666131213697322783095070105623107215331596699973092056135871987714'
);
});

test('should convert negative serialized number to BigInt', () => {
const i16 = new Cairoint16(INT_16_MIN);
const i16 = new CairoInt16(INT_16_MIN);
expect(i16.negativeFelt252ToBigInt()).toEqual(-32767n);
});

test('validate should throw on < INT_16_MIN', () => {
expect(() => {
Cairoint16.validate(INT_16_MIN - 1n);
CairoInt16.validate(INT_16_MIN - 1n);
}).toThrow('bigNumberish is smaller than INT_16_MIN');
});

test('validate should throw on > INT_16_MAX', () => {
expect(() => {
Cairoint16.validate(INT_16_MAX + 1n);
CairoInt16.validate(INT_16_MAX + 1n);
}).toThrow('bigNumberish is bigger than INT_16_MAX');
});

test('validate should pass and return bigint', () => {
const validate = Cairoint16.validate(INT_16_MAX);
const validate = CairoInt16.validate(INT_16_MAX);
expect(typeof validate).toBe('bigint');
});

test('is should return true', () => {
const is = Cairoint16.is(INT_16_MAX);
const is = CairoInt16.is(INT_16_MAX);
expect(is).toBe(true);
});

test('is should return false', () => {
const is = Cairoint16.is(INT_16_MAX + 1n);
const is = CairoInt16.is(INT_16_MAX + 1n);
expect(is).toBe(false);
});

test('constructor should support BigNumberish', () => {
const case1 = new Cairoint16(10n);
const case2 = new Cairoint16(10);
const case3 = new Cairoint16('10');
const case4 = new Cairoint16('0xA');
const case1 = new CairoInt16(10n);
const case2 = new CairoInt16(10);
const case3 = new CairoInt16('10');
const case4 = new CairoInt16('0xA');

expect(case1).toEqual(case2);
expect(case3).toEqual(case4);
expect(case1).toEqual(case4);
});

test('should convert INT_16_MAX to Int16 dec struct', () => {
const i16 = new Cairoint16(INT_16_MAX);
const i16Decimal = i16.toIntDecimalString();
const i16 = new CairoInt16(INT_16_MAX);
const i16Decimal = i16.toIntDecimalString().int;
expect(i16Decimal).toEqual('32767');
});

test('should convert INT_16_MAX to Int16 hex struct', () => {
const i16 = new Cairoint16(INT_16_MAX);
const i16 = new CairoInt16(INT_16_MAX);
const i16Hex = i16.toIntHexString();
expect(i16Hex).toEqual('0x7fff');
});

test('isAbiType should return true', () => {
const isAbiType = Cairoint16.isAbiType('core::integer::i16');
const isAbiType = CairoInt16.isAbiType('core::integer::i16');
expect(isAbiType).toBe(true);
});

test('should convert INT_16_MAX to BigInt', () => {
const i16 = new Cairoint16(INT_16_MAX);
const i16 = new CairoInt16(INT_16_MAX);
expect(i16.toBigInt()).toEqual(INT_16_MAX);
});
});
42 changes: 21 additions & 21 deletions __tests__/utils/CairoTypes/CairoInt32.test.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
/* eslint-disable no-new */
import { Cairoint32, INT_32_MAX, INT_32_MIN } from '../../../src/utils/cairoDataTypes/int32';
import { CairoInt32, INT_32_MAX, INT_32_MIN } from '../../../src/utils/cairoDataTypes/int32';

describe('Cairoint32 class test', () => {
describe('CairoInt32 class test', () => {
test('constructor 1 should throw on < INT_32_MIN', () => {
expect(() => {
new Cairoint32(INT_32_MIN - 1n);
new CairoInt32(INT_32_MIN - 1n);
}).toThrow('bigNumberish is smaller than the int minimum');
});

test('constructor should throw on > INT_32_MAX', () => {
expect(() => {
new Cairoint32(INT_32_MAX + 1n);
new CairoInt32(INT_32_MAX + 1n);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_32_MAX to API Request', () => {
const i32 = new Cairoint32(INT_32_MAX);
const i32 = new CairoInt32(INT_32_MAX);
expect(i32.toApiRequest()).toEqual('2147483647');
});

test('should serialize negative number to felt252', () => {
const i32 = new Cairoint32(INT_32_MIN);
const i32 = new CairoInt32(INT_32_MIN);
expect(i32.toApiRequest()).toEqual(
'3618502788666131213697322783095070105623107215331596699973092056133724536834'
);
});

test('should convert negative serialized number to BigInt', () => {
const i32 = new Cairoint32(INT_32_MIN);
const i32 = new CairoInt32(INT_32_MIN);
expect(i32.negativeFelt252ToBigInt()).toEqual(-2147483647n);
});

test('validate should throw on < INT_32_MIN', () => {
expect(() => {
Cairoint32.validate(INT_32_MIN - 1n);
CairoInt32.validate(INT_32_MIN - 1n);
}).toThrow('bigNumberish is smaller than INT_32_MIN');
});

test('validate should throw on > INT_32_MAX', () => {
expect(() => {
Cairoint32.validate(INT_32_MAX + 1n);
CairoInt32.validate(INT_32_MAX + 1n);
}).toThrow('bigNumberish is bigger than INT_32_MAX');
});

test('validate should pass and return bigint', () => {
const validate = Cairoint32.validate(INT_32_MAX);
const validate = CairoInt32.validate(INT_32_MAX);
expect(typeof validate).toBe('bigint');
});

test('is should return true', () => {
const is = Cairoint32.is(INT_32_MAX);
const is = CairoInt32.is(INT_32_MAX);
expect(is).toBe(true);
});

test('is should return false', () => {
const is = Cairoint32.is(INT_32_MAX + 1n);
const is = CairoInt32.is(INT_32_MAX + 1n);
expect(is).toBe(false);
});

test('constructor should support BigNumberish', () => {
const case1 = new Cairoint32(10n);
const case2 = new Cairoint32(10);
const case3 = new Cairoint32('10');
const case4 = new Cairoint32('0xA');
const case1 = new CairoInt32(10n);
const case2 = new CairoInt32(10);
const case3 = new CairoInt32('10');
const case4 = new CairoInt32('0xA');

expect(case1).toEqual(case2);
expect(case3).toEqual(case4);
expect(case1).toEqual(case4);
});

test('should convert INT_32_MAX to Int32 dec struct', () => {
const i32 = new Cairoint32(INT_32_MAX);
const i32Decimal = i32.toIntDecimalString();
const i32 = new CairoInt32(INT_32_MAX);
const i32Decimal = i32.toIntDecimalString().int;
expect(i32Decimal).toEqual('2147483647');
});

test('should convert INT_32_MAX to Int32 hex struct', () => {
const i32 = new Cairoint32(INT_32_MAX);
const i32 = new CairoInt32(INT_32_MAX);
const i32Hex = i32.toIntHexString();
expect(i32Hex).toEqual('0x7fffffff');
});

test('isAbiType should return true', () => {
const isAbiType = Cairoint32.isAbiType('core::integer::i32');
const isAbiType = CairoInt32.isAbiType('core::integer::i32');
expect(isAbiType).toBe(true);
});

test('should convert INT_32_MAX to BigInt', () => {
const i32 = new Cairoint32(INT_32_MAX);
const i32 = new CairoInt32(INT_32_MAX);
expect(i32.toBigInt()).toEqual(INT_32_MAX);
});
});
Loading

0 comments on commit 088a398

Please sign in to comment.