home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / format.test < prev    next >
Encoding:
Text File  |  1994-12-18  |  13.0 KB  |  365 lines

  1. # Commands covered:  format
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1994 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) format.test 1.19 94/12/17 16:19:58
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. # The following code is needed because some versions of SCO Unix have
  18. # a round-off error in sprintf which would cause some of the tests to
  19. # fail.  Someday I hope this code shouldn't be necessary (code added
  20. # 9/9/91).
  21.  
  22. set roundOffBug 0
  23. if {"[format %7.1e  68.514]" == "6.8e+01"} {
  24.     puts stdout "Note: this system has a sprintf round-off bug, some tests skipped\n"
  25.     set roundOffBug 1
  26. }
  27.  
  28. test format-1.1 {integer formatting} {
  29.     format "%*d %d %d %d" 6 34 16923 -12 -1
  30. } {    34 16923 -12 -1}
  31. if $doNonPortableTests {
  32.     test format-1.2 {integer formatting} {
  33.     format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 14 12
  34.     } {   6   34 16923  -12 -1 0xe 0XC}
  35. }
  36.  
  37. # %u output depends on word length, so this test is not portable.
  38.  
  39. if $doNonPortableTests {
  40.     test format-1.3 {integer formatting} {
  41.     format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
  42.     } {   6   34 16923 4294967284 -1 0}
  43. }
  44. test format-1.4 {integer formatting} {
  45.     format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
  46. } {6    34   16923 -12 }
  47. test format-1.5 {integer formatting} {
  48.     format "%04d %04d %04d %04i" 6 34 16923 -12 -1
  49. } {0006 0034 16923 -012}
  50. test format-1.6 {integer formatting} {
  51.     format "%00*d" 6 34
  52. } {000034}
  53.  
  54. # Printing negative numbers in hex or octal format depends on word
  55. # length, so these tests are not portable.
  56.  
  57. if $doNonPortableTests {
  58.     test format-1.7 {integer formatting} {
  59.     format "%4x %4x %4x %4x" 6 34 16923 -12 -1
  60.     } {   6   22 421b fffffff4}
  61.     test format-1.8 {integer formatting} {
  62.     format "%#x %#X %#X %#x" 6 34 16923 -12 -1
  63.     } {0x6 0X22 0X421B 0xfffffff4}
  64.     test format-1.9 {integer formatting} {
  65.     format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
  66.     } {                 0x6                 0x22               0x421b           0xfffffff4}
  67.     test format-1.10 {integer formatting} {
  68.     format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
  69.     } {0x6                  0x22                 0x421b               0xfffffff4          }
  70.     test format-1.11 {integer formatting} {
  71.     format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
  72.     } {06                   042                  041033               037777777764        }
  73. }
  74.  
  75. test format-2.1 {string formatting} {
  76.     format "%s %s %c %s" abcd {This is a very long test string.} 120 x
  77. } {abcd This is a very long test string. x x}
  78. test format-2.2 {string formatting} {
  79.     format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
  80. } {                abcd This is a very long test string.                    x                    x}
  81. test format-2.3 {string formatting} {
  82.     format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
  83. } {abcd This is a  x x}
  84. test format-2.4 {string formatting} {
  85.     format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
  86. } {abcd This is a very long test string. % x x}
  87.  
  88. test format-3.1 {e and f formats} {
  89.     format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
  90. } {3.420000e+13 6.851400e+01 -1.250000e-01 -1.600000e+04}
  91. test format-3.2 {e and f formats} {
  92.     format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
  93. } {        3.420000e+13         6.851400e+01        -1.250000e-01        -1.600000e+04}
  94. if {!$roundOffBug} {
  95.     test format-3.3 {e and f formats} {
  96.     format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
  97.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  98.     test format-3.4 {e and f formats} {
  99.     format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
  100.     } {000000003.420000e+13 000000006.851400e+01 -00000001.260000e-01 -00000001.600000e+04}
  101.     test format-3.5 {e and f formats} {
  102.     format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
  103.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  104.     test format-3.6 {e and f formats} {
  105.     format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
  106.     } {34200000000000.000000 68.514000 -0.125000 -16000.000000}
  107. }
  108. test format-3.7 {e and f formats} {
  109.     format "%.4f %.4f %.4f %.4f %.4f" 34.2e12 68.514 -.125 -16000. .000053
  110. } {34200000000000.0000 68.5140 -0.1250 -16000.0000 0.0001}
  111. test format-3.8 {e and f formats} {
  112.     format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
  113. } {-1.0000e+01 -9.99996e+00 9.999960e+00}
  114. test format-3.9 {e and f formats} {
  115.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  116. } {-10.0000 -9.99996 9.999960}
  117. test format-3.10 {e and f formats} {
  118.     format "%20f %-20f %020f" -9.99996 -9.99996 9.99996
  119. } {           -9.999960 -9.999960            0000000000009.999960}
  120. test format-3.11 {e and f formats} {
  121.     format "%-020f %020f" -9.99996 -9.99996 9.99996
  122. } {-9.999960            -000000000009.999960}
  123. test format-3.12 {e and f formats} {
  124.     format "%.0e %#.0e" -9.99996 -9.99996 9.99996
  125. } {-1e+01 -1.e+01}
  126. test format-3.13 {e and f formats} {
  127.     format "%.0f %#.0f" -9.99996 -9.99996 9.99996
  128. } {-10 -10.}
  129. test format-3.14 {e and f formats} {
  130.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  131. } {-10.0000 -9.99996 9.999960}
  132. test format-3.15 {e and f formats} {
  133.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  134. } {  1   1   1   1}
  135. test format-3.16 {e and f formats} {
  136.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  137. } {0.0 0.1 0.0 0.0}
  138.  
  139. test format-4.1 {g-format} {
  140.     format "%.3g" 12341.0
  141. } {1.23e+04}
  142. test format-4.2 {g-format} {
  143.     format "%.3G" 1234.12345
  144. } {1.23E+03}
  145. test format-4.3 {g-format} {
  146.     format "%.3g" 123.412345
  147. } {123}
  148. test format-4.4 {g-format} {
  149.     format "%.3g" 12.3412345
  150. } {12.3}
  151. test format-4.5 {g-format} {
  152.     format "%.3g" 1.23412345
  153. } {1.23}
  154. test format-4.6 {g-format} {
  155.     format "%.3g" 1.23412345
  156. } {1.23}
  157. test format-4.7 {g-format} {
  158.     format "%.3g" .123412345
  159. } {0.123}
  160. test format-4.8 {g-format} {
  161.     format "%.3g" .012341
  162. } {0.0123}
  163. test format-4.9 {g-format} {
  164.     format "%.3g" .0012341
  165. } {0.00123}
  166. test format-4.10 {g-format} {
  167.     format "%.3g" .00012341
  168. } {0.000123}
  169. test format-4.11 {g-format} {
  170.     format "%.3g" .00001234
  171. } {1.23e-05}
  172. test format-4.12 {g-format} {
  173.     format "%.4g" 9999.6
  174. } {1e+04}
  175. test format-4.13 {g-format} {
  176.     format "%.4g" 999.96
  177. } {1000}
  178. test format-4.14 {g-format} {
  179.     format "%.3g" 1.0
  180. } {1}
  181. test format-4.15 {g-format} {
  182.     format "%.3g" .1
  183. } {0.1}
  184. test format-4.16 {g-format} {
  185.     format "%.3g" .01
  186. } {0.01}
  187. test format-4.17 {g-format} {
  188.     format "%.3g" .001
  189. } {0.001}
  190. test format-4.19 {g-format} {
  191.     format "%.3g" .00001
  192. } {1e-05}
  193. test format-4.20 {g-format} {
  194.     format "%#.3g" 1234.0
  195. } {1.23e+03}
  196. test format-4.21 {g-format} {
  197.     format "%#.3G" 9999.5
  198. } {1.00E+04}
  199.  
  200. test format-5.1 {floating-point zeroes} {
  201.     format "%e %f %g" 0.0 0.0 0.0 0.0
  202. } {0.000000e+00 0.000000 0}
  203. test format-5.2 {floating-point zeroes} {
  204.     format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
  205. } {0.0000e+00 0.0000 0}
  206. test format-5.3 {floating-point zeroes} {
  207.     format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
  208. } {0.0000e+00 0.0000 0.000}
  209. test format-5.4 {floating-point zeroes} {
  210.     format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
  211. } {0e+00 0 0}
  212. test format-5.5 {floating-point zeroes} {
  213.     format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
  214. } {0.e+00 0. 0.}
  215. test format-5.6 {floating-point zeroes} {
  216.     format "%3.0f %3.0f %3.0f %3.0f" 0.0 0.0 0.0 0.0
  217. } {  0   0   0   0}
  218. test format-5.7 {floating-point zeroes} {
  219.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  220. } {  1   1   1   1}
  221. test format-5.8 {floating-point zeroes} {
  222.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  223. } {0.0 0.1 0.0 0.0}
  224.  
  225. test format-6.1 {various syntax features} {
  226.     format "%*.*f" 12 3 12.345678901
  227. } {      12.346}
  228. test format-6.2 {various syntax features} {
  229.     format "%0*.*f" 12 3 12.345678901
  230. } {00000012.346}
  231. test format-6.3 {various syntax features} {
  232.     format "\*\t\\n"
  233. } {*    \n}
  234.  
  235. test format-7.1 {error conditions} {
  236.     catch format
  237. } 1
  238. test format-7.2 {error conditions} {
  239.     catch format msg
  240.     set msg
  241. } {wrong # args: should be "format formatString ?arg arg ...?"}
  242. test format-7.3 {error conditions} {
  243.     catch {format %*d}
  244. } 1
  245. test format-7.4 {error conditions} {
  246.     catch {format %*d} msg
  247.     set msg
  248. } {not enough arguments for all format specifiers}
  249. test format-7.5 {error conditions} {
  250.     catch {format %*.*f 12}
  251. } 1
  252. test format-7.6 {error conditions} {
  253.     catch {format %*.*f 12} msg
  254.     set msg
  255. } {not enough arguments for all format specifiers}
  256. test format-7.7 {error conditions} {
  257.     catch {format %*.*f 12 3}
  258. } 1
  259. test format-7.8 {error conditions} {
  260.     catch {format %*.*f 12 3} msg
  261.     set msg
  262. } {not enough arguments for all format specifiers}
  263. test format-7.9 {error conditions} {
  264.     list [catch {format %*d x 3} msg] $msg
  265. } {1 {expected integer but got "x"}}
  266. test format-7.10 {error conditions} {
  267.     list [catch {format %*.*f 2 xyz 3} msg] $msg
  268. } {1 {expected integer but got "xyz"}}
  269. test format-7.11 {error conditions} {
  270.     catch {format %d 2a}
  271. } 1
  272. test format-7.12 {error conditions} {
  273.     catch {format %d 2a} msg
  274.     set msg
  275. } {expected integer but got "2a"}
  276. test format-7.13 {error conditions} {
  277.     catch {format %c 2x}
  278. } 1
  279. test format-7.14 {error conditions} {
  280.     catch {format %c 2x} msg
  281.     set msg
  282. } {expected integer but got "2x"}
  283. test format-7.15 {error conditions} {
  284.     catch {format %f 2.1z}
  285. } 1
  286. test format-7.16 {error conditions} {
  287.     catch {format %f 2.1z} msg
  288.     set msg
  289. } {expected floating-point number but got "2.1z"}
  290. test format-7.17 {error conditions} {
  291.     catch {format ab%}
  292. } 1
  293. test format-7.18 {error conditions} {
  294.     catch {format ab% 12} msg
  295.     set msg
  296. } {format string ended in middle of field specifier}
  297. test format-7.19 {error conditions} {
  298.     catch {format %q x}
  299. } 1
  300. test format-7.20 {error conditions} {
  301.     catch {format %q x} msg
  302.     set msg
  303. } {bad field specifier "q"}
  304. test format-7.21 {error conditions} {
  305.     catch {format %d}
  306. } 1
  307. test format-7.22 {error conditions} {
  308.     catch {format %d} msg
  309.     set msg
  310. } {not enough arguments for all format specifiers}
  311.  
  312. test format-8.1 {long result} {
  313.     set a {1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  314.     format {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG %s %s %s} $a $a $a
  315. } {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  316.  
  317. if $doNonPortableTests {
  318.     test format-9.1 {"h" format specifier} {
  319.     format %hd 0xffff
  320.     } -1
  321.     test format-9.2 {"h" format specifier} {
  322.     format %hx 0x10fff
  323.     } fff
  324.     test format-9.3 {"h" format specifier} {
  325.     format %hd 0x10000
  326.     } 0
  327. }
  328.  
  329. test format-10.1 {XPG3 %$n specifiers} {
  330.     format {%2$d %1$d} 4 5
  331. } {5 4}
  332. test format-10.2 {XPG3 %$n specifiers} {
  333.     format {%2$d %1$d %1$d %3$d} 4 5 6
  334. } {5 4 4 6}
  335. test format-10.3 {XPG3 %$n specifiers} {
  336.     list [catch {format {%2$d %3$d} 4 5} msg] $msg
  337. } {1 {"%n$" argument index out of range}}
  338. test format-10.4 {XPG3 %$n specifiers} {
  339.     list [catch {format {%2$d %0$d} 4 5 6} msg] $msg
  340. } {1 {"%n$" argument index out of range}}
  341. test format-10.5 {XPG3 %$n specifiers} {
  342.     list [catch {format {%d %1$d} 4 5 6} msg] $msg
  343. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  344. test format-10.6 {XPG3 %$n specifiers} {
  345.     list [catch {format {%2$d %d} 4 5 6} msg] $msg
  346. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  347. test format-10.7 {XPG3 %$n specifiers} {
  348.     list [catch {format {%2$d %3d} 4 5 6} msg] $msg
  349. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  350. test format-10.8 {XPG3 %$n specifiers} {
  351.     format {%2$*d %3$d} 1 10 4
  352. } {         4 4}
  353. test format-10.9 {XPG3 %$n specifiers} {
  354.     format {%2$.*s %4$d} 1 5 abcdefghijklmnop 44
  355. } {abcde 44}
  356. test format-10.10 {XPG3 %$n specifiers} {
  357.     list [catch {format {%2$*d} 4} msg] $msg
  358. } {1 {"%n$" argument index out of range}}
  359. test format-10.11 {XPG3 %$n specifiers} {
  360.     list [catch {format {%2$*d} 4 5} msg] $msg
  361. } {1 {"%n$" argument index out of range}}
  362. test format-10.12 {XPG3 %$n specifiers} {
  363.     list [catch {format {%2$*d} 4 5 6} msg] $msg
  364. } {0 {    6}}
  365.