From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7b6305d0d57a9f34,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.204.156.155 with SMTP id x27mr500752bkw.7.1319828566645; Fri, 28 Oct 2011 12:02:46 -0700 (PDT) Path: l23ni7890bkv.0!nntp.google.com!news1.google.com!goblin1!goblin2!goblin.stu.neva.ru!feeder.erje.net!news-2.dfn.de!news.dfn.de!news.uni-weimar.de!not-for-mail From: Stefan.Lucks@uni-weimar.de Newsgroups: comp.lang.ada Subject: Normalizing array indices Date: Fri, 28 Oct 2011 20:58:20 +0200 Organization: Bauhaus-Universitaet Weimar Message-ID: NNTP-Posting-Host: medsec1.medien.uni-weimar.de Mime-Version: 1.0 X-Trace: tigger.scc.uni-weimar.de 1319828470 14935 141.54.178.228 (28 Oct 2011 19:01:10 GMT) X-Complaints-To: news@tigger.scc.uni-weimar.de NNTP-Posting-Date: Fri, 28 Oct 2011 19:01:10 +0000 (UTC) X-X-Sender: lucks@medsec1.medien.uni-weimar.de Xref: news1.google.com comp.lang.ada:18739 Content-Type: TEXT/PLAIN; charset=US-ASCII Date: 2011-10-28T20:58:20+02:00 List-Id: 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 ---- ------ I love the taste of Cryptanalysis in the morning! ------