Skip to content

Commit

Permalink
Merge branch 'units' of https://github.com/Alex-Jordan/pg into units
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Jordan committed Jun 28, 2023
2 parents b419175 + 71de40a commit 8bf4c72
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
47 changes: 34 additions & 13 deletions htdocs/helpFiles/Entering-Units.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Seconds</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">s</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">s, sec, second or seconds</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Minutes</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">min</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">min, minute or minutes</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Hours</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">hr</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">hr, hrs, h, hour or hours</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Days</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">day</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">day or days</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Years</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">yr</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">yr, year or years</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Milliseconds</td>
Expand All @@ -43,19 +43,19 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Feet</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ft</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ft, foot or feet</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Inches</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">in</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">in, inch or inches</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Miles</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">mi</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">mi, mile or miles</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Meters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">m</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">m, meter, metre, meters or metres</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Centimeters</td>
Expand Down Expand Up @@ -109,6 +109,27 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
<td style="padding: 0.5rem; border: 1px solid black">Milliliters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ml</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Deciliters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">dL</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Cups</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">cup or cups</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Pints</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">pt, pint or pints</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Quarts</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">qt, quart or quarts</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Gallons</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">gal, gallon or gallons</td>
</tr>


<tr>
<td colspan="2" style="padding: 0.5rem; border: 1px solid black; text-align: center"><b>Force</b></td>
Expand All @@ -123,11 +144,11 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Pounds</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">lb</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">lb, lbs, pound or pounds</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Tons</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ton</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ton or tons</td>
</tr>

<tr>
Expand Down Expand Up @@ -191,11 +212,11 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angle degrees</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">deg</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">deg, &#x00B0;, degree or degrees</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angle radians</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">rad</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">rad, radian or radians</td>
</tr>
</table>
<p style="text-align: center">
Expand Down
13 changes: 12 additions & 1 deletion lib/Units.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ our %known_units = (
m => {
factor => 1,
m => 1,
aliases => [ 'meter', 'meters' ]
aliases => [ 'meter', 'meters', 'metre', 'metres' ]
},
kg => {
factor => 1,
Expand Down Expand Up @@ -230,11 +230,22 @@ our %known_units = (
factor => 0.0001,
m => 3
},
# U.S./English volume units
cup => {
factor => 0.000236588,
m => 3,
aliases => ['cups']
},
pint => {
factor => 0.000473176473,
m => 3,
aliases => [ 'pt', 'pints' ]
},
quart => {
factor => 0.000946352946,
m => 3,
aliases => [ 'qt', 'quarts' ]
},
gallon => {
factor => 0.00378541,
m => 3,
Expand Down
74 changes: 74 additions & 0 deletions t/units/volume.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env perl

use Test2::V0 '!E', { E => 'EXISTS' };

die "PG_ROOT not found in environment.\n" unless $ENV{PG_ROOT};
do "$ENV{PG_ROOT}/t/build_PG_envir.pl";

use lib "$ENV{PG_ROOT}/lib";

use Units qw(evaluate_units);
use Parser::Legacy::NumberWithUnits;

loadMacros('parserNumberWithUnits.pl');

my $cubic_meter = NumberWithUnits(1, 'm^3');
my $liter = NumberWithUnits(1, 'L');
my $milliliter = NumberWithUnits(1, 'ml');
my $deciliter = NumberWithUnits(1, 'dL');

subtest 'metric LaTeX output' => sub {
is $cubic_meter->TeX, '1\ {\rm m^{3}}', 'LaTeX output for 1 cubic meter';
is $liter->TeX, '1\ {\rm L}', 'LaTeX output for 1 liter';
is $deciliter->TeX, '1\ {\rm dL}', 'LaTeX output for 1 deciliter';
is $milliliter->TeX, '1\ {\rm ml}', 'LaTeX output for 1 milliliter';
};

subtest 'metric unit aliases' => sub {
is { evaluate_units('ml') }, { evaluate_units('cc') }, '1 mL = 1 cc';
};

subtest 'metric volume conversion' => sub {
is multiply_by(1000, evaluate_units('L')), { evaluate_units('m^3') }, '1000 L = 1 m^3';
is multiply_by(1000, evaluate_units('ml')), { evaluate_units('L') }, '1000 ml = 1 L';
is multiply_by(10, evaluate_units('dL')), { evaluate_units('L') }, '10 dL = 1 L';
};

my $gallon = NumberWithUnits(1, 'gal');
my $quart = NumberWithUnits(1, 'qt');
my $pint = NumberWithUnits(1, 'pt');
my $cup = NumberWithUnits(1, 'cup');

subtest 'U.S. Units LaTeX output' => sub {
is $gallon->TeX, '1\ {\rm gal}', 'LaTeX output for 1 gallon';
is $quart->TeX, '1\ {\rm qt}', 'LaTeX output for 1 quart';
is $pint->TeX, '1\ {\rm pt}', 'LaTeX output for 1 pint';
is $cup->TeX, '1\ {\rm cup}', 'LaTeX output for 1 cup';
};

subtest 'metric unit aliases' => sub {
is { evaluate_units('cup') }, { evaluate_units('cups') }, 'cups alias';
is { evaluate_units('pt') }, { evaluate_units('pint') }, 'pint alias';
is { evaluate_units('pt') }, { evaluate_units('pints') }, 'pints alias';
is { evaluate_units('qt') }, { evaluate_units('quart') }, 'quart alias';
is { evaluate_units('qt') }, { evaluate_units('quarts') }, 'pint alias';
is { evaluate_units('gallon') }, { evaluate_units('gal') }, 'gal alias';
is { evaluate_units('gallon') }, { evaluate_units('gallons') }, 'gallons alias';
};

subtest 'U.S. volume conversion' => sub {
is multiply_by(3.78541, evaluate_units('L')), { evaluate_units('gal') }, '3.785412 L = 1 gal';
# Switch to check_score to do fuzzy comparison since cups/pints/quart/gallons are defined in
# terms of cubic meters.
is check_score(NumberWithUnits(2, 'cup'), $pint), 1, '2 cups = 1 pint';
is check_score(NumberWithUnits(2, 'pint'), $quart), 1, '2 pints = 1 quart';
is check_score(NumberWithUnits(4, 'quart'), $gallon), 1, '4 quarts = 1 gallon';
};

sub multiply_by {
my ($conversion, %unit) = @_;
$unit{factor} *= $conversion;
return \%unit;
}

done_testing();

0 comments on commit 8bf4c72

Please sign in to comment.