1 /* 2 * 3 * Copyright (c) International Business Machines Corp., 2002 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /* 01/02/2003 Port to LTP avenkat&us.ibm.com */ 21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ 22 23 /* 24 * NAME 25 * memcmp1 -- buffer compare 26 * 27 * CALLS 28 * memcmp(3) 29 * 30 * ALGORITHM 31 * Check boundary conditions. 32 * 33 * RESTRICTIONS 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <unistd.h> 39 #include <string.h> 40 #include <errno.h> 41 42 #include "test.h" 43 44 char *TCID = "memcmp1"; 45 46 #undef BSIZE 47 #define BSIZE 4096 48 #define LEN 100 49 #define FAILED 0 50 #define PASSED 1 51 52 char buf[BSIZE]; 53 54 int local_flag = PASSED; 55 int block_number; 56 FILE *temp; 57 int TST_TOTAL = 2; 58 int anyfail(); 59 int blenter(); 60 int blexit(); 61 int instress(); 62 63 void setup(); 64 65 void clearit(); 66 void fill(char *str); 67 int checkit(char *str); 68 69 int main(int argc, char *argv[]) 70 { 71 char *p, *q; 72 73 tst_parse_opts(argc, argv, NULL, NULL); 74 75 setup(); 76 blenter(); 77 78 clearit(); 79 80 p = &buf[100]; 81 q = &buf[800]; 82 83 fill(p); 84 fill(q); 85 86 if (memcmp(p, q, LEN)) { 87 fprintf(temp, "\tmemcmp fails - should have succeeded.\n"); 88 local_flag = FAILED; 89 } 90 91 p[LEN - 1] = 0; 92 93 if (!memcmp(p, q, LEN)) { 94 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 95 local_flag = FAILED; 96 }; 97 98 p[LEN - 1] = 'a'; 99 p[0] = 0; 100 101 if (!memcmp(p, q, LEN)) { 102 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 103 local_flag = FAILED; 104 }; 105 106 p[0] = 'a'; 107 q[LEN - 1] = 0; 108 109 if (!memcmp(p, q, LEN)) { 110 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 111 local_flag = FAILED; 112 }; 113 114 q[LEN - 1] = 'a'; 115 q[0] = 0; 116 117 if (!memcmp(p, q, LEN)) { 118 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 119 local_flag = FAILED; 120 }; 121 122 q[0] = 'a'; 123 124 if (memcmp(p, q, LEN)) { 125 fprintf(temp, "\tmemcmp fails - should have succeeded.\n"); 126 local_flag = FAILED; 127 } 128 129 blexit(); 130 /*--------------------------------------------------------------*/ 131 blenter(); 132 133 clearit(); 134 135 p = &buf[800]; 136 q = &buf[100]; 137 138 fill(p); 139 fill(q); 140 141 if (memcmp(p, q, LEN)) { 142 fprintf(temp, "\tmemcmp fails - should have succeeded.\n"); 143 local_flag = FAILED; 144 } 145 146 p[LEN - 1] = 0; 147 148 if (!memcmp(p, q, LEN)) { 149 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 150 local_flag = FAILED; 151 }; 152 153 p[LEN - 1] = 'a'; 154 p[0] = 0; 155 156 if (!memcmp(p, q, LEN)) { 157 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 158 local_flag = FAILED; 159 }; 160 161 p[0] = 'a'; 162 q[LEN - 1] = 0; 163 164 if (!memcmp(p, q, LEN)) { 165 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 166 local_flag = FAILED; 167 }; 168 169 q[LEN - 1] = 'a'; 170 q[0] = 0; 171 172 if (!memcmp(p, q, LEN)) { 173 fprintf(temp, "\tmemcmp succeeded - should have failed.\n"); 174 local_flag = FAILED; 175 }; 176 177 q[0] = 'a'; 178 179 if (memcmp(p, q, LEN)) { 180 fprintf(temp, "\tmemcmp fails - should have succeeded.\n"); 181 local_flag = FAILED; 182 } 183 184 blexit(); 185 186 anyfail(); 187 tst_exit(); 188 } 189 190 void clearit(void) 191 { 192 register int i; 193 194 for (i = 0; i < BSIZE; i++) 195 buf[i] = 0; 196 } 197 198 void fill(char *str) 199 { 200 register int i; 201 for (i = 0; i < LEN; i++) 202 *str++ = 'a'; 203 } 204 205 int checkit(char *str) 206 { 207 register int i; 208 for (i = 0; i < LEN; i++) 209 if (*str++ != 'a') 210 return (-1); 211 212 return (0); 213 } 214 215 int anyfail(void) 216 { 217 tst_exit(); 218 } 219 220 void setup(void) 221 { 222 temp = stderr; 223 } 224 225 int blenter(void) 226 { 227 local_flag = PASSED; 228 return 0; 229 } 230 231 int blexit(void) 232 { 233 (local_flag == FAILED) ? tst_resm(TFAIL, 234 "Test failed") : tst_resm(TPASS, 235 "Test passed"); 236 return 0; 237 } 238