Natools

Check-in [e57fb70751]
Login
Overview
Comment:s_expressions-test_tools: new type memory stream to test input and output interfaces to streams
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e57fb70751b8c5c9f075113bb157d714d1646c0c
User & Date: nat on 2014-01-08 20:14:27
Other Links: manifest | tags
Context
2014-01-09
22:19
s_expressions-printers-tests: fully-covering test suite for canonical atom printer check-in: 08a213ab1f user: nat tags: trunk
2014-01-08
20:14
s_expressions-test_tools: new type memory stream to test input and output interfaces to streams check-in: e57fb70751 user: nat tags: trunk
2014-01-07
21:00
s_expressions-printers: new packge defining an interface for S-expression input check-in: 7e2c240617 user: nat tags: trunk
Changes

Modified tests/natools-s_expressions-test_tools.adb from [075dff7ba9] to [d4ac9882a2].

155
156
157
158
159
160
161


























































































































162
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

      else
         Report.Item (Test_Name, NT.Fail);
         Dump_Atom (Report, Found, "Found");
         Dump_Atom (Report, Expected, "Expected");
      end if;
   end Test_Atom;



   -------------------
   -- Memory Stream --
   -------------------

   overriding procedure Read
     (Stream : in out Memory_Stream;
      Item : out Ada.Streams.Stream_Element_Array;
      Last : out Ada.Streams.Stream_Element_Offset) is
   begin
      Last := Item'First - 1;

      while Last + 1 in Item'Range
        and then Stream.Read_Pointer < Stream.Internal.Length
      loop
         Stream.Read_Pointer := Stream.Read_Pointer + 1;
         Last := Last + 1;
         Item (Last) := Stream.Internal.Element (Stream.Read_Pointer);
      end loop;
   end Read;


   overriding procedure Write
     (Stream : in out Memory_Stream;
      Item : in Ada.Streams.Stream_Element_Array) is
   begin
      if Stream.Read_Pointer >= Stream.Internal.Length then
         Stream.Internal.Soft_Reset;
         Stream.Read_Pointer := 0;
      end if;

      Stream.Internal.Append (Item);

      if not Stream.Mismatch then
         for I in Item'Range loop
            if Stream.Expect_Pointer + 1 > Stream.Expected.Length
              or else Stream.Expected.Element (Stream.Expect_Pointer + 1)
                /= Item (I)
            then
               Stream.Mismatch := True;
               exit;
            end if;

            Stream.Expect_Pointer := Stream.Expect_Pointer + 1;
         end loop;
      end if;
   end Write;


   function Get_Data (Stream : Memory_Stream) return Atom is
   begin
      return Stream.Internal.Query;
   end Get_Data;


   function Unread_Data (Stream : Memory_Stream) return Atom is
   begin
      if Stream.Read_Pointer < Stream.Internal.Length then
         return Stream.Internal.Query.Data.all
           (Stream.Read_Pointer + 1 .. Stream.Internal.Length);
      else
         return Null_Atom;
      end if;
   end Unread_Data;


   procedure Set_Data
     (Stream : in out Memory_Stream;
      Data : in Atom) is
   begin
      Stream.Internal.Soft_Reset;
      Stream.Internal.Append (Data);
   end Set_Data;


   function Unread_Expected (Stream : Memory_Stream) return Atom is
   begin
      if Stream.Expect_Pointer < Stream.Expected.Length then
         return Stream.Expected.Query.Data.all
           (Stream.Expect_Pointer + 1 .. Stream.Expected.Length);
      else
         return Null_Atom;
      end if;
   end Unread_Expected;


   procedure Set_Expected
     (Stream : in out Memory_Stream;
      Data : in Atom;
      Reset_Mismatch : in Boolean := True) is
   begin
      Stream.Expected.Soft_Reset;
      Stream.Expected.Append (Data);
      Stream.Expect_Pointer := 0;
      if Reset_Mismatch then
         Stream.Mismatch := False;
      end if;
   end Set_Expected;


   function Has_Mismatch (Stream : Memory_Stream) return Boolean is
   begin
      return Stream.Mismatch;
   end Has_Mismatch;


   procedure Reset_Mismatch (Stream : in out Memory_Stream) is
   begin
      Stream.Mismatch := False;
   end Reset_Mismatch;


   function Mismatch_Index (Stream : Memory_Stream) return Count is
   begin
      if Stream.Mismatch then
         return Stream.Expect_Pointer + 1;
      else
         return 0;
      end if;
   end Mismatch_Index;

end Natools.S_Expressions.Test_Tools;

Modified tests/natools-s_expressions-test_tools.ads from [8aef64b4b9] to [938b61502e].

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
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







+
+
+
+


+
+

+
+




















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Natools.S_Expressions.Test_Tools provides tools used in S-expression     --
-- test suites.                                                             --
-- Memory_Stream is a stream implementation around a memory buffer where    --
-- written data can be subsequently read. A secondary buffer of expected    --
-- data can be optionally used, and the mismatch marker is set when written --
-- data does not match expected data.                                       --
------------------------------------------------------------------------------

with Ada.Streams;

with Natools.Tests;

with Natools.S_Expressions.Atom_Buffers;

package Natools.S_Expressions.Test_Tools is
   pragma Preelaborate (Test_Tools);

   package NT renames Natools.Tests;

   procedure Dump_Atom
     (Report : in out NT.Reporter'Class;
      Data : in Atom;
      Label : in String := "");
      --  Dump contents on Data as info in Report

   procedure Test_Atom
     (Report : in out NT.Reporter'Class;
      Test_Name : in String;
      Expected : in Atom;
      Found : in Atom);
      --  Report success when Found is equal to Expected, and failure
      --  with diagnostics otherwise.


   type Memory_Stream is new Ada.Streams.Root_Stream_Type with private;

   overriding procedure Read
     (Stream : in out Memory_Stream;
      Item : out Ada.Streams.Stream_Element_Array;
      Last : out Ada.Streams.Stream_Element_Offset);
      --  Consume data from the beginning of internal buffer

   overriding procedure Write
     (Stream : in out Memory_Stream;
      Item : in Ada.Streams.Stream_Element_Array);
      --  Append data at the end of internal buffer

   function Get_Data (Stream : Memory_Stream) return Atom;
      --  Return internal buffer

   function Unread_Data (Stream : Memory_Stream) return Atom;
      --  Return part of internal buffer that has not yet been read

   procedure Set_Data
     (Stream : in out Memory_Stream;
      Data : in Atom);
      --  Replace whole internal buffer with Data

   function Unread_Expected (Stream : Memory_Stream) return Atom;
      --  Return part of expected buffer that has not been matched yet

   procedure Set_Expected
     (Stream : in out Memory_Stream;
      Data : in Atom;
      Reset_Mismatch : in Boolean := True);
      --  Replace buffer of expected data

   function Has_Mismatch (Stream : Memory_Stream) return Boolean;
   procedure Reset_Mismatch (Stream : in out Memory_Stream);
      --  Accessor and mutator of the mismatch flag

   function Mismatch_Index (Stream : Memory_Stream) return Count;
      --  Return the position of the first mismatching octet,
      --  or 0 when there has been no mismatch.

private

   type Memory_Stream is new Ada.Streams.Root_Stream_Type with record
      Internal : Atom_Buffers.Atom_Buffer;
      Expected : Atom_Buffers.Atom_Buffer;
      Read_Pointer : Count := 0;
      Expect_Pointer : Count := 0;
      Mismatch : Boolean := False;
   end record;

end Natools.S_Expressions.Test_Tools;