diff resources/tests/UdpClientUdpServerTest.cpp @ 53:6907e2177eb8 ultra-staging

Fixed bugs in Udp classes, updated tests
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 07 Jun 2015 14:58:34 +0100
parents f5b5c648cd5d
children
line wrap: on
line diff
--- a/resources/tests/UdpClientUdpServerTest.cpp	Wed May 20 18:07:16 2015 +0100
+++ b/resources/tests/UdpClientUdpServerTest.cpp	Sun Jun 07 14:58:34 2015 +0100
@@ -6,10 +6,18 @@
 	int buffer;
 	int tot=100;
 	int errors=0;
-	for(int n=0; n<tot; n++)
-		client->send(&n,sizeof(int));
 	for(int n=0; n<tot; n++){
-		server->read(&buffer,sizeof(int));
+		if(client->send(&n,sizeof(int))!=1){
+			printf("error: while sending\n");
+			errors++;
+		}
+	}
+	for(int n=0; n<tot; n++){
+		if(server->read(&buffer,sizeof(int))<0){
+			printf("error: unable to read\n");
+			errors++;
+			continue;
+		};
 		if(n!=buffer){
 			printf("error: %d!=%d\n",n,buffer);
 			errors++;
@@ -17,7 +25,7 @@
 	}
 	int n=server->emptySocket();
 	if(n!=0)
-		printf("Error: the socket had %d bytes",n);
+		printf("Error: the socket had %d bytes\n",n);
 	return errors;
 }
 int compareStrings(char * str1, char * str2){
@@ -71,11 +79,12 @@
 
 
 int main(){
-    int port=1234;
+	int port=1234;
+	char serverName[]="127.0.0.1";
 	UdpServer server(port);
-	UdpClient client(port,"127.0.0.1");
+	UdpClient client(port,serverName);
 	int errors=0;
-	int ret;
+	int ret=0;
 	ret=test1(&server,&client);
 	errors+=ret;
 	if(ret)
@@ -96,6 +105,24 @@
 		printf("test3 failed with %d errors\n", ret);
 	else
 		printf("test3 passed\n");
+//now test if the setPort and setServer methods work
+	client.~UdpClient();
+	server.~UdpServer();
+	port=1235;
+	UdpServer server1;
+	UdpClient client1;
+	client1.setPort(port);
+	client1.setServer(serverName);
+	if(server1.bindToPort(port)==false){
+		printf("unable to bind to port %d\n",port);
+		errors+=1;
+	}
+	ret=test1(&server1, &client1);
+	errors+=ret;
+	if(ret)
+		printf("test1 failed with %d errors\n", ret);
+	else
+		printf("test1 passed\n");
 
 	return errors;
 }