Add Internals::HvREHASH to expose the rehashing flag
Nicholas Clark [Sat, 1 Nov 2003 14:21:38 +0000 (14:21 +0000)]
t/op/hash.t tests that pathological data triggers rehashing

p4raw-id: //depot/perl@21604

t/op/hash.t [new file with mode: 0644]
universal.c

diff --git a/t/op/hash.t b/t/op/hash.t
new file mode 100644 (file)
index 0000000..3beae84
--- /dev/null
@@ -0,0 +1,27 @@
+#!./perl -w
+
+BEGIN {
+    chdir 't' if -d 't';
+    @INC = '../lib';
+    require './test.pl';
+}
+
+use strict;
+
+plan tests => 3;
+
+my %h;
+
+ok (!Internals::HvREHASH(%h), "hash doesn't start with rehash flag on");
+
+foreach (1..10) {
+  $h{"\0"x$_}++;
+}
+
+ok (!Internals::HvREHASH(%h), "10 entries doesn't trigger rehash");
+
+foreach (11..20) {
+  $h{"\0"x$_}++;
+}
+
+ok (Internals::HvREHASH(%h), "20 entries triggers rehash");
index 3a646ea..9a8ec1b 100644 (file)
@@ -188,6 +188,7 @@ XS(XS_Internals_hv_clear_placehold);
 XS(XS_PerlIO_get_layers);
 XS(XS_Regexp_DESTROY);
 XS(XS_Internals_hash_seed);
+XS(XS_Internals_HvREHASH);
 
 void
 Perl_boot_core_UNIVERSAL(pTHX)
@@ -232,6 +233,7 @@ Perl_boot_core_UNIVERSAL(pTHX)
                XS_PerlIO_get_layers, file, "*;@");
     newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
     newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
+    newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
 }
 
 
@@ -916,3 +918,17 @@ XS(XS_Internals_hash_seed)
     XSRETURN_UV(PERL_HASH_SEED);
 }
 
+XS(XS_Internals_HvREHASH)      /* Subject to change  */
+{
+    dXSARGS;
+    if (SvROK(ST(0))) {
+       HV *hv = (HV *) SvRV(ST(0));
+       if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
+           if (HvREHASH(hv))
+               XSRETURN_YES;
+           else
+               XSRETURN_NO;
+       }
+    }
+    Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
+}