123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- #---------------------------------------------------*-TCL-*-----
- #
- # Commands covered:
- # units::new
- # units::convert
- # units::reduce
- #
- # This file contains a collection of tests for the units
- # conversion facility.
- #
- # Copyright 2000-2004 Mayo Foundation. All Rights Reserved.
- # $Id: units.test,v 1.2 2004/02/05 22:24:23 techenti Exp $
- #
- # October 30, 2000
- # Robert W. Techentin
- #
- #----------------------------------------------------------------
- #-----------------------------
- # for development purposes
- #-----------------------------
- if { ! [catch {glob units.tcl}] } {
- set auto_path [linsert $auto_path 0 .]
- }
- if {[lsearch [namespace children] ::tcltest] == -1} {
- package require tcltest
- namespace import -force ::tcltest::*
- }
- if {[catch {package require units 1.0} msg]} {
- catch {puts stderr "Cannot load units 1.0 package: '$msg'"}
- return
- }
- #-----------------------------------------------------------
- #
- # Check that commands exist
- #
- #-----------------------------------------------------------
- test units-1.1 {new command exists} {
- list [catch {units::new} msg] $msg
- } {1 {Wrong # args. units::new name baseUnits }}
- test units-1.2 {convert command exists} {
- list [catch {units::convert} msg] $msg
- } {1 {Wrong # args. units::convert value targetUnits }}
- test units-1.3 {reduce command exists} {
- list [catch {units::reduce} msg] $msg
- } {1 {Wrong # args. units::reduce unitString }}
- #-----------------------------------------------------------
- #
- # exercise new command
- #
- #-----------------------------------------------------------
- test units-2.1 {new unit} {
- list [catch {units::new "longstretch" "42 meter"} msg] $msg
- } {0 {}}
- test units-2.2 {new unit primitive} {
- list [catch {units::new wongo -primitive} msg] $msg
- } {0 {}}
- test units-2.3 {new unit already exists} {
- list [catch {units::new "hertz" "/s"} msg] $msg
- } {1 {unit 'hertz' is already defined}}
- test units-2.4 {new primitive unit already exists} {
- list [catch {units::new "meter" -primitive} msg] $msg
- } {1 {unit 'meter' is already defined}}
- test units-2.5 {new unit with bogus base units} {
- list [catch {units::new "mankledoo" "kudos/smackdown"} msg] $msg
- } {1 {'kudos/smackdown' cannot be reduced to primitive units}}
- test units-2.6 {new unit name invalid characters} {
- list [catch {units::new "supper22" "meter/second"} msg] $msg
- } {1 {non-alphabetic characters in unit name 'supper22'}}
- test units-2.7 {new unit, then exercise it} {
- units::new breadloaf 0.152meter
- format "%.2f" [units::convert 1.0meter breadloaf]
- } {6.58}
- #-----------------------------------------------------------
- #
- # exercise convert command features
- #
- #-----------------------------------------------------------
- test units-3.1 {convert number to number (no units)} {
- units::convert 1.0 1.0
- } {1.0}
- test units-3.2 {convert simple number} {
- units::convert 1.0 meter
- } {1.0}
- test units-3.3 {convert negative number} {
- units::convert -1.0 meter
- } {-1.0}
- test units-3.4 {convert to negative units} {
- units::convert 1.0 -1.0meter
- } {-1.0}
- test units-3.5 {convert identical unit} {
- units::convert 1.0meter meter
- } {1.0}
- test units-3.6 {convert plural units - with "s"} {
- units::convert 1.0meter meters
- } {1.0}
- test units-3.7 {convert plural units - with "es"} {
- units::convert 1.0meter meteres
- } {1.0}
- test units-3.8 {convert prefixed plural units - with "s"} {
- units::convert 1000.0meter kilometers
- } {1.0}
- test units-3.9 {convert prefixed plural units - with "es"} {
- units::convert 1000.0meter kilometeres
- } {1.0}
- test units-3.10 {convert abbreviated (not plural) units} {
- # Make sure that "ms", which is the abbreviation
- # for "millisecond" is not reduced to "meters"
- units::convert 1000.0ms second
- } {1.0}
- test units-3.11 {convert prefixed units} {
- units::convert 1000.0meter kilometer
- } {1.0}
- test units-3.12 {convert units with fraction} {
- units::convert 1.0/second hertz
- } {1.0}
- test units-3.13 {convert power string} {
- units::convert 1.0meter^2 centimeter^2
- } {10000.0}
- test units-3.14 {convert complex fraction unit with power} {
- units::convert 1.0m-kg/s^2 newton
- } {1.0}
- test units-3.15 {convert non-primitive unit with power} {
- units::convert 1.0m^2-kg^2/s^4 newton^2
- } {1.0}
- test units-3.16 {convert complex recursive unit} {
- units::convert 1000.0Sv kilosievert
- } {1.0}
- test units-3.17 {convert value with space between number and unit} {
- units::convert "1000.0 meter" kilometer
- } {1.0}
- test units-3.18 {convert unit with space separators} {
- units::convert "1.0 m kg/s^2" newton
- } {1.0}
- test units-3.19 {convert unit with dash separators} {
- units::convert "1.0 m-kg/s^2" newton
- } {1.0}
- test units-3.20 {convert unit with asterisk separators} {
- units::convert "1.0 m*kg/s^2" newton
- } {1.0}
- #-----------------------------------------------------------
- #
- # Convert scaled and prefixed unit
- #
- #-----------------------------------------------------------
- test units-4.1 {convert simple value} {
- units::convert 60seconds second
- } {60.0}
- test units-4.2 {convert value to scaled target units} {
- units::convert 3600second 60second
- } {60.0}
- test units-4.3 {convert value with prefixed units} {
- units::convert 1.0kilosecond second
- } {1000.0}
- test units-4.4 {convert value with multiple prefixed units} {
- units::convert 1.0millimeter-kilosecond meter-second
- } {1.0}
- test units-4.5 {convert value with prefixed denominator units} {
- units::convert 1000.0meter/kilosecond meter/second
- } {1.0}
- test units-4.6 {convert value with prefixed numerator and denominator units} {
- units::convert 1.0kilometer/kilosecond meter/second
- } {1.0}
- test units-4.6 {convert value with multiple prefixed denominator units} {
- units::convert 1.0meter/kilosecond-milligram meter/second-gram
- } {1.0}
- test units-4.7 {convert value to prefixed target units} {
- units::convert 1.0second millisecond
- } {1000.0}
- test units-4.8 {convert value to prefixed denominator target units} {
- units::convert 1.0meter/second meter/kilosecond
- } {1000.0}
- #-----------------------------------------------------------
- #
- # Bad unit syntaxes
- #
- #-----------------------------------------------------------
- test units-5.1 {bad unit string - too many "/"} {
- list [catch {units::convert 1.0 "m/s/s"} msg ] $msg
- } {1 {invalid unit string 'm/s/s': only one '/' allowed}}
- test units-5.2 {bad unit string - invalid characters} {
- list [catch {units::convert 1.0 "m{}/s"} msg ] $msg
- } {1 {invalid characters in unit string 'm{}/s'}}
- test units-5.3 {bad unit string - invalid unit} {
- list [catch {units::convert 1.0 "foo^2"} msg ] $msg
- } {1 {invalid unit name 'foo' in 'foo^2'}}
- test units-5.4 {bad unit string - invalid non-integer exponent} {
- list [catch {units::convert 1.0 "m^2foo"} msg ] $msg
- } {1 {invalid integer exponent in 'm^2foo'}}
- test units-5.5 {bad unit string - multiple exponent characters} {
- list [catch {units::convert 1.0 "m^2^3"} msg ] $msg
- } {1 {invalid integer exponent in 'm^2^3'}}
- test units-5.6 {bad unit string - negative exponent} {
- list [catch {units::convert 1.0 "m^-2"} msg ] $msg
- } {1 {invalid integer exponent in 'm^-2'}}
- test units-5.7 {bad unit string - zero factor} {
- list [catch {units::convert 1.0 "meter/0*second"} msg ] $msg
- } {1 {illegal zero factor in 'meter/0*second'}}
- test units-5.8 {bad unit string - dot in unit name} {
- list [catch {units::convert 1.0 "meter.second"} msg ] $msg
- } {1 {invalid non-alphabetic unit name in 'meter.second'}}
- test units-5.9 {bad unit string - digit in unit name} {
- list [catch {units::convert 1.0 "meter2second"} msg ] $msg
- } {1 {invalid non-alphabetic unit name in 'meter2second'}}
- #-----------------------------------------------------------
- #
- # conversion errors
- #
- #-----------------------------------------------------------
- test units-6.1 {conversion error - incompatible unit} {
- list [catch {units::convert 1.0meter seconds} msg ] $msg
- } {1 {'1.0meter' and 'seconds' have incompatible units}}
- #-----------------------------------------------------------
- #
- # Derived Units
- #
- # Check derived units definitions by converting a named
- # derived unit into its abbreviated units.
- #
- #-----------------------------------------------------------
- test units-7.1 {derived raidian} {
- ::units::convert radian m/m
- } {1.0}
- test units-7.2 {derived steradian} {
- ::units::convert steradian m^2/m^2
- } {1.0}
- test units-7.3 {derived hertz} {
- ::units::convert hertz /s
- } {1.0}
- test units-7.4 {derived newton} {
- ::units::convert newton m-kg/s^2
- } {1.0}
- test units-7.5 {derived pascal} {
- ::units::convert pascal N/m^2
- } {1.0}
- test units-7.6 {derived joule} {
- ::units::convert joule N-m
- } {1.0}
- test units-7.7 {derived watt} {
- ::units::convert watt J/s
- } {1.0}
- test units-7.8 {derived coulomb} {
- ::units::convert coulomb s-A
- } {1.0}
- test units-7.9 {derived volt} {
- ::units::convert volt W/A
- } {1.0}
- test units-7.10 {derived farad} {
- ::units::convert farad C/V
- } {1.0}
- test units-7.11 {derived ohm} {
- ::units::convert ohm V/A
- } {1.0}
- test units-7.12 {derived siemens} {
- ::units::convert siemens A/V
- } {1.0}
- test units-7.13 {derived weber} {
- ::units::convert weber V-s
- } {1.0}
- test units-7.14 {derived tesla} {
- ::units::convert tesla Wb/m^2
- } {1.0}
- test units-7.15 {derived henry} {
- ::units::convert henry Wb/A
- } {1.0}
- test units-7.16 {derived lumen} {
- ::units::convert lumen cd-sr
- } {1.0}
- test units-7.17 {derived lux} {
- ::units::convert lux lm/m^2
- } {1.0}
- #-----------------------------------------------------------
- #
- # Reduce Derived Units
- #
- # Check derived unit definitions by reducing them
- # to primitive unit strings.
- #
- #-----------------------------------------------------------
- test units-8.1 {reduced raidian} {
- # radian, defined as meter/meter, should reduce
- # to 1.0, but is not a primitive unit.
- ::units::reduce radian
- } {1.0}
- test units-8.2 {reduced steradian} {
- # steradian, defined as meter^2/meter^2, should reduce
- # to 1.0, but is not a primitive unit.
- ::units::reduce steradian
- } {1.0}
- test units-8.3 {reduced hertz} {
- ::units::reduce hertz
- } {1.0 / second}
- test units-8.4 {reduced newton} {
- ::units::reduce newton
- } {1000.0 gram meter / second second}
- test units-8.5 {reduced pascal} {
- ::units::reduce pascal
- } {1000.0 gram / meter second second}
- test units-8.6 {reduced joule} {
- ::units::reduce joule
- } {1000.0 gram meter meter / second second}
- test units-8.7 {reduced watt} {
- ::units::reduce watt
- } {1000.0 gram meter meter / second second second}
- test units-8.8 {reduced coulomb} {
- ::units::reduce coulomb
- } {1.0 ampere second}
- test units-8.9 {reduced volt} {
- ::units::reduce volt
- } {1000.0 gram meter meter / ampere second second second}
- test units-8.10 {reduced farad} {
- ::units::reduce farad
- } {0.001 ampere ampere second second second second / gram meter meter}
- test units-8.11 {reduced ohm} {
- ::units::reduce ohm
- } {1000.0 gram meter meter / ampere ampere second second second}
- test units-8.12 {reduced siemens} {
- ::units::reduce siemens
- } {0.001 ampere ampere second second second / gram meter meter}
- test units-8.13 {reduced weber} {
- ::units::reduce weber
- } {1000.0 gram meter meter / ampere second second}
- test units-8.14 {reduced tesla} {
- ::units::reduce tesla
- } {1000.0 gram / ampere second second}
- test units-8.15 {reduced henry} {
- ::units::reduce henry
- } {1000.0 gram meter meter / ampere ampere second second}
- test units-8.16 {reduced lumen} {
- ::units::reduce lumen
- # The definition includes steradian, but the value cancels out
- } {1.0 candela}
- test units-8.17 {reduced lux} {
- ::units::reduce lux
- # The definition includes steradian, but the value cancels out
- } {1.0 candela / meter meter}
- #-----------------------------------------------------------
- #
- # Reduce Non-SI Units
- #
- # Check non-SI unit definitions by reducing them
- # to primitive unit strings.
- #
- #-----------------------------------------------------------
- test units-9.1 {non-SI angstrom} {
- ::units::convert 1.0E10angstrom meter
- } {1.0}
- test units-9.2 {non-SI astronomicalUnit} {
- ::units::convert 1.0astronomicalUnit kilometer
- } {149597900.0}
- test units-9.1 {non-SI atmosphere} {
- ::units::convert 1.0atmosphere kilopascal
- } {101.325}
- test units-9.1 {non-SI bar} {
- ::units::convert 1.0bar kilopascal
- } {100.0}
- test units-9.1 {non-SI calorie} {
- ::units::convert 1.0calorie joule
- } {4.1868}
- test units-9.1 {non-SI curie} {
- ::units::convert 1.0millicurie becquerel
- } {37000000.0}
|