Added unit tests for std::vector
This commit is contained in:
parent
b0a99cf73b
commit
8c7d30420c
66
main.cpp
66
main.cpp
@ -35,6 +35,7 @@ using namespace metacall;
|
|||||||
|
|
||||||
#define TEST_C_STRING
|
#define TEST_C_STRING
|
||||||
#define TEST_BASIC_STRING
|
#define TEST_BASIC_STRING
|
||||||
|
#define TEST_VECTOR
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -43,26 +44,26 @@ using namespace metacall;
|
|||||||
|
|
||||||
#ifdef TEST_C_STRING
|
#ifdef TEST_C_STRING
|
||||||
|
|
||||||
static void testCStringAnsi(const char str[]) {
|
static void testCStringAnsiImp(const char str[]) {
|
||||||
printf("[testCStringAnsi]: '%s'\n", str);
|
printf("[testCStringAnsiImp]: '%s'\n", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void testCStringUnicode(const wchar_t str[]) {
|
static void testCStringUnicodeImp(const wchar_t str[]) {
|
||||||
printf("[testCStringUnicode]: '%S'\n", str);
|
printf("[testCStringUnicodeImp]: '%S'\n", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void testCString(Binding* binding, Protocol* protocol) {
|
static void testCString(Binding* binding, Protocol* protocol) {
|
||||||
binding->bind(FPARAM(testCStringAnsi));
|
binding->bind(FPARAM(testCStringAnsiImp));
|
||||||
binding->bind(FPARAM(testCStringUnicode));
|
binding->bind(FPARAM(testCStringUnicodeImp));
|
||||||
|
|
||||||
const char* stringsAnsi[] = { "Hello world", "", NULL };
|
const char* stringsAnsi[] = { "Hello world", "", NULL };
|
||||||
for (int i = 0; i < 3; ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
protocol->invoke("testCStringAnsi", stringsAnsi[i]);
|
protocol->invoke("testCStringAnsiImp", stringsAnsi[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const wchar_t* stringsUnicode[] = { L"Hello world", L"", NULL };
|
const wchar_t* stringsUnicode[] = { L"Hello world", L"", NULL };
|
||||||
for (int i = 0; i < 3; ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
protocol->invoke("testCStringUnicode", stringsUnicode[i]);
|
protocol->invoke("testCStringUnicodeImp", stringsUnicode[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,32 +76,61 @@ static void testCString(Binding* binding, Protocol* protocol) {
|
|||||||
|
|
||||||
#ifdef TEST_BASIC_STRING
|
#ifdef TEST_BASIC_STRING
|
||||||
|
|
||||||
static void testBasicStringAnsi(const std::string& str) {
|
static void testBasicStringAnsiImp(const std::string& str) {
|
||||||
printf("[testBasicStringAnsi]: '%s'\n", str.c_str());
|
printf("[testBasicStringAnsiImp]: '%s'\n", str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void testBasicStringUnicode(const std::wstring& str) {
|
static void testBasicStringUnicodeImp(const std::wstring& str) {
|
||||||
wprintf(L"[testBasicStringUnicode]: '%S'\n", str.c_str());
|
wprintf(L"[testBasicStringUnicodeImp]: '%S'\n", str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void testBasicString(Binding* binding, Protocol* protocol) {
|
static void testBasicString(Binding* binding, Protocol* protocol) {
|
||||||
binding->bind(FPARAM(testBasicStringAnsi));
|
binding->bind(FPARAM(testBasicStringAnsiImp));
|
||||||
binding->bind(FPARAM(testBasicStringUnicode));
|
binding->bind(FPARAM(testBasicStringUnicodeImp));
|
||||||
|
|
||||||
std::string stringsAnsi[] = { std::string("Hello world"), std::string() };
|
std::string stringsAnsi[] = { std::string("Hello world"), std::string() };
|
||||||
for (int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
protocol->invoke("testBasicStringAnsi", stringsAnsi[i]);
|
protocol->invoke("testBasicStringAnsiImp", stringsAnsi[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring stringsUnicode[] = { std::wstring(L"Hello world"), std::wstring() };
|
std::wstring stringsUnicode[] = { std::wstring(L"Hello world"), std::wstring() };
|
||||||
for (int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
protocol->invoke("testBasicStringUnicode", stringsUnicode[i]);
|
protocol->invoke("testBasicStringUnicodeImp", stringsUnicode[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// std::vector
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifdef TEST_VECTOR
|
||||||
|
|
||||||
|
static void testVectorImp(const std::vector<float>& vec) {
|
||||||
|
printf("[testStdVector]: ");
|
||||||
|
|
||||||
|
for (std::vector<float>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) {
|
||||||
|
printf("%f ", *iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testVector(Binding* binding, Protocol* protocol) {
|
||||||
|
binding->bind(FPARAM(testVectorImp));
|
||||||
|
|
||||||
|
std::vector<float> vec;
|
||||||
|
vec.push_back(3.14159f);
|
||||||
|
vec.push_back(2.71828f);
|
||||||
|
vec.push_back(1.61803f);
|
||||||
|
|
||||||
|
protocol->invoke("testVectorImp", vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Program entry
|
// Program entry
|
||||||
@ -133,6 +163,10 @@ int main(int, char *[]) {
|
|||||||
testBasicString(&binding, &protocol);
|
testBasicString(&binding, &protocol);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef TEST_VECTOR
|
||||||
|
testVector(&binding, &protocol);
|
||||||
|
#endif
|
||||||
|
|
||||||
server.advance();
|
server.advance();
|
||||||
client.advance();
|
client.advance();
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value)
|
|||||||
template <typename T, typename A>
|
template <typename T, typename A>
|
||||||
void serialize(Serializer* serializer, const std::vector<T, A>& value) {
|
void serialize(Serializer* serializer, const std::vector<T, A>& value) {
|
||||||
serializer->write(value.size());
|
serializer->write(value.size());
|
||||||
for (const typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
|
for (typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
|
||||||
serializer->write(*iter);
|
serializer->write(*iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,12 +69,12 @@ template <typename T, typename A>
|
|||||||
bool deserialize(Deserializer* deserializer, std::vector<T, A>* value) {
|
bool deserialize(Deserializer* deserializer, std::vector<T, A>* value) {
|
||||||
value->clear();
|
value->clear();
|
||||||
|
|
||||||
int length = 0;
|
size_t length = 0;
|
||||||
if (!deserializer->read(&length)) {
|
if (!deserializer->read(&length)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < length; ++i) {
|
for (size_t i = 0; i < length; ++i) {
|
||||||
T item = T();
|
T item = T();
|
||||||
if (!deserializer->read(&item)) {
|
if (!deserializer->read(&item)) {
|
||||||
return false;
|
return false;
|
||||||
@ -103,12 +103,12 @@ template <typename T, typename A>
|
|||||||
bool deserialize(Deserializer* deserializer, std::list<T, A>* value) {
|
bool deserialize(Deserializer* deserializer, std::list<T, A>* value) {
|
||||||
value->clear();
|
value->clear();
|
||||||
|
|
||||||
int length = 0;
|
size_t length = 0;
|
||||||
if (!deserializer->read(&length)) {
|
if (!deserializer->read(&length)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < length; ++i) {
|
for (size_t i = 0; i < length; ++i) {
|
||||||
T item = T();
|
T item = T();
|
||||||
if (!deserializer->read(&item)) {
|
if (!deserializer->read(&item)) {
|
||||||
return false;
|
return false;
|
||||||
@ -154,12 +154,12 @@ template <typename K, typename V, typename C, typename A>
|
|||||||
bool deserialize(Deserializer* deserializer, std::map<K, V, C, A>* value) {
|
bool deserialize(Deserializer* deserializer, std::map<K, V, C, A>* value) {
|
||||||
value->clear();
|
value->clear();
|
||||||
|
|
||||||
int length = 0;
|
size_t length = 0;
|
||||||
if (!deserializer->read(&length)) {
|
if (!deserializer->read(&length)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < length; ++i) {
|
for (size_t i = 0; i < length; ++i) {
|
||||||
std::pair<K, V> item;
|
std::pair<K, V> item;
|
||||||
if (!deserializer->read(&item)) {
|
if (!deserializer->read(&item)) {
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user