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