1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#include "stdtest.h"
|
7 |
|
|
|
8 |
|
|
void ObjectSerialization (void)
|
9 |
|
|
{
|
10 |
|
|
#define RW(stream) rws[stream.pos() == expect]
|
11 |
|
|
const void* pBufC = NULL;
|
12 |
|
|
void* pBuf = NULL;
|
13 |
|
|
memblock buffer;
|
14 |
|
|
string testString ("TestString");
|
15 |
|
|
const string* pStrC = NULL;
|
16 |
|
|
string* pStr = NULL;
|
17 |
|
|
vector<uint16_t> tv (7);
|
18 |
|
|
static const char* rws[2] = { "wrong", "right" };
|
19 |
|
|
|
20 |
|
|
const size_t bufSize = stream_size_of(pBufC) +
|
21 |
|
|
stream_size_of(pBuf) +
|
22 |
|
|
Align(stream_size_of(testString)) +
|
23 |
|
|
stream_size_of(pStrC) +
|
24 |
|
|
stream_size_of(pStr) +
|
25 |
|
|
stream_size_of(tv);
|
26 |
|
|
buffer.resize (bufSize);
|
27 |
|
|
pBufC = buffer.cdata();
|
28 |
|
|
pBuf = buffer.data();
|
29 |
|
|
|
30 |
|
|
uoff_t expect = 0;
|
31 |
|
|
ostream os (buffer);
|
32 |
|
|
os << pBufC; expect += stream_size_of(pBufC);
|
33 |
|
|
cout << "Write const void*, pos is " << RW(os) << endl;
|
34 |
|
|
os << pBuf; expect += stream_size_of(pBuf);
|
35 |
|
|
cout << "Write void*, pos is " << RW(os) << endl;
|
36 |
|
|
os << testString; expect += stream_size_of(testString);
|
37 |
|
|
cout << "Write string, pos is " << RW(os) << endl;
|
38 |
|
|
os.align(); expect = Align (expect);
|
39 |
|
|
os << const_cast<const string*>(&testString); expect += stream_size_of(&testString);
|
40 |
|
|
cout << "Write const string*, pos is " << RW(os) << endl;
|
41 |
|
|
os << &testString; expect += stream_size_of(&testString);
|
42 |
|
|
cout << "Write string*, pos is " << RW(os) << endl;
|
43 |
|
|
os << tv; expect += stream_size_of(tv);
|
44 |
|
|
cout << "Write vector<uint16_t>(7), pos is " << RW(os) << endl;
|
45 |
|
|
if (os.pos() != bufSize)
|
46 |
|
|
cout << "Incorrect number of bytes written: " << os.pos() << " of " << bufSize << endl;
|
47 |
|
|
|
48 |
|
|
istream is (buffer);
|
49 |
|
|
expect = 0;
|
50 |
|
|
is >> pBufC;
|
51 |
|
|
expect += stream_size_of(pBufC);
|
52 |
|
|
cout << "Read const void*, pos is " << RW(is);
|
53 |
|
|
cout << ", value is " << rws[pBufC == buffer.cdata()] << endl;
|
54 |
|
|
is >> pBuf;
|
55 |
|
|
expect += stream_size_of(pBuf);
|
56 |
|
|
cout << "Read void*, pos is " << RW(is);
|
57 |
|
|
cout << ", value is " << rws[pBuf == buffer.cdata()] << endl;
|
58 |
|
|
testString.clear();
|
59 |
|
|
is >> testString;
|
60 |
|
|
expect += stream_size_of(testString);
|
61 |
|
|
cout << "Read string, pos is " << RW(is) << ", value is " << testString << endl;
|
62 |
|
|
is.align();
|
63 |
|
|
expect = Align (expect);
|
64 |
|
|
is >> pStrC;
|
65 |
|
|
expect += stream_size_of(pStrC);
|
66 |
|
|
cout << "Read const string*, pos is " << RW(is);
|
67 |
|
|
cout << ", value is " << rws[pStrC == &testString] << endl;
|
68 |
|
|
is >> pStr;
|
69 |
|
|
expect += stream_size_of(pStr);
|
70 |
|
|
cout << "Read string*, pos is " << RW(is);
|
71 |
|
|
cout << ", value is " << rws[pStr == &testString] << endl;
|
72 |
|
|
vector<uint16_t> rv;
|
73 |
|
|
is >> rv;
|
74 |
|
|
expect += stream_size_of(rv);
|
75 |
|
|
cout << "Read vector<uint16_t>(" << rv.size() << "), pos is " << RW(is);
|
76 |
|
|
cout << ", value is " << rws[rv == tv] << endl;
|
77 |
|
|
if (is.pos() != bufSize)
|
78 |
|
|
cout << "Incorrect number of bytes read: " << is.pos() << " of " << bufSize << endl;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
StdBvtMain (ObjectSerialization)
|