comp.lang.ada
 help / color / mirror / Atom feed
From: Phil Clayton <phil.clayton@lineone.net>
Subject: Re: Float conversion
Date: Mon, 2 Aug 2010 18:07:20 -0700 (PDT)
Date: 2010-08-02T18:07:20-07:00	[thread overview]
Message-ID: <77ee8883-ab9f-42c7-94d5-3d85cdc19693@i28g2000yqa.googlegroups.com> (raw)
In-Reply-To: 82y6cru1lp.fsf@stephe-leake.org

On Jul 31, 4:12 pm, Stephen Leake <stephen_le...@stephe-leake.org>
wrote:
> Phil Clayton <phil.clay...@lineone.net> writes:
> >   if A < B and A < C
> >   then
> >     Y := A;
> >   elsif B < C and B < A
> >   then
> >     Y := B;
> >   else
> >     Y := C;
> >   end if;
>
> > The justification given is
>
> >   if A is smallest, set Y to A
> >   else if B is smallest, set Y to B
> >   else C is smallest so set Y to C
>
> > Unfortunately, the program doesn't work.  If you haven't spotted why,
> > it is well worth trying to work it out, perhaps with a few test cases.
>
> > In fact, this particular error came to various people's attention
> > because it made its way though all stages of a safety-critical
> > software development process.  (Fortunately the consequences were not
> > too serious, though intriguing.)  The program fails exactly when A = B
> > < C because it returns C, which is not the minimum.
>
> I am _always_ suspicious of 'and' conditions in nested if/then/else; it
> is easy to leave out a case. If this had been written:
>
> if A < B then
>    if A < C then
>      Y := A;
>    else
>       --  A >= C
>     ...
>
> The problem would have been clear from the start.
>
> > I often bring this example up to motivate the use of formal methods as
> > it is particularly difficult to find the error through testing,
> > especially when A, B and C are real types.  What are the chances of
> > having A equal to B?  
>
> 100%, for a rationally designed test!

Justifying that a set of test cases has a 100% chance of finding an
error (when not testing all combinations of all input values) will
unavoidably involve formal reasoning, similar to that used for formal
methods.

I was really talking about less formal approaches to testing.


> Clearly to cover all cases, you
> need A < B, A = B, A > B, A < C, etc.

You make it sound easy...  Generally, how do you justify that tests
'cover all cases' so giving you a 100% chance of finding an error?

Note that even if I had test cases that exercise all possible
orderings of A, B and C, I am not guaranteed to find an error in an
incorrect implementation of Y = min {A, B, C}.  For example, I could
have chosen positive values for A in every test but the program could
have contained an extra erroneous condition

  ... and 0 < A

that would have gone undetected.  So the tests need to take account of
the particular implementation too.


> > Where does the original justification go wrong?  Well, when talking
> > about 'the smallest' there is an implicit assumption being made that
> > it is unique.  The justification never considers the case when A or B
> > is the non-unique smallest.
>
> And the same error could be made in a formal specification. "formal"
> does _not_ mean "complete"!

Although I said "formal methods", I use this example to motivate
formal verification, not formal specification.  Any specification,
formal or not, can be a load of rubbish!


> > Of course, the correct program just uses "<=" instead of "<",
>
> That would be one way, but it would still require the same detailed
> analysis and test. I prefer the exhaustive if/then/else style above.

In the absence of any formal verification support, it's probably a
good idea to keep things simple like that.

Phil



  reply	other threads:[~2010-08-03  1:07 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-29 12:23 Float conversion Henrique
2010-07-29 12:44 ` Jacob Sparre Andersen
2010-07-29 12:46 ` Dmitry A. Kazakov
2010-07-29 15:08   ` Georg Bauhaus
2010-07-29 15:10     ` Georg Bauhaus
2010-07-29 15:35     ` Dmitry A. Kazakov
2010-07-29 18:21       ` Henrique
2010-07-29 19:08         ` Jeffrey R. Carter
2010-07-29 19:15         ` Dmitry A. Kazakov
2010-07-30  1:30           ` Phil Clayton
2010-07-30  8:43             ` Dmitry A. Kazakov
2010-07-30 13:14               ` Phil Clayton
2010-07-30 14:34                 ` Dmitry A. Kazakov
2010-07-31 15:12                 ` Stephen Leake
2010-08-03  1:07                   ` Phil Clayton [this message]
2010-08-03  3:31                     ` Shark8
2010-08-03 10:38                     ` Georg Bauhaus
2010-08-04  7:27                       ` Stephen Leake
2010-08-04 16:15                         ` Georg Bauhaus
2010-08-04 16:32                       ` Phil Clayton
2010-08-04  7:26                     ` Stephen Leake
2010-08-04 12:52                       ` Robert A Duff
2010-08-04 14:32                         ` Dmitry A. Kazakov
2010-08-04 19:36                           ` Simon Wright
2010-08-04 19:46                             ` Dmitry A. Kazakov
2010-08-04 20:29                             ` Georg Bauhaus
2010-08-05 12:05                         ` Stephen Leake
2010-08-07  5:54                           ` Shark8
2010-08-07  8:56                             ` Georg Bauhaus
2010-08-07 13:49                               ` Shark8
2010-08-05 10:20                       ` Phil Clayton
2010-07-30 13:16           ` Henrique
2010-07-29 15:37   ` Warren
2010-07-29 14:56 ` Georg Bauhaus
2010-07-29 17:56   ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox