comp.lang.ada
 help / color / mirror / Atom feed
* Normalizing array indices
@ 2011-10-28 18:58 Stefan.Lucks
  2011-10-28 20:36 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Stefan.Lucks @ 2011-10-28 18:58 UTC (permalink / raw)


Hi all, does anyone know a way to change the array indices of a subprogram 
parameter to start with a default index? This question occurred to me when 
I happened to discover a subtle bug in a sort procedure I had implemented. 

  generic
    type Element_Type is private;
    type Sort_Array_Type is array (Positive range <>) of Element_Type;
    with function "<" (Left, Right: Element_Type) return Boolean is <>;
  procedure Sort(A: in out Sort_Array_Type); 

I had a reasonable amount of black box tests and Sort passed all of them. 

Some time later, I added a test with A'range being  
   Positive'Last -2 .. Positive'Last
and boooom -- got a Constraint_Error. As it turned out, there was a 
Positive index variable which could take the value A'Last+1 -- which is 
perfectly OK except when A'Last = Positive'Last. To rescue my 
implementation I considered something like 

  procedure Sort(A: in out Sort_Array_Type) is
    Alias_A: Sort_Array_Type(1 .. A'Length) renames A;
  begin
    ... -- apply your favorite sorting algorithm to Alias_A;
  end Sort;

but the compiler didn't like that renaming:
  "constraint not allowed in object renaming declaration". 
Is there a way to get that effect? The following works, but hey, 
this is ugly and (for large A) very inefficient:

  procedure Sort(A: in out Sort_Array_Type) is
    Copy_Of_A: Sort_Array_Type(1 .. A'Length) := A;
  begin
    ... -- apply your favorite sorting algorithm to Copy_Of_A;
    A := Copy_Of_A;
  end Sort;

I finally solved the problem at hand by changing the logic of the sort 
subprogram. But the problem still continues to haunt my mind, for the 
following reasons:

1. On most compilers/machines it is safe to assume that there is not 
   sufficient storage for arrays of length Positive'Last. So the problem 
   just disappears in a language where arrays always start with a fixed 
   index (say, 0 or 1). So the Ada program is buggy, where the same 
   C program would be perfectly OK. 

2. More generally, proper testing in Ada may require more test cases than 
   testing the apparently same subprogram in another language, like C. Is
   Ada actually less testing-friendly?

In many cases, array ranges starting with an arbitrary index are better 
(higher level) to model an application's demands. But sometimes, like when 
applying a sorting routine, this extra information is actually some 
ballast. 

Ideally, the specification of a subprogram would carry the information 
that the subprogram only uses "normalized" array indices, to free the 
tester from having to consider test cases with different A'First:

  procedure Sort(A: in out Sort_Array_Type(1 .. <>)); 

The user can still call Sort with any array of range, say, 4711 .. 9421, 
but Sort coldn't tell that apart from an array of range 1 .. 4711. Thus, 
there is no reason for additional test cases with different values 
for A'First. 

Such a change is probably too late for Ada 2012 :-/ ... but perhaps it 
would be OK for Ada 2020. :-)



-- 
---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany  ----
    <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html>
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2011-11-02 12:16 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-28 18:58 Normalizing array indices Stefan.Lucks
2011-10-28 20:36 ` Adam Beneschan
2011-11-01 20:18   ` Stefan.Lucks
2011-10-28 21:13 ` Randy Brukardt
2011-10-29  7:29   ` Pascal Obry
2011-10-29 19:18     ` Jeffrey Carter
2011-10-29 19:58       ` tmoran
2011-10-29 21:15         ` Simon Wright
2011-10-29 20:41       ` Randy Brukardt
2011-11-01 20:49         ` stefan-lucks
2011-11-01 20:44     ` stefan-lucks
2011-11-01 20:43   ` stefan-lucks
2011-11-02 12:16     ` Robert A Duff
2011-10-29  9:05 ` Simon Wright
2011-10-29  9:23   ` Dmitry A. Kazakov
2011-11-01 20:55   ` stefan-lucks
2011-11-02 12:14   ` Robert A Duff

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