~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/uti/test_sge_dstring.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
#include <time.h>
 
36
#include <sys/time.h>
 
37
 
 
38
#include "sge_dstring.h"
 
39
 
 
40
#define STATIC_SIZE 20
 
41
 
 
42
static bool
 
43
check_dstring(dstring *sb) {
 
44
   bool ret = true;
 
45
 
 
46
   printf("%5d : %5d : %-50s\n", (int)sb->size, (int)sb->length, 
 
47
          sb->s == NULL ? "(null)" : sb->s);
 
48
 
 
49
   return ret;
 
50
}
 
51
 
 
52
static bool 
 
53
check_all(dstring *sb)
 
54
{
 
55
   bool ret = true;
 
56
   int i;
 
57
 
 
58
   /* sge_dstring_append */
 
59
   printf("\nchecking sge_dstring_append\n");
 
60
   sge_dstring_append(NULL, NULL);
 
61
 
 
62
   sge_dstring_append(sb, NULL);
 
63
   check_dstring(sb);
 
64
 
 
65
   sge_dstring_append(sb, "blah");
 
66
   check_dstring(sb);
 
67
 
 
68
   sge_dstring_clear(sb);
 
69
   sge_dstring_append(sb, "too long string to fit into a static string buffer");
 
70
   check_dstring(sb);
 
71
 
 
72
   sge_dstring_clear(sb);
 
73
   sge_dstring_append(sb, 
 
74
                      "long string that requires multiple chunks ....... ");
 
75
   check_dstring(sb);
 
76
   for (i = 0; i < 20; i++) {
 
77
      sge_dstring_append(sb, 
 
78
                         "long string that requires multiple chunks ....... ");
 
79
   }
 
80
   check_dstring(sb);
 
81
 
 
82
   /* sge_dstring_append_dstring */
 
83
   printf("\nchecking sge_dstring_append_dstring\n");
 
84
   sge_dstring_clear(sb);
 
85
   sge_dstring_append_dstring(NULL, NULL);
 
86
   {
 
87
      dstring second = DSTRING_INIT;
 
88
      sge_dstring_append(&second, "dstring");
 
89
      sge_dstring_append_dstring(NULL, &second);
 
90
      sge_dstring_append_dstring(sb, NULL);
 
91
      sge_dstring_append_dstring(sb, &second);
 
92
      check_dstring(sb);
 
93
   
 
94
      sge_dstring_free(&second);
 
95
   }
 
96
 
 
97
   /* sge_dstring_append_char */
 
98
   printf("\nchecking sge_dstring_append_char\n");
 
99
   sge_dstring_clear(sb);
 
100
   sge_dstring_append_char(NULL, 'a');
 
101
   sge_dstring_append_char(sb, '\0');
 
102
   check_dstring(sb);
 
103
   sge_dstring_append_char(sb, 'a');
 
104
   check_dstring(sb);
 
105
   sge_dstring_append_char(sb, 'b');
 
106
   check_dstring(sb);
 
107
 
 
108
   /* sge_dstring_sprintf */
 
109
   printf("\nchecking sge_dstring_sprintf\n");
 
110
   sge_dstring_sprintf(NULL, "test %s", "string");
 
111
   sge_dstring_sprintf(sb, NULL);
 
112
   sge_dstring_sprintf(sb, "test %s", "string");
 
113
   check_dstring(sb);
 
114
  
 
115
#if 0
 
116
   /* does not build on irix */
 
117
   /* sge_dstring_vsprintf */
 
118
   printf("\nchecking sge_dstring_vsprintf\n");
 
119
   {
 
120
      const char *args[] = { "string", NULL };
 
121
      sge_dstring_clear(sb);
 
122
      sge_dstring_vsprintf(NULL, "test %s", args);
 
123
      sge_dstring_vsprintf(sb, NULL, args);
 
124
      sge_dstring_vsprintf(sb, "test %s", args);
 
125
      check_dstring(sb);
 
126
   }
 
127
#endif
 
128
   
 
129
   /* sge_dstring_sprintf_append */
 
130
   printf("\nchecking sge_dstring_sprintf_append\n");
 
131
   sge_dstring_clear(sb);
 
132
   sge_dstring_sprintf_append(NULL, "test %s", "string");
 
133
   sge_dstring_sprintf_append(sb, NULL);
 
134
   sge_dstring_sprintf_append(sb, "test %s", "string");
 
135
   sge_dstring_sprintf_append(sb, " appended test %s", "string");
 
136
   check_dstring(sb);
 
137
   
 
138
   /* sge_dstring_clear */
 
139
   printf("\nchecking sge_dstring_clear\n");
 
140
   sge_dstring_clear(NULL);
 
141
   sge_dstring_clear(sb);
 
142
   check_dstring(sb);
 
143
   
 
144
   /* sge_dstring_free */
 
145
   printf("\nchecking sge_dstring_free\n");
 
146
   sge_dstring_free(NULL);
 
147
   sge_dstring_free(sb);
 
148
   check_dstring(sb);
 
149
   
 
150
   /* sge_dstring_get_string */
 
151
   printf("\nchecking sge_dstring_get_string\n");
 
152
   sge_dstring_clear(sb);
 
153
   sge_dstring_append(sb, "test string");
 
154
   { 
 
155
      const char *result;
 
156
 
 
157
      result = sge_dstring_get_string(NULL);
 
158
      printf("sge_dstring_get_string(NULL) = %s\n", 
 
159
             result == NULL ? "NULL" : result);
 
160
      result = sge_dstring_get_string(sb);
 
161
      printf("sge_dstring_get_string(sb) = %s\n", 
 
162
             result == NULL ? "NULL" : result);
 
163
   }
 
164
   
 
165
   /* sge_dstring_copy_string */
 
166
   printf("\nchecking sge_dstring_copy_string\n");
 
167
   sge_dstring_copy_string(NULL, NULL);
 
168
   sge_dstring_copy_string(sb, NULL);
 
169
   sge_dstring_copy_string(NULL, "new test string");
 
170
   sge_dstring_copy_string(sb, "new test string");
 
171
   check_dstring(sb);
 
172
   
 
173
   /* sge_dstring_copy_dstring 
 
174
    * check only NULL pointer behaviour, it just calls sge_dstring_copy_string
 
175
    */
 
176
   printf("\nchecking sge_dstring_copy_dstring\n");
 
177
   sge_dstring_copy_dstring(NULL, NULL);
 
178
   sge_dstring_copy_dstring(sb, NULL);
 
179
   check_dstring(sb);
 
180
   
 
181
   /* sge_dstring_strlen */
 
182
   printf("\nchecking sge_dstring_strlen\n");
 
183
   {
 
184
      int len;
 
185
      sge_dstring_copy_string(sb, "test string");
 
186
      len = sge_dstring_strlen(NULL);
 
187
      printf("sge_dstring_strlen(NULL) = %d\n", len);
 
188
      len = sge_dstring_strlen(sb);
 
189
      printf("sge_dstring_strlen(sb) = %d\n", len);
 
190
   }
 
191
   
 
192
   /* sge_dstring_remaining */
 
193
   printf("\nchecking sge_dstring_remaining\n");
 
194
   {
 
195
      int len;
 
196
      sge_dstring_copy_string(sb, "test string");
 
197
      len = sge_dstring_remaining(NULL);
 
198
      printf("sge_dstring_remaining(NULL) = %d\n", len);
 
199
      len = sge_dstring_remaining(sb);
 
200
      printf("sge_dstring_remaining(sb) = %d\n", len);
 
201
   }
 
202
 
 
203
   return ret;
 
204
}
 
205
 
 
206
static void test_dstring_performance(dstring *ds, int max, const char *data)
 
207
{
 
208
   int i;
 
209
   struct timeval before;
 
210
   struct timeval after;
 
211
   double time;
 
212
 
 
213
   gettimeofday(&before, NULL);
 
214
   for (i = 0; i < max; i++) {
 
215
      sge_dstring_sprintf(ds, "%s", data, data);
 
216
   }
 
217
   gettimeofday(&after, NULL);
 
218
 
 
219
   time = after.tv_usec - before.tv_usec;
 
220
   time = after.tv_sec - before.tv_sec + (time/1000000);
 
221
 
 
222
   printf("%d sge_dstring_sprintf took %.2fs\n", max, time);
 
223
}
 
224
 
 
225
static void test_dstring_performance_static(int max, const char *data)
 
226
{
 
227
   int i;
 
228
   struct timeval before;
 
229
   struct timeval after;
 
230
   double time;
 
231
 
 
232
   gettimeofday(&before, NULL);
 
233
   for (i = 0; i < max; i++) {
 
234
      dstring ds;
 
235
      char ds_buffer[MAX_STRING_SIZE];
 
236
      sge_dstring_init(&ds, ds_buffer, sizeof(ds_buffer));
 
237
      sge_dstring_sprintf(&ds, "%s/%s", data, data);
 
238
   }
 
239
   gettimeofday(&after, NULL);
 
240
 
 
241
   time = after.tv_usec - before.tv_usec;
 
242
   time = after.tv_sec - before.tv_sec + (time/1000000);
 
243
 
 
244
   printf("%d static dstring creations took %.2fs\n", max, time);
 
245
}
 
