Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
44:f5b5c648cd5d | 53:6907e2177eb8 |
---|---|
4 | 4 |
5 int test1(UdpServer *server, UdpClient *client){ | 5 int test1(UdpServer *server, UdpClient *client){ |
6 int buffer; | 6 int buffer; |
7 int tot=100; | 7 int tot=100; |
8 int errors=0; | 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++){ | 9 for(int n=0; n<tot; n++){ |
12 server->read(&buffer,sizeof(int)); | 10 if(client->send(&n,sizeof(int))!=1){ |
11 printf("error: while sending\n"); | |
12 errors++; | |
13 } | |
14 } | |
15 for(int n=0; n<tot; n++){ | |
16 if(server->read(&buffer,sizeof(int))<0){ | |
17 printf("error: unable to read\n"); | |
18 errors++; | |
19 continue; | |
20 }; | |
13 if(n!=buffer){ | 21 if(n!=buffer){ |
14 printf("error: %d!=%d\n",n,buffer); | 22 printf("error: %d!=%d\n",n,buffer); |
15 errors++; | 23 errors++; |
16 } | 24 } |
17 } | 25 } |
18 int n=server->emptySocket(); | 26 int n=server->emptySocket(); |
19 if(n!=0) | 27 if(n!=0) |
20 printf("Error: the socket had %d bytes",n); | 28 printf("Error: the socket had %d bytes\n",n); |
21 return errors; | 29 return errors; |
22 } | 30 } |
23 int compareStrings(char * str1, char * str2){ | 31 int compareStrings(char * str1, char * str2){ |
24 if(strlen(str1)!=strlen(str2)) | 32 if(strlen(str1)!=strlen(str2)) |
25 return -1; | 33 return -1; |
69 return errors; | 77 return errors; |
70 } | 78 } |
71 | 79 |
72 | 80 |
73 int main(){ | 81 int main(){ |
74 int port=1234; | 82 int port=1234; |
83 char serverName[]="127.0.0.1"; | |
75 UdpServer server(port); | 84 UdpServer server(port); |
76 UdpClient client(port,"127.0.0.1"); | 85 UdpClient client(port,serverName); |
77 int errors=0; | 86 int errors=0; |
78 int ret; | 87 int ret=0; |
79 ret=test1(&server,&client); | 88 ret=test1(&server,&client); |
80 errors+=ret; | 89 errors+=ret; |
81 if(ret) | 90 if(ret) |
82 printf("test1 failed with %d errors\n", ret); | 91 printf("test1 failed with %d errors\n", ret); |
83 else | 92 else |
94 errors+=ret; | 103 errors+=ret; |
95 if(ret) | 104 if(ret) |
96 printf("test3 failed with %d errors\n", ret); | 105 printf("test3 failed with %d errors\n", ret); |
97 else | 106 else |
98 printf("test3 passed\n"); | 107 printf("test3 passed\n"); |
108 //now test if the setPort and setServer methods work | |
109 client.~UdpClient(); | |
110 server.~UdpServer(); | |
111 port=1235; | |
112 UdpServer server1; | |
113 UdpClient client1; | |
114 client1.setPort(port); | |
115 client1.setServer(serverName); | |
116 if(server1.bindToPort(port)==false){ | |
117 printf("unable to bind to port %d\n",port); | |
118 errors+=1; | |
119 } | |
120 ret=test1(&server1, &client1); | |
121 errors+=ret; | |
122 if(ret) | |
123 printf("test1 failed with %d errors\n", ret); | |
124 else | |
125 printf("test1 passed\n"); | |
99 | 126 |
100 return errors; | 127 return errors; |
101 } | 128 } |