Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 107 additions & 99 deletions t/macros/pgaux.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,105 +13,113 @@ do "$ENV{PG_ROOT}/t/build_PG_envir.pl";

loadMacros('PGauxiliaryFunctions.pl');

# test step functions

is(step(8), 1, "step: positive number");
is(step(-8), 0, "step: negative number");
is(step(0), 0, "step: step(0)=0");

# test floor function

is(floor(0.5), 0, "floor: positive non-integer");
is(floor(-0.5), -1, "floor: negative non-integer");
is(floor(1), 1, "floor: positive integer");
is(floor(0), 0, "floor: floor(0)=0");
is(floor(-1), -1, "floor: negative integer");

# test ceiling function

is(ceil(0.5), 1, "ceil: positive non-integer");
is(ceil(-0.5), 0, "ceil: negative non-integer");
is(ceil(1), 1, "ceil: positive integer");
is(ceil(0), 0, "ceil: floor(0)=0");
is(ceil(-1), -1, "ceil: negative integer");

# max/min functions

is(max(1, 2, 3, 9, 4, 5, 6, 8), 9, "max: set of integers");
is(max(0.1, -2.3, 1.345, 2.71712, -1000.1), 2.71712, "max: set of decimals");
is(min(1, 2, 3, 9, 4, 5, 6, 8), 1, "min: set of integers");
is(min(0.1, -2.3, 1.345, 2.71712, -1000.1), -1000.1, "min: set of decimals");

# round function

is(round(0.95), 1, "round: fractional part > 0.5");
is(round(0.45), 0, "round: fractional part < 0.5");
is(round(0.5), 1, "round: fractional part = 0.5");
is(round(-0.95), -1, "round: fractional part > 0.5 and negative");
is(round(-0.45), 0, "round: fractional part < 0.5 and negative");
is(round(-0.5), -1, "round: fractional part = 0.5 and negative");

# Round function which takes a second number, the number of digits to round to

is(Round(1.793, 2), 1.79, "Round to 2 digits: test 1");
is(Round(1.797, 2), 1.80, "Round to 2 digits: test 2");
is(Round(1.795, 2), 1.80, "Round to 2 digits: test 3");
is(Round(-1.793, 2), -1.79, "Round to 2 digits: test 1");
is(Round(-1.797, 2), -1.80, "Round to 2 digits: test 2");
is(Round(-1.795, 2), -1.80, "Round to 2 digits: test 3");

is(Round(15.793, -1), 20, "Round to -1 digits (nearest 10)");

# lcm

is(lcm(20, 30), 60, "lcm: non relatively prime numbers");
is(lcm(5, 6), 30, "lcm: relatively prime numbers");
is(lcm(2, 3, 4), 12, "lcm: 3 numbers");
is(lcm(2, 3, 4, 5, 6, 7, 8), 840, "lcm: 7 numbers");

# gcd
is(gcd(16, 8), 8, "gcd: 2 powers of 2");
is(gcd(10, 9), 1, "gcd: 2 relatively prime");

is(gcd(10, 20, 30, 40), 10, "gcd: 4 multiples of 10");

# isPrime
is(isPrime(7), 1, "isPrime: 7 is prime");
is(isPrime(2), 1, "isPrime: 2 is prime");
is(isPrime(15), 0, "isPrime: 15 is not prime");
subtest 'Step function' => sub {
is(step(8), 1, "step: positive number");
is(step(-8), 0, "step: negative number");
is(step(0), 0, "step: step(0)=0");
};

subtest 'Floor function' => sub {
is(floor(0.5), 0, "floor: positive non-integer");
is(floor(-0.5), -1, "floor: negative non-integer");
is(floor(1), 1, "floor: positive integer");
is(floor(0), 0, "floor: floor(0)=0");
is(floor(-1), -1, "floor: negative integer");
};

subtest 'Ceiling function' => sub {
is(ceil(0.5), 1, "ceil: positive non-integer");
is(ceil(-0.5), 0, "ceil: negative non-integer");
is(ceil(1), 1, "ceil: positive integer");
is(ceil(0), 0, "ceil: floor(0)=0");
is(ceil(-1), -1, "ceil: negative integer");
};

subtest 'Min and Max functions' => sub {
is(max(1, 2, 3, 9, 4, 5, 6, 8), 9, "max: set of integers");
is(max(0.1, -2.3, 1.345, 2.71712, -1000.1), 2.71712, "max: set of decimals");
is(min(1, 2, 3, 9, 4, 5, 6, 8), 1, "min: set of integers");
is(min(0.1, -2.3, 1.345, 2.71712, -1000.1), -1000.1, "min: set of decimals");
};

subtest 'round and Round functions' => sub {
is(round(0.95), 1, "round: fractional part > 0.5");
is(round(0.45), 0, "round: fractional part < 0.5");
is(round(0.5), 1, "round: fractional part = 0.5");
is(round(-0.95), -1, "round: fractional part > 0.5 and negative");
is(round(-0.45), 0, "round: fractional part < 0.5 and negative");
is(round(-0.5), -1, "round: fractional part = 0.5 and negative");

# Round function which takes a second number, the number of digits to round to

is(Round(1.793, 2), 1.79, "Round to 2 digits: test 1");
is(Round(1.797, 2), 1.80, "Round to 2 digits: test 2");
is(Round(1.795, 2), 1.80, "Round to 2 digits: test 3");
is(Round(-1.793, 2), -1.79, "Round to 2 digits: test 1");
is(Round(-1.797, 2), -1.80, "Round to 2 digits: test 2");
is(Round(-1.795, 2), -1.80, "Round to 2 digits: test 3");

is(Round(15.793, -1), 20, "Round to -1 digits (nearest 10)");

is(Round(134.49999999999997, 0), 134.0, 'Round a number with decimals close to 0.5');
is(Round(1.49999999999991, 0), 1.0, 'Round another number with decimals close to 0.5');
is(Round(0.01499999999991, 2), 0.01, 'Round a number close to 0.005 to 2 digits');
};

subtest 'lcm and gcd functions' => sub {
is(lcm(20, 30), 60, "lcm: non relatively prime numbers");
is(lcm(5, 6), 30, "lcm: relatively prime numbers");
is(lcm(2, 3, 4), 12, "lcm: 3 numbers");
is(lcm(2, 3, 4, 5, 6, 7, 8), 840, "lcm: 7 numbers");

# gcd
is(gcd(16, 8), 8, "gcd: 2 powers of 2");
is(gcd(10, 9), 1, "gcd: 2 relatively prime");

is(gcd(10, 20, 30, 40), 10, "gcd: 4 multiples of 10");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an unnecessary empty line here.

};

subtest 'isPrime function' => sub {
is(isPrime(7), 1, "isPrime: 7 is prime");
is(isPrime(2), 1, "isPrime: 2 is prime");
is(isPrime(15), 0, "isPrime: 15 is not prime");
};

# random_coprime

my $sum = 0;
for my $i (1 .. 100) {
my @coprimes = random_coprime([ 1 .. 20 ], [ 1 .. 20 ]);
$sum += gcd($coprimes[0], $coprimes[1]);
}
is($sum, 100, "random_coprime: 100 tests in 1..20,1..20");

$sum = 0;

for my $i (1 .. 100) {
my @coprimes = random_coprime([ -9 .. -1, 1 .. 9 ], [ 1 .. 9 ], [ 1 .. 9 ]);
$sum += gcd(@coprimes);
}
is($sum, 100, "random_coprime: 100 tests in [-9..-1,1..9],[1..9],[1..9]");

my ($sum1, $sum2, $sum3, $sum4) = (0, 0, 0);
for my $i (1 .. 100) {
my @coprimes = random_pairwise_coprime([ -9 .. -1, 1 .. 9 ], [ 1 .. 9 ], [ 1 .. 9 ]);
$sum1 += gcd(@coprimes);
$sum2 += gcd($coprimes[0], $coprimes[1]);
$sum3 += gcd($coprimes[0], $coprimes[2]);
$sum4 += gcd($coprimes[1], $coprimes[2]);
}
is($sum1 + $sum2 + $sum3 + $sum4, 400, "random_pairwise_coprime: 100 tests of [-9..-1,1..9],[1..9],[1..9]");

# reduce
# it would be nicer to directly compare the arrays
my @my_arr = (3, 4);
my @res = reduce(15, 20);
is($my_arr[0], $res[0], "reduce: correct numerator");
is($my_arr[1], $res[1], "reduce: correct denominator");

subtest 'random_coprime function' => sub {
my $sum = 0;
for my $i (1 .. 100) {
my @coprimes = random_coprime([ 1 .. 20 ], [ 1 .. 20 ]);
$sum += gcd($coprimes[0], $coprimes[1]);
}
is($sum, 100, "random_coprime: 100 tests in 1..20,1..20");

$sum = 0;

for my $i (1 .. 100) {
my @coprimes = random_coprime([ -9 .. -1, 1 .. 9 ], [ 1 .. 9 ], [ 1 .. 9 ]);
$sum += gcd(@coprimes);
}
is($sum, 100, "random_coprime: 100 tests in [-9..-1,1..9],[1..9],[1..9]");

my ($sum1, $sum2, $sum3, $sum4) = (0, 0, 0);
for my $i (1 .. 100) {
my @coprimes = random_pairwise_coprime([ -9 .. -1, 1 .. 9 ], [ 1 .. 9 ], [ 1 .. 9 ]);
$sum1 += gcd(@coprimes);
$sum2 += gcd($coprimes[0], $coprimes[1]);
$sum3 += gcd($coprimes[0], $coprimes[2]);
$sum4 += gcd($coprimes[1], $coprimes[2]);
}
is($sum1 + $sum2 + $sum3 + $sum4, 400, "random_pairwise_coprime: 100 tests of [-9..-1,1..9],[1..9],[1..9]");
};

subtest 'reduce function' => sub {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another unnecessary empty line here.

# Note: this is testing reduction of fractions. Not sure why this is here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test for the PGauxiliaryFunctions.pl macro correct? It does define a reduce function, and that is what this is testing. Please delete this comment.

my @my_arr = (3, 4);
my @res = reduce(15, 20);
is($my_arr[0], $res[0], "reduce: correct numerator");
is($my_arr[1], $res[1], "reduce: correct denominator");
Comment on lines +122 to +123

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous comment in the code that you added, but are now removing stated that "it would be nicer to directly compare the arrays". Why is that not done? If this were changed to

Suggested change
is($my_arr[0], $res[0], "reduce: correct numerator");
is($my_arr[1], $res[1], "reduce: correct denominator");
is(\@res, \@my_arr, "reduce gives correct numerator and denominator");

it would do that.

Note that the order of the arguments for these tests is incorrect (corrected above). The result being tested should be first, and the expected result should be second.

Also, this test could be made quite concise by changing it to

subtest 'reduce function' => sub {
	is([ reduce(15, 20) ], [ 3, 4 ], "reduce gives correct numerator and denominator");
};

};
done_testing;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an empty line before done_testing; to separate the tests from this line.