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: a07f3367d7,7b6305d0d57a9f34 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.204.156.155 with SMTP id x27mr554732bkw.7.1319836602286; Fri, 28 Oct 2011 14:16:42 -0700 (PDT) MIME-Version: 1.0 Path: l23ni8778bkv.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!newsfeed.x-privat.org!news.jacob-sparre.dk!pnx.dk!jacob-sparre.dk!ada-dk.org!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Normalizing array indices Date: Fri, 28 Oct 2011 16:13:58 -0500 Organization: Jacob Sparre Andersen Research & Innovation Message-ID: References: NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: munin.nbi.dk 1319836442 12805 69.95.181.76 (28 Oct 2011 21:14:02 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Fri, 28 Oct 2011 21:14:02 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109 X-RFC2646: Format=Flowed; Original Xref: news1.google.com comp.lang.ada:18741 Date: 2011-10-28T16:13:58-05:00 List-Id: wrote in message news:Pine.LNX.4.64.1110282013540.21821@medsec1.medien.uni-weimar.de... > 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. This is pretty typical. We've often talked about the need in Ada for one-sided array subtypes, but we don't have any first class ones at this point. I probably would constrain the parameter to have a lower bound of 1. Using Ada 2012: 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) with Pre => A'First 1; You could do something similar with a subtype using a dynamic predicate (but not in a generic specification). In Ada 2005 and earlier, I'd simply make it part of the spec with some English wording, and then start the code with if A'First /= 1 then raise Program_Error; end if; (You could also use pragma Assert if you don't mind the possibility that someone turned it off.) Then, if you had any calls that don't have the right bounds (which is likely to be rare), I'd use a sliding trick similar to the one Adam showed. (But I'd probably try hard to figure out how to avoid passing the slice in the first place.) Of course, it's better to make the routine work for all possible bounds. And if you do that, you certainly need to add that to the testing burden (it's commonly forgotten and probably is one of the most common Ada bugs). Randy.