comparison src/test.cpp @ 143:ecb40aa3eaa5 stable-5-23

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