giuliomoro@44: #include "../../include/UdpServer.h" giuliomoro@44: #include "../../include/UdpClient.h" giuliomoro@44: #include giuliomoro@44: giuliomoro@44: int test1(UdpServer *server, UdpClient *client){ giuliomoro@44: int buffer; giuliomoro@44: int tot=100; giuliomoro@44: int errors=0; giuliomoro@44: for(int n=0; nsend(&n,sizeof(int))!=1){ giuliomoro@53: printf("error: while sending\n"); giuliomoro@53: errors++; giuliomoro@53: } giuliomoro@53: } giuliomoro@53: for(int n=0; nread(&buffer,sizeof(int))<0){ giuliomoro@53: printf("error: unable to read\n"); giuliomoro@53: errors++; giuliomoro@53: continue; giuliomoro@53: }; giuliomoro@44: if(n!=buffer){ giuliomoro@44: printf("error: %d!=%d\n",n,buffer); giuliomoro@44: errors++; giuliomoro@44: } giuliomoro@44: } giuliomoro@44: int n=server->emptySocket(); giuliomoro@44: if(n!=0) giuliomoro@53: printf("Error: the socket had %d bytes\n",n); giuliomoro@44: return errors; giuliomoro@44: } giuliomoro@44: int compareStrings(char * str1, char * str2){ giuliomoro@44: if(strlen(str1)!=strlen(str2)) giuliomoro@44: return -1; giuliomoro@44: for(int n=0; nsend(&buffer,sizeof(char)*(num+1)); giuliomoro@44: } giuliomoro@44: char auxBuffer[100]; giuliomoro@44: for(int n=0; nread(&buffer,num*sizeof(char)); giuliomoro@44: if(compareStrings(auxBuffer,buffer)==-1){ giuliomoro@44: printf("error: %s!=%s\n",auxBuffer,buffer); giuliomoro@44: errors++; giuliomoro@44: } giuliomoro@44: } giuliomoro@44: giuliomoro@44: return errors; giuliomoro@44: } giuliomoro@44: giuliomoro@44: int test3(UdpServer *server, UdpClient *client){ giuliomoro@44: char buffer[1000]; giuliomoro@44: int tot=100; giuliomoro@44: int errors=0; giuliomoro@44: int totNum=0; giuliomoro@44: for(int n=0; nsend(&buffer,sizeof(char)*(num+1)); giuliomoro@44: totNum+=1+num; giuliomoro@44: } giuliomoro@44: int n=server->emptySocket(); giuliomoro@44: if(n!=totNum){ giuliomoro@44: errors=1; giuliomoro@44: printf("retrieved bytes differs from sent bytes: %d!=%d\n",n,totNum); giuliomoro@44: } giuliomoro@44: return errors; giuliomoro@44: } giuliomoro@44: giuliomoro@44: giuliomoro@44: int main(){ giuliomoro@53: int port=1234; giuliomoro@53: char serverName[]="127.0.0.1"; giuliomoro@44: UdpServer server(port); giuliomoro@53: UdpClient client(port,serverName); giuliomoro@44: int errors=0; giuliomoro@53: int ret=0; giuliomoro@44: ret=test1(&server,&client); giuliomoro@44: errors+=ret; giuliomoro@44: if(ret) giuliomoro@44: printf("test1 failed with %d errors\n", ret); giuliomoro@44: else giuliomoro@44: printf("test1 passed\n"); giuliomoro@44: giuliomoro@44: ret=test2(&server,&client); giuliomoro@44: errors+=ret; giuliomoro@44: if(ret) giuliomoro@44: printf("test2 failed with %d errors\n", ret); giuliomoro@44: else giuliomoro@44: printf("test2 passed\n"); giuliomoro@44: giuliomoro@44: ret=test3(&server,&client); giuliomoro@44: errors+=ret; giuliomoro@44: if(ret) giuliomoro@44: printf("test3 failed with %d errors\n", ret); giuliomoro@44: else giuliomoro@44: printf("test3 passed\n"); giuliomoro@53: //now test if the setPort and setServer methods work giuliomoro@53: client.~UdpClient(); giuliomoro@53: server.~UdpServer(); giuliomoro@53: port=1235; giuliomoro@53: UdpServer server1; giuliomoro@53: UdpClient client1; giuliomoro@53: client1.setPort(port); giuliomoro@53: client1.setServer(serverName); giuliomoro@53: if(server1.bindToPort(port)==false){ giuliomoro@53: printf("unable to bind to port %d\n",port); giuliomoro@53: errors+=1; giuliomoro@53: } giuliomoro@53: ret=test1(&server1, &client1); giuliomoro@53: errors+=ret; giuliomoro@53: if(ret) giuliomoro@53: printf("test1 failed with %d errors\n", ret); giuliomoro@53: else giuliomoro@53: printf("test1 passed\n"); giuliomoro@44: giuliomoro@44: return errors; giuliomoro@44: }