• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
10  *	The function will locate the first occurrence of c (converted to a char)
11  *	in the string pointed to by s. The terminating NUL character is
12  *	considered to be part of the string. Upon completion, the function will
13  *	return a pointer to the byte, or a null pointer if the byte was not found.
14  *
15  * method:
16  *	-Generate sample string s1
17  *	-Define one char, taking a character from the sample string(char1), one char as NULL(char2)
18 	and third character out of the sample string(char3).
19  *	-Use strchr for string with char1 and store result in result1.
20  *	-Use strchr for string with char2 and store result in result2.
21  *	-Use strchr for string with char3 and store result in result3.
22  *	-Compare the result1 with pointer to the defined char(char1).
23  *	-Compare the result2 with pointer to the defined char(char2).
24  *	-Compare result3 with NULL.
25  *	-Repeat the above all steps for given number of iterations.
26 */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include "posixtest.h"
33 
34 #define STRING_MAX_LEN 50000
35 #define STEP_COUNT 2000
36 #define TNAME "strchr/1-1.c"
37 #define SKIP_CHAR 'n'
38 #define MATCH_CHAR 's'
39 
random_string(int len,int char_pos)40 char *random_string(int len, int char_pos)
41 {
42 	int i;
43 	char *output_string = malloc(len + 1);
44 	if (output_string == NULL) {
45 		printf(TNAME " failed to allocate memory\n");
46 		exit(PTS_UNRESOLVED);
47 	}
48 	for (i = 0; i < len; i++) {
49 		output_string[i] = rand() % 254 + 1;
50 		/*Add character MATCH_CHAR at char_pos*/
51 		if (i == char_pos)
52 			output_string[i] = MATCH_CHAR;
53 		/*Avoid adding SKIP_CHAR and MATCH_CHAR in the string*/
54 		else if (output_string[i] == SKIP_CHAR || output_string[i] == MATCH_CHAR)
55 			i--;
56 	}
57 	output_string[len] = '\0';
58 	return output_string;
59 }
60 
main(void)61 int main(void)
62 {
63 	int i, char_pos;
64 
65 	for (i = 1; i < STRING_MAX_LEN; i += STEP_COUNT) {
66 		char *sample_str;
67 		char sample_char_1;
68 		char sample_char_2 = '\0';
69 		char sample_char_3 = 'n';
70 		char_pos = rand() % i;
71 
72 		sample_str = random_string(i, char_pos);
73 		sample_char_1  = sample_str[char_pos];
74 
75 		char *ret_str_1 = strchr(sample_str, sample_char_1);
76 		char *ret_str_2 = strchr(sample_str, sample_char_2);
77 		char *ret_str_3 = strchr(sample_str, sample_char_3);
78 
79 		if (ret_str_1 != &sample_str[char_pos]) {
80 			printf(TNAME " Test Failed, Failed to return pointer to the byte, when matching"
81 					" character is found. Expected: %p, but returned: %p\n",
82 					&sample_str[char_pos], ret_str_1);
83 			exit(PTS_FAIL);
84 		} else if (ret_str_2 != &sample_str[i]) {
85 			printf(TNAME " Test Failed, Failed to consider NUL character as a part of the"
86 					" string,\n\t\t\t\t   so failed to return pointer to the NUL character,"
87 					" when matching character is found.\n\t\t\t\t   Expected: %p,"
88 					" but returned: %p\n", &sample_str[i - 1], ret_str_2);
89 			exit(PTS_FAIL);
90 		} else if (ret_str_3 != NULL) {
91 			printf(TNAME " Test Failed, Failed to return NULL when character is not found"
92 					" in the string. Expected: NULL, but returned: %p\n", ret_str_3);
93 			exit(PTS_FAIL);
94 		}
95 		free(sample_str);
96 	}
97 	printf(TNAME " Test Passed, First case: character is found, "
98 				"so returned pointer to the byte.\n\t\t\t  Second case: The"
99 				" terminating NUL character is considered as part of the string,"
100 				" \n\t\t\t\t\tso returned pointer to the NUL character.\n\t\t\t  "
101 				"Third case: character is not found, so"
102 				" NULL pointer is returned.\n");
103 	exit(PTS_PASS);
104 }
105