[racket] Testing "impossible" branches in the code

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Apr 11 05:44:05 EDT 2014

On Apr 3, 2014, at 8:53 PM, Mikko Tiihonen wrote:

> Hi,
> 
> I've spent some time on the following problem, and would like to ask for your help:
> 
> I'm writing a critical piece of an application, and would like to ascertain in a "belt and suspenders" kind of way that I get a customized exception if something goes wrong.
> 
> The approach I have used is based on independent checks (conditionals) that might trigger the exception. The problem is that I have not found an elegant way to test this code, since there should not be such combinations of parameters that could lead to this branch in the conditional. One way would be to extract these pieces of code to auxiliary functions and call these from the tests with those "impossible" parameters that would trigger the exception. However, this would split the code into ugly, detached pieces.
> 
> Simplified example:
> 
> (let ((a 2)(b 2))
>  (if (= (+ a b) 4)
>      #t
>      (error "impossible!")))
> 
> Is there some practical way to access the variable bindings inside a function from tests (similarly to the "set! <var>" pop-up menu in the debugger window) in order to inject faults to trigger these exceptions?

Mikko, I do not understand why you would want to write the code as above. If you specify
lexical variables and their initial values you don't need to test them and you can prove
to yourself that (= (+ a b) 4) will always hold. Perhaps you want to construct an example
that is closer to what you really want. 

You could pre-process the code with a debugger-like strategy so that third-party code can
inspect lexically bound variables. But the same pre-processing would expose lexical variables
for manipulation, defeating a belt-and-suspender approach. 

-- Matthias



Posted on the users mailing list.