On Tue, Feb 11, 2014 at 04:48:29PM +0000, Nicholas Clark wrote:
> Attached are a first stab at some tests for native integer serialisation.
> They pass on the JVM, and on MoarVM (tested 64 bit and 32 bit Linux) with
> patching. Specifically the two commits from the branch maybe-varintfix plus
> the attached patch that fixes some varint serialisation bugs.
> 
> The tests aren't correct *yet*. They fail like this on Parrot:

Revised versions attached which pass on all 3 backends.

Nicholas Clark
>From d76d4f5b075ba9dead20fcf269a5757ff1c44fe1 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Tue, 11 Feb 2014 09:12:17 +0100
Subject: [PATCH 1/3] Basic serialization tests for integers.

Start by testing that -258 .. 258 round trip correctly.
---
 t/serialization/01-basic.t | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/t/serialization/01-basic.t b/t/serialization/01-basic.t
index 6131955..55f98bd 100644
--- a/t/serialization/01-basic.t
+++ b/t/serialization/01-basic.t
@@ -1,6 +1,6 @@
 #! nqp
 
-plan(66);
+plan(585);
 
 sub add_to_sc($sc, $idx, $obj) {
     nqp::scsetobj($sc, $idx, $obj);
@@ -380,3 +380,41 @@ sub add_to_sc($sc, $idx, $obj) {
     ok(nqp::atpos_s(nqp::scgetobj($dsc, 0).a, 1) eq 'sheep', 'string array second element is correct');
     ok(nqp::atpos_s(nqp::scgetobj($dsc, 0).a, 2) eq 'pig',   'string array third element is correct');
 }
+
+# integers
+{
+    my @a;
+
+    my int $i := -258;
+    while ($i <= 258) {
+        nqp::push(@a, $i);
+        $i := $i + 1;
+    }
+    my $elems := nqp::elems(@a);
+
+    my $sc := nqp::createsc('TEST_SC_13_IN');
+    my $sh := nqp::list_s();
+
+    class T12 {
+        has $!a;
+        method set_a(@a) { $!a := @a }
+        method get_a() { $!a }
+    }
+    my $v := T12.new();
+    $v.set_a(@a);
+    add_to_sc($sc, 0, $v);
+
+    my $serialized := nqp::serialize($sc, $sh);
+
+    my $dsc := nqp::createsc('TEST_SC_13_OUT');
+    nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
+
+    ok(nqp::istype(nqp::scgetobj($dsc, 0), Array), 'deserialized object has correct type');
+    my $j := 0;
+    my @b := nqp::scgetobj($dsc, 0).get_a();
+    ok(nqp::elems(@b) == $elems, 'array came back with correct element count');
+    while ($j < $elems) {
+        ok(@b[$j] == @a[$j], 'integer ' ~ @a[$j] ~ ' serialization round trip (' ~ $j ~ ')');
+        ++$j;
+    }
+}
-- 
1.8.4.2

>From cd8b9d7bc902a5408968198ff2fbe4da5ddc0f47 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Tue, 11 Feb 2014 12:10:04 +0100
Subject: [PATCH 2/3] Serialization tests for integers up to 2**62.

---
 t/serialization/01-basic.t | 52 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 14 deletions(-)

diff --git a/t/serialization/01-basic.t b/t/serialization/01-basic.t
index 55f98bd..1b09160 100644
--- a/t/serialization/01-basic.t
+++ b/t/serialization/01-basic.t
@@ -1,6 +1,6 @@
 #! nqp
 
-plan(585);
+plan(1341);
 
 sub add_to_sc($sc, $idx, $obj) {
     nqp::scsetobj($sc, $idx, $obj);
@@ -382,17 +382,12 @@ sub add_to_sc($sc, $idx, $obj) {
 }
 
 # integers
-{
-    my @a;
-
-    my int $i := -258;
-    while ($i <= 258) {
-        nqp::push(@a, $i);
-        $i := $i + 1;
-    }
+sub round_trip_int_array($seq, $desc, @a) {
+    $seq := 'TEST_SC_' ~ $seq;
+    $desc := 'for ' ~ $desc ~ ', ';
     my $elems := nqp::elems(@a);
 
-    my $sc := nqp::createsc('TEST_SC_13_IN');
+    my $sc := nqp::createsc($seq ~ '_IN');
     my $sh := nqp::list_s();
 
     class T12 {
@@ -406,15 +401,44 @@ sub add_to_sc($sc, $idx, $obj) {
 
     my $serialized := nqp::serialize($sc, $sh);
 
-    my $dsc := nqp::createsc('TEST_SC_13_OUT');
+    my $dsc := nqp::createsc($seq ~ '_OUT');
     nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
 
-    ok(nqp::istype(nqp::scgetobj($dsc, 0), Array), 'deserialized object has correct type');
+    ok(nqp::istype(nqp::scgetobj($dsc, 0), Array), $desc ~ 'deserialized object has correct type');
     my $j := 0;
     my @b := nqp::scgetobj($dsc, 0).get_a();
-    ok(nqp::elems(@b) == $elems, 'array came back with correct element count');
+    ok(nqp::elems(@b) == $elems, $desc ~ 'array came back with correct element count');
     while ($j < $elems) {
-        ok(@b[$j] == @a[$j], 'integer ' ~ @a[$j] ~ ' serialization round trip (' ~ $j ~ ')');
+        ok(nqp::iseq_i(@b[$j], @a[$j]), $desc ~ 'integer ' ~ @a[$j] ~ ' serialization round trip (' ~ $j ~ ')');
         ++$j;
     }
 }
+
+{
+    my @a;
+
+    my int $i := -258;
+    while ($i <= 258) {
+        nqp::push(@a, $i);
+        $i := $i + 1;
+    }
+    round_trip_int_array(13, 'small integers', @a);
+}
+
+{
+    my $i := 9;
+    my int $b := 512;
+
+    while ($i < 63) {
+        my @a;
+        my int $j := nqp::sub_i($b, 4);
+        while (nqp::islt_i($j, nqp::add_i($b, 2))) {
+            nqp::push(@a, $j);
+            nqp::push(@a, nqp::neg_i($j));
+            $j := nqp::add_i($j, 1);
+        }
+        round_trip_int_array($i + 7, 'integers around 2 ** ' ~ $i, @a);
+        ++$i;
+        $b := nqp::add_i($b, $b);
+    }
+}
-- 
1.8.4.2

>From e0ed38871d712e17b521a82d9ac406e2ab4b073a Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Tue, 11 Feb 2014 15:25:10 +0100
Subject: [PATCH 3/3] Serialization tests for integers around 2**63, and other
 interesting values.

---
 t/serialization/01-basic.t | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/t/serialization/01-basic.t b/t/serialization/01-basic.t
index 1b09160..d2c25b7 100644
--- a/t/serialization/01-basic.t
+++ b/t/serialization/01-basic.t
@@ -1,6 +1,6 @@
 #! nqp
 
-plan(1341);
+plan(1355);
 
 sub add_to_sc($sc, $idx, $obj) {
     nqp::scsetobj($sc, $idx, $obj);
@@ -442,3 +442,31 @@ sub round_trip_int_array($seq, $desc, @a) {
         $b := nqp::add_i($b, $b);
     }
 }
+
+{
+    # values around 2 ** 63, and interesting bit patterns.
+    # Need to do these as BigInts parsing strings due to a bug in nqp::radix
+    my @s := (
+               '9223372036854775805', # 0x7FFFFFFFFFFFFFFD
+               '9223372036854775806', # 0x7FFFFFFFFFFFFFFE
+               '9223372036854775807', # 0x7FFFFFFFFFFFFFFF
+              '-9223372036854775808', # 0x8000000000000000
+              '-9223372036854775807', # 0x8000000000000001
+              '-9223372036854775806', # 0x8000000000000002
+                '-81985529216486896', # 0xFEDCBA9876543210
+               '1147797409030816545', # 0x0FEDCBA987654321
+              '-6148914691236517206', # 0xAAAAAAAAAAAAAAAA
+               '6148914691236517205', # 0x5555555555555555
+              '-6510615555426900571', # 0xA5A5A5A5A5A5A5A5
+               '6510615555426900570', # 0x5A5A5A5A5A5A5A5A
+             );
+    my $bi_type := nqp::knowhow().new_type(:name("TestBigInt"), :repr("P6bigint"));
+    $bi_type.HOW.compose($bi_type);
+    my @a;
+    for (@s) {
+        my $bi := nqp::fromstr_I($_, $bi_type);
+        nqp::push(@a, nqp::unbox_i($bi));
+    }
+
+    round_trip_int_array(70, 'special case integers', @a);
+}
-- 
1.8.4.2

Reply via email to