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 copy the string pointed to by s1 (including the 10 * terminating NUL character) into the array pointed to by s2. And 11 * it will return s2. 12 * 13 * method: 14 * -Generate sample string s1 of variable lengths in a loop. 15 * -Use strcpy() to copy s1 into sample string s2 in each iteration. 16 * -Compare both strings(s1 and s2). 17 * -Also compare returned pointer with s2. 18 */ 19 20 #include <stdio.h> 21 #include <string.h> 22 #include <stdlib.h> 23 #include <unistd.h> 24 #include <time.h> 25 #include "posixtest.h" 26 27 #define STRING_MAX_LEN 50000 28 #define STEP_COUNT 2000 29 #define TNAME "strcpy/1-1.c" 30 31 char *random_string(int len) 32 { 33 int i; 34 char *output_string; 35 output_string = malloc(len + 1); 36 if (output_string == NULL) { 37 printf(TNAME " Failed to allocate memory\n"); 38 exit(PTS_UNRESOLVED); 39 } 40 for (i = 0; i < len; i++) 41 /* Limiting characters from 1-255 */ 42 output_string[i] = rand() % 254 + 1; 43 output_string[len] = '\0'; 44 return output_string; 45 } 46 47 int main(void) 48 { 49 char *ret_str; 50 int i; 51 52 for (i = 0; i < STRING_MAX_LEN; i += STEP_COUNT) { 53 char *sample_str_1; 54 char *sample_str_2 = malloc(i + 1); 55 if (sample_str_2 == NULL) { 56 printf(TNAME "Failed to allocate memory\n"); 57 exit(PTS_UNRESOLVED); 58 } 59 sample_str_1 = random_string(i); 60 ret_str = strcpy(sample_str_2, sample_str_1); 61 62 if (strcmp(sample_str_1, sample_str_2) != 0) { 63 printf(TNAME " Test Failed, string copy failed. " 64 "Expected string: %s, But got: %s\n" 65 , sample_str_1, sample_str_2); 66 exit(PTS_FAIL); 67 } else if (ret_str != sample_str_2) { 68 printf(TNAME " Test Failed, return is not as expected. " 69 "Expected return: %p, But obtained: %p\n" 70 , sample_str_2, ret_str); 71 exit(PTS_FAIL); 72 } 73 free(sample_str_1); 74 free(sample_str_2); 75 } 76 printf(TNAME " Test Passed, string copied successfully\n"); 77 exit(PTS_PASS); 78 } 79