3 /// Tests for the Data classes
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <barry/data.h>
30 using namespace Barry
;
32 static bool Equal(const Data
&d1
, const Data
&d2
)
34 return d1
.GetSize() == d2
.GetSize() &&
35 memcmp(d1
.GetData(), d2
.GetData(), d1
.GetSize()) == 0;
40 // typedef std::vector<Data> DataVec;
42 // if( !LoadDataArray("data/parsed.log", array) ) {
43 // cout << "Can't load file" << endl;
47 // DataVec::iterator i = array.begin();
48 // Data::PrintAscii(false);
49 // for( ; i != array.end(); i++ ) {
50 // cout << "Endpoint: " << i->GetEndpoint() << endl;
56 TEST( d
.GetBufSize() == 0x4000, "Unexpected default buffer size");
58 const char *str
= "hello world";
59 Data
ed(str
, strlen(str
));
60 TEST( ed
.GetSize() == strlen(str
),
61 "Incorrect GetSize() on external data");
62 TEST( ed
.GetData() == (unsigned char*) str
,
63 "GetData() external data pointer not the same");
67 try { ed
.ReleaseBuffer(1); }
68 catch( std::logic_error
&e
) {
73 "ReleaseBuffer() didn't catch GetBuffer() order: " << msg
);
75 TEST( ed
.GetBuffer() != (unsigned char*) str
,
76 "Data::GetBuffer() did not copy on write correctly");
79 TEST( Equal(d
, ed
), "Real copy didn't work");
82 try { ed
.ReleaseBuffer(1300); }
83 catch( std::logic_error
& ) {
86 TEST( caught
== true, "Data::ReleaseBuffer() did not catch overflow");
89 TEST( ed
.GetSize() == 0 && ed
.GetData(), "QuickZap did not work");
91 const unsigned char *old
= ed
.GetBuffer();
92 Data
ed2(str
, strlen(str
));
94 TEST( ed
.GetData() == (unsigned char* )str
,
95 "operator=() did not recognize easy external data copy");
96 TEST( ed
.GetBuffer() == old
,
97 "GetBuffer() did not recover existing memBlock");
98 TEST( Equal(ed
, ed2
), "Data not equal!\n" << ed
<< endl
<< ed2
);
100 const char *str2
= " and goodbye";
101 const char *str3
= "hello world and goodbye";
102 ed2
.Append(str2
, strlen(str2
));
103 Data
ed3(str3
, strlen(str3
));
104 TEST( Equal(ed2
, ed3
), "Append failed");
107 size_t old_size
= ed
.GetSize();
108 ed
.Prepend("pre", 3);
109 TEST( (ed
.GetData() + 3) == old
,
110 "Prepend buffer failed: "
111 << (void*)old
<< ":\n" << Data(old
, old_size
)
112 << (void*)ed
.GetData() << ":\n" << ed
);
114 TEST( ed
.GetData() == old
, "Prechop failed");
116 cout
<< "Examples of Diff() output" << endl
;
118 one
.GetBuffer()[0] = 0x01;
119 one
.ReleaseBuffer(1);
120 two
.GetBuffer()[0] = 0x02;
121 two
.ReleaseBuffer(2);
123 cout
<< Diff(one
, two
) << endl
;
124 cout
<< Diff(two
, one
) << endl
;
127 two
.ReleaseBuffer(32);
128 cout
<< Diff(one
, two
) << endl
;
133 NewTest
testdata("Data classes", &TestData
);