-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest_CSteream.cpp
205 lines (161 loc) · 4.87 KB
/
test_CSteream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @file test_CStream.cpp
* @brief
*
* @author Yonhgwhan, Roh ([email protected])
* @date 2019/10/05 09:51 created.
* @copyright All rights reserved by Yonghwan, Roh.
**/
#include "stdafx.h"
#include "_MyLib/src/CStream.h"
bool test_cstream()
{
_mem_check_begin
{
CMemoryStream strm;
// 스트림이 아직 할당되지 않음
_ASSERTE(!strm.SetPos(0));
_ASSERTE(true == strm.Reserve(1024));
_ASSERTE(strm.GetCapacity() >= 1024);
_ASSERTE(0 == strm.GetPos());
_ASSERTE(strm.GetSize() == strm.GetPos());
//
// 문자열 쓰기
//
const char* test_string = "0123456789";
size_t size = strlen(test_string) * sizeof(char);
_ASSERTE(strm.WriteInt<size_t>(size));
_ASSERTE(size == strm.WriteToStream(test_string, size));
size_t pos = sizeof(size) + size;
_ASSERTE(pos == strm.GetPos());
_ASSERTE(strm.GetSize() == strm.GetPos());
_ASSERTE(strm.GetCapacity() >= pos);
//
// 정수형 쓰기/읽기
//
_ASSERTE(strm.WriteInt<uint8_t> (0x11));
_ASSERTE(strm.WriteInt<uint16_t>(0x1122));
_ASSERTE(strm.WriteInt<uint32_t>(0x11223344));
_ASSERTE(strm.WriteInt<uint64_t>(0x1122334411223344));
// 스트림 영역 밖으로 이동시 실패
_ASSERTE(false == strm.SetPos(strm.GetPos() + 1));
//
// 읽기 & 검증 (참조, 읽기)
//
{
_ASSERTE(strm.SetPos(0));
size = strm.ReadInt<size_t>();
// 스트림 문자열 참조
pos = strm.GetPos();
const char* p = nullptr;
_ASSERTE(size >= strm.RefFromStream(p, size));
_ASSERTE(strm.GetPos() > pos);
std::string s_ref((char*)p, size);
log_info "ref from stream=%s", s_ref.c_str() log_end;
_ASSERTE(0x11 == strm.ReadInt<uint8_t>());
_ASSERTE(0x1122 == strm.ReadInt<uint16_t>());
_ASSERTE(0x11223344 == strm.ReadInt<uint32_t>());
_ASSERTE(0x1122334411223344 == strm.ReadInt<uint64_t>());
}
//
// 읽기 & 검증 (복사, 읽기)
//
{
_ASSERTE(strm.SetPos(0));
size = strm.ReadInt<size_t>();
auto ptr = std::make_unique<char[]>(size);
_ASSERTE(ptr);
_ASSERTE(size >= strm.ReadFromStream(ptr.get(), size));
std::string s(ptr.get(), size);
log_info "read from stream=%s", s.c_str() log_end;
_ASSERTE(0x11 == strm.ReadInt<uint8_t>());
_ASSERTE(0x1122 == strm.ReadInt<uint16_t>());
_ASSERTE(0x11223344 == strm.ReadInt<uint32_t>());
_ASSERTE(0x1122334411223344 == strm.ReadInt<uint64_t>());
}
strm.ClearStream();
_ASSERTE(0 == strm.GetPos());
_ASSERTE(0 == strm.GetSize());
_ASSERTE(0 == strm.GetCapacity());
_ASSERTE(nullptr == strm.GetMemory());
}
_mem_check_end;
return true;
}
bool test_cstream_read_only()
{
_mem_check_begin
{
CMemoryStream strm(1024, (const char* const)GetModuleHandleW(nullptr));
// 스트림이 할당되었으므로 SetPos 가능
_ASSERTE(strm.SetPos(1024));
_ASSERTE(!strm.SetPos(1024 + 1));
_ASSERTE(strm.SetPos(0));
_ASSERTE(true != strm.Reserve(1024));
_ASSERTE(strm.GetCapacity() == 1024);
_ASSERTE(0 == strm.GetPos());
_ASSERTE(strm.GetSize() == 1024);
//
// 문자열 쓰기 실패
//
const char* test_string = "0123456789";
size_t size = strlen(test_string) * sizeof(char);
_ASSERTE(!strm.WriteInt<size_t>(size));
_ASSERTE(size != strm.WriteToStream(test_string, size));
//
// 정수형 쓰기/읽기 실패
//
_ASSERTE(!strm.WriteInt<uint8_t>(0x11));
_ASSERTE(!strm.WriteInt<uint16_t>(0x1122));
_ASSERTE(!strm.WriteInt<uint32_t>(0x11223344));
_ASSERTE(!strm.WriteInt<uint64_t>(0x1122334411223344));
//
// 읽기 & 검증
//
_ASSERTE(strm.SetPos(0));
_ASSERTE(strm.ReadInt<uint8_t>() > 0);
_ASSERTE(strm.ReadInt<uint16_t>() > 0);
_ASSERTE(strm.ReadInt<uint32_t>() > 0);
_ASSERTE(strm.ReadInt<uint64_t>() > 0);
strm.ClearStream();
_ASSERTE(0 == strm.GetPos());
_ASSERTE(0 == strm.GetSize());
_ASSERTE(0 == strm.GetCapacity());
_ASSERTE(nullptr == strm.GetMemory());
}
_mem_check_end;
return true;
}
bool test_cstream_read_write_string()
{
std::string test_str = "0123456789";
std::wstring test_wstr = L"0123456789";
//log_info "%zu, %zu", test_str.size(), test_wstr.size() log_end;
_mem_check_begin
{
CMemoryStream strm;
//
_ASSERTE(true == strm.WriteString(_null_stringa));
_ASSERTE(true == strm.WriteWstring(_null_stringw));
_ASSERTE(true == strm.WriteString(test_str));
_ASSERTE(true == strm.WriteWstring(test_wstr));
_ASSERTE(true == strm.WriteString(test_str.c_str()));
_ASSERTE(true == strm.WriteWstring(test_wstr.c_str()));
_ASSERTE(strm.SetPos(0));
//
std::string s = strm.ReadString();
std::wstring ws = strm.ReadWstring();
_ASSERTE(true == s.empty());
_ASSERTE(true == ws.empty());
s = strm.ReadString();
ws = strm.ReadWstring();
_ASSERTE(0 == s.compare(test_str));
_ASSERTE(0 == ws.compare(test_wstr));
s = strm.ReadString();
ws = strm.ReadWstring();
_ASSERTE(0 == s.compare(test_str));
_ASSERTE(0 == ws.compare(test_wstr));
}
_mem_check_end;
return true;
}