-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
a few additional units #859
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b1161e9
a few additional units
Alex-Jordan b1c186b
move meter word units to aliases of m
Alex-Jordan bc1e2d2
Add pints, quarts and some volume unit unit tests
pstaabp f61d966
add metre, meters and some other documentation
pstaabp b802c8d
a few more changes.
pstaabp d3e0405
a few additional units
Alex-Jordan f4f638a
move meter word units to aliases of m
Alex-Jordan 5c68306
specify US gallons in help file
Alex-Jordan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think there are some other related units that could be added here. Pint and quart go with these volume measurements, and are equally standard with cup and gallon. There are also teaspoon and tablespoon, that are rather common. If you really want to go the distance there are also pottle, barrel, shot, gill, hogshead, etc. I haven't heard of some of those before. Some fun names too! See customary US units.
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.
I'll have a hogsheads of mead with lunch today. 😄
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.
For those that like extreme edge cases, the Gal is the acceleration of 1 cm/s^2, but was deprecated by the Int. Ast. Union in 1989. Probably an idea to include a comment that the gallon is US, not Imperial.
https://en.wikipedia.org/wiki/Gal_(unit)
https://www.iau.org/publications/proceedings_rules/units/
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.
@duffee In the help file, I put "US " in front of the title for each of gallon, quart, pint, and cup.