1 /*
2  * This file is licensed under the GPL license. For the full content
3  * of this license, see the COPYING file at the top level of this
4  * source tree.
5  */
6 
7 /*
8  * assertion:
9  *	The function will compute the number of bytes in the string to which s points,
10  *	not including the terminating NUL character. and will return the length of s.
11  *
12  * method:
13  *	-Generate sample string s1 of variable lengths in a loop.
14  *	-Use strlen() for generated strings and check whether return value is as expected.
15  *
16 */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <time.h>
23 #include "posixtest.h"
24 
25 #define STRING_MAX_LEN 50000
26 #define STEP_COUNT 2000
27 #define TNAME "strlen/1-1.c"
28 
random_string(int len)29 char *random_string(int len)
30 {
31 	int i;
32 	char *output_string;
33 	output_string = malloc(len + 1);
34 	if (output_string == NULL) {
35 		printf(" Failed to allocate memory\n");
36 		exit(PTS_UNRESOLVED);
37 	}
38 	for (i = 0; i < len; i++)
39 		output_string[i] = rand() % 254 + 1;
40 	output_string[len] = '\0';
41 	return output_string;
42 }
43 
main(void)44 int main(void)
45 {
46 	char *ret_str;
47 	int i;
48 	size_t obtained_len;
49 
50 	for (i = 0; i < STRING_MAX_LEN; i += STEP_COUNT) {
51 		ret_str = random_string(i);
52 		obtained_len = strlen(ret_str);
53 
54 		if (obtained_len != (size_t)i) {
55 			printf(TNAME " Test Failed, return value is not as expected, "
56 					"for the given string. Expected string length: %d,"
57 					" But got: %zu.\n", i, obtained_len);
58 			exit(PTS_FAIL);
59 		}
60 		free(ret_str);
61 	}
62 	printf(TNAME " Test Passed, strlen() return value is as expected, "
63 			"for the given strings.\n");
64 	exit(PTS_PASS);
65 }
66