#---------------------------------------------------*-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}

