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_BASIC_STRING
|
||||
#define TEST_VECTOR
|
||||
|
||||
|
||||
//
|
||||
@ -43,26 +44,26 @@ using namespace metacall;
|
||||
|
||||
#ifdef TEST_C_STRING
|
||||
|
||||
static void testCStringAnsi(const char str[]) {
|
||||
printf("[testCStringAnsi]: '%s'\n", str);
|
||||
static void testCStringAnsiImp(const char str[]) {
|
||||
printf("[testCStringAnsiImp]: '%s'\n", str);
|
||||
}
|
||||
|
||||
static void testCStringUnicode(const wchar_t str[]) {
|
||||
printf("[testCStringUnicode]: '%S'\n", str);
|
||||
static void testCStringUnicodeImp(const wchar_t str[]) {
|
||||
printf("[testCStringUnicodeImp]: '%S'\n", str);
|
||||
}
|
||||
|
||||
static void testCString(Binding* binding, Protocol* protocol) {
|
||||
binding->bind(FPARAM(testCStringAnsi));
|
||||
binding->bind(FPARAM(testCStringUnicode));
|
||||
binding->bind(FPARAM(testCStringAnsiImp));
|
||||
binding->bind(FPARAM(testCStringUnicodeImp));
|
||||
|
||||
const char* stringsAnsi[] = { "Hello world", "", NULL };
|
||||
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 };
|
||||
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
|
||||
|
||||
static void testBasicStringAnsi(const std::string& str) {
|
||||
printf("[testBasicStringAnsi]: '%s'\n", str.c_str());
|
||||
static void testBasicStringAnsiImp(const std::string& str) {
|
||||
printf("[testBasicStringAnsiImp]: '%s'\n", str.c_str());
|
||||
}
|
||||
|
||||
static void testBasicStringUnicode(const std::wstring& str) {
|
||||
wprintf(L"[testBasicStringUnicode]: '%S'\n", str.c_str());
|
||||
static void testBasicStringUnicodeImp(const std::wstring& str) {
|
||||
wprintf(L"[testBasicStringUnicodeImp]: '%S'\n", str.c_str());
|
||||
}
|
||||
|
||||
static void testBasicString(Binding* binding, Protocol* protocol) {
|
||||
binding->bind(FPARAM(testBasicStringAnsi));
|
||||
binding->bind(FPARAM(testBasicStringUnicode));
|
||||
binding->bind(FPARAM(testBasicStringAnsiImp));
|
||||
binding->bind(FPARAM(testBasicStringUnicodeImp));
|
||||
|
||||
std::string stringsAnsi[] = { std::string("Hello world"), std::string() };
|
||||
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() };
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
protocol->invoke("testBasicStringUnicode", stringsUnicode[i]);
|
||||
protocol->invoke("testBasicStringUnicodeImp", stringsUnicode[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@ -133,6 +163,10 @@ int main(int, char *[]) {
|
||||
testBasicString(&binding, &protocol);
|
||||
#endif
|
||||
|
||||
#ifdef TEST_VECTOR
|
||||
testVector(&binding, &protocol);
|
||||
#endif
|
||||
|
||||
server.advance();
|
||||
client.advance();
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value)
|
||||
template <typename T, typename A>
|
||||
void serialize(Serializer* serializer, const std::vector<T, A>& value) {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -69,12 +69,12 @@ template <typename T, typename A>
|
||||
bool deserialize(Deserializer* deserializer, std::vector<T, A>* value) {
|
||||
value->clear();
|
||||
|
||||
int length = 0;
|
||||
size_t length = 0;
|
||||
if (!deserializer->read(&length)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; ++i) {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
T item = T();
|
||||
if (!deserializer->read(&item)) {
|
||||
return false;
|
||||
@ -103,12 +103,12 @@ template <typename T, typename A>
|
||||
bool deserialize(Deserializer* deserializer, std::list<T, A>* value) {
|
||||
value->clear();
|
||||
|
||||
int length = 0;
|
||||
size_t length = 0;
|
||||
if (!deserializer->read(&length)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; ++i) {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
T item = T();
|
||||
if (!deserializer->read(&item)) {
|
||||
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) {
|
||||
value->clear();
|
||||
|
||||
int length = 0;
|
||||
size_t length = 0;
|
||||
if (!deserializer->read(&length)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < length; ++i) {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
std::pair<K, V> item;
|
||||
if (!deserializer->read(&item)) {
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user