format.test
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:19k
- # Commands covered: format
- #
- # This file contains a collection of tests for one or more of the Tcl
- # built-in commands. Sourcing this file into Tcl runs the tests and
- # generates output for errors. No output means no errors were found.
- #
- # Copyright (c) 1991-1994 The Regents of the University of California.
- # Copyright (c) 1994-1998 Sun Microsystems, Inc.
- #
- # See the file "license.terms" for information on usage and redistribution
- # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- #
- # RCS: @(#) $Id: format.test,v 1.11.2.8 2005/10/13 21:45:32 dkf Exp $
- if {[lsearch [namespace children] ::tcltest] == -1} {
- package require tcltest 2
- namespace import -force ::tcltest::*
- }
- # The following code is needed because some versions of SCO Unix have
- # a round-off error in sprintf which would cause some of the tests to
- # fail. Someday I hope this code shouldn't be necessary (code added
- # 9/9/91).
- set ::tcltest::testConstraints(roundOffBug)
- [expr {"[format %7.1e 68.514]" != "6.8e+01"}]
- test format-1.1 {integer formatting} {
- format "%*d %d %d %d" 6 34 16923 -12 -1
- } { 34 16923 -12 -1}
- test format-1.2 {integer formatting} {nonPortable} {
- format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 14 12
- } { 6 34 16923 -12 -1 0xe 0XC}
- # %u output depends on word length, so this test is not portable.
- test format-1.3 {integer formatting} {nonPortable} {
- format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
- } { 6 34 16923 4294967284 -1 0}
- test format-1.4 {integer formatting} {
- format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
- } {6 34 16923 -12 }
- test format-1.5 {integer formatting} {
- format "%04d %04d %04d %04i" 6 34 16923 -12 -1
- } {0006 0034 16923 -012}
- test format-1.6 {integer formatting} {
- format "%00*d" 6 34
- } {000034}
- # Printing negative numbers in hex or octal format depends on word
- # length, so these tests are not portable.
- test format-1.7 {integer formatting} {nonPortable} {
- format "%4x %4x %4x %4x" 6 34 16923 -12 -1
- } { 6 22 421b fffffff4}
- test format-1.8 {integer formatting} {nonPortable} {
- format "%#x %#X %#X %#x" 6 34 16923 -12 -1
- } {0x6 0X22 0X421B 0xfffffff4}
- test format-1.9 {integer formatting} {nonPortable} {
- format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
- } { 0x6 0x22 0x421b 0xfffffff4}
- test format-1.10 {integer formatting} {nonPortable} {
- format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
- } {0x6 0x22 0x421b 0xfffffff4 }
- test format-1.11 {integer formatting} {nonPortable} {
- format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
- } {06 042 041033 037777777764 }
- test format-2.1 {string formatting} {
- format "%s %s %c %s" abcd {This is a very long test string.} 120 x
- } {abcd This is a very long test string. x x}
- test format-2.2 {string formatting} {
- format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
- } { abcd This is a very long test string. x x}
- test format-2.3 {string formatting} {
- format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
- } {abcd This is a x x}
- test format-2.4 {string formatting} {
- format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
- } {abcd This is a very long test string. % x x}
- test format-2.5 {string formatting, embedded nulls} {
- format "%10s" abc def
- } " abc def"
- test format-2.6 {string formatting, international chars} {
- format "%10s" abcufeffdef
- } " abcufeffdef"
- test format-2.7 {string formatting, international chars} {
- format "%.5s" abcufeffdef
- } "abcufeffd"
- test format-2.8 {string formatting, international chars} {
- format "fooufeffbar%s" baz
- } "fooufeffbarbaz"
- test format-2.9 {string formatting, width} {
- format "a%5sa" f
- } "a fa"
- test format-2.10 {string formatting, width} {
- format "a%-5sa" f
- } "af a"
- test format-2.11 {string formatting, width} {
- format "a%2sa" foo
- } "afooa"
- test format-2.12 {string formatting, width} {
- format "a%0sa" foo
- } "afooa"
- test format-2.13 {string formatting, precision} {
- format "a%.2sa" foobarbaz
- } "afoa"
- test format-2.14 {string formatting, precision} {
- format "a%.sa" foobarbaz
- } "aa"
- test format-2.15 {string formatting, precision} {
- list [catch {format "a%.-2sa" foobarbaz} msg] $msg
- } {1 {bad field specifier "-"}}
- test format-2.16 {string formatting, width and precision} {
- format "a%5.2sa" foobarbaz
- } "a foa"
- test format-2.17 {string formatting, width and precision} {
- format "a%5.7sa" foobarbaz
- } "afoobarba"
- test format-3.1 {Tcl_FormatObjCmd: character formatting} {
- format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 65 65 65 65 65 65 3 65 -4 65
- } "|A|A|A|A|A | A| A|A |"
- test format-3.2 {Tcl_FormatObjCmd: international character formatting} {
- format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 0xa2 0x4e4e 0x25a 0xc3 0xff08 0 3 0x6575 -4 0x4e4f
- } "|ua2|u4e4e|u25a|uc3|uff08 |