annotate src/test.cpp @ 14:c9a1fd8f633b

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