Skip to content

Commit e3f79ea

Browse files
committed
Add the rand option to generate a random string
1 parent 80a32a7 commit e3f79ea

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

qtest.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <strings.h> /* strcasecmp */
1010
#include <sys/stat.h>
1111
#include <sys/wait.h>
12+
#include <time.h>
1213
#include <unistd.h>
1314

1415
/* Our program needs to use regular malloc/free */
@@ -56,6 +57,10 @@ static int fail_count = 0;
5657

5758
static int string_length = MAXSTRING;
5859

60+
#define MIN_RANDSTR_LEN 5
61+
#define MAX_RANDSTR_LEN 10
62+
static const char charset[] = "abcdefghijklmnopqrstuvwxyz";
63+
5964
/* Forward declarations */
6065
static bool show_queue(int vlevel);
6166
static bool do_new(int argc, char *argv[]);
@@ -76,11 +81,11 @@ static void console_init()
7681
add_cmd("new", do_new, " | Create new queue");
7782
add_cmd("free", do_free, " | Delete queue");
7883
add_cmd("ih", do_insert_head,
79-
" str [n] | Insert string str at head of queue n times "
80-
"(default: n == 1)");
84+
" str [n] | Insert string str at head of queue n times. "
85+
"Insert a random string if str is RAND (default: n == 1)");
8186
add_cmd("it", do_insert_tail,
8287
" str [n] | Insert string str at tail of queue n times "
83-
"(default: n == 1)");
88+
"Insert a random string if str is RAND (default: n == 1)");
8489
add_cmd("rh", do_remove_head,
8590
" [str] | Remove from head of queue. Optionally compare "
8691
"to expected value str");
@@ -156,11 +161,24 @@ static bool do_free(int argc, char *argv[])
156161
return ok && !error_check();
157162
}
158163

164+
static void random_string(char *buf, size_t buf_size)
165+
{
166+
size_t len = 0, n;
167+
while (len < MIN_RANDSTR_LEN)
168+
len = rand() % buf_size;
169+
170+
for (n = 0; n < len; n++) {
171+
buf[n] = charset[rand() % (sizeof charset - 1)];
172+
}
173+
buf[len] = '\0';
174+
}
175+
159176
static bool do_insert_head(int argc, char *argv[])
160177
{
161178
char *lasts = NULL;
179+
char random_str[MAX_RANDSTR_LEN];
162180
int reps = 1;
163-
bool ok = true;
181+
bool ok = true, is_random = false;
164182
if (argc != 2 && argc != 3) {
165183
report(1, "%s needs 1-2 arguments", argv[0]);
166184
return false;
@@ -174,12 +192,19 @@ static bool do_insert_head(int argc, char *argv[])
174192
}
175193
}
176194

195+
if (!strcmp(inserts, "RAND")) {
196+
is_random = true;
197+
inserts = random_str;
198+
}
199+
177200
if (!q)
178201
report(3, "Warning: Calling insert head on null queue");
179202
error_check();
180203

181204
if (exception_setup(true)) {
182205
for (int r = 0; ok && r < reps; r++) {
206+
if (is_random)
207+
random_string(random_str, MAX_RANDSTR_LEN);
183208
bool rval = q_insert_head(q, inserts);
184209
if (rval) {
185210
qcnt++;
@@ -222,8 +247,9 @@ static bool do_insert_head(int argc, char *argv[])
222247

223248
static bool do_insert_tail(int argc, char *argv[])
224249
{
250+
char random_str[MAX_RANDSTR_LEN];
225251
int reps = 1;
226-
bool ok = true;
252+
bool ok = true, is_random = false;
227253
if (argc != 2 && argc != 3) {
228254
report(1, "%s needs 1-2 arguments", argv[0]);
229255
return false;
@@ -236,12 +262,20 @@ static bool do_insert_tail(int argc, char *argv[])
236262
return false;
237263
}
238264
}
265+
266+
if (!strcmp(inserts, "RAND")) {
267+
is_random = true;
268+
inserts = random_str;
269+
}
270+
239271
if (!q)
240272
report(3, "Warning: Calling insert tail on null queue");
241273
error_check();
242274

243275
if (exception_setup(true)) {
244276
for (int r = 0; ok && r < reps; r++) {
277+
if (is_random)
278+
random_string(random_str, MAX_RANDSTR_LEN);
245279
bool rval = q_insert_tail(q, inserts);
246280
if (rval) {
247281
qcnt++;
@@ -686,6 +720,7 @@ int main(int argc, char *argv[])
686720
}
687721
}
688722

723+
srand((unsigned int) (time(NULL)));
689724
queue_init();
690725
init_cmd();
691726
console_init();

0 commit comments

Comments
 (0)