1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include <time.h>
#include <stdlib.h>
#include <criterion/criterion.h>
extern void do_test (const char *word, size_t exp_len, const size_t expected[exp_len]);
#define ARR_LEN(array) (sizeof(array) / sizeof *(array))
#define sample_test(word, expected) do_test(word, ARR_LEN(expected), expected)
Test(tests_suite, sample_tests)
{
do_test("", 0, NULL);
do_test("4ysdf4", 0, NULL);
sample_test("CodEStAr", ((const size_t[]){0, 3, 4, 6}));
sample_test("aAbB", ((const size_t[]){1, 3}));
sample_test("ABCDEF", ((const size_t[]){0, 1, 2, 3, 4, 5}));
}
enum { MAX_LEN = 40 };
#define take_char(str) ((str)[rand() % (sizeof(str) - 1)])
// generates a random word, fill the indexes[] array, return the number of uppercase letters
static size_t random_word (char *word, size_t uppercase_indexes[])
{
size_t length = 1 + (rand() % MAX_LEN);
size_t idx_caps = 0;
for (size_t i = 0; i < length; i++) {
switch (rand() % 3) {
case 0:
word[i] = take_char("abcdefghijklmnopqrstuvwxyz");
break;
case 1:
word[i] = take_char("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
uppercase_indexes[idx_caps++] = i;
break;
default:
word[i] = take_char("@&~#0123456789-*/=,;:!.");
break;
}
}
word[length] = '\0';
return idx_caps;
}
Test(tests_suite, random_tests)
{
srand(time(NULL));
size_t expected[MAX_LEN];
char word[MAX_LEN + 1];
for (int i = 0; i < 100; i++) {
size_t exp_len = random_word(word, expected);
do_test(word, exp_len, expected);
}
}
|