Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.

Commit 983584c

Browse files
committed
initialize stack with default null element
ref #18
1 parent 48237e2 commit 983584c

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

src/basalt-stack.adb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ is
3434
procedure Push (S : in out Context;
3535
E : Element_Type) is
3636
begin
37-
S.Index := S.Index + 1;
38-
S.List (S.Index) := E;
37+
S.Index := S.Index + 1;
38+
S.List (S.Index).Value := E;
3939
end Push;
4040

4141
------------------
@@ -46,7 +46,7 @@ is
4646
is
4747
begin
4848
S.Index := S.Index + 1;
49-
Push (S.List (S.Index));
49+
Push (S.List (S.Index).Value);
5050
end Generic_Push;
5151

5252
---------
@@ -56,7 +56,7 @@ is
5656
procedure Pop (S : in out Context;
5757
E : out Element_Type) is
5858
begin
59-
E := S.List (S.Index);
59+
E := S.List (S.Index).Value;
6060
Drop (S);
6161
end Pop;
6262

@@ -67,7 +67,7 @@ is
6767
procedure Generic_Pop (S : in out Context)
6868
is
6969
begin
70-
Pop (S.List (S.Index));
70+
Pop (S.List (S.Index).Value);
7171
Drop (S);
7272
end Generic_Pop;
7373

@@ -93,19 +93,10 @@ is
9393
-- Initialize --
9494
----------------
9595

96-
procedure Initialize (S : out Context;
97-
Null_Element : Element_Type)
96+
procedure Initialize (S : in out Context)
9897
is
9998
begin
10099
S.Index := 0;
101-
-- This would be the correct way to initialize S.List:
102-
-- S.List := (others => Null_Element);
103-
-- As this creates a (potentially large) object on the stack, we initialize in a loop. The resulting flow
104-
-- error is justified in the spec.
105-
for E of S.List loop
106-
pragma Loop_Invariant (S.Index = 0);
107-
E := Null_Element;
108-
end loop;
109100
end Initialize;
110101

111102
end Basalt.Stack;

src/basalt-stack.ads

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
generic
1313
type Element_Type is private;
14+
Null_Element : Element_Type;
1415
package Basalt.Stack with
1516
SPARK_Mode,
1617
Pure,
@@ -106,21 +107,20 @@ is
106107
-- Initialize stack and clear stack buffer
107108
--
108109
-- @param S Stack
109-
procedure Initialize (S : out Context;
110-
Null_Element : Element_Type) with
110+
procedure Initialize (S : in out Context) with
111111
Post => Is_Empty (S)
112112
and then not Is_Full (S);
113113

114-
pragma Annotate (GNATprove, False_Positive,
115-
"""S.List"" might not be initialized*",
116-
"Initialized in complete loop");
117-
118114
private
119115

120-
type Simple_List is array (Positive range <>) of Element_Type;
116+
type List_Element is record
117+
Value : Element_Type := Null_Element;
118+
end record;
119+
120+
type Simple_List is array (Positive range <>) of List_Element;
121121

122122
type Context (Size : Positive) is record
123-
Index : Natural;
123+
Index : Natural := 0;
124124
List : Simple_List (1 .. Size);
125125
end record with
126126
Predicate => Index <= List'Last;

test/aunit/stack_tests.adb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ with Basalt.Stack;
33

44
package body Stack_Tests
55
is
6-
package F is new Basalt.Stack (Integer);
6+
package F is new Basalt.Stack (Integer, 0);
77

88
procedure Test_Stack (T : in out Aunit.Test_Cases.Test_Case'Class)
99
is
1010
pragma Unreferenced (T);
1111
Q : F.Context (100);
1212
J : Integer;
1313
begin
14-
F.Initialize (Q, 0);
14+
F.Initialize (Q);
1515
for I in 70 .. 120 loop
1616
F.Push (Q, I);
1717
end loop;
@@ -26,7 +26,7 @@ is
2626
pragma Unreferenced (T);
2727
Q : F.Context (2);
2828
begin
29-
F.Initialize (Q, 0);
29+
F.Initialize (Q);
3030
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count not 0");
3131
F.Push (Q, 1);
3232
Aunit.Assertions.Assert (F.Count (Q) = 1, "Count not 1 after Push");
@@ -43,7 +43,7 @@ is
4343
pragma Unreferenced (T);
4444
Q : F.Context (1);
4545
begin
46-
F.Initialize (Q, 0);
46+
F.Initialize (Q);
4747
Aunit.Assertions.Assert (F.Is_Empty (Q), "Stack not empty");
4848
Aunit.Assertions.Assert (F.Count (Q) = 0, "Stack not empty");
4949
F.Push (Q, 1);
@@ -59,7 +59,7 @@ is
5959
pragma Unreferenced (T);
6060
Q : F.Context (100);
6161
begin
62-
F.Initialize (Q, 0);
62+
F.Initialize (Q);
6363
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count should be 0");
6464
for I in Integer range 1 .. 20 loop
6565
F.Push (Q, I);
@@ -91,10 +91,10 @@ is
9191
Q3 : F.Context (200);
9292
Q4 : F.Context (13000);
9393
begin
94-
F.Initialize (Q1, 0);
95-
F.Initialize (Q2, 0);
96-
F.Initialize (Q3, 0);
97-
F.Initialize (Q4, 0);
94+
F.Initialize (Q1);
95+
F.Initialize (Q2);
96+
F.Initialize (Q3);
97+
F.Initialize (Q4);
9898
Aunit.Assertions.Assert (F.Size (Q1) = 1, "Size of Q1 should be 1");
9999
Aunit.Assertions.Assert (F.Size (Q2) = 50, "Size of Q2 should be 50");
100100
Aunit.Assertions.Assert (F.Size (Q3) = 200, "Size of Q3 should be 200");
@@ -120,7 +120,7 @@ is
120120
Aunit.Assertions.Assert (I = 42, "I should be 42");
121121
end Peek;
122122
begin
123-
F.Initialize (Q, 0);
123+
F.Initialize (Q);
124124
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count should be 0");
125125
Push (Q);
126126
Aunit.Assertions.Assert (F.Count (Q) = 1, "Count should be 1");

test/proof/proof_stack.adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package body Proof_Stack with
55
SPARK_Mode
66
is
77

8-
package Stack is new Basalt.Stack (Integer);
8+
package Stack is new Basalt.Stack (Integer, 0);
99
S : Stack.Context (10);
1010

1111
procedure Prove
@@ -26,7 +26,7 @@ is
2626
J_Ignore := I;
2727
end Pop;
2828
begin
29-
Stack.Initialize (S, 0);
29+
Stack.Initialize (S);
3030
for I in Integer range 7 .. 13 loop
3131
Stack.Push (S, I);
3232
exit when Stack.Count (S) >= Stack.Size (S);

0 commit comments

Comments
 (0)