comparison src/test.cpp @ 13:2752e512fd32 stable-2-1

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