metacall/testing.cpp

269 lines
5.9 KiB
C++
Raw Normal View History

2011-08-28 15:45:42 +00:00
//
// Copyright (c) 2011 Alex Yatskov
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
2011-09-03 16:05:56 +00:00
#include <stdio.h>
2011-08-28 15:45:42 +00:00
#include "metacall.hpp"
using namespace metacall;
2011-09-03 16:05:56 +00:00
2011-09-05 03:26:55 +00:00
//
2012-02-19 22:34:52 +00:00
// Defines
2011-09-05 03:26:55 +00:00
//
2012-02-20 02:47:09 +00:00
#define TEST_C_STRING
2012-02-19 22:34:52 +00:00
#define TEST_BASIC_STRING
2012-04-09 21:01:57 +00:00
#define TEST_VECTOR
2012-04-15 02:32:30 +00:00
#define TEST_LIST
#define TEST_PAIR
#define TEST_MAP
2012-02-19 22:34:52 +00:00
//
// C string
//
2012-04-06 13:52:10 +00:00
#ifdef TEST_C_STRING
2012-04-09 21:01:57 +00:00
static void testCStringAnsiImp(const char str[]) {
printf("[testCStringAnsiImp]: '%s'\n", str);
2011-09-05 03:26:55 +00:00
}
2012-04-09 21:01:57 +00:00
static void testCStringUnicodeImp(const wchar_t str[]) {
printf("[testCStringUnicodeImp]: '%S'\n", str);
2012-03-04 03:10:36 +00:00
}
static void testCString(Binding* binding, Protocol* protocol) {
2012-04-09 21:01:57 +00:00
binding->bind(FPARAM(testCStringAnsiImp));
binding->bind(FPARAM(testCStringUnicodeImp));
2012-03-04 03:10:36 +00:00
2012-04-06 13:52:10 +00:00
const char* stringsAnsi[] = { "Hello world", "", NULL };
for (int i = 0; i < 3; ++i) {
2012-04-09 21:01:57 +00:00
protocol->invoke("testCStringAnsiImp", stringsAnsi[i]);
2012-03-04 03:10:36 +00:00
}
2012-04-06 13:52:10 +00:00
const wchar_t* stringsUnicode[] = { L"Hello world", L"", NULL };
for (int i = 0; i < 3; ++i) {
2012-04-09 21:01:57 +00:00
protocol->invoke("testCStringUnicodeImp", stringsUnicode[i]);
2012-03-04 03:10:36 +00:00
}
2012-02-19 22:34:52 +00:00
}
2012-04-06 13:52:10 +00:00
#endif
2012-02-19 22:34:52 +00:00
//
// std::basic_string
//
2012-04-06 13:52:10 +00:00
#ifdef TEST_BASIC_STRING
static void testBasicStringAnsiImp(const std::string& string) {
printf("[testBasicStringAnsiImp]: '%s'\n", string.c_str());
2012-02-19 22:34:52 +00:00
}
static void testBasicStringUnicodeImp(const std::wstring& string) {
2012-04-22 13:33:34 +00:00
printf("[testBasicStringUnicodeImp]: '%S'\n", string.c_str());
2012-02-19 22:34:52 +00:00
}
2012-03-04 03:10:36 +00:00
static void testBasicString(Binding* binding, Protocol* protocol) {
2012-04-09 21:01:57 +00:00
binding->bind(FPARAM(testBasicStringAnsiImp));
binding->bind(FPARAM(testBasicStringUnicodeImp));
2012-03-04 03:10:36 +00:00
2012-04-06 13:52:10 +00:00
std::string stringsAnsi[] = { std::string("Hello world"), std::string() };
for (int i = 0; i < 2; ++i) {
2012-04-09 21:01:57 +00:00
protocol->invoke("testBasicStringAnsiImp", stringsAnsi[i]);
2012-03-04 03:10:36 +00:00
}
2012-04-06 13:52:10 +00:00
std::wstring stringsUnicode[] = { std::wstring(L"Hello world"), std::wstring() };
for (int i = 0; i < 2; ++i) {
2012-04-09 21:01:57 +00:00
protocol->invoke("testBasicStringUnicodeImp", stringsUnicode[i]);
2012-03-04 03:10:36 +00:00
}
}
2012-04-06 13:52:10 +00:00
#endif
2012-02-19 22:34:52 +00:00
2012-04-09 21:01:57 +00:00
//
// std::vector
//
#ifdef TEST_VECTOR
static void testVectorImp(const std::vector<float>& vector) {
2012-04-15 02:32:30 +00:00
printf("[testVectorImp]: ");
2012-04-09 21:01:57 +00:00
for (std::vector<float>::const_iterator iter = vector.begin(); iter != vector.end(); ++iter) {
2012-04-09 21:01:57 +00:00
printf("%f ", *iter);
}
printf("\n");
}
static void testVector(Binding* binding, Protocol* protocol) {
binding->bind(FPARAM(testVectorImp));
std::vector<float> vector;
vector.push_back(3.14159f);
vector.push_back(2.71828f);
vector.push_back(1.61803f);
2012-04-09 21:01:57 +00:00
protocol->invoke("testVectorImp", vector);
2012-04-09 21:01:57 +00:00
}
#endif
2011-09-05 03:26:55 +00:00
2012-04-15 02:32:30 +00:00
//
// std::list
//
#ifdef TEST_LIST
static void testListImp(const std::list<float>& list) {
2012-04-15 02:32:30 +00:00
printf("[testListImp]: ");
for (std::list<float>::const_iterator iter = list.begin(); iter != list.end(); ++iter) {
2012-04-15 02:32:30 +00:00
printf("%f ", *iter);
}
printf("\n");
}
static void testList(Binding* binding, Protocol* protocol) {
binding->bind(FPARAM(testListImp));
std::list<float> list;
list.push_back(3.14159f);
list.push_back(2.71828f);
list.push_back(1.61803f);
2012-04-15 02:32:30 +00:00
protocol->invoke("testListImp", list);
2012-04-15 02:32:30 +00:00
}
#endif
//
// std::pair
//
#ifdef TEST_PAIR
static void testPairImp(const std::pair<float, int>& pair) {
printf("[testPairImp]: (%f, %d)\n", pair.first, pair.second);
}
static void testPair(Binding* binding, Protocol* protocol) {
binding->bind(FPARAM(testPairImp));
protocol->invoke("testPairImp", std::make_pair<float, int>(3.14159f, 123));
}
#endif
//
// std::map
//
#ifdef TEST_MAP
static void testMapImp(const std::map<float, int>& map) {
printf("[testMapImp]: ");
for (std::map<float, int>::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
printf("(%f, %d) ", iter->first, iter->second);
}
printf("\n");
}
static void testMap(Binding* binding, Protocol* protocol) {
binding->bind(FPARAM(testMapImp));
std::map<float, int> map;
map[3.14159f] = 123;
map[2.71828f] = 456;
map[1.61803f] = 789;
protocol->invoke("testMapImp", map);
}
#endif
2012-04-15 02:32:30 +00:00
2011-09-03 16:05:56 +00:00
//
2011-09-04 03:34:30 +00:00
// Program entry
2011-09-03 16:05:56 +00:00
//
2012-02-19 15:02:35 +00:00
int main(int, char *[]) {
2011-09-05 03:26:55 +00:00
const int port = 1234;
Server server;
if (!server.start(port)) {
perror("Cannot start server\n");
return 1;
}
Client client;
if (!client.connect("localhost", port)) {
perror("Cannot connect to server\n");
return 1;
}
2012-02-20 02:47:09 +00:00
Binding& binding = server.binding();
Protocol& protocol = client.protocol();
2011-09-03 16:05:56 +00:00
2012-04-06 13:52:10 +00:00
#ifdef TEST_C_STRING
2012-04-22 13:33:34 +00:00
testCString(&binding, &protocol);
2012-04-06 13:52:10 +00:00
#endif
#ifdef TEST_BASIC_STRING
2012-04-22 13:33:34 +00:00
testBasicString(&binding, &protocol);
2012-04-06 13:52:10 +00:00
#endif
2012-02-19 22:34:52 +00:00
2012-04-09 21:01:57 +00:00
#ifdef TEST_VECTOR
2012-04-22 13:33:34 +00:00
testVector(&binding, &protocol);
2012-04-09 21:01:57 +00:00
#endif
2012-04-15 02:32:30 +00:00
#ifdef TEST_LIST
2012-04-22 13:33:34 +00:00
testList(&binding, &protocol);
2012-04-15 02:32:30 +00:00
#endif
#ifdef TEST_PAIR
2012-04-22 13:33:34 +00:00
testPair(&binding, &protocol);
#endif
#ifdef TEST_MAP
2012-04-22 13:33:34 +00:00
testMap(&binding, &protocol);
#endif
2012-04-23 12:24:20 +00:00
while (protocol.pendingTasks()) {
2011-09-05 03:26:55 +00:00
server.advance();
client.advance();
}
2011-09-03 16:05:56 +00:00
2011-08-28 15:45:42 +00:00
return 0;
}