TIP #85 Version 1.8: Custom Comparisons in Tcltest

This is not necessarily the current version of this TIP.


TIP:85
Title:Custom Comparisons in Tcltest
Version:$Revision: 1.8 $
Authors: Arjen Markus <arjen dot markus at wldelft dot nl>
Don Porter <dgp at users dot sf dot net>
State:Draft
Type:Project
Tcl-Version:8.4
Vote:Pending
Created:Thursday, 31 January 2002
Keywords:test, string comparison, floating-point

Abstract

This TIP proposes a simple mechanism to make the tcltest package an even more flexible package than it already is by allowing the programmer to define his or her own comparison procedures. Such procedures can deal with issues like allowing a (small) tolerance in floating-point results.

Rationale

The most recent version of the tcltest package supports the comparison of the actual result with the expected result by a number of methods: exact matching, glob-style matching and matching via a regular expression. The flexibility is indeed enhanced as it is now much easier to allow for small variations in string results. But it is nearly impossible to define an accurate test that checks if floating-point results are the "same" - exact matching will seldom suffice due to platform-specific round-off errors or differences in formatting a floating-point number (0.12 versus .12 for instance).

It is also impossible to compare results that are not easily expressed as strings, for instance an application that produces binary files that need to be compared or simply very long strings - these could easily be stored in an external file, but would be awkward in a file with a large number of such tests.

Proposal

The tcltest package defines a comparison procedure, stringCompare, which takes care of the three cases. This procedure can easily be replaced by one that invokes registered commands or procedures. Such a command or procedure takes two arguments and returns 1 for a match and a 0 for failure, just like the current implementation:

 proc mymatchProc { expected actual } { 
   if { $expected (is somehow equal) $actual } {
      return 1
   } else
      return 0
   }
 }

Such a procedure can be registered by a simple mechanism:

 ::tcltest::customMatch mytype mymatchproc

or, as in the sample implementation, an incomplete command:

 ::tcltest::customMatch exact [list ::string equal]

and will be invoked when the match type is set to "mytype", so one has to specify this per test case, just as in the current situation. The default method will still be "exact".

Allowing procedures to be invoked by their type names gives us the flexibility to register as many such procedures or commands as required.

Two Examples

To show how this works, we include two simple examples:

First, suppose you have defined a package for calculating the value of a general Bessel function, just the sort of function that returns floating-point numbers. Then the results may be imprecise due to rounding-off errors, different values of tcl_precision or, even more banally, differences in the formatting of floating-point numbers (0.12 versus .12 for instance).

The following shows how to do this:

 #
 # Test implementation of Bessel functions
 # (Table only provides 4 decimals)
 #
 customMatch 4decimals matchFloat4Decimals

 proc matchFloat4Decimals { expected actual } {
    return [expr {abs($expected-$actual) <= 0.5e-4}]
 }

 test "J0-1.1" "J0 for x=1.0" -match 4decimals -body {
    J0 1.0
 } -result 0.7652

 test "J1-1.1" "J0 for x=1.0" -match 4decimals -body {
    J1 1.0
 } -result 0.4401

The second example occurs for instance when testing alternative implementations: you want to check that the original standard feature is failing whereas the new but incompatible alternative gets it right. Then:

 proc matchNegative { expected actual } {
    set match 0
    foreach a $actual e $expected {
       if { $a != $e } {
          set match 1
          break
       }
    }
    return $match
 }

 customMatch negative matchNegative

 #
 # Floating-point comparisons are imprecise. The following
 # test returns typically such a list as {643 1357 1921 79 781 1219}
 # so nothing even close to the expected values.
 # 
 test "ManyCompares-1.2" "Compare fails - naive comparison" \
    -match negative -body {
    set naiv_eq 0
    set naiv_ne 0
    set naiv_ge 0
    set naiv_gt 0
    set naiv_le 0
    set naiv_lt 0

    for { set i -1000 } { $i <= 1000 } { incr i } {
       if { $i == 0 } continue

       set x [expr {1.01/double($i)}]
       set y [expr {(2.1*$x)*(double($i)/2.1)}]

       if { $y == 1.01 } { incr naiv_eq }
       if { $y != 1.01 } { incr naiv_ne }
       if { $y >= 1.01 } { incr naiv_ge }
       if { $y >  1.01 } { incr naiv_gt }
       if { $y <= 1.01 } { incr naiv_le }
       if { $y <  1.01 } { incr naiv_lt }
    }
    set result [list $naiv_eq $naiv_ne $naiv_ge $naiv_gt $naiv_le $naiv_lt]
 } -result {2000 0 2000 0 2000 0}

makes sure that a mismatch is treated as the expected outcome.

Alternatives and objections

Of course, it is possible to achieve these effects within the current framework of tcltest, by putting these match procedures inside the body of the test case. No extra user command would be necessary then.

There are at least two drawbacks to this approach:

As a matter of fact, the proposed mechanism actually simplifies the current implementation of the three match types to a certain degree by turning a switch between the three types into an array index.

See Also

Tcl Feature Request 490298. http://sf.net/tracker/?func=detail&aid=490298&group_id=10894&atid=360894

History

Cameron Laird was quite enthousiastic about the idea of providing custom match procedures.

Mo DeJong requested the explicit examples (the second is actually the situation that triggered this TIP in the first place).

Don Porter <dgp at users dot sf dot net> revised the registration mechanism such that an arbitrary set of matching commands or procedures can be supported. His suggestions led to a revision of the TIP. He also revised the draft implementation.

Copyright

This document is placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows