Skip to content

Commit

Permalink
vtable overrides for cmp* and is_equal* with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bubaflub committed Jul 7, 2011
1 parent e75759e commit 658b5e6
Show file tree
Hide file tree
Showing 9 changed files with 1,264 additions and 980 deletions.
2,047 changes: 1,093 additions & 954 deletions src/GMP/Integer.pir

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions src/GMP/Integer.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -290,36 +290,44 @@ namespace GMP {
mpz_neg(self, self);
}

/* vtable overrides to implement
function is_equal[vtable](var value) {
return
return mpz_cmp(self, value) == 0;
}

function is_equal_num[vtable](var value) {
return
return mpz_cmp_si(self, int(value)) == 0;
}

function is_equal_string[vtable](var value) {
return
}

function is_same[vtable](var value) {
return
return mpz_get_str("", 10, self) == value;
}

function cmp[vtable](var value) {
return
int i = mpz_cmp(self, value);
if(i > 0)
return 1;
if(i < 0)
return -1;
return 0;
}

function cmp_num[vtable](var value) {
return
int i = mpz_cmp_si(self, int(value));
if(i > 0)
return 1;
if(i < 0)
return -1;
return 0;
}

function cmp_string[vtable](var value) {
return
string rep = mpz_get_str("", 10, self);
if (rep < value)
return -1;
if (rep > value)
return 1;
return 0;
}

*/
}
}

Expand Down
24 changes: 11 additions & 13 deletions src/GMP/Integer.winxed.template
Original file line number Diff line number Diff line change
Expand Up @@ -290,36 +290,34 @@ namespace GMP {
mpz_neg(self, self);
}

/* vtable overrides to implement
function is_equal[vtable](var value) {
return
return mpz_cmp(self, value) == 0;
}

function is_equal_num[vtable](var value) {
return
return mpz_cmp_si(self, int(value)) == 0;
}

function is_equal_string[vtable](var value) {
return
}

function is_same[vtable](var value) {
return
return mpz_get_str("", 10, self) == value;
}

function cmp[vtable](var value) {
return
return mpz_cmp(self, value);
}

function cmp_num[vtable](var value) {
return
return mpz_cmp_si(self, int(value));
}

function cmp_string[vtable](var value) {
return
string rep = mpz_get_str("", 10, self);
if (rep < value)
return -1;
if (rep > value)
return 1;
return 0;
}

*/
}
}

Expand Down
24 changes: 24 additions & 0 deletions t/integer/vtable/cmp.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_cmp {
function test_mpz_cmp() {
var x = new GMP.Integer(30);
var y = new GMP.Integer(29);
int i = x > y;
self.assert.is_true(x > y);
y = new GMP.Integer(31);
self.assert.is_true(x < y);
y = new GMP.Integer(30);
self.assert.is_true(x == y);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_cmp);
}
24 changes: 24 additions & 0 deletions t/integer/vtable/cmp_num.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_cmp_num {
function test_mpz_cmp_num() {
var x = new GMP.Integer(30);
int y = 29;
int i = x > y;
self.assert.is_true(x > y);
y = 31;
self.assert.is_true(x < y);
y = 30;
self.assert.is_true(x == y);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_cmp_num);
}
24 changes: 24 additions & 0 deletions t/integer/vtable/cmp_string.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_cmp_string {
function test_mpz_cmp_string() {
var x = new GMP.Integer(30);
string y = "29";
int i = x > y;
self.assert.is_true(x > y);
y = "31";
self.assert.is_true(x < y);
y = "30";
self.assert.is_true(x == y);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_cmp_string);
}
21 changes: 21 additions & 0 deletions t/integer/vtable/is_equal.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_is_equal {
function test_mpz_is_equal() {
var x = new GMP.Integer(30);
var y = new GMP.Integer(30);
self.assert.equal(x, y);
y = new GMP.Integer(-24);
self.assert.not_equal(x, y);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_is_equal);
}
23 changes: 23 additions & 0 deletions t/integer/vtable/is_equal_num.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_is_equal_num {
function test_mpz_is_equal_num() {
var x = new GMP.Integer(30);
int y = 30;
int i = x == y;
self.assert.equal(i, 1);
y = -24;
i = x == y;
self.assert.equal(i, 0);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_is_equal_num);
}
23 changes: 23 additions & 0 deletions t/integer/vtable/is_equal_string.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$load "rosella/test.pbc";
$load "GMP/Integer.pbc";

namespace GMP {
class Integer;
}

class Test_GMP_Vtable_is_equal_string {
function test_mpz_is_equal_string() {
var x = new GMP.Integer(30);
string y = "30";
int i = x == y;
self.assert.equal(i, 1);
y = "-24";
i = x == y;
self.assert.equal(i, 0);
}
}

function main[main]() {
using Rosella.Test.test;
test(class Test_GMP_Vtable_is_equal_string);
}

0 comments on commit 658b5e6

Please sign in to comment.