1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
38
#include "sge_dstring.h"
40
#define STATIC_SIZE 20
43
check_dstring(dstring *sb) {
46
printf("%5d : %5d : %-50s\n", (int)sb->size, (int)sb->length,
47
sb->s == NULL ? "(null)" : sb->s);
53
check_all(dstring *sb)
58
/* sge_dstring_append */
59
printf("\nchecking sge_dstring_append\n");
60
sge_dstring_append(NULL, NULL);
62
sge_dstring_append(sb, NULL);
65
sge_dstring_append(sb, "blah");
68
sge_dstring_clear(sb);
69
sge_dstring_append(sb, "too long string to fit into a static string buffer");
72
sge_dstring_clear(sb);
73
sge_dstring_append(sb,
74
"long string that requires multiple chunks ....... ");
76
for (i = 0; i < 20; i++) {
77
sge_dstring_append(sb,
78
"long string that requires multiple chunks ....... ");
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);
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);
94
sge_dstring_free(&second);
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');
103
sge_dstring_append_char(sb, 'a');
105
sge_dstring_append_char(sb, 'b');
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");
116
/* does not build on irix */
117
/* sge_dstring_vsprintf */
118
printf("\nchecking sge_dstring_vsprintf\n");
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);
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");
138
/* sge_dstring_clear */
139
printf("\nchecking sge_dstring_clear\n");
140
sge_dstring_clear(NULL);
141
sge_dstring_clear(sb);
144
/* sge_dstring_free */
145
printf("\nchecking sge_dstring_free\n");
146
sge_dstring_free(NULL);
147
sge_dstring_free(sb);
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");
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);
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");
173
/* sge_dstring_copy_dstring
174
* check only NULL pointer behaviour, it just calls sge_dstring_copy_string
176
printf("\nchecking sge_dstring_copy_dstring\n");
177
sge_dstring_copy_dstring(NULL, NULL);
178
sge_dstring_copy_dstring(sb, NULL);
181
/* sge_dstring_strlen */
182
printf("\nchecking sge_dstring_strlen\n");
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);
192
/* sge_dstring_remaining */
193
printf("\nchecking sge_dstring_remaining\n");
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);
206
static void test_dstring_performance(dstring *ds, int max, const char *data)
209
struct timeval before;
210
struct timeval after;
213
gettimeofday(&before, NULL);
214
for (i = 0; i < max; i++) {
215
sge_dstring_sprintf(ds, "%s", data, data);
217
gettimeofday(&after, NULL);
219
time = after.tv_usec - before.tv_usec;
220
time = after.tv_sec - before.tv_sec + (time/1000000);
222
printf("%d sge_dstring_sprintf took %.2fs\n", max, time);
225
static void test_dstring_performance_static(int max, const char *data)
228
struct timeval before;
229
struct timeval after;
232
gettimeofday(&before, NULL);
233
for (i = 0; i < max; i++) {
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);
239
gettimeofday(&after, NULL);
241
time = after.tv_usec - before.tv_usec;
242
time = after.tv_sec - before.tv_sec + (time/1000000);
244
printf("%d static dstring creations took %.2fs\n", max, time);
247
static void test_dstring_performance_dynamic(int max, const char *data)
250
struct timeval before;
251
struct timeval after;
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);
260
gettimeofday(&after, NULL);
262
time = after.tv_usec - before.tv_usec;
263
time = after.tv_sec - before.tv_sec + (time/1000000);
265
printf("%d dstring creations took %.2fs\n", max, time);
268
int main(int argc, char *argv[])
271
dstring dynamic_dstring = DSTRING_INIT;
272
dstring static_dstring;
273
char static_buffer[MAX_STRING_SIZE];
275
sge_dstring_init(&static_dstring, static_buffer, STATIC_SIZE);
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));
284
printf("\n\nrunning all checks with a static dstring of length %d\n",
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));
294
sge_dstring_free(&dynamic_dstring);
296
return ret ? EXIT_SUCCESS : EXIT_FAILURE;