Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test and support for negative Signed integers smaller than 252 bits #1177

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
93 changes: 93 additions & 0 deletions __tests__/utils/CairoTypes/CairoInt128.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-new */
import { Cairoint128, INT_128_MAX, INT_128_MIN } from '../../../src/utils/cairoDataTypes/int128';

describe('Cairoint128 class test', () => {
test('constructor 1 should throw on < INT_128_MIN', () => {
expect(() => {
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);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_128_MAX to API Request', () => {
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);
expect(i128.toApiRequest()).toEqual(
'3618502788666131213697322783095070105452966031871127468241404752419987914754'
);
});

test('should convert negative serialized number to BigInt', () => {
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);
}).toThrow('bigNumberish is smaller than INT_128_MIN');
});

test('validate should throw on > INT_128_MAX', () => {
expect(() => {
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);
expect(typeof validate).toBe('bigint');
});

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

test('is should return false', () => {
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');

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();
expect(i128Decimal).toEqual('170141183460469231731687303715884105727');
});

test('should convert INT_128_MAX to Int128 hex struct', () => {
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');
expect(isAbiType).toBe(true);
});

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

describe('Cairoint16 class test', () => {
test('constructor 1 should throw on < INT_16_MIN', () => {
expect(() => {
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);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_16_MAX to API Request', () => {
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);
expect(i16.toApiRequest()).toEqual(
'3618502788666131213697322783095070105623107215331596699973092056135871987714'
);
});

test('should convert negative serialized number to BigInt', () => {
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);
}).toThrow('bigNumberish is smaller than INT_16_MIN');
});

test('validate should throw on > INT_16_MAX', () => {
expect(() => {
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);
expect(typeof validate).toBe('bigint');
});

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

test('is should return false', () => {
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');

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();
expect(i16Decimal).toEqual('32767');
});

test('should convert INT_16_MAX to Int16 hex struct', () => {
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');
expect(isAbiType).toBe(true);
});

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

describe('Cairoint32 class test', () => {
test('constructor 1 should throw on < INT_32_MIN', () => {
expect(() => {
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);
}).toThrow('bigNumberish is bigger than the int maximum');
});

test('should convert INT_32_MAX to API Request', () => {
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);
expect(i32.toApiRequest()).toEqual(
'3618502788666131213697322783095070105623107215331596699973092056133724536834'
);
});

test('should convert negative serialized number to BigInt', () => {
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);
}).toThrow('bigNumberish is smaller than INT_32_MIN');
});

test('validate should throw on > INT_32_MAX', () => {
expect(() => {
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);
expect(typeof validate).toBe('bigint');
});

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

test('is should return false', () => {
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');

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();
expect(i32Decimal).toEqual('2147483647');
});

test('should convert INT_32_MAX to Int32 hex struct', () => {
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');
expect(isAbiType).toBe(true);
});

test('should convert INT_32_MAX to BigInt', () => {
const i32 = new Cairoint32(INT_32_MAX);
expect(i32.toBigInt()).toEqual(INT_32_MAX);
});
});
93 changes: 93 additions & 0 deletions __tests__/utils/CairoTypes/CairoInt64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-new */
import { Cairoint64, INT_64_MAX, INT_64_MIN } from '../../../src/utils/cairoDataTypes/int64';

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

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

test('should convert INT_64_MAX to API Request', () => {
const i64 = new Cairoint64(INT_64_MAX);
expect(i64.toApiRequest()).toEqual('9223372036854775807');
});

test('should serialize negative number to felt252', () => {
const i64 = new Cairoint64(INT_64_MIN);
expect(i64.toApiRequest()).toEqual(
'3618502788666131213697322783095070105623107215331596699963868684099017244674'
);
});

test('should convert negative serialized number to BigInt', () => {
const i64 = new Cairoint64(INT_64_MIN);
expect(i64.negativeFelt252ToBigInt()).toEqual(-9223372036854775807n);
});

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

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

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

test('is should return true', () => {
const is = Cairoint64.is(INT_64_MAX);
expect(is).toBe(true);
});

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

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

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

test('should convert INT_64_MAX to Int64 dec struct', () => {
const i64 = new Cairoint64(INT_64_MAX);
const i64Decimal = i64.toIntDecimalString();
expect(i64Decimal).toEqual('9223372036854775807');
});

test('should convert INT_64_MAX to Int64 hex struct', () => {
const i64 = new Cairoint64(INT_64_MAX);
const i64Hex = i64.toIntHexString();
expect(i64Hex).toEqual('0x7fffffffffffffff');
});

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

test('should convert INT_64_MAX to BigInt', () => {
const i64 = new Cairoint64(INT_64_MAX);
expect(i64.toBigInt()).toEqual(INT_64_MAX);
});
});
Loading