TsByteArray.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 // Unit-Test TerraLib
21 #include "TsByteArray.h"
22 
24 
26 {
27 }
28 
30 {
31 }
32 
34 {
35 //#ifdef TE_COMPILE_ALL
37  CPPUNIT_ASSERT(b->getData() == 0);
38  CPPUNIT_ASSERT(b->capacity() == 0);
39  CPPUNIT_ASSERT(b->bytesUsed() == 0);
40 
42  CPPUNIT_ASSERT(b1.getData() == 0);
43  CPPUNIT_ASSERT(b1.capacity() == 0);
44  CPPUNIT_ASSERT(b1.bytesUsed() == 0);
45 
46  delete b;
47 
48 //#endif
49 }
50 
52 {
53 //#ifdef TE_COMPILE_ALL
55  CPPUNIT_ASSERT(b->getData() != 0);
56  CPPUNIT_ASSERT(b->capacity() == 100);
57  CPPUNIT_ASSERT(b->bytesUsed() == 0);
58 
59  te::dt::ByteArray b1(100);
60  CPPUNIT_ASSERT(b1.getData() != 0);
61  CPPUNIT_ASSERT(b1.capacity() == 100);
62  CPPUNIT_ASSERT(b1.bytesUsed() == 0);
63 
64  delete b;
65 
66 //#endif
67 }
68 
70 {
71 //#ifdef TE_COMPILE_ALL
72  char *data;
73  data = new char[21];
74  strcpy(data,"12345678901234567890"); //20 bytes
75  //char data[] = "12345678901234567890"; //20 bytes - on delete b - cai se declarar data assim.
76 
77  te::dt::ByteArray *b = new te::dt::ByteArray(data,20) ;
78  CPPUNIT_ASSERT(b->capacity() == 20);
79  CPPUNIT_ASSERT(b->bytesUsed() == 20);
80  char* dataR = b->getData();
81  size_t s = strlen(dataR);
82  CPPUNIT_ASSERT(memcmp(data,dataR,s) == 0);
83  CPPUNIT_ASSERT(strlen(data) == strlen(dataR));
84  CPPUNIT_ASSERT( data == dataR ); //init will take the ownership of the data
85  CPPUNIT_ASSERT( *data == *dataR );
86  CPPUNIT_ASSERT( strncmp(data,dataR,20) == 0);
87 
88  delete b; //it will also delete char *data
89 
90 //#endif
91 }
93 {
94 //#ifdef TE_COMPILE_ALL
95  char *data2;
96  data2 = new char[33];
97  strcpy(data2,"12345678901234567890123456789012"); //32 bytes
98  te::dt::ByteArray *b2 = new te::dt::ByteArray(data2,100,32) ;
99  CPPUNIT_ASSERT(b2->capacity() == 100);
100  CPPUNIT_ASSERT(b2->bytesUsed() == 32);
101 
102  char* dataR2 = b2->getData();
103  size_t s = strlen(dataR2);
104  CPPUNIT_ASSERT(strlen(data2) == strlen(dataR2));
105  CPPUNIT_ASSERT(memcmp(data2,dataR2,s) == 0);
106  CPPUNIT_ASSERT(data2 == dataR2);
107 
108  delete b2;
109 
110 //#endif
111 }
112 
114 {
115 //#ifdef TE_COMPILE_ALL
116  char *data;
117  data = new char[21];
118  strcpy(data,"12345678901234567890"); //20 positions
119 
121  b->take(data,20);
122  CPPUNIT_ASSERT(b->capacity() == 20);
123  char* dataR = b->getData();
124  size_t s = strlen(dataR);
125  CPPUNIT_ASSERT(strlen(data) == strlen(dataR));
126  CPPUNIT_ASSERT(data == dataR );
127  CPPUNIT_ASSERT(strncmp(data,dataR,strlen(data)) == 0);
128  CPPUNIT_ASSERT(memcmp(data,dataR,s) == 0);
129 
130  delete b;
131 
132 //#endif
133 }
134 
136 {
137 //#ifdef TE_COMPILE_ALL
138 
139  char *data;
140  data = new char[33];
141  strcpy(data,"12345678901234567890123456789012"); //32 bytes
142  te::dt::ByteArray *b2 = new te::dt::ByteArray(100) ;
143  CPPUNIT_ASSERT(b2->capacity() == 100);
144  CPPUNIT_ASSERT(b2->bytesUsed() == 0);
145 
146  b2->take(data,100,32);
147  CPPUNIT_ASSERT(b2->bytesUsed() == 32);
148  CPPUNIT_ASSERT(b2->capacity() == 100);
149 
150  char* dataR2 = b2->getData();
151  //size_t s2 = strlen(dataR2);
152  CPPUNIT_ASSERT(data == dataR2);
153  CPPUNIT_ASSERT(strncmp(data,dataR2,strlen(data)) == 0);
154  CPPUNIT_ASSERT(memcmp(data,dataR2,strlen(data)) == 0);
155 
156  delete b2;
157 
158 //#endif
159 }
160 
162 {
163 //#ifdef TE_COMPILE_ALL
164  char *data;
165  data = new char[21];
166  strcpy(data,"12345678901234567890"); //20 positions
167 
169  b->copy(data,20);
170  CPPUNIT_ASSERT(b->capacity() == 20);
171  char* dataR = b->getData();
172  CPPUNIT_ASSERT(data != dataR ); //as the capacity(15) < 20, a new pointer is allocated with 20 bytes
173  CPPUNIT_ASSERT(strncmp(data,dataR,strlen(data)) == 0);
174  CPPUNIT_ASSERT(memcmp(data,dataR,strlen(data)) == 0);
175 
176  delete b;
177 
178 //#endif
179 }
180 
182 {
183 //#ifdef TE_COMPILE_ALL
184  char *data;
185  data = new char[21];
186  strcpy(data,"12345678901234567890"); //20 positions
188  CPPUNIT_ASSERT(b->capacity() == 100);
189  CPPUNIT_ASSERT(b->bytesUsed() == 0);
190 
191  b->copy(data,20);
192  CPPUNIT_ASSERT(b->capacity() == 100);
193  CPPUNIT_ASSERT(b->bytesUsed() == 20);
194 
195  char *data2;
196  data2 = new char[33];
197  strcpy(data2,"12345678901234567890123456789012"); //32 bytes
198 
199  b->copy(data2,32,20); //offset 20
200  //size_t su = b->bytesUsed();
201  //size_t sc = b->capacity();
202 
203  CPPUNIT_ASSERT(b->bytesUsed() == 52);
204  CPPUNIT_ASSERT(b->capacity() == 100);
205 
206  //char* dataR2 = b->getData();
207  //size_t s2 = strlen(dataR2);
208  CPPUNIT_ASSERT(strlen(data2)+strlen(data) == b->bytesUsed());
209 
210  delete b;
211 
212 //#endif
213 }
214 
216 {
217 //#ifdef TE_COMPILE_ALL
218  char* d20 = new char[21];
219  strcpy(d20,"12345678901234567890");
220  size_t s = strlen(d20);
221  te::dt::ByteArray* b = new te::dt::ByteArray(d20,s);
222  CPPUNIT_ASSERT(b->capacity() == 20);
223  CPPUNIT_ASSERT(b->bytesUsed() == 20);
224  b->clear();
225 
226  CPPUNIT_ASSERT(b->bytesUsed() == 0);
227  CPPUNIT_ASSERT(b->capacity() == 0);
228 
229  delete b;
230 
231 //#endif
232 }
233 
235 {
236 //#ifdef TE_COMPILE_ALL
237  char* data = new char[21];
238  strcpy(data,"12345678901234567890");
239  size_t s = strlen(data);
240  te::dt::ByteArray* b = new te::dt::ByteArray(data,s);
241  CPPUNIT_ASSERT(b->capacity() == 20);
242  CPPUNIT_ASSERT(b->bytesUsed() == 20);
243  te::dt::ByteArray bclone = *static_cast<te::dt::ByteArray*>(b->clone());
244  CPPUNIT_ASSERT(bclone.capacity() == 20);
245  CPPUNIT_ASSERT(bclone.bytesUsed() == 20);
246  CPPUNIT_ASSERT(strncmp(bclone.getData(),b->getData(),b->bytesUsed()) == 0);
247  CPPUNIT_ASSERT(b->getData() != bclone.getData());
248  char* dataR = b->getData();
249  CPPUNIT_ASSERT(strncmp(data,dataR,s) == 0);
250  CPPUNIT_ASSERT(memcmp(data,dataR,strlen(data)) == 0);
251 
252  delete b;
253 
254 //#endif
255 }
256 
258 {
259 //#ifdef TE_COMPILE_ALL
260 
261  char *data;
262  data = new char[21];
263  strcpy(data,"12345678901234567890"); //20 positions
264  te::dt::ByteArray baux(data,20);
265  te::dt::ByteArray b(baux);
266  te::dt::ByteArray b1 = b;
267 
268 // Checking Assign Operator
269  //char * datab1 = b1.getData();
270  CPPUNIT_ASSERT(memcmp(b.getData(),baux.getData(),strlen(data)) == 0);
271  CPPUNIT_ASSERT(memcmp(b1.getData(),b.getData(),strlen(data)) == 0);
272  CPPUNIT_ASSERT(strncmp(b1.getData(),b.getData(), strlen(data)) == 0);
273 
274 //#endif
275 }
276 
278 {
279 //#ifdef TE_COMPILE_ALL
280  char* d20 = new char[21];
281  strcpy(d20,"12345678901234567890");
282  std::string hex("3132333435363738393031323334353637383930");
283  strcpy(d20,"12345678901234567890");
284  size_t s = strlen(d20);
285  te::dt::ByteArray* b = new te::dt::ByteArray(d20,s);
286  CPPUNIT_ASSERT(b->capacity() == 20);
287  CPPUNIT_ASSERT(b->bytesUsed() == 20);
288  std::string ss = b->toString();
289 
290  CPPUNIT_ASSERT(strncmp(hex.c_str(),ss.c_str(),s*2) == 0);
291 
292  delete b;
293 
294 //#endif
295 }
297 {
298 //#ifdef TE_COMPILE_ALL
299  char* d20 = new char[21];
300  strcpy(d20,"12345678901234567890");
301  size_t s = strlen(d20);
302  te::dt::ByteArray* b = new te::dt::ByteArray(d20,s);
303  CPPUNIT_ASSERT(b->getTypeCode() == te::dt::BYTE_ARRAY_TYPE);
304 
305  delete b;
306 
307 //#endif
308 }
309 
311 {
312 //#ifdef TE_COMPILE_ALL
314  b->setBytesUsed(100);
315  CPPUNIT_ASSERT(b->bytesUsed() == 100);
316  delete b;
317 //#endif
318 }
319 
321 {
322 //#ifdef TE_COMPILE_ALL
323 
325  int i = 3 ;
326  te::dt::ByteArray b2 = operator <<( b1, i);
327  char* res = b2.getData();
328  std::string ss = b2.toString();
329  //int x = *res;
330  CPPUNIT_ASSERT(*res == i );
331 
332  b1.clear(); b2.clear();
333  unsigned int ui = 127 ; // 2147483647 ;
334  te::dt::ByteArray bui = operator <<( b1, ui);
335  char* res1 = bui.getData();
336  ss = bui.toString();
337  //unsigned int xx = *res1;
338  CPPUNIT_ASSERT(*res1 == ui );
339 
340 
341  b1.clear(); b2.clear();
342  float f = 3.55f;
343  b2 = operator <<( b1, f);
344  res = b2.getData();
345  ss = b2.toString();
346  //float ff = *res;
347  CPPUNIT_ASSERT(*res == f );
348 
349  b1.clear(); b2.clear();
350  double d = 3.55;
351  b2 = operator <<( b1, f);
352  res = b2.getData();
353  ss = b2.toString();
354  //double dd = *res;
355  CPPUNIT_ASSERT(*res == d );
356 
357 //#endif
358 }
void tcSetBytesUsed()
Test Case: Set bytes used by ByteArray.
void tcGetTypeCode()
Test Case: Get Type code of ByteArray.
void tcTake1()
Test Case: It takes the ownership of the external data buffer.
void tcInit1()
Test Case: Creates a new byte array initializing it with data less than capacity. ...
Definition: TsByteArray.cpp:92
void tcOperator()
Test Case: Test operator << (writes simple data types (int,double, float) to bytearray.
void tcClear()
Test Case: Clear the byte array.
std::size_t bytesUsed() const
It returns the number of used bytes in the internal buffer.
Definition: ByteArray.cpp:169
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
void tcByteArrayConstructor()
Test Case: Copy Constructor with size bigger than data.
void tearDown()
Definition: TsByteArray.cpp:29
std::string toString() const
It returns the byte array in an string notation.
Definition: ByteArray.cpp:192
Test suite for the ByteArray class.
int b
Definition: TsRtree.cpp:32
Test suite for the ByteArray Class.
Definition: TsByteArray.h:60
char * getData() const
It returns the data array.
Definition: ByteArray.cpp:105
AbstractData * clone() const
It creates a new clone of the byte array.
Definition: ByteArray.cpp:187
void tcInit()
Test Case: Creates a new byte array initializing it with data using all capacity. ...
Definition: TsByteArray.cpp:69
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
void tcClone()
Test Case: Clone ByteArray.
void tcCopy1()
Test Case: It copies the data from the given pointer to the byte array and will expand the internal b...
CPPUNIT_TEST_SUITE_REGISTRATION(TsByteArray)
std::size_t capacity() const
It returns the size of the internal buffer.
Definition: ByteArray.cpp:164
int getTypeCode() const
The type code for byte array data: BYTE_ARRAY_TYPE.
Definition: ByteArray.h:198
void tcCopy()
Test Case: It copies the data from the given pointer to the byte array.
void tcByteArraySize()
Test Case: Creates a new byte array and allocate size bytes.
Definition: TsByteArray.cpp:51
void tcByteArray()
Test Case: Constructs an empty byte array.
Definition: TsByteArray.cpp:33
void setUp()
Definition: TsByteArray.cpp:25
void copy(char *data, std::size_t size)
It copies the data from the given pointer to the byte array.
Definition: ByteArray.cpp:128
void setBytesUsed(std::size_t size)
It sets the number of used bytes in the internal buffer.
Definition: ByteArray.cpp:174
void clear()
It clears the byte array.
Definition: ByteArray.cpp:179
void tcTake()
Test Case: It takes the ownership of the external data buffer.
A class for representing binary data.
Definition: ByteArray.h:51
void take(char *data, std::size_t size)
It takes the ownership of the external data buffer.
Definition: ByteArray.cpp:110
void tcToString()
Test Case: It return an string representation of ByteArray.