units.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #---------------------------------------------------*-TCL-*-----
  2. #
  3. # Commands covered:
  4. # units::new
  5. # units::convert
  6. # units::reduce
  7. #
  8. # This file contains a collection of tests for the units
  9. # conversion facility.
  10. #
  11. # Copyright 2000-2004 Mayo Foundation. All Rights Reserved.
  12. # $Id: units.test,v 1.2 2004/02/05 22:24:23 techenti Exp $
  13. #
  14. # October 30, 2000
  15. # Robert W. Techentin
  16. #
  17. #----------------------------------------------------------------
  18. #-----------------------------
  19. # for development purposes
  20. #-----------------------------
  21. if { ! [catch {glob units.tcl}] } {
  22. set auto_path [linsert $auto_path 0 .]
  23. }
  24. if {[lsearch [namespace children] ::tcltest] == -1} {
  25. package require tcltest
  26. namespace import -force ::tcltest::*
  27. }
  28. if {[catch {package require units 1.0} msg]} {
  29. catch {puts stderr "Cannot load units 1.0 package: '$msg'"}
  30. return
  31. }
  32. #-----------------------------------------------------------
  33. #
  34. # Check that commands exist
  35. #
  36. #-----------------------------------------------------------
  37. test units-1.1 {new command exists} {
  38. list [catch {units::new} msg] $msg
  39. } {1 {Wrong # args. units::new name baseUnits }}
  40. test units-1.2 {convert command exists} {
  41. list [catch {units::convert} msg] $msg
  42. } {1 {Wrong # args. units::convert value targetUnits }}
  43. test units-1.3 {reduce command exists} {
  44. list [catch {units::reduce} msg] $msg
  45. } {1 {Wrong # args. units::reduce unitString }}
  46. #-----------------------------------------------------------
  47. #
  48. # exercise new command
  49. #
  50. #-----------------------------------------------------------
  51. test units-2.1 {new unit} {
  52. list [catch {units::new "longstretch" "42 meter"} msg] $msg
  53. } {0 {}}
  54. test units-2.2 {new unit primitive} {
  55. list [catch {units::new wongo -primitive} msg] $msg
  56. } {0 {}}
  57. test units-2.3 {new unit already exists} {
  58. list [catch {units::new "hertz" "/s"} msg] $msg
  59. } {1 {unit 'hertz' is already defined}}
  60. test units-2.4 {new primitive unit already exists} {
  61. list [catch {units::new "meter" -primitive} msg] $msg
  62. } {1 {unit 'meter' is already defined}}
  63. test units-2.5 {new unit with bogus base units} {
  64. list [catch {units::new "mankledoo" "kudos/smackdown"} msg] $msg
  65. } {1 {'kudos/smackdown' cannot be reduced to primitive units}}
  66. test units-2.6 {new unit name invalid characters} {
  67. list [catch {units::new "supper22" "meter/second"} msg] $msg
  68. } {1 {non-alphabetic characters in unit name 'supper22'}}
  69. test units-2.7 {new unit, then exercise it} {
  70. units::new breadloaf 0.152meter
  71. format "%.2f" [units::convert 1.0meter breadloaf]
  72. } {6.58}
  73. #-----------------------------------------------------------
  74. #
  75. # exercise convert command features
  76. #
  77. #-----------------------------------------------------------
  78. test units-3.1 {convert number to number (no units)} {
  79. units::convert 1.0 1.0
  80. } {1.0}
  81. test units-3.2 {convert simple number} {
  82. units::convert 1.0 meter
  83. } {1.0}
  84. test units-3.3 {convert negative number} {
  85. units::convert -1.0 meter
  86. } {-1.0}
  87. test units-3.4 {convert to negative units} {
  88. units::convert 1.0 -1.0meter
  89. } {-1.0}
  90. test units-3.5 {convert identical unit} {
  91. units::convert 1.0meter meter
  92. } {1.0}
  93. test units-3.6 {convert plural units - with "s"} {
  94. units::convert 1.0meter meters
  95. } {1.0}
  96. test units-3.7 {convert plural units - with "es"} {
  97. units::convert 1.0meter meteres
  98. } {1.0}
  99. test units-3.8 {convert prefixed plural units - with "s"} {
  100. units::convert 1000.0meter kilometers
  101. } {1.0}
  102. test units-3.9 {convert prefixed plural units - with "es"} {
  103. units::convert 1000.0meter kilometeres
  104. } {1.0}
  105. test units-3.10 {convert abbreviated (not plural) units} {
  106. # Make sure that "ms", which is the abbreviation
  107. # for "millisecond" is not reduced to "meters"
  108. units::convert 1000.0ms second
  109. } {1.0}
  110. test units-3.11 {convert prefixed units} {
  111. units::convert 1000.0meter kilometer
  112. } {1.0}
  113. test units-3.12 {convert units with fraction} {
  114. units::convert 1.0/second hertz
  115. } {1.0}
  116. test units-3.13 {convert power string} {
  117. units::convert 1.0meter^2 centimeter^2
  118. } {10000.0}
  119. test units-3.14 {convert complex fraction unit with power} {
  120. units::convert 1.0m-kg/s^2 newton
  121. } {1.0}
  122. test units-3.15 {convert non-primitive unit with power} {
  123. units::convert 1.0m^2-kg^2/s^4 newton^2
  124. } {1.0}
  125. test units-3.16 {convert complex recursive unit} {
  126. units::convert 1000.0Sv kilosievert
  127. } {1.0}
  128. test units-3.17 {convert value with space between number and unit} {
  129. units::convert "1000.0 meter" kilometer
  130. } {1.0}
  131. test units-3.18 {convert unit with space separators} {
  132. units::convert "1.0 m kg/s^2" newton
  133. } {1.0}
  134. test units-3.19 {convert unit with dash separators} {
  135. units::convert "1.0 m-kg/s^2" newton
  136. } {1.0}
  137. test units-3.20 {convert unit with asterisk separators} {
  138. units::convert "1.0 m*kg/s^2" newton
  139. } {1.0}
  140. #-----------------------------------------------------------
  141. #
  142. # Convert scaled and prefixed unit
  143. #
  144. #-----------------------------------------------------------
  145. test units-4.1 {convert simple value} {
  146. units::convert 60seconds second
  147. } {60.0}
  148. test units-4.2 {convert value to scaled target units} {
  149. units::convert 3600second 60second
  150. } {60.0}
  151. test units-4.3 {convert value with prefixed units} {
  152. units::convert 1.0kilosecond second
  153. } {1000.0}
  154. test units-4.4 {convert value with multiple prefixed units} {
  155. units::convert 1.0millimeter-kilosecond meter-second
  156. } {1.0}
  157. test units-4.5 {convert value with prefixed denominator units} {
  158. units::convert 1000.0meter/kilosecond meter/second
  159. } {1.0}
  160. test units-4.6 {convert value with prefixed numerator and denominator units} {
  161. units::convert 1.0kilometer/kilosecond meter/second
  162. } {1.0}
  163. test units-4.6 {convert value with multiple prefixed denominator units} {
  164. units::convert 1.0meter/kilosecond-milligram meter/second-gram
  165. } {1.0}
  166. test units-4.7 {convert value to prefixed target units} {
  167. units::convert 1.0second millisecond
  168. } {1000.0}
  169. test units-4.8 {convert value to prefixed denominator target units} {
  170. units::convert 1.0meter/second meter/kilosecond
  171. } {1000.0}
  172. #-----------------------------------------------------------
  173. #
  174. # Bad unit syntaxes
  175. #
  176. #-----------------------------------------------------------
  177. test units-5.1 {bad unit string - too many "/"} {
  178. list [catch {units::convert 1.0 "m/s/s"} msg ] $msg
  179. } {1 {invalid unit string 'm/s/s': only one '/' allowed}}
  180. test units-5.2 {bad unit string - invalid characters} {
  181. list [catch {units::convert 1.0 "m{}/s"} msg ] $msg
  182. } {1 {invalid characters in unit string 'm{}/s'}}
  183. test units-5.3 {bad unit string - invalid unit} {
  184. list [catch {units::convert 1.0 "foo^2"} msg ] $msg
  185. } {1 {invalid unit name 'foo' in 'foo^2'}}
  186. test units-5.4 {bad unit string - invalid non-integer exponent} {
  187. list [catch {units::convert 1.0 "m^2foo"} msg ] $msg
  188. } {1 {invalid integer exponent in 'm^2foo'}}
  189. test units-5.5 {bad unit string - multiple exponent characters} {
  190. list [catch {units::convert 1.0 "m^2^3"} msg ] $msg
  191. } {1 {invalid integer exponent in 'm^2^3'}}
  192. test units-5.6 {bad unit string - negative exponent} {
  193. list [catch {units::convert 1.0 "m^-2"} msg ] $msg
  194. } {1 {invalid integer exponent in 'm^-2'}}
  195. test units-5.7 {bad unit string - zero factor} {
  196. list [catch {units::convert 1.0 "meter/0*second"} msg ] $msg
  197. } {1 {illegal zero factor in 'meter/0*second'}}
  198. test units-5.8 {bad unit string - dot in unit name} {
  199. list [catch {units::convert 1.0 "meter.second"} msg ] $msg
  200. } {1 {invalid non-alphabetic unit name in 'meter.second'}}
  201. test units-5.9 {bad unit string - digit in unit name} {
  202. list [catch {units::convert 1.0 "meter2second"} msg ] $msg
  203. } {1 {invalid non-alphabetic unit name in 'meter2second'}}
  204. #-----------------------------------------------------------
  205. #
  206. # conversion errors
  207. #
  208. #-----------------------------------------------------------
  209. test units-6.1 {conversion error - incompatible unit} {
  210. list [catch {units::convert 1.0meter seconds} msg ] $msg
  211. } {1 {'1.0meter' and 'seconds' have incompatible units}}
  212. #-----------------------------------------------------------
  213. #
  214. # Derived Units
  215. #
  216. # Check derived units definitions by converting a named
  217. # derived unit into its abbreviated units.
  218. #
  219. #-----------------------------------------------------------
  220. test units-7.1 {derived raidian} {
  221. ::units::convert radian m/m
  222. } {1.0}
  223. test units-7.2 {derived steradian} {
  224. ::units::convert steradian m^2/m^2
  225. } {1.0}
  226. test units-7.3 {derived hertz} {
  227. ::units::convert hertz /s
  228. } {1.0}
  229. test units-7.4 {derived newton} {
  230. ::units::convert newton m-kg/s^2
  231. } {1.0}
  232. test units-7.5 {derived pascal} {
  233. ::units::convert pascal N/m^2
  234. } {1.0}
  235. test units-7.6 {derived joule} {
  236. ::units::convert joule N-m
  237. } {1.0}
  238. test units-7.7 {derived watt} {
  239. ::units::convert watt J/s
  240. } {1.0}
  241. test units-7.8 {derived coulomb} {
  242. ::units::convert coulomb s-A
  243. } {1.0}
  244. test units-7.9 {derived volt} {
  245. ::units::convert volt W/A
  246. } {1.0}
  247. test units-7.10 {derived farad} {
  248. ::units::convert farad C/V
  249. } {1.0}
  250. test units-7.11 {derived ohm} {
  251. ::units::convert ohm V/A
  252. } {1.0}
  253. test units-7.12 {derived siemens} {
  254. ::units::convert siemens A/V
  255. } {1.0}
  256. test units-7.13 {derived weber} {
  257. ::units::convert weber V-s
  258. } {1.0}
  259. test units-7.14 {derived tesla} {
  260. ::units::convert tesla Wb/m^2
  261. } {1.0}
  262. test units-7.15 {derived henry} {
  263. ::units::convert henry Wb/A
  264. } {1.0}
  265. test units-7.16 {derived lumen} {
  266. ::units::convert lumen cd-sr
  267. } {1.0}
  268. test units-7.17 {derived lux} {
  269. ::units::convert lux lm/m^2
  270. } {1.0}
  271. #-----------------------------------------------------------
  272. #
  273. # Reduce Derived Units
  274. #
  275. # Check derived unit definitions by reducing them
  276. # to primitive unit strings.
  277. #
  278. #-----------------------------------------------------------
  279. test units-8.1 {reduced raidian} {
  280. # radian, defined as meter/meter, should reduce
  281. # to 1.0, but is not a primitive unit.
  282. ::units::reduce radian
  283. } {1.0}
  284. test units-8.2 {reduced steradian} {
  285. # steradian, defined as meter^2/meter^2, should reduce
  286. # to 1.0, but is not a primitive unit.
  287. ::units::reduce steradian
  288. } {1.0}
  289. test units-8.3 {reduced hertz} {
  290. ::units::reduce hertz
  291. } {1.0 / second}
  292. test units-8.4 {reduced newton} {
  293. ::units::reduce newton
  294. } {1000.0 gram meter / second second}
  295. test units-8.5 {reduced pascal} {
  296. ::units::reduce pascal
  297. } {1000.0 gram / meter second second}
  298. test units-8.6 {reduced joule} {
  299. ::units::reduce joule
  300. } {1000.0 gram meter meter / second second}
  301. test units-8.7 {reduced watt} {
  302. ::units::reduce watt
  303. } {1000.0 gram meter meter / second second second}
  304. test units-8.8 {reduced coulomb} {
  305. ::units::reduce coulomb
  306. } {1.0 ampere second}
  307. test units-8.9 {reduced volt} {
  308. ::units::reduce volt
  309. } {1000.0 gram meter meter / ampere second second second}
  310. test units-8.10 {reduced farad} {
  311. ::units::reduce farad
  312. } {0.001 ampere ampere second second second second / gram meter meter}
  313. test units-8.11 {reduced ohm} {
  314. ::units::reduce ohm
  315. } {1000.0 gram meter meter / ampere ampere second second second}
  316. test units-8.12 {reduced siemens} {
  317. ::units::reduce siemens
  318. } {0.001 ampere ampere second second second / gram meter meter}
  319. test units-8.13 {reduced weber} {
  320. ::units::reduce weber
  321. } {1000.0 gram meter meter / ampere second second}
  322. test units-8.14 {reduced tesla} {
  323. ::units::reduce tesla
  324. } {1000.0 gram / ampere second second}
  325. test units-8.15 {reduced henry} {
  326. ::units::reduce henry
  327. } {1000.0 gram meter meter / ampere ampere second second}
  328. test units-8.16 {reduced lumen} {
  329. ::units::reduce lumen
  330. # The definition includes steradian, but the value cancels out
  331. } {1.0 candela}
  332. test units-8.17 {reduced lux} {
  333. ::units::reduce lux
  334. # The definition includes steradian, but the value cancels out
  335. } {1.0 candela / meter meter}
  336. #-----------------------------------------------------------
  337. #
  338. # Reduce Non-SI Units
  339. #
  340. # Check non-SI unit definitions by reducing them
  341. # to primitive unit strings.
  342. #
  343. #-----------------------------------------------------------
  344. test units-9.1 {non-SI angstrom} {
  345. ::units::convert 1.0E10angstrom meter
  346. } {1.0}
  347. test units-9.2 {non-SI astronomicalUnit} {
  348. ::units::convert 1.0astronomicalUnit kilometer
  349. } {149597900.0}
  350. test units-9.1 {non-SI atmosphere} {
  351. ::units::convert 1.0atmosphere kilopascal
  352. } {101.325}
  353. test units-9.1 {non-SI bar} {
  354. ::units::convert 1.0bar kilopascal
  355. } {100.0}
  356. test units-9.1 {non-SI calorie} {
  357. ::units::convert 1.0calorie joule
  358. } {4.1868}
  359. test units-9.1 {non-SI curie} {
  360. ::units::convert 1.0millicurie becquerel
  361. } {37000000.0}