# New Ticket Created by Simon Glover
# Please include the string: [perl #17896]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=17896 >
This patch:
i) Adds several new tests for PerlHash, specifically:
- storing scalar PMCs, and retrieving their values into PMC
registers and into INT/NUM/STRING registers as appropriate
(Some of this is already tested in the clone test, but really
deserves a stand-alone test)
- retrieving undefined values
- if (PerlHash), unless(PerlHash)
ii) Removes one test -- the former test no. 1 -- which tests exactly the
same functionality as a (more complete) later test
iii) Reorders the tests into a more rational order: we now test simple
things (like getting & setting STRINGs, INTs & NUMs) near the top, and
more complex things (clone, compound keys etc.) near the end. I find
this makes it easier to figure out what we're actually testing.
Simon
--- t/pmc/perlhash.t.old Sun Oct 13 10:48:53 2002
+++ t/pmc/perlhash.t Sun Oct 13 12:04:26 2002
@@ -1,27 +1,59 @@
#! perl
-use Parrot::Test tests => 17;
+use Parrot::Test tests => 21;
use Test::More;
-output_is(<<'CODE', <<OUTPUT, "simple set / get");
- new P0, .PerlHash
- set S0, "one"
- set S1, "two"
+output_is(<<CODE, <<OUTPUT, "Initial PerlHash tests");
+ new P0, .PerlHash
- set P0[S0], 1 # $P0{one} = 1
- set P0[S1], 2 # $P0{two} = 2
+ set P0["foo"], -7
+ set P0["bar"], 3.5
+ set P0["baz"], "value"
- set I0, P0[S0]
- set I1, P0[S1]
+ set I0, P0["foo"]
+ set N0, P0["bar"]
+ set S0, P0["baz"]
+
+ eq I0,-7,OK_1
+ print "not "
+OK_1: print "ok 1\\n"
+ eq N0,3.500000,OK_2
+ print N0
+OK_2: print "ok 2\\n"
+ eq S0,"value",OK_3
+ print S0
+OK_3: print "ok 3\\n"
+
+ set S1, "oof"
+ set S2, "rab"
+ set S3, "zab"
+
+ set P0[S1], 7
+ set P0[S2], -3.5
+ set P0[S3], "VALUE"
+
+ set I0, P0[S1]
+ set N0, P0[S2]
+ set S0, P0[S3]
+
+ eq I0,7,OK_4
+ print "not "
+OK_4: print "ok 4\\n"
+ eq N0,-3.500000,OK_5
+ print N0
+OK_5: print "ok 5\\n"
+ eq S0,"VALUE",OK_6
+ print S0
+OK_6: print "ok 6\\n"
- print I0
- print "\n"
- print I1
- print "\n"
end
CODE
-1
-2
+ok 1
+ok 2
+ok 3
+ok 4
+ok 5
+ok 6
OUTPUT
output_is(<<'CODE', <<OUTPUT, "more than one PerlHash");
@@ -111,6 +143,21 @@ CODE
2
OUTPUT
+# NB Next test depends on "key2" hashing to zero, which it does with
+# the current algorithm; if the algorithm changes, change the test!
+
+output_is(<<'CODE', <<OUTPUT, "key that hashes to zero");
+ new P0, .PerlHash
+ set S0, "key2"
+ set P0[S0], 1
+ set I0, P0[S0]
+ print I0
+ print "\n"
+ end
+CODE
+1
+OUTPUT
+
output_is(<<'CODE', <<OUTPUT, "size of the hash");
new P0, .PerlHash
@@ -136,50 +183,6 @@ CODE
2
OUTPUT
-
-# NB Next test depends on "key2" hashing to zero, which it does with
-# the current algorithm; if the algorithm changes, change the test!
-
-output_is(<<'CODE', <<OUTPUT, "key that hashes to zero");
- new P0, .PerlHash
- set S0, "key2"
- set P0[S0], 1
- set I0, P0[S0]
- print I0
- print "\n"
- end
-CODE
-1
-OUTPUT
-
-output_is(<<CODE, <<OUTPUT, "Initial PerlHash tests");
- new P0, .PerlHash
-
- set P0["foo"], -7
- set P0["bar"], 3.5
- set P0["baz"], "value"
-
- set I0, P0["foo"]
- set N0, P0["bar"]
- set S0, P0["baz"]
-
- eq I0,-7,OK_1
- print "not "
-OK_1: print "ok 1\\n"
- eq N0,3.500000,OK_2
- print N0
-OK_2: print "ok 2\\n"
- eq S0,"value",OK_3
- print S0
-OK_3: print "ok 3\\n"
-
- end
-CODE
-ok 1
-ok 2
-ok 3
-OUTPUT
-
output_is(<<CODE, <<OUTPUT, "stress test: loop(set, check)");
new P0, .PerlHash
@@ -288,21 +291,6 @@ CODE
done
OUTPUT
-output_is(<<CODE, <<OUTPUT, "String as keys");
- new P0,.PerlHash
- new P1,.PerlArray
- new P2,.PerlArray
- set P1[4],"string"
- set P0["one"],P1
- set P2,P0["one"]
- set S0,P2[4]
- print S0
- print "\\n"
- end
-CODE
-string
-OUTPUT
-
output_is(<<'CODE', <<OUTPUT, "Testing two hash indices with integers at a time");
new P0, .PerlHash
@@ -411,6 +399,138 @@ ok 3
ok 4
OUTPUT
+
+# So far, we've only used INTVALs, FLOATVALs and STRINGs as values
+# and/or keys. Now we try PMCs.
+
+output_is(<<'CODE', <<OUTPUT, "Setting & getting scalar PMCs");
+ new P0, .PerlHash
+ new P1, .PerlInt
+ new P2, .PerlInt
+
+ set S0, "non-PMC key"
+
+ set P1, 10
+ set P0[S0], P1
+ set P2, P0[S0]
+ eq P2, P1, OK1
+ print "not "
+OK1: print "ok 1\n"
+
+ set P1, -1234.000000
+ set P0[S0], P1
+ set P2, P0[S0]
+ eq P2, P1, OK2
+ print "not "
+OK2: print "ok 2\n"
+
+ set P1, "abcdefghijklmnopq"
+ set P0[S0], P1
+ set P2, P0[S0]
+ eq P2, P1, OK3
+ print "not "
+OK3: print "ok 3\n"
+
+ new P1, .PerlUndef
+ set P0[S0], P1
+ set P2, P0[S0]
+ typeof S1, P2
+ eq S1, "PerlUndef", OK4
+ print "not "
+OK4: print "ok 4\n"
+
+ end
+CODE
+ok 1
+ok 2
+ok 3
+ok 4
+OUTPUT
+
+output_is(<<'CODE', <<OUTPUT, "Setting scalar PMCs & getting scalar values");
+ new P0, .PerlHash
+ new P1, .PerlInt
+
+ set S0, "A rather large key"
+
+ set I0, 10
+ set P1, I0
+ set P0[S0], P1
+ set I1, P0[S0]
+ eq I1, I0, OK1
+ print "not "
+OK1: print "ok 1\n"
+
+ set N0, -1234.000000
+ set P1, N0
+ set P0[S0], P1
+ set N1, P0[S0]
+ eq N1, N0, OK2
+ print "not "
+OK2: print "ok 2\n"
+
+ set S1, "abcdefghijklmnopq"
+ set P1, S1
+ set P0[S0], P1
+ set S2, P0[S0]
+ eq S2, S1, OK3
+ print "not "
+OK3: print "ok 3\n"
+
+ end
+CODE
+ok 1
+ok 2
+ok 3
+OUTPUT
+
+output_is(<<'CODE', <<OUTPUT, "Getting values from undefined keys");
+ new P2, .PerlHash
+
+ set I0, P2["qwerty"]
+ set N0, P2["asdfgh"]
+ set S0, P2["zxcvbn"]
+ set P0, P2["123456"]
+
+ eq, I0, 0, OK1
+ print "not "
+OK1: print "ok 1\n"
+
+ eq, N0, 0.0, OK2
+ print "not "
+OK2: print "ok 2\n"
+
+ eq, S0, "", OK3
+ print "not "
+OK3: print "ok 3\n"
+
+ typeof S1, P0
+ eq S1, "PerlUndef", OK4
+ print "not "
+OK4: print "ok 4\n"
+ end
+CODE
+ok 1
+ok 2
+ok 3
+ok 4
+OUTPUT
+
+output_is(<<CODE, <<OUTPUT, "Setting & getting non-scalar PMCs");
+ new P0,.PerlHash
+ new P1,.PerlArray
+ new P2,.PerlArray
+ set P1[4],"string"
+ set P0["one"],P1
+ set P2,P0["one"]
+ set S0,P2[4]
+ print S0
+ print "\\n"
+ end
+CODE
+string
+OUTPUT
+
output_is(<<'CODE', <<OUTPUT, "Testing clone");
new P0, .PerlHash
set S0, "a"
@@ -552,4 +672,77 @@ CODE
12
OUTPUT
+# A hash is only false if it has size 0
+
+output_is(<<'CODE', <<OUTPUT, "if (PerlHash)");
+ new P0, .PerlHash
+
+ if P0, BAD1
+ print "ok 1\n"
+ branch OK1
+BAD1: print "not ok 1\n"
+OK1:
+
+ set P0["key"], "value"
+ if P0, OK2
+ print "not "
+OK2: print "ok 2\n"
+
+ set P0["key"], ""
+ if P0, OK3
+ print "not "
+OK3: print "ok 3\n"
+
+ new P1, .PerlUndef
+ set P0["key"], P1
+ if P0, OK4
+ print "not "
+OK4: print "ok 4\n"
+
+ end
+CODE
+ok 1
+ok 2
+ok 3
+ok 4
+OUTPUT
+
+output_is(<<'CODE', <<OUTPUT, "unless (PerlHash)");
+ new P0, .PerlHash
+
+ unless P0, OK1
+ print "not "
+OK1: print "ok 1\n"
+
+ set P0["key"], "value"
+ unless P0, BAD2
+ print "ok 2\n"
+ branch OK2
+BAD2: print "not ok 2"
+OK2:
+
+ set P0["key"], "\0"
+ unless P0, BAD3
+ print "ok 3\n"
+ branch OK3
+BAD3: print "not ok 3"
+OK3:
+
+ new P1, .PerlUndef
+ set P0["key"], P1
+ unless P0, BAD4
+ print "ok 4\n"
+ branch OK4
+BAD4: print "not ok 4"
+OK4:
+
+ end
+CODE
+ok 1
+ok 2
+ok 3
+ok 4
+OUTPUT
+
1;
+