1 /*******************************************************************************
2 
3     Unit tests for ocean.core.Buffer
4 
5     Each test block covers all `Buffer` method and is supposed to be copied and
6     adapted for each new element type to test.
7 
8     Copyright:
9         Copyright (c) 2016-2017 dunnhumby Germany GmbH.
10         All rights reserved.
11 
12     License:
13         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
14         Alternatively, this file may be distributed under the terms of the Tango
15         3-Clause BSD License (see LICENSE_BSD.txt for details).
16 
17 *******************************************************************************/
18 
19 module ocean.core.Buffer_test;
20 
21 import ocean.core.Buffer;
22 import ocean.core.Test;
23 import ocean.core.TypeConvert : arrayOf;
24 
25 unittest
26 {
27     // char buffers
28 
29     auto buffer = createBuffer("abcd");
30     test!("==")(buffer.length, 4);
31 
32     buffer.length = 1;
33     test!("==")(buffer[], "a");
34 
35     buffer.reset();
36     test!("==")(buffer[], "");
37     test!("!is")(buffer[].ptr, null);
38 
39     buffer.reserve(20);
40     testNoAlloc({
41         buffer ~= 'a';
42         buffer ~= "abc";
43     } ());
44     test!("==")(buffer[], "aabc");
45 
46     test!("==")(buffer[1 .. buffer.length], "abc");
47 
48     auto old_ptr = buffer[].ptr;
49     buffer = "xyz";
50     test!("==")(buffer[], "xyz");
51     test!("is")(buffer[].ptr, old_ptr);
52 
53     buffer[1] = 'b';
54     test!("==")(buffer[], "xbz");
55 
56     buffer[1 .. 2] = "a";
57     test!("==")(buffer[], "xaz");
58 
59     buffer = "abcd";
60     size_t sum = 0;
61     foreach (val; buffer)
62     {
63         if (val == 'c')
64             break;
65         sum += cast(int) val;
66     }
67     test!("==")(sum, 195);
68 
69     sum = 0;
70     foreach (index, val; buffer)
71     {
72         if (val == 'c')
73             break;
74         sum += index;
75     }
76     test!("==")(sum, 1);
77 }
78 
79 unittest
80 {
81     // classes
82 
83     static class C
84     {
85         int x;
86 
87         this ( int x )
88         {
89             this.x = x;
90         }
91 
92         override int opCmp ( Object _rhs )
93         {
94             auto rhs = cast(C) _rhs;
95             return this.x < rhs.x ? 1
96                 : this.x > rhs.x ? -1 : 0;
97         }
98 
99         override equals_t opEquals ( Object rhs )
100         {
101             return this.opCmp(rhs) == 0;
102         }
103     }
104 
105     static C[] createObjects(int[] arr...)
106     {
107         C[] result;
108         foreach (elem; arr)
109             result ~= new C(elem);
110         return result;
111     }
112 
113     auto buffer = createBuffer(createObjects(1, 2, 3));
114     test!("==")(buffer.length, 3);
115 
116     buffer.length = 1;
117     test!("==")(buffer[], createObjects(1));
118 
119     buffer.reset();
120     test!("==")(buffer[], (C[]).init);
121     test!("!is")(buffer[].ptr, null);
122 
123     buffer.reserve(20);
124     auto c1 = new C(1);
125     auto c23 = createObjects(2, 3);
126     testNoAlloc({
127         buffer ~= c1;
128         buffer ~= c23;
129     } ());
130     test!("==")(buffer[], createObjects(1, 2, 3));
131 
132     test!("==")(buffer[1 .. buffer.length], createObjects(2, 3));
133 
134     auto old_ptr = buffer[].ptr;
135     buffer = createObjects(1, 2, 4);
136     test!("==")(buffer[], createObjects(1, 2, 4));
137     test!("is")(buffer[].ptr, old_ptr);
138 
139     buffer[1] = new C(3);
140     test!("==")(buffer[], createObjects(1, 3, 4));
141 
142     buffer[1 .. 2] = createObjects(2);
143     test!("==")(buffer[], createObjects(1, 2, 4));
144 
145     buffer = createObjects(1, 2, 3, 4);
146     size_t sum = 0;
147     foreach (val; buffer)
148     {
149         if (val.x == 3)
150             break;
151         sum += val.x;
152     }
153     test!("==")(sum, 3);
154 
155     sum = 0;
156     foreach (index, val; buffer)
157     {
158         if (val.x == 3)
159             break;
160         sum += index;
161     }
162     test!("==")(sum, 1);
163 }
164 
165 unittest
166 {
167     // void buffer
168 
169     Buffer!(void) buffer;
170     buffer = arrayOf!(ubyte)(1, 2, 3, 4);
171     test!("==")(buffer.length, 4);
172 
173     buffer.length = 1;
174     test!("==")(buffer[], arrayOf!(ubyte)(1));
175 
176     buffer.reset();
177     test!("==")(buffer[], (void[]).init);
178     test!("!is")(buffer[].ptr, null);
179 
180     buffer.reserve(20);
181     auto arr = arrayOf!(ubyte)(1, 2, 3);
182     testNoAlloc({
183         buffer ~= 42;
184         buffer ~= arr;
185     } ());
186     test!("==")(buffer[], arrayOf!(ubyte)(42, 1, 2, 3));
187 
188     test!("==")(buffer[1 .. buffer.length], arrayOf!(ubyte)(1, 2, 3));
189 
190     auto old_ptr = buffer[].ptr;
191     buffer = arrayOf!(ubyte)(1, 2, 3);
192     test!("==")(buffer[], arrayOf!(ubyte)(1, 2, 3));
193     test!("is")(buffer[].ptr, old_ptr);
194 
195     buffer[1] = 42;
196     test!("==")(buffer[], arrayOf!(ubyte)(1, 42, 3));
197 
198     buffer[1 .. 2] = arrayOf!(ubyte)(2);
199     test!("==")(buffer[], arrayOf!(ubyte)(1, 2, 3));
200 
201     buffer = arrayOf!(ubyte)(1, 2, 3, 4);
202     size_t sum = 0;
203     foreach (val; buffer)
204     {
205         if (val == 3)
206             break;
207         sum += val;
208     }
209     test!("==")(sum, 3);
210 
211     sum = 0;
212     foreach (index, val; buffer)
213     {
214         if (val == 3)
215             break;
216         sum += index;
217     }
218     test!("==")(sum, 1);
219 }
220