Skip to content

Commit

Permalink
rename macros in ofxUnitTests (#6017)
Browse files Browse the repository at this point in the history
* rename macros

test to ofxTest
test_eq to ofxTestEq
test_gt to ofxTestGt
test_lt to ofxTestLt

* updated tests to new syntax
  • Loading branch information
2bbb authored and arturoc committed Jul 30, 2018
1 parent c274c7f commit 86c0e5e
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 391 deletions.
8 changes: 4 additions & 4 deletions addons/ofxUnitTests/src/ofxUnitTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class ofxUnitTestsApp: public ofBaseApp{
std::shared_ptr<ofColorsLoggerChannel> logger{new ofColorsLoggerChannel};
};

#define test(x, ...) this->do_test(x,__VA_ARGS__,__FILE__,__LINE__)
#define test_eq(x,y, ...) this->do_test_eq(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
#define test_gt(x,y, ...) this->do_test_gt(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
#define test_lt(x,y, ...) this->do_test_lt(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
#define ofxTest(x, ...) this->do_test(x,__VA_ARGS__,__FILE__,__LINE__)
#define ofxTestEq(x,y, ...) this->do_test_eq(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
#define ofxTestGt(x,y, ...) this->do_test_gt(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
#define ofxTestLt(x,y, ...) this->do_test_lt(x,y,# x,# y,__VA_ARGS__,__FILE__,__LINE__)
94 changes: 47 additions & 47 deletions tests/addons/networkTcp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,false), "non blocking server");
ofxTest(server.setup(port,false), "non blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1",port,false), "non blocking client");
ofxTest(client.setup("127.0.0.1",port,false), "non blocking client");

server.waitConnectedClient(500);

bool received = false;
std::string messageSent = "message";
std::string messageReceived;

test(client.send(messageSent), "send non blocking from client");
ofxTest(client.send(messageSent), "send non blocking from client");
for(int i=0;i<server.getLastID();i++){
if(!server.isClientConnected(i)){
continue;
Expand All @@ -35,14 +35,14 @@ class ofApp: public ofxUnitTestsApp{
received = true;
}
}
test(received, "receive non blocking from server");
ofxTest(received, "receive non blocking from server");
}

for(int i=0;i<server.getLastID();i++){
if(!server.isClientConnected(i)){
continue;
}
test(server.send(i,messageSent), "send non blocking from server");
ofxTest(server.send(i,messageSent), "send non blocking from server");
messageReceived = "";
for(int i=0;i<10 && !received;i++){
ofSleepMillis(200);
Expand All @@ -51,7 +51,7 @@ class ofApp: public ofxUnitTestsApp{
received = true;
}
}
test(received, "receive non blocking from client");
ofxTest(received, "receive non blocking from client");
break;
}
}
Expand All @@ -64,19 +64,19 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,true), "blocking server");
ofxTest(server.setup(port,true), "blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1",port,true), "blocking client");
ofxTest(client.setup("127.0.0.1",port,true), "blocking client");

// wait for connection to be made
server.waitConnectedClient(500);

std::string messageSent = "message";
test(client.send(messageSent), "send blocking from client");
test_eq(server.receive(0), messageSent, "receive blocking from server");
test(server.send(0, messageSent), "send blocking from server");
test_eq(client.receive(), messageSent, "receive blocking from client");
ofxTest(client.send(messageSent), "send blocking from client");
ofxTestEq(server.receive(0), messageSent, "receive blocking from server");
ofxTest(server.send(0, messageSent), "send blocking from server");
ofxTestEq(client.receive(), messageSent, "receive blocking from client");
}

void disconnectionAutoDetection(){
Expand All @@ -87,25 +87,25 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,true), "blocking server");
ofxTest(server.setup(port,true), "blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1",port,true), "blocking client");
ofxTest(client.setup("127.0.0.1",port,true), "blocking client");

// wait for connection to be made
server.waitConnectedClient(500);

test(server.isConnected(), "server is still connected");
test(client.isConnected(), "client is still connected");
ofxTest(server.isConnected(), "server is still connected");
ofxTest(client.isConnected(), "client is still connected");

test(server.disconnectAllClients(), "server closes correctly");
test(!client.isConnected(), "client detects disconnection");
ofxTest(server.disconnectAllClients(), "server closes correctly");
ofxTest(!client.isConnected(), "client detects disconnection");

test(client.setup("127.0.0.1",port,true), "client reconnects");
ofxTest(client.setup("127.0.0.1",port,true), "client reconnects");

test(client.close(), "client disconnects");
test(!server.isClientConnected(0), "server detects disconnection");
test(!server.isClientConnected(1), "server detects disconnection");
ofxTest(client.close(), "client disconnects");
ofxTest(!server.isClientConnected(0), "server detects disconnection");
ofxTest(!server.isClientConnected(1), "server detects disconnection");
}

void testSendRaw(){
Expand All @@ -116,13 +116,13 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,true), "blocking server");
ofxTest(server.setup(port,true), "blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1", port, true), "blocking client");
ofxTest(client.setup("127.0.0.1", port, true), "blocking client");

std::string messageSent = "message";
test(client.sendRaw(messageSent), "send blocking from client");
ofxTest(client.sendRaw(messageSent), "send blocking from client");

