Bumped copyright dates for 2013
[barry.git] / test / data.cc
blob85929b1e2cb0fbb0fa8f149f6ca05005dd481b46
1 ///
2 /// \file data.cc
3 /// Tests for the Data classes
4 ///
6 /*
7 Copyright (C) 2005-2013, 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>
23 #include "libtest.h"
24 #include <iostream>
25 #include <sstream>
26 #include <iomanip>
27 #include <string.h>
28 #include <stdexcept>
29 using namespace std;
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;
38 bool TestData()
40 // typedef std::vector<Data> DataVec;
41 // DataVec array;
42 // if( !LoadDataArray("data/parsed.log", array) ) {
43 // cout << "Can't load file" << endl;
44 // return 1;
45 // }
47 // DataVec::iterator i = array.begin();
48 // Data::PrintAscii(false);
49 // for( ; i != array.end(); i++ ) {
50 // cout << "Endpoint: " << i->GetEndpoint() << endl;
51 // cout << *i;
52 // cout << "\n\n";
53 // }
55 Data d;
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");
65 bool caught = false;
66 std::string msg;
67 try { ed.ReleaseBuffer(1); }
68 catch( std::logic_error &e ) {
69 caught = true;
70 msg = e.what();
72 TEST( caught == true,
73 "ReleaseBuffer() didn't catch GetBuffer() order: " << msg);
75 TEST( ed.GetBuffer() != (unsigned char*) str,
76 "Data::GetBuffer() did not copy on write correctly");
78 d = ed;
79 TEST( Equal(d, ed), "Real copy didn't work");
81 caught = false;
82 try { ed.ReleaseBuffer(1300); }
83 catch( std::logic_error & ) {
84 caught = true;
86 TEST( caught == true, "Data::ReleaseBuffer() did not catch overflow");
88 ed.QuickZap();
89 TEST( ed.GetSize() == 0 && ed.GetData(), "QuickZap did not work");
91 const unsigned char *old = ed.GetBuffer();
92 Data ed2(str, strlen(str));
93 ed = ed2;
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");
106 old = ed.GetData();
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);
113 ed.Prechop(3);
114 TEST( ed.GetData() == old, "Prechop failed");
116 cout << "Examples of Diff() output" << endl;
117 Data one, two;
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;
126 two.GetBuffer();
127 two.ReleaseBuffer(32);
128 cout << Diff(one, two) << endl;
130 return true;
133 NewTest testdata("Data classes", &TestData);