05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX

This commit is contained in:
peri4
2011-12-05 23:51:02 +03:00
parent e25553b97b
commit 74b4173c4c
43 changed files with 1495 additions and 694 deletions

334
main.cpp
View File

@@ -1,150 +1,232 @@
#include <pip.h>
#include "pip.h"
class Prot: public PIProtocol {
class ThreadTest: public PIThread {
public:
Prot(): PIProtocol() {
setName("test");
ba.resize(1024);
setReceiverAddress("127.0.0.1", 3003, true);
setReceiverData(ba.data(), ba.size_s());
setSenderAddress("127.0.0.2", 3003, true);
setSenderData(ba.data(), ba.size_s());
setExpectedFrequency(100);
//setSenderFrequency();
//startReceive(100);
startSend(10);
};
PIByteArray ba;
ThreadTest(): PIThread() {cnt = 0; b = r = e = false;}
int cnt;
bool b, r, e;
private:
void begin() {b = true; cout << " thread begin\n";}
void run() {r = true; cout << " thread run " << cnt++ << endl; if (cnt == 10) stop();}
void end() {e = true; cout << " thread end\n";}
};
class MProt: public PIMultiProtocol {
PIMutex mutex_;
bool m = false;
int cnt = 0, gm = 0;
class MutexTest: public PIThread {
public:
MProt() {cor = incor = 0;}
int cor, incor;
virtual void received(PIProtocol * prot, bool corrected, char * data, int size) {if (corrected) cor++; else incor++; if (prot->name() == "test" && corrected) prot->send();}
MutexTest(): PIThread() {;}
private:
void run() {mutex_.lock(); if (m && cnt > 1) gm++; m = true; cout << " " << flush; if (cnt++ >= 128) stop(); msleep(20); mutex_.unlock();}
};
struct BitsStruct {
uchar b1: 1;
uchar b2: 2;
uchar b4: 4;
class MutexTest2: public PIThread {
public:
MutexTest2(): PIThread() {;}
private:
void run() {mutex_.lock(); if (!m && cnt > 1) gm++; m = false; cout << "|" << flush; if (cnt++ >= 128) stop(); msleep(20); mutex_.unlock();}
};
int i = 1;
BitsStruct bits;
void keyFunc(char key, void * );
PIConsole c(false, keyFunc);
void keyFunc(char key, void * ) {
switch (key) {
case '-': i--; break;
case '+': i++; break;
case '[': bits.b1--; break;
case ']': bits.b1++; break;
case ';': bits.b2--; break;
case '\'': bits.b2++; break;
case ',': bits.b4--; break;
case '.': bits.b4++; break;
case 'i': i = c.getInt(1, 2); break;
}
};
void signalFunc(PISignals::Signal signal) {
if (signal == PISignals::Interrupt) cout << "Ctrl+C pressed" << endl;
if (signal != PISignals::Interrupt) return;
cout << "Ctrl+C pressed, exiting ..." << endl;
exit(0);
};
void timerEvent(void*, int delim) {
cout << "tick " << delim << endl;
bool t = false;
void timerEvent(void * data, int delim) {
t = true;
cout << " tick from constuctor, delimiter = " << delim << ", data = " << data << endl;
};
PITimer timer(timerEvent);
bool
t2 = false;
void timerEvent2(void * data, int delim) {
t2 = true;
cout << " tick from delimiter " << delim << ", data = " << data << endl;
};
void timerEvent2(void*, int delim) {
cout << "tick2 " << delim << endl;
class ObjectTest: public PIObject {
public:
ObjectTest(const PIString & name = PIString()): PIObject(name) {h = hi = ht = false;}
EVENT_HANDLER(ObjectTest, void, handler) {h = true; cout << " handler in \"" << name() << "\"" << endl;}
EVENT_HANDLER2(ObjectTest, void, handler_i_s, int, i, PIString, s) {hi = true; cout << " handler_i_s in \"" << name() << "\", i = " << i << ", s = \"" << s << "\"" << endl;}
EVENT_HANDLER2(ObjectTest, void, handler_timeout, void * , data, int, delim) {ht = true; cout << " handler_timeout in \"" << name() << "\", data = " << data << ", delim = " << delim << endl;}
bool h, hi, ht;
};
class ObjectTest2: public PIObject {
public:
ObjectTest2(const PIString & name = PIString()): PIObject(name) {;}
void raise0(const PIString & e) {cout << " event \"" << e << "\" from \"" << name() << "\"" << endl; raiseEvent(this, e);}
void raise2(const PIString & e, int i, const PIString & s) {cout << " event \"" << e << "\" from \"" << name() << "\"" << endl; raiseEvent<int, PIString>(this, e, i, s);}
};
class CA: public PIObject {
public:
CA(const PIString & n): PIObject(n) {;}
EVENT_HANDLER(CA, void, handler_ca) {a = true; cout << " handler CA" << endl;}
bool a;
};
class CB: public CA {
public:
CB(const PIString & n): CA(n) {;}
EVENT_HANDLER(CB, void, handler_cb) {b = true; cout << " handler CB" << endl;}
bool b;
};
class CC: public CB {
public:
CC(const PIString & n): CB(n) {;}
EVENT_HANDLER(CC, void, handler_cc) {c = true; cout << " handler CC" << endl;}
bool c;
};
class CD: public CC {
public:
CD(const PIString & n): CC(n) {;}
EVENT_HANDLER(CD, void, handler_cd) {d = true; cout << " handler CD" << endl;}
bool d;
};
int main(int argc, char * argv[]) {
/*PICLI cli(argc, argv);
cli.addArgument("debug");
cli.addArgument("value", "V", "Val", true);
if (cli.hasArgument("debug"))
cout << "has debug" << endl;
if (cli.hasArgument("value"))
cout << "value = " << cli.argumentValue("value") << endl;*/
/*
timer.start(100);
timer.addDelimiter(2, timerEvent2);
PIFile f;
f.open("_test", PIFile::ReadWrite || PIFile::New);
if (f.isEmpty()) f.remove();
return 0;
bool r_string = true, r_thread = true, r_mutex = true, r_timer = true, r_file = true, r_eval = true, r_event = true;
bool succ = true;
cout << "== PIP test program ==" << endl;
cout << "== Built with PIP " << PIPVersion() << " ==" << endl << endl;
cout << "== String test ==" << endl;
PIString string("test string");
cout << " \"test string\" -> \"" << string << "\"" << endl;
if (string.length() != 11) succ = r_string = false;
cout << " to char * = \"" << string.data() << "\"" << endl;
cout << " to std::string = \"" << string.stdString() << "\"" << endl;
if (string.stdString().length() != 11) succ = r_string = false;
cout << " to std::wstring = \"" << string.stdWString() << "\"" << endl;
if (string.stdWString().length() != 11) succ = r_string = false;
if (succ) cout << " convertions success" << endl;
else cout << " convertions fail" << endl;
succ = true;
string = PIString("αβγ°℃");
cout << " \"αβγ°℃\" -> \"" << string << "\"" << endl;
if (string.length() != 5) succ = r_string = false;
cout << " to char * = \"" << string.data() << "\"" << endl;
cout << " to std::string = \"" << string.stdString() << "\"" << endl;
if (string.stdString().length() != 10) succ = r_string = false;
cout << " to std::wstring = \"" << string.stdWString() << "\"" << endl;
if (string.stdWString().length() != 5) succ = r_string = false;
if (succ) cout << " complex convertions success" << endl;
else cout << " complex convertions fail" << endl;
if (r_string) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
cout << endl << "== Thread test ==" << endl;
ThreadTest thread;
thread.start(100);
msleep(10);
thread.waitForFinish();
r_thread = thread.b && thread.r && thread.e && thread.cnt == 10;
if (r_thread) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
cout << endl << "== Mutex test ==" << endl << " ";
MutexTest thread_m;
MutexTest2 thread_m2;
thread_m.start();
thread_m2.start();
thread_m.waitForFinish();
thread_m2.waitForFinish();
cout << endl;
r_mutex = gm < 5;
if (r_mutex) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
cout << endl << "== Timer test ==" << endl;
PITimer timer(timerEvent, (void*)255);
timer.addDelimiter(2);
timer.addDelimiter(5, timerEvent2);
FOREVER_WAIT
*/
/*PISignals::setSlot(signalFunc);
PISignals::grabSignals(PISignals::Interrupt);
FOREVER_WAIT*/
timer.start(100.f);
msleep(1005);
timer.stop();
r_timer = t && t2;
if (r_timer) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
/*PIString ip;
int port;
PIConfig conf("protocols.conf~");
ip = conf.getValue("mcp1.receiver.ip");
port = conf.getValue("mcp1.receiver.port");
cout << ip << ":" << port << endl;*/
cout << endl << "== File test ==" << endl;
PIFile file(" file_test", PIFile::New | PIFile::ReadWrite);
cout << " file \"" << file.path() << "\" is ";
if (!file.isOpened()) cout << "not ";
cout << "opened" << endl;
file << "test string";
cout << " write " << file.pos() << " bytes" << endl;
if (file.pos() != 11) r_file = false;
PIByteArray ba = file.readAll();
if (ba.size() != 11) r_file = false;
cout << " read " << ba.size() << " bytes: \"" << PIString(ba) << '\"' << endl;
file.remove();
if (r_file) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
cout << endl << "== Evaluator test ==" << endl;
PIEvaluator evaluator;
evaluator.setVariable("x", complexd(2., 1.));
PIString expression("2x^2 + i");
evaluator.check(expression);
cout << " expression = \"" << expression << '\"' << endl;
cout << " recognized = \"" << evaluator.expression() << '\"' << endl;
cout << " error = \"" << evaluator.error() << '\"' << endl;
cout << " \"x\" = " << evaluator.content.variable("x").value << endl;
cout << " result = " << evaluator.evaluate() << endl;
r_eval = round(evaluator.lastResult()) == complexd(9., 12.);
if (r_eval) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
c.addString("PIConsole example");
c.addEmptyLine();
cout << endl << "== Event test ==" << endl;
ObjectTest object("obj");
ObjectTest2 object2("obj2");
PITimer timer2;
timer2.setData((void * )128);
timer2.addDelimiter(2);
timer2.addDelimiter(5);
CONNECT0(void, &object2, event0, &object, handler);
CONNECT2(void, int, PIString, &object2, event2, &object, handler_i_s);
CONNECT2(void, void * , int, &timer2, timeout, &object, handler_timeout);
object2.raise0("event0");
object2.raise2("event2", 123, "string");
timer2.start(100.f);
msleep(505);
timer2.stop();
CA ca("cd");
CD cd("cd");
CONNECT(void, &ca, event_ca, &cd, handler_cd);
CONNECT(void, &ca, event_ca, &cd, handler_cc);
CONNECT(void, &ca, event_ca, &cd, handler_cb);
CONNECT(void, &ca, event_ca, &cd, handler_ca);
PIObject::raiseEvent(&ca, "event_ca");
r_event = object.h && object.hi && object.ht && cd.a && cd.b && cd.c && cd.d;
if (r_event) cout << "== Success ==" << endl;
else cout << "== Fail ==" << endl;
c.addTab("colors", '1');
c.addString("Red string", 1, PIConsole::Red);
c.addString("Blue on yellow", 1, PIConsole::Blue | PIConsole::BackYellow);
c.addEmptyLine();
c.addTab("columns", '2');
c.addString("column 1", 1, PIConsole::BackYellow);
c.addString("column 2", 2, PIConsole::BackCyan);
c.addString("column 3", 3, PIConsole::BackMagenta);
c.addEmptyLine();
c.addTab("bits", '3');
c.addBitVariable("b1", &bits, 0, 1);
c.addBitVariable("b2", &bits, 1, 2);
c.addBitVariable("b4", &bits, 3, 4);
c.addCustomStatus("[] - -/+ b1, ;\' - -/+ b2, ,. - -/+ b4");
c.addEmptyLine();
c.addTab("formats", '4');
c.addVariable("our int", &i);
c.addVariable("dec", &i);
c.addVariable("oct", &i, 1, PIConsole::Oct);
c.addVariable("hex", &i, 1, PIConsole::Hex);
c.addCustomStatus("-+ - -/+ number");
c.addEmptyLine();
c.setTab(0);
c.enableExitCapture();
c.start(true);
/*MProt mp;
Prot p;
PIRepeater r("protocols.conf", "r", 1024);
mp.addProtocol(p);
c.addVariable("rec : " + mp[0]->receiverDeviceName() + ":", mp[0]->receiverDeviceState_ptr());
c.addVariable("send: " + mp[0]->senderDeviceName() + ":", mp[0]->senderDeviceState_ptr());
c.addVariable("freq", mp[0]->immediateFrequency_ptr());
c.addVariable("sent ", mp[0]->sendCount_ptr());
c.addVariable("ok ", mp[0]->receiveCount_ptr());
c.addVariable("wrong", mp[0]->wrongCount_ptr());
c.addEmptyLine();
c.addVariable("mp_corr", &mp.cor);
c.addVariable("mp_incr", &mp.incor);
c.addVariable(r.firstChannelName() + ", rec ", r.receiveCount_ptr());
c.addVariable(r.secondChannelName() + ", send", r.sendCount_ptr());
c.enableExitCapture();
mp.start();
c.start(false);*/
c.waitForFinish();
cout << endl << "== Results ==" << endl;
cout << "= String " << r_string << " =" << endl;
cout << "= Thread " << r_thread << " =" << endl;
cout << "= Mutex " << r_mutex << " =" << endl;
cout << "= Timer " << r_timer << " =" << endl;
cout << "= File " << r_file << " =" << endl;
cout << "= Evaluator " << r_eval << " =" << endl;
cout << "= Event " << r_event << " =" << endl;
if (r_string && r_thread && r_mutex && r_timer && r_file && r_eval && r_event)
cout << "== All tests successful ==" << endl;
};