Skip to content

Commit

Permalink
feat(JInt): implemented signed 32bit overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-santoro committed Jul 17, 2017
1 parent ada3c46 commit 7c51455
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
5 changes: 2 additions & 3 deletions packages/java.lang.native.operator/src/jrelational.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Jboolean } from "../../java.lang/src/jboolean_primitive";
import { Jboolean } from '../../java.lang/src/jboolean_primitive';

export interface JRelational<T> {

/** Emulate the operator < */
lt(expr: T): Jboolean;

Expand All @@ -16,4 +15,4 @@ export interface JRelational<T> {

/** Emulate the operator instanceof */
instanceof(t: Function): Jboolean;
}
}
32 changes: 19 additions & 13 deletions packages/java.lang/src/jint_primitive.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { JObject } from './jobject';
import { JEquality } from '../../java.lang.native.operator/src/jequality';
import { Jboolean } from "../../java.lang/src/jboolean_primitive";
import { Jboolean } from '../../java.lang/src/jboolean_primitive';
import { JRelational } from '../../java.lang.native.operator/src/jrelational';

/**
* By default, the jint data type is a 32-bit signed two's complement integer,
* which has a minimum value of -231 and a maximum value of 231-1.
* In Java SE 8 and later, you can use the jint data type to represent an unsigned
* 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.
* Use the Integer class to use jint data type as an unsigned integer.
* See the section The Number Classes for more information.
* Static methods like compareUnsigned, divideUnsigned etc have been added to
* which has a minimum value of -2^31 and a maximum value of 2^31-1.
* In Java SE 8 and later, you can use the jint data type to represent an unsigned
* 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1.
* Use the Integer class to use jint data type as an unsigned integer.
* See the section The Number Classes for more information.
* Static methods like compareUnsigned, divideUnsigned etc have been added to
* the Integer class to support the arithmetic operations for unsigned integers.
*
* Note: To retrieve the actual boolean value of a jint you have to use {@code .value} syntax.
Expand All @@ -18,23 +19,28 @@ export class Jint implements JEquality<Jint>, JRelational<Jint> {
private _value: number;

constructor(value: number = 0) {
this._value = value;
this.value = value;
}

get value() {
return this._value;
}

/**
* Set int value in respect of the Java int costraint.
* In order to simulate 32 bit signed int, is used the
* bitwise operation `| 0`.
*/
set value(value: number) {
this._value = value;
this._value = value | 0;
}

public eq(expr: Jint): Jboolean {
return new Jboolean(this.value == expr.value);
return new Jboolean(this.value === expr.value);
}

public ne(expr: Jint): Jboolean {
return new Jboolean(this.value != expr.value);
return new Jboolean(this.value !== expr.value);
}

public lt(expr: Jint): Jboolean {
Expand All @@ -54,7 +60,7 @@ export class Jint implements JEquality<Jint>, JRelational<Jint> {
}

public instanceof(t: Function): Jboolean {
throw new Error("Method instanceof not implemented.");
throw new Error('Method instanceof not implemented.');
}

}
}
12 changes: 12 additions & 0 deletions packages/java.lang/test/jint_primitive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ describe('Jint', () => {
expect(i.value).to.be.eq(0);
});

it('should overflow when a number grater then 2^31-1 is set.', () => {
const i = new Jint(Math.pow(2, 31));

expect(i.value).to.be.eq(-Math.pow(2, 31));
});

it('should overflow when a number lower then -2^31 is set.', () => {
const i = new Jint(-Math.pow(2, 31) - 1);

expect(i.value).to.be.eq(Math.pow(2, 31) - 1);
});

it('new Jint(24) should be equal to new Jint(24)', () => {
const i1 = new Jint(24);
const i2 = new Jint();
Expand Down

0 comments on commit 7c51455

Please sign in to comment.