diff --git a/htdocs/helpFiles/Entering-Units.html b/htdocs/helpFiles/Entering-Units.html index da55b4ad05..271aa54eae 100644 --- a/htdocs/helpFiles/Entering-Units.html +++ b/htdocs/helpFiles/Entering-Units.html @@ -15,23 +15,23 @@
diff --git a/lib/Units.pm b/lib/Units.pm index 3b0912fd37..5fb6c479a3 100644 --- a/lib/Units.pm +++ b/lib/Units.pm @@ -40,8 +40,9 @@ our $PI = 4 * atan2(1, 1); our %known_units = ( m => { - factor => 1, - m => 1 + factor => 1, + m => 1, + aliases => [ 'meter', 'meters', 'metre', 'metres' ] }, kg => { factor => 1, @@ -122,11 +123,12 @@ our %known_units = ( hour => { factor => 3600, s => 1, - aliases => [ 'hours', 'hr', 'h' ] + aliases => [ 'hours', 'hrs', 'hr', 'h' ] }, day => { - factor => 86400, - s => 1 + factor => 86400, + s => 1, + aliases => ['days'] }, month => { # 60 * 60 * 24 * 30 factor => 2592000, @@ -228,19 +230,35 @@ 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, + aliases => [ 'gallons', 'gal' ] + }, # VELOCITY: fundamental unit m/s (meters per second) - knots => { # nautical miles per hour + knots => { # nautical miles per hour factor => 0.5144444444, m => 1, s => -1 }, - c => { # exact speed of light + c => { # exact speed of light factor => 299792458, m => 1, s => -1 @@ -335,10 +353,11 @@ our %known_units = ( aliases => [ 'pound', 'pounds', 'lbs' ] }, ton => { - factor => 8900, - m => 1, - kg => 1, - s => -2 + factor => 8900, + m => 1, + kg => 1, + s => -2, + aliases => ['tons'] }, # ENERGY: fundamental unit m^2 kg / s^2 diff --git a/t/units/volume.t b/t/units/volume.t new file mode 100644 index 0000000000..f8b1c1d808 --- /dev/null +++ b/t/units/volume.t @@ -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();