FAQ

Tony Cook via RT wrote:
It's the same bug and is fixed in blead, that line now reads:

like &CORE::rand, qr/^[.\d+-e]*\z/, '&rand';
I wouldn't exactly call that "fixed"...

It certainly quells the false negative – i.e., it no longer reports a
failure when it actually succeeds – but it doesn't do very much to
ensure a valid result (which, if I'm not mistaken, is the whole purpose
of testing, no?)

Now even an empty string will succeed! As will any of the following:

      -1
      6.6.6
      ERROR
      :-\
      +,-./:;<=>?@[\]^_`
      ^LL_Y0UR_b^SE_aRE_BeL@NG_+O_US

I would argue the following is "more correct":

      like &CORE::rand, qr/^(?:0(?:\.\d+)?|[1-9](?:\.\d+)?e-\d+)\z/, '&rand';

or, (I hope) more readably:

    like&CORE::rand, qr/^(?: # match either
                           0 (?:\.\d+)? # 0, and maybe "dot numbers"
# or
                           [1-9] (:\.\d+)? # non-zero and optional decimal
                           e-\d+ # followed by a negative exponent
                          )\z/x, '&rand';

On Tue Dec 10 15:58:22 2013, dabe@dabe.com wrote:
Here's a quick patch that extends the search pattern to explicitly match just plain zero (it's possible, I suppose), a decimal that starts with zero, or a very small number in scientific notation:

--- t/op/coreamp.t_OLD 2013-12-10 18:24:46.000000000 -0500
+++ t/op/coreamp.t 2013-12-10 18:29:04.000000000 -0500
@@ -637,7 +637,7 @@

test_proto 'rand';
$tests += 3;
-like&CORE::rand, qr/^0[.\d+-e]*\z/, '&rand';
+like&CORE::rand, qr/^(?:0(?:\.\d+)?|[1-9](?:\.\d+)?e-\d+)\z/, '&rand';
unlike join(" ",&CORE::rand), qr/ /, '&rand in list context';
&cmp_ok(&CORE::rand(78), qw '< 78', '&rand with 1 arg');


NOTE: Even as written in the original test, I don't think it's right
to match a plus
(e.g., "3.4e+05") since CORE::rand() with no arguments should never
return a number
greater than one...

PS: And, looking at it again, the "-" in the "[]" character class
isn't at the beginning,
so it's actually looking for any character from '+' (0x2B) through
lowercase 'e' (0x65),
which includes the entire uppercase alphabet and a slew of other
punctuation. Oops!

prompt% perl -E '$f = "0AQb_`,J"; say "$f: " . ($f =~ qr/^0[.\d+-e]*\z/ ? "YES" : "NO")'
0AQb_`,J: YES
--
:- Dabe

Search Discussions

  • Dabrien 'Dabe' Murphy at Dec 11, 2013 at 6:33 pm

    Tony Cook via RT wrote:
    It's the same bug and is fixed in blead, that line now reads:

    like &CORE::rand, qr/^[.\d+-e]*\z/, '&rand';
    I wouldn't exactly call that "fixed"...

    It certainly quells the false positive -- i.e., it no longer reports a
    failure when the test actually succeeds -- but it doesn't do very much
    to ensure a valid result (which, if I'm not mistaken, is the whole
    purpose of TDD, no?)

    As it's written now, even an empty string will succeed! As will any of
    the following:

          -1
          6.6.6
          ERROR
          :-\
          +,-./:;<=>?@[\]^_`
          ^LL_Y0UR_b^SE_aRE_BeL@NG_+O_US

    I would argue the following is "more correct":

          like &CORE::rand, qr/^(?:0(?:\.\d+)?|[1-9](?:\.\d+)?e-\d+)\z/, '&rand';

    or, (I hope) more readably:

        like&CORE::rand, qr/^(?: # match either
                               0 (?:\.\d+)? # 0, and maybe "dot numbers"
    # or
                               [1-9] (?:\.\d+)? # non-zero and optional decimal
                               e-\d+ # followed by a negative exponent
                              )\z/x, '&rand';


    --
    :- Dabe
  • Zefram at Dec 13, 2013 at 10:46 am

    Dabrien 'Dabe' Murphy wrote:
    I would argue the following is "more correct":
    How about a numeric comparison?

      my $r = &CORE::rand;
      ok $r >= 0 && $r < 1;

    All this regexp business is entertaining, and I'm not one to shy away
    from solving a problem with a regexp, but in this case it's firmly the
    wrong tool for the job.

    -zefram
  • Demerphq at Dec 13, 2013 at 1:31 pm

    On 13 December 2013 11:45, Zefram wrote:
    Dabrien 'Dabe' Murphy wrote:
    I would argue the following is "more correct":
    How about a numeric comparison?

    my $r = &CORE::rand;
    ok $r >= 0 && $r < 1;

    All this regexp business is entertaining, and I'm not one to shy away
    from solving a problem with a regexp, but in this case it's firmly the
    wrong tool for the job.
    I like this test. But since we all use the same RNG now, cant we just
    set srand() explicitly and test that it produces the right sequence of
    random numbers?

    Yves



    --
    perl -Mre=debug -e "/just|another|perl|hacker/"
  • Dabrien 'Dabe' Murphy at Dec 13, 2013 at 6:54 pm

    On 12/13/13, 5:46 AM, Zefram via RT wrote:
    All this regexp business is entertaining, and I'm not one to shy away
    from solving a problem with a regexp, but in this case it's firmly the
    wrong tool for the job.
    You're right, of course... Not that the test code does it, but I wasn't
    even thinking about what would happen in a locale that uses "," as the
    decimal separator.
    How about a numeric comparison?

    my $r =&CORE::rand;
    ok $r>= 0&& $r< 1;
    My only issue there is that, as written, the empty string and
    non-numeric text output (like, "ERROR: Frobnication Failure") would
    still pass.

    How 'bout:

          my $r = &CORE::rand;
          ok eval {
              use warnings FATAL => qw{numeric uninitialized};
              $r >= 0 && $r < 1;
          }, '&rand returns a valid number';

    ?

    --
    :- Dabe
  • Father Chrysostomos via RT at Dec 15, 2013 at 2:33 pm

    Dabrien 'Dabe' Murphy wrote:
    How 'bout:

    my $r = &CORE::rand;
    ok eval {
    use warnings FATAL => qw{numeric uninitialized};
    $r >= 0 && $r < 1;
    }, '&rand returns a valid number';

    ?
    That looks good. Could you turn that into a patch?


    ---
    via perlbug: queue: perl5 status: resolved
    https://rt.perl.org/Ticket/Display.html?id=118237
  • Father Chrysostomos at Dec 16, 2013 at 5:31 am

    I wrote:
    Dabrien 'Dabe' Murphy wrote:
    How 'bout:

    my $r = &CORE::rand;
    ok eval {
    use warnings FATAL => qw{numeric uninitialized};
    $r >= 0 && $r < 1;
    }, '&rand returns a valid number';

    ?
    That looks good. Could you turn that into a patch?
    Never mind. I have just applied it as d62c8fd18 and added you to
    AUTHORS in f17522ddf. Thank you.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedDec 11, '13 at 5:26a
activeDec 16, '13 at 5:31a
posts7
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase