-
-
Notifications
You must be signed in to change notification settings - Fork 79
Update the pgaux.t test for subtests and add some new #1428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: PG-2.21
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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"); | ||||||||
|
|
||||||||
| }; | ||||||||
|
|
||||||||
| 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 { | ||||||||
|
|
||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a test for the |
||||||||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 |
||||||||
| }; | ||||||||
| done_testing; | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an empty line before |
||||||||
There was a problem hiding this comment.
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.