in reply to Stupidest Prime Number detector ever!!

Per Anomalous Monk (and the original intention of the post) "Ok, you've got the lousy algorithm. So where's the module and its .t script? That seems to me to be the thing to focus on right now."

So here goes. Here's what I have:

I changed the directory to prime_scripts cause that's where I have everything related to this stuff

$ cd prime_scripts/

The  tree command shows the contents of the directory.

$  tree
.
├── lib
│   └── LousyPrime.pm
├── README.md
└── t
    └── prime_test_1.t

2 directories, 3 files

Here's the output of the very basic test. Note:- The  v flag was purposefully not included in the  prove output below so as to avoid long running output.

$ prove -l t/prime_test_1.t t/prime_test_1.t .. ok All tests successful. Files=1, Tests=1000, 0 wallclock secs ( 0.06 usr 0.01 sys + 0.14 cu +sr 0.01 csys = 0.22 CPU) Result: PASS $

Here's the Perl Module created based on the script. The output had to be tweaked to match that of Math::Prime::Util is_prime() function.

package LousyPrime;

use strict;
use warnings;

use Exporter qw(import);
our $VERSION = '0.001';
$VERSION = eval {$VERSION};
our @EXPORT_OK = qw (is_lousy_prime);

sub is_lousy_prime {

	my $prime_candidate = shift @_;

	if ( $prime_candidate <= 0 ) {
		return 0;
		exit;
	}
	exit if $prime_candidate == 1 && return 0;

  my $bool_is_prime = 1;

  foreach my $each_num (2..sqrt $prime_candidate) {

  	if ($prime_candidate % $each_num == 0 && $prime_candidate - $each_num != 0 ) {
      $bool_is_prime = 0;
			return 0;
      exit;
    }

  }
  
	return 2 if  $bool_is_prime != 0;
}

1;

And here is the test script

use strict; use warnings; use 5.10.1; use Math::Prime::Util qw (is_prime); use Test::More; use FindBin::libs; use LousyPrime qw(is_lousy_prime); for (1..1000) { say is_prime($_), is_lousy_prime($_); is ( is_prime($_) , is_lousy_prime($_), "matches"); } done_testing;

There's a lot more other ways to test, but this is what I've come up with as of now. If the revered monks here do find anything I can improve in the test code written above, please do let me know.

Replies are listed 'Best First'.
Re^2: Stupidest Prime Number detector ever!! (updated)
by AnomalousMonk (Archbishop) on Jun 23, 2021 at 23:27 UTC

    The naming convention for SomeModule.pm test code is SomeModule.t. This is not enforced in any way, but it's what everyone expects. | Well, it's what I expected, anyway. See Update Note 1 below.

    Some comments on the is_lousy_prime() function code.

    if ( $prime_candidate <= 0 ) { return 0; exit; }
    The exit; statement in the code above will never be reached. No code in the function will be executed after the return 0; statement executes. There's another example of this unreachable-code syntax in the foreach loop further on in the function.

    exit if $prime_candidate == 1 && return 0;
    This is more involved, but essentially the same thing is happening: the exit built-in function will never be executed.
    • If the $prime_candidate == 1 expression is false, exit will not be executed because the if-condition is not true. (Update: The return 0 expression will not be executed because && short-circuits.)
    • If the $prime_candidate == 1 expression is true, the return 0 expression will be executed and will immediately return from the function; exit will not be executed.

    And one more thing: Please, please choose a reasonable indentation style and stick to it!

    Update:
    Notes:

    1. After reading kcott's reply, I thought to myself "Yeah, I do seem to recall spending many happy hours watching files with exactly that nn_name.t format roll by during countless module installs." WTF?!? I think my confusion stems from my practice of writing test scripts with the naming format I mentioned as part of my personal code development best practices. I then conflated personal and general. Oh, well...


    Give a man a fish:  <%-{-{-{-<

      G'day AnomalousMonk,

      "The naming convention for SomeModule.pm test code is SomeModule.t. This is not enforced in any way, but it's what everyone expects."

      I have never encountered that convention — where have you seen it? I certainly wouldn't expect it; I've mostly seen test files starting with numbers and are all lowercase (e.g. nn-name.t).

      A t/ directory I'd create for an OO module might look something like:

      00-load.t # test 'use' 01-instantiate.t # test 'new()' 02-some_func.t # test 'some_func()' ... # and so on ...

      And, as a real world example, here's part of the output of make test for a $work module I ran in the last hour:

      t/00-load.t ............... ok t/01-instantiate.t ........ ok t/02-overload.t ........... ok t/03-validation.t ......... ok ...

      Just in case I was having a sudden, and unexpected, mental breakdown, I checked a few arbitrary, but well-known, CPAN modules' t/ directories:

      Module Repo t/
      Text::CSV https://github.com/makamaka/Text-CSV/tree/master/t
      JSON https://github.com/makamaka/JSON/tree/master/t
      DBI https://github.com/perl5-dbi/dbi/tree/master/t
      XML::LibXML https://github.com/shlomif/perl-XML-LibXML/tree/master/t

      As you can see, for the most part they all follow the same basic naming convention (i.e. number, name, .t). There are a few exceptions (e.g. pod.t) but there are none that look like your SomeModule.t.

      — Ken

        > I have never encountered that convention — where have you seen it? I certainly wouldn't expect it; I've mostly seen test files starting with numbers and are all lowercase (e.g. nn-name.t).

        Yes. Strongly agree.

        Here's another convention I'm sure you'd expect and approve of: convert a haiku (three lines of 5-7-5 syllables) into the test names, so as to entertain the bored tester (or AI robot) by gradually revealing an inspiring haiku as the test suite runs. For example, this haiku:

        Coffee mug shatters
        Larry Apocalyptic
        Parrot not a hoax

        Gallop Ponie bold!
        Beer to gulp, Buffy astride
        Orange sky surrounds

        can be easily converted into a CPAN test suite to display:

        t/00_Coffee.t ....... ok t/01_mug.t .......... ok t/02_shatters.t ..... ok t/03_Larry.t ........ ok t/04_Apocalyptic.t .. ok t/05_Parrot.t ....... ok t/06_not.t .......... ok t/07_a.t ............ ok t/08_hoax.t ......... ok t/09_Gallop.t ....... ok t/10_Ponie.t ........ ok t/11_bold.t ......... ok t/12_Beer.t ......... ok t/13_to.t ........... ok t/14_gulp.t ......... ok t/15_Buffy.t ........ ok t/16_astride.t ...... ok t/17_Orange.t ....... ok t/18_sky.t .......... ok t/19_surrounds.t .... ok

        What? I'm the only one using that convention? Really?

        Seeing it again after so many years, it's looking a bit dated, composed during the early Perl 6 era, and heavily influenced by London.pm culture I see. Probably should compose a new one.

        > I have never encountered that convention — where have you seen it?

        Interestingly, we use it at work for 1K+ libraries that drive our product.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Damn ..indeed foolish of me to write the return 0 and exit that way .. it's not that I don't know about this but I still wrote it...and thanks for the naming convention and indenting tips...I've bothered you enough for today, but I'll update this tomorrow..