annotate resources/tests/UdpClientUdpServerTest.cpp @ 44:f5b5c648cd5d ultra-staging

- added (unused) simple c++ classes for udp datagrams\n- added tests for the new classes
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 May 2015 18:07:16 +0100
parents
children 6907e2177eb8
rev   line source
giuliomoro@44 1 #include "../../include/UdpServer.h"
giuliomoro@44 2 #include "../../include/UdpClient.h"
giuliomoro@44 3 #include <unistd.h>
giuliomoro@44 4
giuliomoro@44 5 int test1(UdpServer *server, UdpClient *client){
giuliomoro@44 6 int buffer;
giuliomoro@44 7 int tot=100;
giuliomoro@44 8 int errors=0;
giuliomoro@44 9 for(int n=0; n<tot; n++)
giuliomoro@44 10 client->send(&n,sizeof(int));
giuliomoro@44 11 for(int n=0; n<tot; n++){
giuliomoro@44 12 server->read(&buffer,sizeof(int));
giuliomoro@44 13 if(n!=buffer){
giuliomoro@44 14 printf("error: %d!=%d\n",n,buffer);
giuliomoro@44 15 errors++;
giuliomoro@44 16 }
giuliomoro@44 17 }
giuliomoro@44 18 int n=server->emptySocket();
giuliomoro@44 19 if(n!=0)
giuliomoro@44 20 printf("Error: the socket had %d bytes",n);
giuliomoro@44 21 return errors;
giuliomoro@44 22 }
giuliomoro@44 23 int compareStrings(char * str1, char * str2){
giuliomoro@44 24 if(strlen(str1)!=strlen(str2))
giuliomoro@44 25 return -1;
giuliomoro@44 26 for(int n=0; n<strlen(str1); n++){
giuliomoro@44 27 if(str1[n]!=str2[n])
giuliomoro@44 28 return -1;
giuliomoro@44 29 }
giuliomoro@44 30 return 0;
giuliomoro@44 31 }
giuliomoro@44 32
giuliomoro@44 33 int test2(UdpServer *server, UdpClient *client){
giuliomoro@44 34 char buffer[1000];
giuliomoro@44 35 int tot=100;
giuliomoro@44 36 int errors=0;
giuliomoro@44 37 for(int n=0; n<tot; n++){
giuliomoro@44 38 int num=sprintf(buffer,"%08.8f",n/1000.0);
giuliomoro@44 39 client->send(&buffer,sizeof(char)*(num+1));
giuliomoro@44 40 }
giuliomoro@44 41 char auxBuffer[100];
giuliomoro@44 42 for(int n=0; n<tot; n++){
giuliomoro@44 43 int num=sprintf(auxBuffer,"%08.8f",n/1000.0);
giuliomoro@44 44 server->read(&buffer,num*sizeof(char));
giuliomoro@44 45 if(compareStrings(auxBuffer,buffer)==-1){
giuliomoro@44 46 printf("error: %s!=%s\n",auxBuffer,buffer);
giuliomoro@44 47 errors++;
giuliomoro@44 48 }
giuliomoro@44 49 }
giuliomoro@44 50
giuliomoro@44 51 return errors;
giuliomoro@44 52 }
giuliomoro@44 53
giuliomoro@44 54 int test3(UdpServer *server, UdpClient *client){
giuliomoro@44 55 char buffer[1000];
giuliomoro@44 56 int tot=100;
giuliomoro@44 57 int errors=0;
giuliomoro@44 58 int totNum=0;
giuliomoro@44 59 for(int n=0; n<tot; n++){
giuliomoro@44 60 int num=sprintf(buffer,"%.8f",n/1000.0);
giuliomoro@44 61 client->send(&buffer,sizeof(char)*(num+1));
giuliomoro@44 62 totNum+=1+num;
giuliomoro@44 63 }
giuliomoro@44 64 int n=server->emptySocket();
giuliomoro@44 65 if(n!=totNum){
giuliomoro@44 66 errors=1;
giuliomoro@44 67 printf("retrieved bytes differs from sent bytes: %d!=%d\n",n,totNum);
giuliomoro@44 68 }
giuliomoro@44 69 return errors;
giuliomoro@44 70 }
giuliomoro@44 71
giuliomoro@44 72
giuliomoro@44 73 int main(){
giuliomoro@44 74 int port=1234;
giuliomoro@44 75 UdpServer server(port);
giuliomoro@44 76 UdpClient client(port,"127.0.0.1");
giuliomoro@44 77 int errors=0;
giuliomoro@44 78 int ret;
giuliomoro@44 79 ret=test1(&server,&client);
giuliomoro@44 80 errors+=ret;
giuliomoro@44 81 if(ret)
giuliomoro@44 82 printf("test1 failed with %d errors\n", ret);
giuliomoro@44 83 else
giuliomoro@44 84 printf("test1 passed\n");
giuliomoro@44 85
giuliomoro@44 86 ret=test2(&server,&client);
giuliomoro@44 87 errors+=ret;
giuliomoro@44 88 if(ret)
giuliomoro@44 89 printf("test2 failed with %d errors\n", ret);
giuliomoro@44 90 else
giuliomoro@44 91 printf("test2 passed\n");
giuliomoro@44 92
giuliomoro@44 93 ret=test3(&server,&client);
giuliomoro@44 94 errors+=ret;
giuliomoro@44 95 if(ret)
giuliomoro@44 96 printf("test3 failed with %d errors\n", ret);
giuliomoro@44 97 else
giuliomoro@44 98 printf("test3 passed\n");
giuliomoro@44 99
giuliomoro@44 100 return errors;
giuliomoro@44 101 }