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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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().int;
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().int;
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().int;
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().int;
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