From 1c4fbbc3332e525ff1368d9ba26681d826d2efde Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 9 Oct 2016 15:21:26 +0000 Subject: [7114ac6141] Improve testing and docs of Tcl_GetInt and Tcl_GetDouble. No behaviour change. --- doc/GetInt.3 | 31 +++++++++++++++++++++++-------- tests/get.test | 22 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/doc/GetInt.3 b/doc/GetInt.3 index 4e9d636..3e7204c 100644 --- a/doc/GetInt.3 +++ b/doc/GetInt.3 @@ -51,27 +51,42 @@ in the interpreter's result, and nothing is stored at *\fIintPtr\fR or *\fIdoublePtr\fR or *\fIboolPtr\fR. .PP \fBTcl_GetInt\fR expects \fIsrc\fR to consist of a collection -of integer digits, optionally signed and optionally preceded by -white space. If the first two characters of \fIsrc\fR +of integer digits, optionally signed and optionally preceded and +followed by white space. If the first two characters of \fIsrc\fR after the optional white space and sign are -.QW 0x +.QW \fB0x\fR then \fIsrc\fR is expected to be in hexadecimal form; otherwise, +if the first such characters are +.QW \fB0o\fR +then \fIsrc\fR is expected to be in octal form; otherwise, +if the first such characters are +.QW \fB0b\fR +then \fIsrc\fR is expected to be in binary form; otherwise, if the first such character is -.QW 0 +.QW \fB0\fR then \fIsrc\fR is expected to be in octal form; otherwise, \fIsrc\fR is expected to be in decimal form. .PP \fBTcl_GetDouble\fR expects \fIsrc\fR to consist of a floating-point number, which is: white space; a sign; a sequence of digits; a -decimal point; a sequence of digits; the letter -.QW e ; +decimal point +.QW \fB.\fR ; +a sequence of digits; the letter +.QW \fBe\fR ; a signed decimal exponent; and more white space. Any of the fields may be omitted, except that the digits either before or after the decimal point must be present and if the -.QW e -is present then it must be followed by the exponent number. +.QW \fBe\fR +is present then it must be followed by the exponent number. If there +are no fields apart from the sign and initial sequence of digits +(i.e., no decimal point or exponent indicator), that +initial sequence of digits should take one of the forms that +\fBTcl_GetInt\fR supports, described above. The use of +.QW \fB,\fR +as a decimal point is not supported nor should any other sort of +inter-digit separator be present. .PP \fBTcl_GetBoolean\fR expects \fIsrc\fR to specify a boolean value. If \fIsrc\fR is any of \fB0\fR, \fBfalse\fR, diff --git a/tests/get.test b/tests/get.test index d51ec6d..7aa06c1 100644 --- a/tests/get.test +++ b/tests/get.test @@ -19,9 +19,10 @@ if {[lsearch [namespace children] ::tcltest] == -1} { catch [list package require -exact Tcltest [info patchlevel]] testConstraint testgetint [llength [info commands testgetint]] +testConstraint testdoubleobj [llength [info commands testdoubleobj]] testConstraint longIs32bit [expr {int(0x80000000) < 0}] testConstraint longIs64bit [expr {int(0x8000000000000000) < 0}] - + test get-1.1 {Tcl_GetInt procedure} testgetint { testgetint 44 { 22} } {66} @@ -95,7 +96,24 @@ test get-3.2 {Tcl_GetDouble(FromObj), bad numbers} { } set result } {0 1 0 1 1 {expected floating-point number but got "++1.0"} 1 {expected floating-point number but got "+-1.0"} 1 {expected floating-point number but got "-+1.0"} 0 -1 1 {expected floating-point number but got "--1.0"} 1 {expected floating-point number but got "- +1.0"}} - +# Bug 7114ac6141 +test get-3.3 {tcl_GetInt with iffy numbers} testgetint { + lmap x {0 " 0" "0 " " 0 " " 0xa " " 010 " " 0o10 " " 0b10 "} { + catch {testgetint 44 $x} x + set x + } +} {44 44 44 44 54 52 52 46} +test get-3.4 {Tcl_GetDouble with iffy numbers} testdoubleobj { + lmap x {0 0.0 " .0" ".0 " " 0e0 " "09" "- 0" "-0" "0o12" "0b10"} { + catch {testdoubleobj set 1 $x} x + set x + } +} {0.0 0.0 0.0 0.0 0.0 {expected floating-point number but got "09" (looks like invalid octal number)} {expected floating-point number but got "- 0"} 0.0 10.0 2.0} + # cleanup ::tcltest::cleanupTests return + +# Local Variables: +# mode: tcl +# End: -- cgit v0.12