246
 
 
247
static void test_dstring_performance_dynamic(int max, const char *data)
 
248
{
 
249
   int i;
 
250
   struct timeval before;
 
251
   struct timeval after;
 
252
   double time;
 
253
 
 
254
   gettimeofday(&before, NULL);
 
255
   for (i = 0; i < max; i++) {
 
256
      dstring ds = DSTRING_INIT;
 
257
      sge_dstring_sprintf(&ds, "%s/%s", data, data);
 
258
      sge_dstring_free(&ds);
 
259
   }
 
260
   gettimeofday(&after, NULL);
 
261
 
 
262
   time = after.tv_usec - before.tv_usec;
 
263
   time = after.tv_sec - before.tv_sec + (time/1000000);
 
264
 
 
265
   printf("%d dstring creations took %.2fs\n", max, time);
 
266
}
 
267
 
 
268
int main(int argc, char *argv[])
 
269
{
 
270
   bool ret = true;
 
271
   dstring dynamic_dstring = DSTRING_INIT;
 
272
   dstring static_dstring;
 
273
   char    static_buffer[MAX_STRING_SIZE];
 
274
   
 
275
   sge_dstring_init(&static_dstring, static_buffer, STATIC_SIZE);
 
276
 
 
277
   printf("running all checks with a dynamic dstring\n");
 
278
   ret = check_all(&dynamic_dstring);
 
279
   test_dstring_performance(&dynamic_dstring, 100000, "test_data"); 
 
280
   test_dstring_performance_dynamic(100000, "test_data"); 
 
281
   printf("%s\n", sge_dstring_get_string(&dynamic_dstring));
 
282
 
 
283
   if (ret) {
 
284
      printf("\n\nrunning all checks with a static dstring of length %d\n", 
 
285
             STATIC_SIZE);
 
286
         ret = check_all(&static_dstring);
 
287
         test_dstring_performance(&static_dstring, 100000, "test_data"); 
 
288
         test_dstring_performance_static(100000, "test_data"); 
 
289
         printf("%s\n", sge_dstring_get_string(&static_dstring));
 
290
   }
 
291
 
 
292
 
 
293
 
 
294
   sge_dstring_free(&dynamic_dstring);
 
295
 
 
296
   return ret ? EXIT_SUCCESS : EXIT_FAILURE;
 
297
}