// wait for connection to be made
server.waitConnectedClient(500);
Expand All @@ -132,15 +132,15 @@ class ofApp: public ofxUnitTestsApp{
int received = 0;
do{
auto ret = server.receiveRawBytes(0, messageReceived.data() + received, messageSent.size());
test(ret>0, "received blocking from server");
ofxTest(ret>0, "received blocking from server");
if(ret>0){
received += ret;
}else{
break;
}
}while(received<messageSent.size());

test_eq(messageSent, std::string(messageReceived.data()), "messageSent == messageReceived");
ofxTestEq(messageSent, std::string(messageReceived.data()), "messageSent == messageReceived");
}

void testSendRawBytes(){
Expand All @@ -151,31 +151,31 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,true), "blocking server");
ofxTest(server.setup(port,true), "blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1", port, true), "blocking client");
ofxTest(client.setup("127.0.0.1", port, true), "blocking client");

// wait for connection to be made
server.waitConnectedClient(500);


std::string messageSent = "message";
test(client.sendRawBytes(messageSent.c_str(), messageSent.size()), "send blocking from client");
ofxTest(client.sendRawBytes(messageSent.c_str(), messageSent.size()), "send blocking from client");

std::vector<char> messageReceived(messageSent.size()+1, 0);
int received = 0;
do{
auto ret = server.receiveRawBytes(0, messageReceived.data() + received, messageSent.size());
test(ret>0, "received blocking from server");
ofxTest(ret>0, "received blocking from server");
if(ret>0){
received += ret;
}else{
break;
}
}while(received<messageSent.size());

test_eq(messageSent, std::string(messageReceived.data()), "messageSent == messageReceived");
ofxTestEq(messageSent, std::string(messageReceived.data()), "messageSent == messageReceived");
}

void testWrongConnect(){
Expand All @@ -185,11 +185,11 @@ class ofApp: public ofxUnitTestsApp{

ofxTCPManager client;
client.SetTimeoutConnect(5);
test(client.Create(), "socket creation");
ofxTest(client.Create(), "socket creation");
auto then = ofGetElapsedTimeMillis();
test(!client.Connect("127.0.0.1", 200), "connect to non open port, if this fails the port might be really open:");
ofxTest(!client.Connect("127.0.0.1", 200), "connect to non open port, if this fails the port might be really open:");
auto now = ofGetElapsedTimeMillis();
test(now-then<6000, "Connect waits 5s to timeout, waited: " + ofToString(now - then));
ofxTest(now-then<6000, "Connect waits 5s to timeout, waited: " + ofToString(now - then));
}

void testReceiveTimeout(){
Expand All @@ -199,31 +199,31 @@ class ofApp: public ofxUnitTestsApp{

int port = ofRandom(15000, 65535);
ofxTCPManager server;
test(server.Create(), "server socket creation");
test(server.Bind(port), "server socket bind");
test(server.Listen(1), "server socket listen");
ofxTest(server.Create(), "server socket creation");
ofxTest(server.Bind(port), "server socket bind");
ofxTest(server.Listen(1), "server socket listen");
std::condition_variable done;
std::mutex mtx;
std::thread serverThread([&]{
ofxTCPManager client;
test(server.Accept(client), "server socket accept");
ofxTest(server.Accept(client), "server socket accept");
std::unique_lock<std::mutex> lck(mtx);
done.wait(lck);
});

ofxTCPManager client;
test(client.Create(), "client socket create");
test(client.Connect("127.0.0.1", port), "client socket connect");
ofxTest(client.Create(), "client socket create");
ofxTest(client.Connect("127.0.0.1", port), "client socket connect");

ofSleepMillis(100);
client.SetTimeoutReceive(5);
auto then = ofGetElapsedTimeMillis();
char buffer;
test_eq(client.Receive(&buffer,1), SOCKET_TIMEOUT, "socket timeouts on no receive");
ofxTestEq(client.Receive(&buffer,1), SOCKET_TIMEOUT, "socket timeouts on no receive");
auto now = ofGetElapsedTimeMillis();
// seems timers in the test servers are not very accurate so
// we test this with a margin of 500ms
test_gt(now-then, 4500, "Connect waits 5s to timeout, waited: " + ofToString(now - then));
ofxTestGt(now-then, 4500, "Connect waits 5s to timeout, waited: " + ofToString(now - then));
done.notify_all();
serverThread.join();
}
Expand All @@ -236,10 +236,10 @@ class ofApp: public ofxUnitTestsApp{
int port = ofRandom(15000, 65535);

ofxTCPServer server;
test(server.setup(port,true), "blocking server");
ofxTest(server.setup(port,true), "blocking server");

ofxTCPClient client;
test(client.setup("127.0.0.1", port, true), "blocking client");
ofxTest(client.setup("127.0.0.1", port, true), "blocking client");

// wait for connection to be made
server.waitConnectedClient(500);
Expand All @@ -249,11 +249,11 @@ class ofApp: public ofxUnitTestsApp{
for(int i=0;i<TCP_MAX_MSG_SIZE;i++){
str.append(ofToString((int)ofRandom(10)));
}
test(server.sendRawMsg(0, str.c_str(), str.size()), "could send max size");
ofxTest(server.sendRawMsg(0, str.c_str(), str.size()), "could send max size");
do{
received += client.receiveRaw();
}while(received.size()<str.size());
test_eq(received, str, "received max size message == sent message");
ofxTestEq(received, str, "received max size message == sent message");
}

void run(){
Expand Down
Loading

0 comments on commit 86c0e5e

Please sign in to comment.