comp.lang.ada
 help / color / mirror / Atom feed
* Vectors.Insert_Space bug?
@ 2009-05-20 20:59 Thomas Løcke
  2009-05-21  1:56 ` John B. Matthews
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Løcke @ 2009-05-20 20:59 UTC (permalink / raw)


Hey all,

I've stumbled on some weird behavior while trying to make use of the 
Ada.Containers.Vectors.Insert_Space procedure. According to the RM, this 
is what it should do:

"... Then Insert_Space slides the elements in the range Before .. 
Last_Index (Container) up by Count positions, and then inserts empty 
elements in the positions starting at Before."

But that is not at all what I'm seeing, as this small test-program will 
show:

--  Pasted code start  --

with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded;      use Ada.Strings.Unbounded;
with Ada.Containers.Vectors;     use Ada.Containers;

procedure Test is
    package SUIO renames Ada.Text_IO.Unbounded_IO;
    package US_Container is new Vectors (Positive, Unbounded_String);
    Testing : US_Container.Vector;
begin
    Testing.Append (New_Item => To_Unbounded_String ("Item 1"));
    Testing.Append (New_Item => To_Unbounded_String ("Item 2"));
    Testing.Append (New_Item => To_Unbounded_String ("Item 3"));
    Testing.Append (New_Item => To_Unbounded_String ("Item 4"));
    Testing.Append (New_Item => To_Unbounded_String ("Item 5"));

    Testing.Insert_Space (Before => 5,
                       Count => 3);

    for i in 1 .. Testing.Length loop
       SUIO.Put_Line (i'Img & " " & Testing.Element (Integer (i)));
    end loop;
end Test;

--  Pasted code end  --

The output from the Test program is:

  1 Item 1
  2 Item 2
  3 Item 3
  4 Item 4
  5 Item 5
  6
  7
  8 Item 5

Which IMHO is wrong. There shouldn't be two "Item 5" elements. And it's 
possible to make it even worse, by calling Insert_Space multiple times 
with different Before and Count parameters.

It seems as if the element located at the Before index is copied and 
inserted as the first new element, and only after that, does 
Insert_Space actually start inserting empty elements.

I've tried to compile with two different compilers, and both exhibit 
this odd behavior:
	GNATMAKE 4.2.3
	GNATMAKE  GPL 2008 (20080521)

I admit to just being a hobbyist, so this might just be me not "getting" 
what the RM is trying to tell me, but I do think the text is fairly clear.

Any help would be greatly appreciated.

Regards,
Thomas L�cke



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

* Re: Vectors.Insert_Space bug?
  2009-05-20 20:59 Vectors.Insert_Space bug? Thomas Løcke
@ 2009-05-21  1:56 ` John B. Matthews
  2009-05-22  5:07   ` Thomas Løcke
  0 siblings, 1 reply; 3+ messages in thread
From: John B. Matthews @ 2009-05-21  1:56 UTC (permalink / raw)


In article <4a146f41$0$90265$14726298@news.sunsite.dk>,
 Thomas Løcke <thomas.granvej6@gmail.com> wrote:

> I've stumbled on some weird behavior while trying to make use of the 
> Ada.Containers.Vectors.Insert_Space procedure. According to the RM, this 
> is what it should do:
> 
> "... Then Insert_Space slides the elements in the range Before .. 
> Last_Index (Container) up by Count positions, and then inserts empty 
> elements in the positions starting at Before."
> 
> But that is not at all what I'm seeing, as this small test-program will 
> show:
[...]
> The output from the Test program is:
> 
>   1 Item 1
>   2 Item 2
>   3 Item 3
>   4 Item 4
>   5 Item 5
>   6
>   7
>   8 Item 5
> 
> Which IMHO is wrong. There shouldn't be two "Item 5" elements. And it's 
> possible to make it even worse, by calling Insert_Space multiple times 
> with different Before and Count parameters.
> 
> It seems as if the element located at the Before index is copied and 
> inserted as the first new element, and only after that, does 
> Insert_Space actually start inserting empty elements.
> 
> I've tried to compile with two different compilers, and both exhibit 
> this odd behavior:
> 	GNATMAKE 4.2.3
> 	GNATMAKE  GPL 2008 (20080521)
> 
> I admit to just being a hobbyist, so this might just be me not "getting" 
> what the RM is trying to tell me, but I do think the text is fairly clear.
> 
> Any help would be greatly appreciated.

I get the same result with 4.3.4.

It looks like the old element 5 slid to position 8. The three empties 
are at positions 5, 6 and 7. Note that "Empty elements do not have a 
specified value." In particular, "Reading the value of an empty element 
by calling Element" is a bounded error, and "The implementation may 
treat the [empty] element as having any normal value...":

<http://www.adaic.com/standards/05rm/html/RM-A-18-2.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



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

* Re: Vectors.Insert_Space bug?
  2009-05-21  1:56 ` John B. Matthews
@ 2009-05-22  5:07   ` Thomas Løcke
  0 siblings, 0 replies; 3+ messages in thread
From: Thomas Løcke @ 2009-05-22  5:07 UTC (permalink / raw)


John B. Matthews wrote:
> 
> I get the same result with 4.3.4.
> 
> It looks like the old element 5 slid to position 8. The three empties 
> are at positions 5, 6 and 7. Note that "Empty elements do not have a 
> specified value." In particular, "Reading the value of an empty element 
> by calling Element" is a bounded error, and "The implementation may 
> treat the [empty] element as having any normal value...":
> 
> <http://www.adaic.com/standards/05rm/html/RM-A-18-2.html>
> 

Hello John,

Thank you for the explanation. Between your post and some help from the 
#ada guys, I've finally succeeded in understanding how this works.

I guess I just needed to wrap my head around the fact that "empty" in 
this context actually means "unpredictable value".  :o)

Regards,
Thomas Løcke



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

end of thread, other threads:[~2009-05-22  5:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-20 20:59 Vectors.Insert_Space bug? Thomas Løcke
2009-05-21  1:56 ` John B. Matthews
2009-05-22  5:07   ` Thomas Løcke

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