annotate src/test.cpp @ 201:752d4315675c stable-6-0-16

add reference to mercurial repository in the documentation
author Carl Byington <carl@five-ten-sg.com>
date Sat, 02 Feb 2008 12:31:53 -0800
parents c7fc218686f5
children 4db1457cd11a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
1 /*
2752e512fd32 finish documentation
carl
parents:
diff changeset
2
152
c7fc218686f5 gpl3, block mail to recipients that cannot reply
carl
parents: 143
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
c7fc218686f5 gpl3, block mail to recipients that cannot reply
carl
parents: 143
diff changeset
4 the GPL version 3 or any later version at your choice available at
c7fc218686f5 gpl3, block mail to recipients that cannot reply
carl
parents: 143
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
6
2752e512fd32 finish documentation
carl
parents:
diff changeset
7 */
2752e512fd32 finish documentation
carl
parents:
diff changeset
8
2752e512fd32 finish documentation
carl
parents:
diff changeset
9 #include <stdio.h>
2752e512fd32 finish documentation
carl
parents:
diff changeset
10 #include <unistd.h>
2752e512fd32 finish documentation
carl
parents:
diff changeset
11 #include <pthread.h>
2752e512fd32 finish documentation
carl
parents:
diff changeset
12 #include <set>
2752e512fd32 finish documentation
carl
parents:
diff changeset
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
14 static char* test_version="$Id$";
2752e512fd32 finish documentation
carl
parents:
diff changeset
15
2752e512fd32 finish documentation
carl
parents:
diff changeset
16 using namespace std;
2752e512fd32 finish documentation
carl
parents:
diff changeset
17
2752e512fd32 finish documentation
carl
parents:
diff changeset
18 struct ltstr {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
19 bool operator()(char* s1, char* s2) const {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
20 return strcmp(s1, s2) < 0;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
21 }
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
22 };
2752e512fd32 finish documentation
carl
parents:
diff changeset
23
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
24 typedef set<char *, ltstr> string_set;
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
25
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
26 static string_set all_strings; // owns all the strings, only modified by the config loader thread
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
27
2752e512fd32 finish documentation
carl
parents:
diff changeset
28 struct stats {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
29 bool stop;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
30 bool running;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
31 int counter;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
32 int errors;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
33 stats();
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
34 };
2752e512fd32 finish documentation
carl
parents:
diff changeset
35 stats::stats() {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
36 stop = false;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
37 running = false;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
38 counter = 0;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
39 errors = 0;
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
40 }
2752e512fd32 finish documentation
carl
parents:
diff changeset
41
2752e512fd32 finish documentation
carl
parents:
diff changeset
42 ////////////////////////////////////////////////
2752e512fd32 finish documentation
carl
parents:
diff changeset
43 // helper to discard the strings held by a string_set
2752e512fd32 finish documentation
carl
parents:
diff changeset
44 //
2752e512fd32 finish documentation
carl
parents:
diff changeset
45 static void discard(string_set &s);
2752e512fd32 finish documentation
carl
parents:
diff changeset
46 static void discard(string_set &s) {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
47 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
48 free(*i);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
49 }
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
50 s.clear();
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
51 }
2752e512fd32 finish documentation
carl
parents:
diff changeset
52
2752e512fd32 finish documentation
carl
parents:
diff changeset
53 ////////////////////////////////////////////////
2752e512fd32 finish documentation
carl
parents:
diff changeset
54 // helper to register a string in a string set
2752e512fd32 finish documentation
carl
parents:
diff changeset
55 //
2752e512fd32 finish documentation
carl
parents:
diff changeset
56 static char* register_string(string_set &s, char *name);
2752e512fd32 finish documentation
carl
parents:
diff changeset
57 static char* register_string(string_set &s, char *name) {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
58 string_set::iterator i = s.find(name);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
59 if (i != s.end()) return *i;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
60 char *x = strdup(name);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
61 s.insert(x);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
62 return x;
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
63 }
2752e512fd32 finish documentation
carl
parents:
diff changeset
64
2752e512fd32 finish documentation
carl
parents:
diff changeset
65
2752e512fd32 finish documentation
carl
parents:
diff changeset
66 ////////////////////////////////////////////////
2752e512fd32 finish documentation
carl
parents:
diff changeset
67 // thread tester
2752e512fd32 finish documentation
carl
parents:
diff changeset
68 //
2752e512fd32 finish documentation
carl
parents:
diff changeset
69 static void* tester(void *arg);
2752e512fd32 finish documentation
carl
parents:
diff changeset
70 static void* tester(void *arg) {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
71 stats &st = *((stats *)arg);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
72 st.running = true;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
73 while (!st.stop) {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
74 const int LIMIT = 1000;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
75 string_set *mine = new string_set;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
76 string_set &me = *mine;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
77 for (int i=0; i<LIMIT; i++) {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
78 char buf[100];
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
79 snprintf(buf, sizeof(buf), "this is string %d", i);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
80 register_string(me, buf);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
81 }
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
82 for (int i=0; i<LIMIT; i+=5) {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
83 char buf[100];
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
84 snprintf(buf, sizeof(buf), "this is string %d", i);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
85 string_set::iterator j = me.find(buf);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
86 if (j == me.end()) st.errors++;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
87 }
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
88 discard(me);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
89 delete mine;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
90 st.counter++;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
91 }
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
92 st.running = false;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
93 return NULL;
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
94 }
2752e512fd32 finish documentation
carl
parents:
diff changeset
95
2752e512fd32 finish documentation
carl
parents:
diff changeset
96 int main(int argc, char**argv)
2752e512fd32 finish documentation
carl
parents:
diff changeset
97 {
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
98 stats st1;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
99 stats st2;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
100 pthread_t tid;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
101 if (pthread_create(&tid, 0, tester, &st1))
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
102 fprintf(stdout, "failed to create test thread");
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
103 if (pthread_detach(tid))
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
104 fprintf(stdout, "failed to detach test thread");
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
105 if (pthread_create(&tid, 0, tester, &st2))
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
106 fprintf(stdout, "failed to create test thread");
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
107 if (pthread_detach(tid))
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
108 fprintf(stdout, "failed to detach test thread");
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
109
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
110 fprintf(stdout, "tests are running\n");
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
111 sleep(60);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
112 st1.stop = true;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
113 st2.stop = true;
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
114 while (st1.running || st2.running) {
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
115 sleep(1);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
116 }
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
117
143
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
118 fprintf(stdout, "counter 1 = %d\n", st1.counter);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
119 fprintf(stdout, "counter 2 = %d\n", st2.counter);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
120 fprintf(stdout, "errors 1 = %d\n", st1.errors);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
121 fprintf(stdout, "errors 2 = %d\n", st2.errors);
ecb40aa3eaa5 require two periods for ip addresses
carl
parents: 13
diff changeset
122 return 0;
13
2752e512fd32 finish documentation
carl
parents:
diff changeset
123 }
2752e512fd32 finish documentation
carl
parents:
diff changeset
124