Natools

Check-in [2fa0bb02a5]
Login
Overview
Comment:reference_tests-pools: new test suite for reference pools
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2fa0bb02a587f32aa7912095d3f80107c4bda643
User & Date: nat on 2014-08-27 19:43:06
Other Links: manifest | tags
Context
2014-08-29
20:37
s_expressions-atom_buffers: rename Read procedure to Peek, to make room for stream primitives check-in: a2fd0fbfb6 user: nat tags: trunk
2014-08-27
19:43
reference_tests-pools: new test suite for reference pools check-in: 2fa0bb02a5 user: nat tags: trunk
2014-08-26
20:50
references-pools: new package that provides a task-safe pool of references check-in: f530098004 user: nat tags: trunk
Changes

Added tests/natools-reference_tests-pools.adb version [f03e509b12].





































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
------------------------------------------------------------------------------
-- Copyright (c) 2014, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

package body Natools.Reference_Tests.Pools is

   use type Ref_Pools.Pool_Size;


   procedure Check_Counts
     (Test : in out NT.Test;
      Pool : in Ref_Pools.Pool;
      Active, Initialized, Total : in Ref_Pools.Pool_Size);

   procedure Check_Order
     (Test : in out NT.Test;
      Pool : in out Ref_Pools.Pool);


   ------------------------------
   -- Local Helper Subprograms --
   ------------------------------

   procedure Check_Counts
     (Test : in out NT.Test;
      Pool : in Ref_Pools.Pool;
      Active, Initialized, Total : in Ref_Pools.Pool_Size)
   is
      S : Ref_Pools.Pool_Size;
   begin
      S := Pool.Active_Size;
      if S /= Active then
         Test.Fail
           ("Pool.Active_Size is"
            & Ref_Pools.Pool_Size'Image (S)
            & ", expected "
            & Ref_Pools.Pool_Size'Image (Active));
      end if;

      S := Pool.Initialized_Size;
      if S /= Initialized then
         Test.Fail
           ("Pool.Initialized_Size is"
            & Ref_Pools.Pool_Size'Image (S)
            & ", expected "
            & Ref_Pools.Pool_Size'Image (Initialized));
      end if;

      S := Pool.Capacity;
      if S /= Total then
         Test.Fail
           ("Pool.Initialized_Size is"
            & Ref_Pools.Pool_Size'Image (S)
            & ", expected "
            & Ref_Pools.Pool_Size'Image (Total));
      end if;
   end Check_Counts;


   procedure Check_Order
     (Test : in out NT.Test;
      Pool : in out Ref_Pools.Pool)
   is
      procedure Process (Ref : in Refs.Reference);

      Rank, Last : Natural := 0;

      procedure Process (Ref : in Refs.Reference) is
      begin
         Rank := Rank + 1;

         if Ref.Is_Empty then
            Test.Fail ("Unexpected empty reference at rank"
              & Natural'Image (Rank));
            return;
         end if;

         declare
            Accessor : constant Refs.Accessor := Ref.Query;
         begin
            if Accessor.Data.Instance_Number = 0 then
               Test.Fail ("Unexpected null instance number at rank"
                 & Natural'Image (Rank));
            elsif Last = 0 then
               Last := Accessor.Data.Instance_Number;
            elsif Accessor.Data.Instance_Number /= Last + 1 then
               Test.Fail ("At rank"
                 & Natural'Image (Rank)
                 & ", reference to instance"
                 & Natural'Image (Accessor.Data.Instance_Number)
                 & " following reference to instance"
                 & Natural'Image (Last));
               Last := 0;
            else
               Last := Accessor.Data.Instance_Number;
            end if;
         end;
      end Process;
   begin
      Pool.Unchecked_Iterate (Process'Access);
   end Check_Order;



   ------------------------
   -- Peudo_Process Task --
   ------------------------

   task body Pseudo_Process is
      Time : Duration;
      Ref : Refs.Reference;
   begin
      select
         accept Start (Target : in Refs.Reference; Amount : in Duration) do
            Time := Amount;
            Ref := Target;
         end Start;
      or terminate;
      end select;

      delay Time;
      Ref.Reset;
   end Pseudo_Process;


   procedure Bounded_Start
     (Process : in out Pseudo_Process;
      Pool : in out Ref_Pools.Pool;
      Amount : in Duration;
      Test : in out NT.Test;
      Expected_Instance : in Natural)
   is
      Ref : Refs.Reference;
   begin
      Pool.Get (Factory'Access, Ref);

      if Ref.Query.Data.Instance_Number /= Expected_Instance then
         Test.Fail ("Got reference to instance"
           & Natural'Image (Ref.Query.Data.Instance_Number)
           & ", expected"
           & Natural'Image (Expected_Instance));
      end if;

      Process.Start (Ref, Amount);
   end Bounded_Start;


   procedure Unbounded_Start
     (Process : in out Pseudo_Process;
      Pool : in out Ref_Pools.Pool;
      Amount : in Duration;
      Test : in out NT.Test;
      Expected_Instance : in Natural)
   is
      Ref : Refs.Reference;
   begin
      Pool.Create (Factory'Access, Ref);

      if Ref.Query.Data.Instance_Number /= Expected_Instance then
         Test.Fail ("Got reference to instance"
           & Natural'Image (Ref.Query.Data.Instance_Number)
           & ", expected"
           & Natural'Image (Expected_Instance));
      end if;

      Process.Start (Ref, Amount);
   end Unbounded_Start;



   -------------------------
   -- Complete Test Suite --
   -------------------------

   procedure All_Tests (Report : in out NT.Reporter'Class) is
   begin
      Bounded_Pool (Report);
      Static_Pool (Report);
      Unbounded_Pool (Report);
   end All_Tests;



   -----------------------
   -- Inidividual Tests --
   -----------------------

   procedure Bounded_Pool (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Bounded pool typical usage");
   begin
      declare
         Test_Length : constant Duration := 0.5;
         Ref_Pool : Ref_Pools.Pool;
         Workers : array (1 .. 4) of Pseudo_Process;
      begin
         --  Timeline (in Test_Length/10):     <-------->
         --  Task using reference 1:           1111111111
         --  Task using reference 2:             2222 44
         --  Task using reference 3:               3333

         Check_Counts (Test, Ref_Pool, 0, 0, 0);
         Ref_Pool.Preallocate (3);
         Check_Counts (Test, Ref_Pool, 0, 0, 3);

         Bounded_Start (Workers (1), Ref_Pool, Test_Length, Test, 1);
         Check_Counts (Test, Ref_Pool, 1, 1, 3);

         delay Test_Length * 0.2;
         Bounded_Start (Workers (2), Ref_Pool, Test_Length * 0.4, Test, 2);
         Check_Counts (Test, Ref_Pool, 2, 2, 3);

         delay Test_Length * 0.2;
         Bounded_Start (Workers (3), Ref_Pool, Test_Length * 0.4, Test, 3);
         Check_Counts (Test, Ref_Pool, 3, 3, 3);

         delay Test_Length * 0.1;
         begin
            Bounded_Start (Workers (4), Ref_Pool, Test_Length * 0.2, Test, 0);
            Test.Fail ("Expected exception after filling bounded pool");
         exception
            when Constraint_Error =>
               null;
            when Error : others =>
               Test.Info ("At Get on full bounded pool,");
               Test.Report_Exception (Error, NT.Fail);
         end;

         delay Test_Length * 0.2;
         Check_Counts (Test, Ref_Pool, 2, 3, 3);
         Bounded_Start (Workers (4), Ref_Pool, Test_Length * 0.2, Test, 2);
         Check_Counts (Test, Ref_Pool, 3, 3, 3);

         Check_Order (Test, Ref_Pool);
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Bounded_Pool;


   procedure Static_Pool (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Static pool typical usage");
   begin
      declare
         Size : constant Ref_Pools.Pool_Size := 10;
         Ref_Pool : Ref_Pools.Pool;
         Ref : array (Ref_Pools.Pool_Size range 1 .. Size) of Refs.Reference;
      begin
         Check_Counts (Test, Ref_Pool, 0, 0, 0);
         Ref_Pool.Preallocate (Size, Factory'Access);
         Check_Counts (Test, Ref_Pool, 0, Size, Size);

         for I in Ref'Range loop
            Ref_Pool.Get (Ref (I));
            Check_Counts (Test, Ref_Pool, I, Size, Size);
         end loop;

         Ref (2).Reset;
         Check_Counts (Test, Ref_Pool, Size - 1, Size, Size);

         Ref_Pool.Get (Ref (2));
         Check_Counts (Test, Ref_Pool, Size, Size, Size);

         declare
            Extra_Ref : Refs.Reference;
         begin
            Ref_Pool.Get (Extra_Ref);
            Test.Fail ("Expected exception at Get on full pool");
         exception
            when Constraint_Error =>
               null;
            when Error : others =>
               Test.Info ("At Get on full pool,");
               Test.Report_Exception (Error, NT.Fail);
         end;

         Check_Order (Test, Ref_Pool);
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Static_Pool;


   procedure Unbounded_Pool (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Unbounded pool typical usage");
   begin
      declare
         Test_Length : constant Duration := 0.5;
         Ref_Pool : Ref_Pools.Pool;
         Workers : array (1 .. 5) of Pseudo_Process;
      begin
         Check_Counts (Test, Ref_Pool, 0, 0, 0);
         Ref_Pool.Preallocate (1);
         Check_Counts (Test, Ref_Pool, 0, 0, 1);

         --  Timeline (in Test_Length/10):     <-------->
         --  Task using reference 1:           11111 444
         --  Task using reference 2:             22222 55
         --  Task using reference 3:               33333

         Unbounded_Start (Workers (1), Ref_Pool, Test_Length * 0.5, Test, 1);
         Check_Counts (Test, Ref_Pool, 1, 1, 1);

         delay Test_Length * 0.2;
         Unbounded_Start (Workers (2), Ref_Pool, Test_Length * 0.5, Test, 2);
         Check_Counts (Test, Ref_Pool, 2, 2, 2);

         delay Test_Length * 0.2;
         Unbounded_Start (Workers (3), Ref_Pool, Test_Length * 0.5, Test, 3);
         Check_Counts (Test, Ref_Pool, 3, 3, 3);

         delay Test_Length * 0.2;
         Check_Counts (Test, Ref_Pool, 2, 3, 3);
         Unbounded_Start (Workers (4), Ref_Pool, Test_Length * 0.3, Test, 1);
         Check_Counts (Test, Ref_Pool, 3, 3, 3);

         delay Test_Length * 0.1;
         Check_Counts (Test, Ref_Pool, 2, 3, 3);
         Ref_Pool.Purge;
         Check_Counts (Test, Ref_Pool, 2, 2, 2);
         Unbounded_Start (Workers (5), Ref_Pool, Test_Length * 0.2, Test, 3);
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Unbounded_Pool;

end Natools.Reference_Tests.Pools;

Added tests/natools-reference_tests-pools.ads version [12c8940507].















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
------------------------------------------------------------------------------
-- Copyright (c) 2014, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Natools.Reference_Tests.Pools expands reference test suite with          --
-- reference pools.                                                         --
------------------------------------------------------------------------------

private with Natools.References.Pools;

package Natools.Reference_Tests.Pools is

   procedure All_Tests (Report : in out NT.Reporter'Class);

   procedure Bounded_Pool (Report : in out NT.Reporter'Class);
   procedure Static_Pool (Report : in out NT.Reporter'Class);
   procedure Unbounded_Pool (Report : in out NT.Reporter'Class);

private

   package Ref_Pools is new Refs.Pools;


   task type Pseudo_Process is
      entry Start (Target : in Refs.Reference; Amount : in Duration);
   end Pseudo_Process;

   procedure Bounded_Start
     (Process : in out Pseudo_Process;
      Pool : in out Ref_Pools.Pool;
      Amount : in Duration;
      Test : in out NT.Test;
      Expected_Instance : in Natural);

   procedure Unbounded_Start
     (Process : in out Pseudo_Process;
      Pool : in out Ref_Pools.Pool;
      Amount : in Duration;
      Test : in out NT.Test;
      Expected_Instance : in Natural);

end Natools.Reference_Tests.Pools;

Modified tests/test_all.adb from [6a63b6c2bf] to [f12c5e815d].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Cron.Tests;
with Natools.Getopt_Long_Tests;
with Natools.HMAC_Tests;
with Natools.Reference_Tests;

with Natools.S_Expressions.Atom_Buffers.Tests;
with Natools.S_Expressions.Cache_Tests;
with Natools.S_Expressions.Dynamic_Interpreter_Tests;
with Natools.S_Expressions.Encodings.Tests;
with Natools.S_Expressions.File_RW_Tests;
with Natools.S_Expressions.Interpreter_Tests;
with Natools.S_Expressions.Lockable.Tests;







>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Cron.Tests;
with Natools.Getopt_Long_Tests;
with Natools.HMAC_Tests;
with Natools.Reference_Tests;
with Natools.Reference_Tests.Pools;
with Natools.S_Expressions.Atom_Buffers.Tests;
with Natools.S_Expressions.Cache_Tests;
with Natools.S_Expressions.Dynamic_Interpreter_Tests;
with Natools.S_Expressions.Encodings.Tests;
with Natools.S_Expressions.File_RW_Tests;
with Natools.S_Expressions.Interpreter_Tests;
with Natools.S_Expressions.Lockable.Tests;
87
88
89
90
91
92
93




94
95
96
97
98
99
100
   Natools.HMAC_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Natools.Reference_Tests.Test_Task_Safety (Report);
   Report.End_Section;





   Report.Section ("S_Expressions.Atom_Buffers");
   Natools.S_Expressions.Atom_Buffers.Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("S_Expressions.Caches");
   Natools.S_Expressions.Cache_Tests.All_Tests (Report);







>
>
>
>







88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
   Natools.HMAC_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Natools.Reference_Tests.Test_Task_Safety (Report);
   Report.End_Section;

   Report.Section ("References.Pools");
   Natools.Reference_Tests.Pools.All_Tests (Report);
   Report.End_Section;

   Report.Section ("S_Expressions.Atom_Buffers");
   Natools.S_Expressions.Atom_Buffers.Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("S_Expressions.Caches");
   Natools.S_Expressions.Cache_Tests.All_Tests (Report);