| 1 |
/* Copyright 2002-2015 Paul Hsieh |
| 2 |
* This file is part of Bstrlib. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* 3. Neither the name of bstrlib nor the names of its contributors may be |
| 15 |
* used to endorse or promote products derived from this software |
| 16 |
* without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
* POSSIBILITY OF SUCH DAMAGE. |
| 29 |
* |
| 30 |
* Alternatively, the contents of this file may be used under the terms of |
| 31 |
* GNU General Public License Version 2 (the "GPL"). |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* This file is the C unit test for Bstrlib. |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifdef HAVE_CONFIG_H |
| 39 |
#include <config.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "bstraux.h" |
| 43 |
#include "bstrlib.h" |
| 44 |
#include <check.h> |
| 45 |
#include <ctype.h> |
| 46 |
#include <limits.h> |
| 47 |
#include <stdarg.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
|
| 51 |
static void |
| 52 |
test0_0(const char *s, const char *res) |
| 53 |
{ |
| 54 |
int ret = 0; |
| 55 |
bstring b0 = bfromcstr(s); |
| 56 |
if (s) { |
| 57 |
if (b0 == NULL || res == NULL) { |
| 58 |
ck_abort(); |
| 59 |
return; /* Just a safeguard */ |
| 60 |
} |
| 61 |
ret = strlen(res); |
| 62 |
ck_assert_int_eq(b0->slen, ret); |
| 63 |
ret = memcmp(res, b0->data, b0->slen); |
| 64 |
ck_assert_int_eq(ret, 0); |
| 65 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 66 |
ret = bdestroy(b0); |
| 67 |
ck_assert_int_eq(ret, BSTR_OK); |
| 68 |
} else { |
| 69 |
ck_assert(res == NULL); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
test0_1(const char *s, int len, const char *res) |
| 75 |
{ |
| 76 |
int ret = 0; |
| 77 |
bstring b0 = bfromcstralloc(len, s); |
| 78 |
if (s) { |
| 79 |
if (b0 == NULL || res == NULL) { |
| 80 |
ck_abort(); |
| 81 |
return; /* Just a safeguard */ |
| 82 |
} |
| 83 |
ret = strlen(res); |
| 84 |
ck_assert_int_eq(b0->slen, ret); |
| 85 |
ret = memcmp(res, b0->data, b0->slen); |
| 86 |
ck_assert_int_eq(ret, 0); |
| 87 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 88 |
ck_assert(b0->mlen >= len); |
| 89 |
ret = bdestroy(b0); |
| 90 |
ck_assert_int_eq(ret, BSTR_OK); |
| 91 |
} else { |
| 92 |
ck_assert(res == NULL); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#define EMPTY_STRING "" |
| 97 |
|
| 98 |
#define SHORT_STRING "bogus" |
| 99 |
|
| 100 |
#define EIGHT_CHAR_STRING "Waterloo" |
| 101 |
|
| 102 |
#define LONG_STRING \ |
| 103 |
"This is a bogus but reasonably long string. " \ |
| 104 |
"Just long enough to cause some mallocing." |
| 105 |
|
| 106 |
static void |
| 107 |
test0_2(const char *s, int l) |
| 108 |
{ |
| 109 |
int i_count = l * 2; |
| 110 |
int j_count = l * 2; |
| 111 |
int k_count = l + 1; |
| 112 |
int is_valgrind = getenv("VALGRIND_OPTS") != NULL; |
| 113 |
|
| 114 |
/* |
| 115 |
* The full matrix is O(l^3) and turns into millions of allocations for |
| 116 |
* LONG_STRING. Keep exhaustive coverage for small inputs; sample evenly |
| 117 |
* for large inputs and valgrind runs. |
| 118 |
*/ |
| 119 |
if (l > 32) { |
| 120 |
i_count = 32; |
| 121 |
j_count = 32; |
| 122 |
k_count = 33; |
| 123 |
} |
| 124 |
if (is_valgrind) { |
| 125 |
if (i_count > 12) i_count = 12; |
| 126 |
if (j_count > 12) j_count = 12; |
| 127 |
if (k_count > 10) k_count = 10; |
| 128 |
} |
| 129 |
|
| 130 |
for (int ii = 0; ii < i_count; ii++) { |
| 131 |
int i = (i_count <= 1) ? 0 : (ii * ((l * 2) - 1)) / (i_count - 1); |
| 132 |
for (int jj = 0; jj < j_count; jj++) { |
| 133 |
int j = (j_count <= 1) ? 0 : (jj * ((l * 2) - 1)) / (j_count - 1); |
| 134 |
for (int kk = 0; kk < k_count; kk++) { |
| 135 |
int k = (k_count <= 1) ? 0 : (kk * l) / (k_count - 1); |
| 136 |
const char *t = s ? (s + k) : NULL; |
| 137 |
bstring b = bfromcstrrangealloc(i, j, t); |
| 138 |
if (NULL == b) { |
| 139 |
ck_assert(!(i < j && t != NULL)); |
| 140 |
continue; |
| 141 |
} |
| 142 |
if (t == NULL || b->data == NULL) { |
| 143 |
bdestroy(b); |
| 144 |
ck_abort(); |
| 145 |
return; /* Just a safeguard */ |
| 146 |
} |
| 147 |
ck_assert_int_eq(b->slen, l - k); |
| 148 |
ck_assert_int_eq(b->data[l - k], '\0'); |
| 149 |
ck_assert(b->mlen > b->slen); |
| 150 |
ck_assert_int_eq(memcmp(t, b->data, l - k + 1), 0); |
| 151 |
bdestroy(b); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
START_TEST(core_000) |
| 158 |
{ |
| 159 |
/* tests with NULL */ |
| 160 |
test0_0(NULL, NULL); |
| 161 |
/* normal operation tests */ |
| 162 |
test0_0(EMPTY_STRING, EMPTY_STRING); |
| 163 |
test0_0(SHORT_STRING, SHORT_STRING); |
| 164 |
test0_0(LONG_STRING, LONG_STRING); |
| 165 |
/* tests with NULL */ |
| 166 |
test0_1(NULL, 0, NULL); |
| 167 |
test0_1(NULL, 30, NULL); |
| 168 |
/* normal operation tests */ |
| 169 |
test0_1(EMPTY_STRING, 0, EMPTY_STRING); |
| 170 |
test0_1(EMPTY_STRING, 30, EMPTY_STRING); |
| 171 |
test0_1(SHORT_STRING, 0, SHORT_STRING); |
| 172 |
test0_1(SHORT_STRING, 30, SHORT_STRING); |
| 173 |
test0_1(LONG_STRING, 0, LONG_STRING); |
| 174 |
test0_1(LONG_STRING, 30, LONG_STRING); |
| 175 |
/* bfromcstrrangealloc tests */ |
| 176 |
test0_2(NULL, 2); |
| 177 |
test0_2(EMPTY_STRING, sizeof(EMPTY_STRING) - 1); |
| 178 |
test0_2(LONG_STRING, sizeof(LONG_STRING) - 1); |
| 179 |
} |
| 180 |
END_TEST |
| 181 |
|
| 182 |
static void |
| 183 |
test1_0(const void *blk, int len, const char *res) |
| 184 |
{ |
| 185 |
int ret = 0; |
| 186 |
bstring b0 = blk2bstr(blk, len); |
| 187 |
if (res && b0) { |
| 188 |
ck_assert_int_eq(b0->slen, len); |
| 189 |
ret = memcmp(res, b0->data, len); |
| 190 |
ck_assert_int_eq(ret, 0); |
| 191 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 192 |
ret = bdestroy(b0); |
| 193 |
ck_assert_int_eq(ret, BSTR_OK); |
| 194 |
} else { |
| 195 |
ck_assert(b0 == NULL); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
START_TEST(core_001) |
| 200 |
{ |
| 201 |
/* tests with NULL */ |
| 202 |
test1_0(NULL, 10, NULL); |
| 203 |
test1_0(NULL, 0, NULL); |
| 204 |
test1_0(NULL, -1, NULL); |
| 205 |
/* normal operation tests */ |
| 206 |
test1_0(SHORT_STRING, sizeof(SHORT_STRING) - 1, SHORT_STRING); |
| 207 |
test1_0(LONG_STRING, sizeof(LONG_STRING) - 1, LONG_STRING); |
| 208 |
test1_0(LONG_STRING, 5, "This "); |
| 209 |
test1_0(LONG_STRING, 0, ""); |
| 210 |
test1_0(LONG_STRING, -1, NULL); |
| 211 |
} |
| 212 |
END_TEST |
| 213 |
|
| 214 |
static void |
| 215 |
test2_0(const bstring b, char z, const unsigned char *res) |
| 216 |
{ |
| 217 |
int ret = 0; |
| 218 |
char *s = bstr2cstr(b, z); |
| 219 |
if (res && s) { |
| 220 |
ret = strlen(s); |
| 221 |
ck_assert_int_eq(b->slen, ret); |
| 222 |
ret = memcmp(res, b->data, b->slen); |
| 223 |
ck_assert_int_eq(ret, 0); |
| 224 |
} else { |
| 225 |
ck_assert(s == NULL); |
| 226 |
} |
| 227 |
free(s); |
| 228 |
} |
| 229 |
|
| 230 |
/* test input used below */ |
| 231 |
struct tagbstring emptyBstring = bsStatic(EMPTY_STRING); |
| 232 |
|
| 233 |
struct tagbstring shortBstring = bsStatic(SHORT_STRING); |
| 234 |
|
| 235 |
struct tagbstring longBstring = bsStatic(LONG_STRING); |
| 236 |
|
| 237 |
struct tagbstring badBstring1 = { |
| 238 |
8, |
| 239 |
4, |
| 240 |
(unsigned char *)NULL |
| 241 |
}; |
| 242 |
|
| 243 |
struct tagbstring badBstring2 = { |
| 244 |
2, |
| 245 |
-5, |
| 246 |
(unsigned char *)SHORT_STRING |
| 247 |
}; |
| 248 |
|
| 249 |
struct tagbstring badBstring3 = { |
| 250 |
2, |
| 251 |
5, |
| 252 |
(unsigned char *)SHORT_STRING |
| 253 |
}; |
| 254 |
|
| 255 |
struct tagbstring xxxxxBstring = bsStatic("xxxxx"); |
| 256 |
|
| 257 |
START_TEST(core_002) |
| 258 |
{ |
| 259 |
/* tests with NULL */ |
| 260 |
test2_0(NULL, '?', NULL); |
| 261 |
/* normal operation tests */ |
| 262 |
test2_0(&emptyBstring, '?', emptyBstring.data); |
| 263 |
test2_0(&shortBstring, '?', shortBstring.data); |
| 264 |
test2_0(&longBstring, '?', longBstring.data); |
| 265 |
test2_0(&badBstring1, '?', NULL); |
| 266 |
test2_0(&badBstring2, '?', NULL); |
| 267 |
} |
| 268 |
END_TEST |
| 269 |
|
| 270 |
static void |
| 271 |
test3_0(const bstring b) |
| 272 |
{ |
| 273 |
int ret = 0; |
| 274 |
bstring b0 = bstrcpy(b); |
| 275 |
if (!b || !b->data || b->slen < 0) { |
| 276 |
ck_assert(b0 == NULL); |
| 277 |
} else if (b0 == NULL) { |
| 278 |
ck_abort(); |
| 279 |
} else { |
| 280 |
ck_assert_int_eq(b0->slen, b->slen); |
| 281 |
ret = memcmp(b->data, b0->data, b->slen); |
| 282 |
ck_assert_int_eq(ret, 0); |
| 283 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 284 |
ret = bdestroy(b0); |
| 285 |
ck_assert_int_eq(ret, BSTR_OK); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
START_TEST(core_003) |
| 290 |
{ |
| 291 |
/* tests with NULL to make sure that there is NULL propogation */ |
| 292 |
test3_0(NULL); |
| 293 |
test3_0(&badBstring1); |
| 294 |
test3_0(&badBstring2); |
| 295 |
/* normal operation tests */ |
| 296 |
test3_0(&emptyBstring); |
| 297 |
test3_0(&shortBstring); |
| 298 |
test3_0(&longBstring); |
| 299 |
} |
| 300 |
END_TEST |
| 301 |
|
| 302 |
static void |
| 303 |
test4_0(const bstring b, int left, int len, const char *res) |
| 304 |
{ |
| 305 |
int ret = 0; |
| 306 |
bstring b0 = bmidstr(b, left, len); |
| 307 |
if (b0 == NULL) { |
| 308 |
ck_assert(!b || !b->data || b->slen < 0 || len < 0); |
| 309 |
} else { |
| 310 |
if (b == NULL || res == NULL) { |
| 311 |
ck_abort(); |
| 312 |
return; /* Just a safeguard */ |
| 313 |
} |
| 314 |
if (len >= 0) { |
| 315 |
ck_assert(b0->slen <= len); |
| 316 |
} |
| 317 |
ret = strlen(res); |
| 318 |
ck_assert_int_eq(b0->slen, ret); |
| 319 |
ret = memcmp(res, b0->data, b0->slen); |
| 320 |
ck_assert_int_eq(ret, 0); |
| 321 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 322 |
ret = bdestroy(b0); |
| 323 |
ck_assert_int_eq(ret, BSTR_OK); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
START_TEST(core_004) |
| 328 |
{ |
| 329 |
/* tests with NULL to make sure that there is NULL propogation */ |
| 330 |
test4_0(NULL, 0, 0, NULL); |
| 331 |
test4_0(NULL, 0, 2, NULL); |
| 332 |
test4_0(NULL, 0, -2, NULL); |
| 333 |
test4_0(NULL, -5, 2, NULL); |
| 334 |
test4_0(NULL, -5, -2, NULL); |
| 335 |
test4_0(&badBstring1, 1, 3, NULL); |
| 336 |
test4_0(&badBstring2, 1, 3, NULL); |
| 337 |
|
| 338 |
/* normal operation tests on all sorts of subranges */ |
| 339 |
test4_0(&emptyBstring, 0, 0, ""); |
| 340 |
test4_0(&emptyBstring, 0, -1, ""); |
| 341 |
test4_0(&emptyBstring, 1, 3, ""); |
| 342 |
test4_0(&shortBstring, 0, 0, ""); |
| 343 |
test4_0(&shortBstring, 0, -1, ""); |
| 344 |
test4_0(&shortBstring, 1, 3, "ogu"); |
| 345 |
test4_0(&shortBstring, -1, 3, "bo"); |
| 346 |
test4_0(&shortBstring, -1, 9, "bogus"); |
| 347 |
test4_0(&shortBstring, 3, -1, ""); |
| 348 |
test4_0(&shortBstring, 9, 3, ""); |
| 349 |
} |
| 350 |
END_TEST |
| 351 |
|
| 352 |
static void |
| 353 |
test5_0(bstring b0, const bstring b1, const char *res) |
| 354 |
{ |
| 355 |
bstring b2; |
| 356 |
int ret = 0; |
| 357 |
if (b0 && b0->data && b0->slen >= 0 && |
| 358 |
b1 && b1->data && b1->slen >= 0) { |
| 359 |
b2 = bstrcpy(b0); |
| 360 |
if (b2 == NULL) { |
| 361 |
ck_abort(); |
| 362 |
return; /* Just a safeguard */ |
| 363 |
} |
| 364 |
bwriteprotect(*b2); |
| 365 |
ret = bconcat(b2, b1); |
| 366 |
ck_assert_int_ne(ret, 0); |
| 367 |
ret = biseq(b0, b2); |
| 368 |
ck_assert_int_eq(ret, 1); |
| 369 |
bwriteallow(*b2); |
| 370 |
ret = bconcat(b2, b1); |
| 371 |
ck_assert_int_eq(b2->slen, b0->slen + b1->slen); |
| 372 |
if (res == NULL) { |
| 373 |
ck_abort(); |
| 374 |
return; /* Just a safeguard */ |
| 375 |
} |
| 376 |
ret = memcmp(b2->data, res, b2->slen); |
| 377 |
ck_assert_int_eq(ret, 0); |
| 378 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 379 |
ret = bdestroy(b2); |
| 380 |
ck_assert_int_eq(ret, BSTR_OK); |
| 381 |
} else { |
| 382 |
ret = bconcat(b0, b1); |
| 383 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
static void |
| 388 |
test5_1(void) |
| 389 |
{ |
| 390 |
bstring b, c; |
| 391 |
struct tagbstring t; |
| 392 |
int i, ret; |
| 393 |
for (i = 0; i < longBstring.slen; i++) { |
| 394 |
b = bstrcpy(&longBstring); |
| 395 |
if (b == NULL) { |
| 396 |
ck_abort(); |
| 397 |
return; /* Just a safeguard */ |
| 398 |
} |
| 399 |
c = bstrcpy(&longBstring); |
| 400 |
if (c == NULL) { |
| 401 |
ck_abort(); |
| 402 |
return; /* Just a safeguard */ |
| 403 |
} |
| 404 |
bmid2tbstr(t, b, i, longBstring.slen); |
| 405 |
ret = bconcat(c, &t); |
| 406 |
ck_assert_int_eq(ret, 0); |
| 407 |
ret = bconcat(b, &t); |
| 408 |
ck_assert_int_eq(ret, 0); |
| 409 |
ret = biseq(b, c); |
| 410 |
ck_assert_int_eq(ret, 1); |
| 411 |
ret = bdestroy(b); |
| 412 |
ck_assert_int_eq(ret, BSTR_OK); |
| 413 |
ret = bdestroy(c); |
| 414 |
ck_assert_int_eq(ret, BSTR_OK); |
| 415 |
} |
| 416 |
b = bfromcstr("abcd"); |
| 417 |
if (b == NULL) { |
| 418 |
ck_abort(); |
| 419 |
return; /* Just a safeguard */ |
| 420 |
} |
| 421 |
c = bfromcstr("abcd"); |
| 422 |
if (c == NULL) { |
| 423 |
ck_abort(); |
| 424 |
return; /* Just a safeguard */ |
| 425 |
} |
| 426 |
for (i = 0; i < 100; i++) { |
| 427 |
bmid2tbstr(t, b, 0, 3); |
| 428 |
ret = bcatcstr(c, "abc"); |
| 429 |
ck_assert_int_eq(ret, 0); |
| 430 |
ret = bconcat(b, &t); |
| 431 |
ck_assert_int_eq(ret, 0); |
| 432 |
ret = biseq(b, c); |
| 433 |
ck_assert_int_eq(ret, 1); |
| 434 |
} |
| 435 |
ret = bdestroy(b); |
| 436 |
ck_assert_int_eq(ret, BSTR_OK); |
| 437 |
ret = bdestroy(c); |
| 438 |
ck_assert_int_eq(ret, BSTR_OK); |
| 439 |
} |
| 440 |
|
| 441 |
START_TEST(core_005) |
| 442 |
{ |
| 443 |
/* tests with NULL */ |
| 444 |
test5_0(NULL, NULL, NULL); |
| 445 |
test5_0(NULL, &emptyBstring, NULL); |
| 446 |
test5_0(&emptyBstring, NULL, ""); |
| 447 |
test5_0(&emptyBstring, &badBstring1, NULL); |
| 448 |
test5_0(&emptyBstring, &badBstring2, NULL); |
| 449 |
test5_0(&badBstring1, &emptyBstring, NULL); |
| 450 |
test5_0(&badBstring2, &emptyBstring, NULL); |
| 451 |
/* normal operation tests on all sorts of subranges */ |
| 452 |
test5_0(&emptyBstring, &emptyBstring, ""); |
| 453 |
test5_0(&emptyBstring, &shortBstring, "bogus"); |
| 454 |
test5_0(&shortBstring, &emptyBstring, "bogus"); |
| 455 |
test5_0(&shortBstring, &shortBstring, "bogusbogus"); |
| 456 |
test5_1(); |
| 457 |
} |
| 458 |
END_TEST |
| 459 |
|
| 460 |
static void |
| 461 |
test6_0(bstring b, char c, const char *res) |
| 462 |
{ |
| 463 |
bstring b0; |
| 464 |
int ret = 0; |
| 465 |
if (b && b->data && b->slen >= 0) { |
| 466 |
b0 = bstrcpy(b); |
| 467 |
if (b0 == NULL) { |
| 468 |
ck_abort(); |
| 469 |
return; /* Just a safeguard */ |
| 470 |
} |
| 471 |
bwriteprotect(*b0); |
| 472 |
ret = bconchar(b0, c); |
| 473 |
ck_assert_int_ne(ret, 0); |
| 474 |
ret = biseq(b0, b); |
| 475 |
ck_assert_int_eq(ret, 1); |
| 476 |
bwriteallow(*b0); |
| 477 |
ret = bconchar(b0, c); |
| 478 |
ck_assert_int_eq(ret, 0); |
| 479 |
ck_assert_int_eq(b0->slen, b->slen + 1); |
| 480 |
if (res == NULL) { |
| 481 |
ck_abort(); |
| 482 |
return; /* Just a safeguard */ |
| 483 |
} |
| 484 |
ret = strlen(res); |
| 485 |
ck_assert_int_eq(b0->slen, ret); |
| 486 |
ret = memcmp(b0->data, res, b0->slen); |
| 487 |
ck_assert_int_eq(ret, 0); |
| 488 |
ck_assert_int_eq(b0->data[b0->slen], '\0'); |
| 489 |
ret = bdestroy(b0); |
| 490 |
ck_assert_int_eq(ret, BSTR_OK); |
| 491 |
} else { |
| 492 |
ret = bconchar(b, c); |
| 493 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
START_TEST(core_006) |
| 498 |
{ |
| 499 |
/* tests with NULL */ |
| 500 |
test6_0(NULL, 'x', NULL); |
| 501 |
test6_0(&badBstring1, 'x', NULL); |
| 502 |
test6_0(&badBstring2, 'x', NULL); |
| 503 |
/* normal operation tests on all sorts of subranges */ |
| 504 |
test6_0(&emptyBstring, 'x', "x"); |
| 505 |
test6_0(&shortBstring, 'x', "bogusx"); |
| 506 |
} |
| 507 |
END_TEST |
| 508 |
|
| 509 |
static void |
| 510 |
test7x8_0(int (*fnptr)(const bstring, const bstring), |
| 511 |
const bstring b0, const bstring b1, int res) |
| 512 |
{ |
| 513 |
int ret = 0; |
| 514 |
ret = fnptr(b0, b1); |
| 515 |
ck_assert_int_eq(ret, res); |
| 516 |
} |
| 517 |
|
| 518 |
static void |
| 519 |
test7x8(int (* fnptr)(const bstring, const bstring), |
| 520 |
int retFail, int retLT, int retGT, int retEQ) |
| 521 |
{ |
| 522 |
/* tests with NULL */ |
| 523 |
test7x8_0(fnptr, NULL, NULL, retFail); |
| 524 |
test7x8_0(fnptr, &emptyBstring, NULL, retFail); |
| 525 |
test7x8_0(fnptr, NULL, &emptyBstring, retFail); |
| 526 |
test7x8_0(fnptr, &shortBstring, NULL, retFail); |
| 527 |
test7x8_0(fnptr, NULL, &shortBstring, retFail); |
| 528 |
test7x8_0(fnptr, &badBstring1, &badBstring1, retFail); |
| 529 |
test7x8_0(fnptr, &badBstring2, &badBstring2, retFail); |
| 530 |
test7x8_0(fnptr, &shortBstring, &badBstring2, retFail); |
| 531 |
test7x8_0(fnptr, &badBstring2, &shortBstring, retFail); |
| 532 |
/* normal operation tests on all sorts of subranges */ |
| 533 |
test7x8_0(fnptr, &emptyBstring, &emptyBstring, retEQ); |
| 534 |
test7x8_0(fnptr, &shortBstring, &emptyBstring, retGT); |
| 535 |
test7x8_0(fnptr, &emptyBstring, &shortBstring, retLT); |
| 536 |
test7x8_0(fnptr, &shortBstring, &shortBstring, retEQ); |
| 537 |
bstring b = bstrcpy(&shortBstring); |
| 538 |
if (b == NULL) { |
| 539 |
ck_abort(); |
| 540 |
} else { |
| 541 |
b->data[1]++; |
| 542 |
test7x8_0(fnptr, b, &shortBstring, retGT); |
| 543 |
int ret = bdestroy(b); |
| 544 |
ck_assert_int_eq(ret, BSTR_OK); |
| 545 |
if (fnptr == biseq) { |
| 546 |
test7x8_0(fnptr, &shortBstring, &longBstring, retGT); |
| 547 |
test7x8_0(fnptr, &longBstring, &shortBstring, retLT); |
| 548 |
} else { |
| 549 |
test7x8_0(fnptr, &shortBstring, &longBstring, 'b' - 'T'); |
| 550 |
test7x8_0(fnptr, &longBstring, &shortBstring, 'T' - 'b'); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
START_TEST(core_007) |
| 556 |
{ |
| 557 |
test7x8(biseq, -1, 0, 0, 1); |
| 558 |
} |
| 559 |
END_TEST |
| 560 |
|
| 561 |
START_TEST(core_008) |
| 562 |
{ |
| 563 |
test7x8(bstrcmp, SHRT_MIN, -1, 1, 0); |
| 564 |
} |
| 565 |
END_TEST |
| 566 |
|
| 567 |
static void |
| 568 |
test9_0(const bstring b0, const bstring b1, int n, int res) |
| 569 |
{ |
| 570 |
int ret = 0; |
| 571 |
ret = bstrncmp(b0, b1, n); |
| 572 |
ck_assert_int_eq(ret, res); |
| 573 |
} |
| 574 |
|
| 575 |
START_TEST(core_009) |
| 576 |
{ |
| 577 |
/* tests with NULL */ |
| 578 |
test9_0(NULL, NULL, 0, SHRT_MIN); |
| 579 |
test9_0(NULL, NULL, -1, SHRT_MIN); |
| 580 |
test9_0(NULL, NULL, 1, SHRT_MIN); |
| 581 |
test9_0(&emptyBstring, NULL, 0, SHRT_MIN); |
| 582 |
test9_0(NULL, &emptyBstring, 0, SHRT_MIN); |
| 583 |
test9_0(&emptyBstring, NULL, 1, SHRT_MIN); |
| 584 |
test9_0(NULL, &emptyBstring, 1, SHRT_MIN); |
| 585 |
test9_0(&badBstring1, &badBstring1, 1, SHRT_MIN); |
| 586 |
test9_0(&badBstring2, &badBstring2, 1, SHRT_MIN); |
| 587 |
test9_0(&emptyBstring, &badBstring1, 1, SHRT_MIN); |
| 588 |
test9_0(&emptyBstring, &badBstring2, 1, SHRT_MIN); |
| 589 |
test9_0(&badBstring1, &emptyBstring, 1, SHRT_MIN); |
| 590 |
test9_0(&badBstring2, &emptyBstring, 1, SHRT_MIN); |
| 591 |
/* normal operation tests on all sorts of subranges */ |
| 592 |
test9_0(&emptyBstring, &emptyBstring, -1, 0); |
| 593 |
test9_0(&emptyBstring, &emptyBstring, 0, 0); |
| 594 |
test9_0(&emptyBstring, &emptyBstring, 1, 0); |
| 595 |
test9_0(&shortBstring, &shortBstring, -1, 0); |
| 596 |
test9_0(&shortBstring, &shortBstring, 0, 0); |
| 597 |
test9_0(&shortBstring, &shortBstring, 1, 0); |
| 598 |
test9_0(&shortBstring, &shortBstring, 9, 0); |
| 599 |
} |
| 600 |
END_TEST |
| 601 |
|
| 602 |
static void |
| 603 |
test10_0(bstring b, int res, int nochange) |
| 604 |
{ |
| 605 |
struct tagbstring sb = bsStatic ("<NULL>"); |
| 606 |
int x, ret = 0; |
| 607 |
if (b) { |
| 608 |
sb = *b; |
| 609 |
} |
| 610 |
ret = bdestroy(b); |
| 611 |
if (b) { |
| 612 |
if (ret >= 0) { |
| 613 |
/* If the bdestroy was successful we have to assume |
| 614 |
* the contents were "changed" |
| 615 |
*/ |
| 616 |
x = 1; |
| 617 |
} else { |
| 618 |
x = memcmp (&sb, b, sizeof sb); |
| 619 |
} |
| 620 |
} else { |
| 621 |
x = !nochange; |
| 622 |
} |
| 623 |
ck_assert_int_eq(ret, res); |
| 624 |
ck_assert_int_ne(!nochange, !x); |
| 625 |
} |
| 626 |
|
| 627 |
START_TEST(core_010) |
| 628 |
{ |
| 629 |
bstring c = bstrcpy(&shortBstring); |
| 630 |
if (c == NULL) { |
| 631 |
ck_abort(); |
| 632 |
return; /* Just a safeguard */ |
| 633 |
} |
| 634 |
bstring b = bstrcpy(&emptyBstring); |
| 635 |
if (b == NULL) { |
| 636 |
ck_abort(); |
| 637 |
return; /* Just a safeguard */ |
| 638 |
} |
| 639 |
/* tests with NULL */ |
| 640 |
test10_0(NULL, BSTR_ERR, 1); |
| 641 |
/* protected, constant and regular instantiations on empty or not */ |
| 642 |
bwriteprotect(*b); |
| 643 |
bwriteprotect(*c); |
| 644 |
test10_0(b, BSTR_ERR, 1); |
| 645 |
test10_0(c, BSTR_ERR, 1); |
| 646 |
bwriteallow(*b); |
| 647 |
bwriteallow(*c); |
| 648 |
test10_0(b, BSTR_OK, 0); |
| 649 |
test10_0(c, BSTR_OK, 0); |
| 650 |
test10_0(&emptyBstring, BSTR_ERR, 1); |
| 651 |
bwriteallow(emptyBstring); |
| 652 |
test10_0(&emptyBstring, BSTR_ERR, 1); |
| 653 |
test10_0(&shortBstring, BSTR_ERR, 1); |
| 654 |
bwriteallow (emptyBstring); |
| 655 |
test10_0(&shortBstring, BSTR_ERR, 1); |
| 656 |
test10_0(&badBstring1, BSTR_ERR, 1); |
| 657 |
test10_0(&badBstring2, BSTR_ERR, 1); |
| 658 |
} |
| 659 |
END_TEST |
| 660 |
|
| 661 |
static void |
| 662 |
test11_0(bstring s1, int pos, const bstring s2, int res) |
| 663 |
{ |
| 664 |
int ret = binstr(s1, pos, s2); |
| 665 |
ck_assert_int_eq(ret, res); |
| 666 |
} |
| 667 |
|
| 668 |
static void |
| 669 |
test11_1(bstring s1, int pos, const bstring s2, int res) |
| 670 |
{ |
| 671 |
int ret = binstrcaseless(s1, pos, s2); |
| 672 |
ck_assert_int_eq(ret, res); |
| 673 |
} |
| 674 |
|
| 675 |
START_TEST(core_011) |
| 676 |
{ |
| 677 |
int ret; |
| 678 |
bstring b, c; |
| 679 |
test11_0(NULL, 0, NULL, BSTR_ERR); |
| 680 |
test11_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 681 |
test11_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 682 |
test11_0(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 683 |
test11_0(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 684 |
test11_0(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 685 |
test11_0(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 686 |
test11_0(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 687 |
test11_0(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 688 |
test11_0(&emptyBstring, 0, &emptyBstring, 0); |
| 689 |
test11_0(&emptyBstring, 1, &emptyBstring, BSTR_ERR); |
| 690 |
test11_0(&shortBstring, 1, &shortBstring, BSTR_ERR); |
| 691 |
test11_0(&shortBstring, 5, &emptyBstring, 5); |
| 692 |
test11_0(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 693 |
test11_0(&shortBstring, 0, &shortBstring, 0); |
| 694 |
test11_0(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 695 |
ret = bdestroy(b); |
| 696 |
ck_assert_int_eq(ret, BSTR_OK); |
| 697 |
test11_0(&shortBstring, 0, b = bfromcstr("BOGUS"), BSTR_ERR); |
| 698 |
ret = bdestroy(b); |
| 699 |
ck_assert_int_eq(ret, BSTR_OK); |
| 700 |
test11_0(&longBstring, 0, &shortBstring, 10); |
| 701 |
test11_0(&longBstring, 20, &shortBstring, BSTR_ERR); |
| 702 |
test11_0(c = bfromcstr("sssssssssap"), 0, b = bfromcstr("sap"), 8); |
| 703 |
ret = bdestroy(c); |
| 704 |
ck_assert_int_eq(ret, BSTR_OK); |
| 705 |
ret = bdestroy(b); |
| 706 |
ck_assert_int_eq(ret, BSTR_OK); |
| 707 |
test11_0(c = bfromcstr("sssssssssap"), 3, b = bfromcstr("sap"), 8); |
| 708 |
ret = bdestroy(c); |
| 709 |
ck_assert_int_eq(ret, BSTR_OK); |
| 710 |
ret = bdestroy(b); |
| 711 |
ck_assert_int_eq(ret, BSTR_OK); |
| 712 |
test11_0(c = bfromcstr("ssssssssssap"), 3, b = bfromcstr("sap"), 9); |
| 713 |
ret = bdestroy(c); |
| 714 |
ck_assert_int_eq(ret, BSTR_OK); |
| 715 |
ret = bdestroy(b); |
| 716 |
ck_assert_int_eq(ret, BSTR_OK); |
| 717 |
test11_0(c = bfromcstr("sssssssssap"), 0, b = bfromcstr("s"), 0); |
| 718 |
ret = bdestroy(c); |
| 719 |
ck_assert_int_eq(ret, BSTR_OK); |
| 720 |
ret = bdestroy(b); |
| 721 |
ck_assert_int_eq(ret, BSTR_OK); |
| 722 |
test11_0(c = bfromcstr("sssssssssap"), 3, b = bfromcstr("s"), 3); |
| 723 |
ret = bdestroy(c); |
| 724 |
ck_assert_int_eq(ret, BSTR_OK); |
| 725 |
ret = bdestroy(b); |
| 726 |
ck_assert_int_eq(ret, BSTR_OK); |
| 727 |
test11_0(c = bfromcstr("sssssssssap"), 0, b = bfromcstr("a"), 9); |
| 728 |
ret = bdestroy(c); |
| 729 |
ck_assert_int_eq(ret, BSTR_OK); |
| 730 |
ret = bdestroy(b); |
| 731 |
ck_assert_int_eq(ret, BSTR_OK); |
| 732 |
test11_0(c = bfromcstr("sssssssssap"), 5, b = bfromcstr("a"), 9); |
| 733 |
ret = bdestroy(c); |
| 734 |
ck_assert_int_eq(ret, BSTR_OK); |
| 735 |
ret = bdestroy(b); |
| 736 |
ck_assert_int_eq(ret, BSTR_OK); |
| 737 |
test11_0(c = bfromcstr("sasasasasap"), 0, b = bfromcstr("sap"), 8); |
| 738 |
ret = bdestroy(c); |
| 739 |
ck_assert_int_eq(ret, BSTR_OK); |
| 740 |
ret = bdestroy(b); |
| 741 |
ck_assert_int_eq(ret, BSTR_OK); |
| 742 |
test11_0(c = bfromcstr("ssasasasasap"), 0, b = bfromcstr("sap"), 9); |
| 743 |
ret = bdestroy(c); |
| 744 |
ck_assert_int_eq(ret, BSTR_OK); |
| 745 |
ret = bdestroy(b); |
| 746 |
ck_assert_int_eq(ret, BSTR_OK); |
| 747 |
test11_1(NULL, 0, NULL, BSTR_ERR); |
| 748 |
test11_1(&emptyBstring, 0, NULL, BSTR_ERR); |
| 749 |
test11_1(NULL, 0, &emptyBstring, BSTR_ERR); |
| 750 |
test11_1(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 751 |
test11_1(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 752 |
test11_1(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 753 |
test11_1(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 754 |
test11_1(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 755 |
test11_1(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 756 |
test11_1(&emptyBstring, 0, &emptyBstring, 0); |
| 757 |
test11_1(&emptyBstring, 1, &emptyBstring, BSTR_ERR); |
| 758 |
test11_1(&shortBstring, 1, &shortBstring, BSTR_ERR); |
| 759 |
test11_1(&shortBstring, 5, &emptyBstring, 5); |
| 760 |
test11_1(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 761 |
test11_1(&shortBstring, 0, &shortBstring, 0); |
| 762 |
test11_1(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 763 |
ret = bdestroy(b); |
| 764 |
ck_assert_int_eq(ret, BSTR_OK); |
| 765 |
test11_1(&shortBstring, 0, b = bfromcstr("BOGUS"), 0); |
| 766 |
ret = bdestroy(b); |
| 767 |
ck_assert_int_eq(ret, BSTR_OK); |
| 768 |
test11_1(&longBstring, 0, &shortBstring, 10); |
| 769 |
test11_1(&longBstring, 20, &shortBstring, BSTR_ERR); |
| 770 |
} |
| 771 |
END_TEST |
| 772 |
|
| 773 |
static void |
| 774 |
test12_0(bstring s1, int pos, const bstring s2, int res) |
| 775 |
{ |
| 776 |
int ret = binstrr(s1, pos, s2); |
| 777 |
ck_assert_int_eq(ret, res); |
| 778 |
} |
| 779 |
|
| 780 |
static void |
| 781 |
test12_1(bstring s1, int pos, const bstring s2, int res) |
| 782 |
{ |
| 783 |
int ret = binstrrcaseless (s1, pos, s2); |
| 784 |
ck_assert_int_eq(ret, res); |
| 785 |
} |
| 786 |
|
| 787 |
START_TEST(core_012) |
| 788 |
{ |
| 789 |
bstring b; |
| 790 |
int ret = 0; |
| 791 |
test12_0(NULL, 0, NULL, BSTR_ERR); |
| 792 |
test12_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 793 |
test12_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 794 |
test12_0(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 795 |
test12_0(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 796 |
test12_0(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 797 |
test12_0(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 798 |
test12_0(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 799 |
test12_0(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 800 |
test12_0(&emptyBstring, 0, &emptyBstring, 0); |
| 801 |
test12_0(&emptyBstring, 1, &emptyBstring, BSTR_ERR); |
| 802 |
test12_0(&shortBstring, 1, &shortBstring, 0); |
| 803 |
test12_0(&shortBstring, 5, &emptyBstring, 5); |
| 804 |
test12_0(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 805 |
test12_0(&shortBstring, 0, &shortBstring, 0); |
| 806 |
test12_0(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 807 |
ret = bdestroy(b); |
| 808 |
ck_assert_int_eq(ret, BSTR_OK); |
| 809 |
test12_0(&shortBstring, 0, b = bfromcstr("BOGUS"), BSTR_ERR); |
| 810 |
ret = bdestroy(b); |
| 811 |
ck_assert_int_eq(ret, BSTR_OK); |
| 812 |
test12_0(&longBstring, 0, &shortBstring, BSTR_ERR); |
| 813 |
test12_0(&longBstring, 20, &shortBstring, 10); |
| 814 |
test12_1(NULL, 0, NULL, BSTR_ERR); |
| 815 |
test12_1(&emptyBstring, 0, NULL, BSTR_ERR); |
| 816 |
test12_1(NULL, 0, &emptyBstring, BSTR_ERR); |
| 817 |
test12_1(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 818 |
test12_1(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 819 |
test12_1(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 820 |
test12_1(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 821 |
test12_1(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 822 |
test12_1(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 823 |
test12_1(&emptyBstring, 0, &emptyBstring, 0); |
| 824 |
test12_1(&emptyBstring, 1, &emptyBstring, BSTR_ERR); |
| 825 |
test12_1(&shortBstring, 1, &shortBstring, 0); |
| 826 |
test12_1(&shortBstring, 5, &emptyBstring, 5); |
| 827 |
test12_1(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 828 |
test12_1(&shortBstring, 0, &shortBstring, 0); |
| 829 |
test12_1(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 830 |
ret = bdestroy(b); |
| 831 |
ck_assert_int_eq(ret, BSTR_OK); |
| 832 |
test12_1(&shortBstring, 0, b = bfromcstr("BOGUS"), 0); |
| 833 |
ret = bdestroy(b); |
| 834 |
ck_assert_int_eq(ret, BSTR_OK); |
| 835 |
test12_1(&longBstring, 0, &shortBstring, BSTR_ERR); |
| 836 |
test12_1(&longBstring, 20, &shortBstring, 10); |
| 837 |
} |
| 838 |
END_TEST |
| 839 |
|
| 840 |
static void |
| 841 |
test13_0(bstring s1, int pos, const bstring s2, int res) |
| 842 |
{ |
| 843 |
int ret = binchr(s1, pos, s2); |
| 844 |
ck_assert_int_eq(ret, res); |
| 845 |
} |
| 846 |
|
| 847 |
START_TEST(core_013) |
| 848 |
{ |
| 849 |
bstring b; |
| 850 |
int ret = 0; |
| 851 |
struct tagbstring multipleOs = bsStatic("ooooo"); |
| 852 |
test13_0(NULL, 0, NULL, BSTR_ERR); |
| 853 |
test13_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 854 |
test13_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 855 |
test13_0(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 856 |
test13_0(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 857 |
test13_0(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 858 |
test13_0(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 859 |
test13_0(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 860 |
test13_0(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 861 |
test13_0(&emptyBstring, 0, &emptyBstring, BSTR_ERR); |
| 862 |
test13_0(&shortBstring, 0, &emptyBstring, BSTR_ERR); |
| 863 |
test13_0(&shortBstring, 0, &shortBstring, 0); |
| 864 |
test13_0(&shortBstring, 0, &multipleOs, 1); |
| 865 |
test13_0(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 866 |
ret = bdestroy(b); |
| 867 |
ck_assert_int_eq(ret, BSTR_OK); |
| 868 |
test13_0(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 869 |
test13_0(&shortBstring, 10, &shortBstring, BSTR_ERR); |
| 870 |
test13_0(&shortBstring, 1, &shortBstring, 1); |
| 871 |
test13_0(&emptyBstring, 0, &shortBstring, BSTR_ERR); |
| 872 |
test13_0(&xxxxxBstring, 0, &shortBstring, BSTR_ERR); |
| 873 |
test13_0(&longBstring, 0, &shortBstring, 3); |
| 874 |
test13_0(&longBstring, 10, &shortBstring, 10); |
| 875 |
} |
| 876 |
END_TEST |
| 877 |
|
| 878 |
static void |
| 879 |
test14_0(bstring s1, int pos, const bstring s2, int res) |
| 880 |
{ |
| 881 |
int ret = binchrr(s1, pos, s2); |
| 882 |
ck_assert_int_eq(ret, res); |
| 883 |
} |
| 884 |
|
| 885 |
START_TEST(core_014) |
| 886 |
{ |
| 887 |
bstring b; |
| 888 |
int ret = 0; |
| 889 |
test14_0(NULL, 0, NULL, BSTR_ERR); |
| 890 |
test14_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 891 |
test14_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 892 |
test14_0(&emptyBstring, 0, &emptyBstring, BSTR_ERR); |
| 893 |
test14_0(&shortBstring, 0, &emptyBstring, BSTR_ERR); |
| 894 |
test14_0(&emptyBstring, 0, &badBstring1, BSTR_ERR); |
| 895 |
test14_0(&emptyBstring, 0, &badBstring2, BSTR_ERR); |
| 896 |
test14_0(&badBstring1, 0, &emptyBstring, BSTR_ERR); |
| 897 |
test14_0(&badBstring2, 0, &emptyBstring, BSTR_ERR); |
| 898 |
test14_0(&badBstring2, 0, &badBstring1, BSTR_ERR); |
| 899 |
test14_0(&badBstring1, 0, &badBstring2, BSTR_ERR); |
| 900 |
test14_0(&shortBstring, 0, &shortBstring, 0); |
| 901 |
test14_0(&shortBstring, 0, b = bstrcpy(&shortBstring), 0); |
| 902 |
ret = bdestroy(b); |
| 903 |
ck_assert_int_eq(ret, BSTR_OK); |
| 904 |
test14_0(&shortBstring, -1, &shortBstring, BSTR_ERR); |
| 905 |
test14_0(&shortBstring, 5, &shortBstring, 4); |
| 906 |
test14_0(&shortBstring, 4, &shortBstring, 4); |
| 907 |
test14_0(&shortBstring, 1, &shortBstring, 1); |
| 908 |
test14_0(&emptyBstring, 0, &shortBstring, BSTR_ERR); |
| 909 |
test14_0(&xxxxxBstring, 4, &shortBstring, BSTR_ERR); |
| 910 |
test14_0(&longBstring, 0, &shortBstring, BSTR_ERR); |
| 911 |
test14_0(&longBstring, 10, &shortBstring, 10); |
| 912 |
} |
| 913 |
END_TEST |
| 914 |
|
| 915 |
static void |
| 916 |
test15_0(bstring b0, int pos, const bstring b1, unsigned char fill, char * res) |
| 917 |
{ |
| 918 |
bstring b2; |
| 919 |
int ret = 0; |
| 920 |
if (b0 && b0->data && b0->slen >= 0 && |
| 921 |
b1 && b1->data && b1->slen >= 0) { |
| 922 |
b2 = bstrcpy(b0); |
| 923 |
if (b2 == NULL) { |
| 924 |
ck_abort(); |
| 925 |
return; /* Just a safeguard */ |
| 926 |
} |
| 927 |
bwriteprotect(*b2); |
| 928 |
ret = bsetstr(b2, pos, b1, fill); |
| 929 |
ck_assert_int_ne(ret, 0); |
| 930 |
ret = biseq(b0, b2); |
| 931 |
ck_assert_int_eq(ret, 1); |
| 932 |
bwriteallow(*b2); |
| 933 |
ret = bsetstr(b2, pos, b1, fill); |
| 934 |
if (b1) { |
| 935 |
if (pos < 0) { |
| 936 |
ck_assert_int_eq(b2->slen, b0->slen); |
| 937 |
} else { |
| 938 |
if (b2->slen != b0->slen + b1->slen) { |
| 939 |
ck_assert_int_eq(b2->slen, |
| 940 |
pos + b1->slen); |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
if (res == NULL) { |
| 945 |
ck_abort(); |
| 946 |
return; /* Just a safeguard */ |
| 947 |
} |
| 948 |
ck_assert(!((ret == 0) != (pos >= 0))); |
| 949 |
ret = strlen(res); |
| 950 |
ck_assert(b2->slen >= ret); |
| 951 |
ret = memcmp(b2->data, res, b2->slen); |
| 952 |
ck_assert_int_eq(ret, 0); |
| 953 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 954 |
ret = bdestroy(b2); |
| 955 |
ck_assert_int_eq(ret, BSTR_OK); |
| 956 |
} else { |
| 957 |
ret = bsetstr(b0, pos, b1, fill); |
| 958 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
START_TEST(core_015) |
| 963 |
{ |
| 964 |
/* tests with NULL */ |
| 965 |
test15_0(NULL, 0, NULL, (unsigned char) '?', NULL); |
| 966 |
test15_0(NULL, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 967 |
test15_0(&badBstring1, 0, NULL, (unsigned char) '?', NULL); |
| 968 |
test15_0(&badBstring1, 0, &badBstring1, (unsigned char) '?', NULL); |
| 969 |
test15_0(&emptyBstring, 0, &badBstring1, (unsigned char) '?', NULL); |
| 970 |
test15_0(&badBstring1, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 971 |
test15_0(&badBstring2, 0, NULL, (unsigned char) '?', NULL); |
| 972 |
test15_0(&badBstring2, 0, &badBstring2, (unsigned char) '?', NULL); |
| 973 |
test15_0(&emptyBstring, 0, &badBstring2, (unsigned char) '?', NULL); |
| 974 |
test15_0(&badBstring2, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 975 |
/* normal operation tests */ |
| 976 |
test15_0(&emptyBstring, 0, &emptyBstring, (unsigned char) '?', ""); |
| 977 |
test15_0(&emptyBstring, 5, &emptyBstring, (unsigned char) '?', "?????"); |
| 978 |
test15_0(&emptyBstring, 5, &shortBstring, (unsigned char) '?', "?????bogus"); |
| 979 |
test15_0(&shortBstring, 0, &emptyBstring, (unsigned char) '?', "bogus"); |
| 980 |
test15_0(&emptyBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); |
| 981 |
test15_0(&shortBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); |
| 982 |
test15_0(&shortBstring, -1, &shortBstring, (unsigned char) '?', "bogus"); |
| 983 |
test15_0(&shortBstring, 2, &shortBstring, (unsigned char) '?', "bobogus"); |
| 984 |
test15_0(&shortBstring, 6, &shortBstring, (unsigned char) '?', "bogus?bogus"); |
| 985 |
test15_0(&shortBstring, 6, NULL, (unsigned char) '?', "bogus?"); |
| 986 |
} |
| 987 |
END_TEST |
| 988 |
|
| 989 |
static void |
| 990 |
test16_0(bstring b0, int pos, const bstring b1, unsigned char fill, char *res) |
| 991 |
{ |
| 992 |
bstring b2; |
| 993 |
int ret = 0; |
| 994 |
if (b0 && b0->data && b0->slen >= 0 && |
| 995 |
b1 && b1->data && b1->slen >= 0) { |
| 996 |
b2 = bstrcpy(b0); |
| 997 |
if (b2 == NULL) { |
| 998 |
ck_abort(); |
| 999 |
return; /* Just a safeguard */ |
| 1000 |
} |
| 1001 |
bwriteprotect(*b2); |
| 1002 |
ret = binsert(b2, pos, b1, fill); |
| 1003 |
ck_assert_int_ne(ret, 0); |
| 1004 |
ret = biseq(b0, b2); |
| 1005 |
ck_assert_int_eq(ret, 1); |
| 1006 |
bwriteallow(*b2); |
| 1007 |
ret = binsert(b2, pos, b1, fill); |
| 1008 |
if (b1) { |
| 1009 |
ck_assert(!((pos >= 0) && (b2->slen != b0->slen + b1->slen) && (b2->slen != pos + b1->slen))); |
| 1010 |
ck_assert(!((pos < 0) && (b2->slen != b0->slen))); |
| 1011 |
ck_assert(!((ret == 0) != (pos >= 0 && pos <= b2->slen))); |
| 1012 |
ck_assert(!((ret == 0) != (pos >= 0 && pos <= b2->slen))); |
| 1013 |
} |
| 1014 |
if (res == NULL) { |
| 1015 |
ck_abort(); |
| 1016 |
return; /* Just a safeguard */ |
| 1017 |
} |
| 1018 |
ret = strlen(res); |
| 1019 |
ck_assert_int_eq(b2->slen, ret); |
| 1020 |
ret = memcmp(b2->data, res, b2->slen); |
| 1021 |
ck_assert_int_eq(ret, 0); |
| 1022 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 1023 |
ret = bdestroy(b2); |
| 1024 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1025 |
} else { |
| 1026 |
ret = binsert (b0, pos, b1, fill); |
| 1027 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
START_TEST(core_016) |
| 1032 |
{ |
| 1033 |
/* tests with NULL */ |
| 1034 |
test16_0(NULL, 0, NULL, (unsigned char) '?', NULL); |
| 1035 |
test16_0(NULL, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 1036 |
test16_0(&badBstring1, 0, NULL, (unsigned char) '?', NULL); |
| 1037 |
test16_0(&badBstring1, 0, &badBstring1, (unsigned char) '?', NULL); |
| 1038 |
test16_0(&emptyBstring, 0, &badBstring1, (unsigned char) '?', NULL); |
| 1039 |
test16_0(&badBstring1, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 1040 |
test16_0(&badBstring2, 0, NULL, (unsigned char) '?', NULL); |
| 1041 |
test16_0(&badBstring2, 0, &badBstring2, (unsigned char) '?', NULL); |
| 1042 |
test16_0(&emptyBstring, 0, &badBstring2, (unsigned char) '?', NULL); |
| 1043 |
test16_0(&badBstring2, 0, &emptyBstring, (unsigned char) '?', NULL); |
| 1044 |
/* normal operation tests */ |
| 1045 |
test16_0(&emptyBstring, 0, &emptyBstring, (unsigned char) '?', ""); |
| 1046 |
test16_0(&emptyBstring, 5, &emptyBstring, (unsigned char) '?', "?????"); |
| 1047 |
test16_0(&emptyBstring, 5, &shortBstring, (unsigned char) '?', "?????bogus"); |
| 1048 |
test16_0(&shortBstring, 0, &emptyBstring, (unsigned char) '?', "bogus"); |
| 1049 |
test16_0(&emptyBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); |
| 1050 |
test16_0(&shortBstring, 0, &shortBstring, (unsigned char) '?', "bogusbogus"); |
| 1051 |
test16_0(&shortBstring, -1, &shortBstring, (unsigned char) '?', "bogus"); |
| 1052 |
test16_0(&shortBstring, 2, &shortBstring, (unsigned char) '?', "bobogusgus"); |
| 1053 |
test16_0(&shortBstring, 6, &shortBstring, (unsigned char) '?', "bogus?bogus"); |
| 1054 |
test16_0(&shortBstring, 6, NULL, (unsigned char) '?', "bogus"); |
| 1055 |
} |
| 1056 |
END_TEST |
| 1057 |
|
| 1058 |
static void |
| 1059 |
test17_0(bstring s1, int pos, int len, char * res) |
| 1060 |
{ |
| 1061 |
bstring b2; |
| 1062 |
int ret = 0; |
| 1063 |
if (s1 && s1->data && s1->slen >= 0) { |
| 1064 |
b2 = bstrcpy(s1); |
| 1065 |
if (b2 == NULL) { |
| 1066 |
ck_abort(); |
| 1067 |
return; /* Just a safeguard */ |
| 1068 |
} |
| 1069 |
bwriteprotect(*b2); |
| 1070 |
ret = bdelete(b2, pos, len); |
| 1071 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1072 |
ret = biseq(s1, b2); |
| 1073 |
ck_assert_int_eq(ret, 1); |
| 1074 |
bwriteallow(*b2); |
| 1075 |
ret = bdelete(b2, pos, len); |
| 1076 |
ck_assert(!((len >= 0) != (ret == 0))); |
| 1077 |
ck_assert(!((b2->slen > s1->slen) || (b2->slen < pos && s1->slen >= pos))); |
| 1078 |
if (res == NULL) { |
| 1079 |
ck_abort(); |
| 1080 |
return; /* Just a safeguard */ |
| 1081 |
} |
| 1082 |
ret = strlen(res); |
| 1083 |
ck_assert_int_eq(b2->slen, ret); |
| 1084 |
ret = memcmp(b2->data, res, b2->slen); |
| 1085 |
ck_assert_int_eq(ret, 0); |
| 1086 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 1087 |
ret = bdestroy(b2); |
| 1088 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1089 |
} else { |
| 1090 |
ret = bdelete (s1, pos, len); |
| 1091 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
START_TEST(core_017) |
| 1096 |
{ |
| 1097 |
/* tests with NULL */ |
| 1098 |
test17_0(NULL, 0, 0, NULL); |
| 1099 |
test17_0(&badBstring1, 0, 0, NULL); |
| 1100 |
test17_0(&badBstring2, 0, 0, NULL); |
| 1101 |
/* normal operation tests */ |
| 1102 |
test17_0(&emptyBstring, 0, 0, ""); |
| 1103 |
test17_0(&shortBstring, 1, 3, "bs"); |
| 1104 |
test17_0(&shortBstring, -1, 3, "gus"); |
| 1105 |
test17_0(&shortBstring, 1, -3, "bogus"); |
| 1106 |
test17_0(&shortBstring, 3, 9, "bog"); |
| 1107 |
test17_0(&shortBstring, 3, 1, "bogs"); |
| 1108 |
test17_0(&longBstring, 4, 300, "This"); |
| 1109 |
} |
| 1110 |
END_TEST |
| 1111 |
|
| 1112 |
static void |
| 1113 |
test18_0(bstring b, int len, int res, int mlen) |
| 1114 |
{ |
| 1115 |
int ret = 0; |
| 1116 |
int ol = 0; |
| 1117 |
if (b) { |
| 1118 |
ol = b->mlen; |
| 1119 |
} |
| 1120 |
ret = balloc(b, len); |
| 1121 |
ck_assert(!((b && b->data && b->slen >=0 && ol > b->mlen))); |
| 1122 |
ck_assert_int_eq(ret, res); |
| 1123 |
ck_assert(!((b && (mlen > b->mlen || b->mlen == 0)))); |
| 1124 |
} |
| 1125 |
|
| 1126 |
static void |
| 1127 |
test18_1(bstring b, int len, int res, int mlen) |
| 1128 |
{ |
| 1129 |
int ret = 0; |
| 1130 |
ret = ballocmin(b, len); |
| 1131 |
if (b && b->data) { |
| 1132 |
ck_assert_int_eq(b->mlen, mlen); |
| 1133 |
} |
| 1134 |
ck_assert_int_eq(ret, res); |
| 1135 |
} |
| 1136 |
|
| 1137 |
START_TEST(core_018) |
| 1138 |
{ |
| 1139 |
int ret = 0; |
| 1140 |
bstring b = bfromcstr("test"); |
| 1141 |
ck_assert(b != NULL); |
| 1142 |
/* tests with NULL */ |
| 1143 |
test18_0(NULL, 2, BSTR_ERR, 0); |
| 1144 |
test18_0(&badBstring1, 2, BSTR_ERR, 0); |
| 1145 |
test18_0(&badBstring2, 2, BSTR_ERR, 0); |
| 1146 |
/* normal operation tests */ |
| 1147 |
test18_0(b, 2, 0, b->mlen); |
| 1148 |
test18_0(b, -1, BSTR_ERR, b->mlen); |
| 1149 |
test18_0(b, 9, 0, 9); |
| 1150 |
test18_0(b, 2, 0, 9); |
| 1151 |
bwriteprotect(*b); |
| 1152 |
test18_0(b, 4, BSTR_ERR, b->mlen); |
| 1153 |
bwriteallow(*b); |
| 1154 |
test18_0(b, 2, 0, b->mlen); |
| 1155 |
test18_0(&emptyBstring, 9, BSTR_ERR, emptyBstring.mlen); |
| 1156 |
ret = bdestroy(b); |
| 1157 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1158 |
b = bfromcstr ("test"); |
| 1159 |
/* tests with NULL */ |
| 1160 |
test18_1(NULL, 2, BSTR_ERR, 0); |
| 1161 |
test18_1(&badBstring1, 2, BSTR_ERR, 0); |
| 1162 |
test18_1(&badBstring2, 2, BSTR_ERR, 2); |
| 1163 |
/* normal operation tests */ |
| 1164 |
test18_1(b, 2, 0, b->slen + 1); |
| 1165 |
test18_1(b, -1, BSTR_ERR, b->mlen); |
| 1166 |
test18_1(b, 9, 0, 9); |
| 1167 |
test18_1(b, 2, 0, b->slen + 1); |
| 1168 |
test18_1(b, 9, 0, 9); |
| 1169 |
bwriteprotect (*b); |
| 1170 |
test18_1(b, 4, BSTR_ERR, -1); |
| 1171 |
bwriteallow (*b); |
| 1172 |
test18_1(b, 2, 0, b->slen + 1); |
| 1173 |
test18_1(&emptyBstring, 9, BSTR_ERR, emptyBstring.mlen); |
| 1174 |
ret = bdestroy(b); |
| 1175 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1176 |
} |
| 1177 |
END_TEST |
| 1178 |
|
| 1179 |
static void |
| 1180 |
test19_0(bstring b, int len, const char *res, int erv) |
| 1181 |
{ |
| 1182 |
int ret = 0; |
| 1183 |
bstring b1 = NULL; |
| 1184 |
if (b && b->data && b->slen >= 0) { |
| 1185 |
b1 = bstrcpy(b); |
| 1186 |
if (b1 == NULL) { |
| 1187 |
ck_abort(); |
| 1188 |
return; /* Just a safeguard */ |
| 1189 |
} |
| 1190 |
bwriteprotect(*b1); |
| 1191 |
ret = bpattern(b1, len); |
| 1192 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1193 |
ret = biseq(b1, b); |
| 1194 |
ck_assert_int_eq(ret, 1); |
| 1195 |
bwriteallow(*b1); |
| 1196 |
ret = bpattern(b1, len); |
| 1197 |
ck_assert_int_eq(ret, erv); |
| 1198 |
if (res == NULL) { |
| 1199 |
ck_abort(); |
| 1200 |
return; /* Just a safeguard */ |
| 1201 |
} |
| 1202 |
ret = strlen(res); |
| 1203 |
ck_assert_int_eq(b1->slen, ret); |
| 1204 |
ret = memcmp(b1->data, res, b1->slen); |
| 1205 |
ck_assert_int_eq(ret, 0); |
| 1206 |
ck_assert_int_eq(b1->data[b1->slen], '\0'); |
| 1207 |
ret = bdestroy(b1); |
| 1208 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1209 |
} else { |
| 1210 |
ret = bpattern(b, len); |
| 1211 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
START_TEST(core_019) |
| 1216 |
{ |
| 1217 |
/* tests with NULL */ |
| 1218 |
test19_0(NULL, 0, NULL, BSTR_ERR); |
| 1219 |
test19_0(NULL, 5, NULL, BSTR_ERR); |
| 1220 |
test19_0(NULL, -5, NULL, BSTR_ERR); |
| 1221 |
test19_0(&badBstring1, 5, NULL, BSTR_ERR); |
| 1222 |
test19_0(&badBstring2, 5, NULL, BSTR_ERR); |
| 1223 |
/* normal operation tests */ |
| 1224 |
test19_0(&emptyBstring, 0, "", BSTR_ERR); |
| 1225 |
test19_0(&emptyBstring, 10, "", BSTR_ERR); |
| 1226 |
test19_0(&emptyBstring, -1, "", BSTR_ERR); |
| 1227 |
test19_0(&shortBstring, 0, "", 0); |
| 1228 |
test19_0(&shortBstring, 12, "bogusbogusbo", 0); |
| 1229 |
test19_0(&shortBstring, -1, "bogus", BSTR_ERR); |
| 1230 |
} |
| 1231 |
END_TEST |
| 1232 |
|
| 1233 |
START_TEST(core_020) |
| 1234 |
{ |
| 1235 |
int ret = 0; |
| 1236 |
bstring b, c; |
| 1237 |
/* tests with NULL */ |
| 1238 |
b = bformat(NULL, 1, 2); |
| 1239 |
ck_assert(b == NULL); |
| 1240 |
/* normal operation tests */ |
| 1241 |
b = bformat("%d %s", 1, "xy"); |
| 1242 |
ck_assert(b != NULL); |
| 1243 |
ret = biseq(c = bfromcstr("1 xy"), b); |
| 1244 |
ck_assert_int_eq(ret, 1); |
| 1245 |
ret = bdestroy(b); |
| 1246 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1247 |
b = bformat("%d %s(%s)", 6, c->data, shortBstring.data); |
| 1248 |
ret = bdestroy(c); |
| 1249 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1250 |
ret = biseq(c = bfromcstr("6 1 xy(bogus)"), b); |
| 1251 |
ck_assert_int_eq(ret, 1); |
| 1252 |
ret = bdestroy(c); |
| 1253 |
ret = bdestroy(b); |
| 1254 |
b = bformat("%s%s%s%s%s%s%s%s",longBstring.data, longBstring.data, |
| 1255 |
longBstring.data, longBstring.data, longBstring.data, |
| 1256 |
longBstring.data, longBstring.data, longBstring.data); |
| 1257 |
c = bstrcpy(&longBstring); |
| 1258 |
bconcat(c, c); |
| 1259 |
bconcat(c, c); |
| 1260 |
bconcat(c, c); |
| 1261 |
ret = biseq(c, b); |
| 1262 |
ck_assert_int_eq(ret, 1); |
| 1263 |
ret = bdestroy(c); |
| 1264 |
ret = bdestroy(b); |
| 1265 |
b = bfromcstr(""); |
| 1266 |
/* tests with NULL */ |
| 1267 |
ret = bformata(b, NULL, 1, 2); |
| 1268 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1269 |
ret = bformata(&badBstring1, "%d %d", 1, 2); |
| 1270 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1271 |
ret = bformata(b, "%d %d", 1, 2); |
| 1272 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1273 |
ret = biseq(c = bfromcstr("1 2"), b); |
| 1274 |
ck_assert_int_eq(ret, 1); |
| 1275 |
ret = bdestroy(c); |
| 1276 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1277 |
ret = bdestroy(b); |
| 1278 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1279 |
ret = bformata(b = bfromcstr("x"), "%s%s%s%s%s%s%s%s", |
| 1280 |
longBstring.data, longBstring.data, |
| 1281 |
longBstring.data, longBstring.data, |
| 1282 |
longBstring.data, longBstring.data, |
| 1283 |
longBstring.data, longBstring.data); |
| 1284 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1285 |
c = bstrcpy(&longBstring); |
| 1286 |
bconcat(c, c); |
| 1287 |
bconcat(c, c); |
| 1288 |
bconcat(c, c); |
| 1289 |
binsertch(c, 0, 1, 'x'); |
| 1290 |
ret = biseq(c, b); |
| 1291 |
ck_assert_int_eq(ret, 1); |
| 1292 |
ret = bdestroy(c); |
| 1293 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1294 |
ret = bdestroy(b); |
| 1295 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1296 |
b = bfromcstr("Initial"); |
| 1297 |
/* tests with NULL */ |
| 1298 |
ret = bassignformat(b, NULL, 1, 2); |
| 1299 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1300 |
ret = bassignformat(&badBstring1, "%d %d", 1, 2); |
| 1301 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1302 |
ret = bassignformat(b, "%d %d", 1, 2); |
| 1303 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1304 |
ret = biseq(c = bfromcstr("1 2"), b); |
| 1305 |
ck_assert_int_eq(ret, 1); |
| 1306 |
ret = bdestroy(c); |
| 1307 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1308 |
ret = bdestroy(b); |
| 1309 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1310 |
ret = bassignformat(b = bfromcstr("x"), "%s%s%s%s%s%s%s%s", |
| 1311 |
longBstring.data, longBstring.data, |
| 1312 |
longBstring.data, longBstring.data, |
| 1313 |
longBstring.data, longBstring.data, |
| 1314 |
longBstring.data, longBstring.data); |
| 1315 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1316 |
c = bstrcpy(&longBstring); |
| 1317 |
ck_assert(c != NULL); |
| 1318 |
bconcat(c, c); |
| 1319 |
bconcat(c, c); |
| 1320 |
bconcat(c, c); |
| 1321 |
ret = biseq(c, b); |
| 1322 |
ck_assert_int_eq(ret, 1); |
| 1323 |
ret = bdestroy(c); |
| 1324 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1325 |
ret = bdestroy(b); |
| 1326 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1327 |
} |
| 1328 |
END_TEST |
| 1329 |
|
| 1330 |
static void |
| 1331 |
test21_0(bstring b, char sc, int ns) |
| 1332 |
{ |
| 1333 |
struct bstrList * l; |
| 1334 |
int ret = 0; |
| 1335 |
if (b && b->data && b->slen >= 0) { |
| 1336 |
bstring c; |
| 1337 |
struct tagbstring t; |
| 1338 |
blk2tbstr(t, &sc, 1); |
| 1339 |
l = bsplit (b, sc); |
| 1340 |
if (l == NULL) { |
| 1341 |
ck_abort(); |
| 1342 |
return; /* Just a safeguard */ |
| 1343 |
} |
| 1344 |
ck_assert_int_eq(ns, l->qty); |
| 1345 |
c = bjoin (l, &t); |
| 1346 |
ret = biseq(c, b); |
| 1347 |
ck_assert_int_eq(ret, 1); |
| 1348 |
ret = bdestroy(c); |
| 1349 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1350 |
ret = bstrListDestroy(l); |
| 1351 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1352 |
} else { |
| 1353 |
l = bsplit(b, sc); |
| 1354 |
ck_assert(l == NULL); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
static void |
| 1359 |
test21_1(bstring b, const bstring sc, int ns) |
| 1360 |
{ |
| 1361 |
struct bstrList * l; |
| 1362 |
int ret = 0; |
| 1363 |
if (b && b->data && b->slen >= 0) { |
| 1364 |
bstring c; |
| 1365 |
l = bsplitstr (b, sc); |
| 1366 |
if (l == NULL) { |
| 1367 |
ck_abort(); |
| 1368 |
} else { |
| 1369 |
ck_assert_int_eq(ns, l->qty); |
| 1370 |
c = bjoin(l, sc); |
| 1371 |
ck_assert(c != NULL); |
| 1372 |
ret = biseq (c, b); |
| 1373 |
ck_assert_int_eq(ret, 1); |
| 1374 |
ret = bdestroy(c); |
| 1375 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1376 |
ret = bstrListDestroy(l); |
| 1377 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1378 |
} |
| 1379 |
} else { |
| 1380 |
l = bsplitstr(b, sc); |
| 1381 |
ck_assert(l == NULL); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
START_TEST(core_021) |
| 1386 |
{ |
| 1387 |
struct tagbstring is = bsStatic ("is"); |
| 1388 |
struct tagbstring ng = bsStatic ("ng"); |
| 1389 |
struct tagbstring delim = bsStatic("aa"); |
| 1390 |
struct tagbstring beginWithDelim = bsStatic("aaabcdaa1"); |
| 1391 |
struct tagbstring endWithDelim = bsStatic("1aaabcdaa"); |
| 1392 |
struct tagbstring conseqDelim = bsStatic("1aaaa1"); |
| 1393 |
struct tagbstring oneCharLeft = bsStatic("aaaaaaa"); |
| 1394 |
struct tagbstring allDelim = bsStatic("aaaaaa"); |
| 1395 |
int ret = 0; |
| 1396 |
/* tests with NULL */ |
| 1397 |
test21_0(NULL, (char) '?', 0); |
| 1398 |
test21_0(&badBstring1, (char) '?', 0); |
| 1399 |
test21_0(&badBstring2, (char) '?', 0); |
| 1400 |
/* normal operation tests */ |
| 1401 |
test21_0(&emptyBstring, (char) '?', 1); |
| 1402 |
test21_0(&shortBstring, (char) 'o', 2); |
| 1403 |
test21_0(&shortBstring, (char) 's', 2); |
| 1404 |
test21_0(&shortBstring, (char) 'b', 2); |
| 1405 |
test21_0(&longBstring, (char) 'o', 9); |
| 1406 |
test21_1(NULL, NULL, 0); |
| 1407 |
test21_1(&badBstring1, &emptyBstring, 0); |
| 1408 |
test21_1(&badBstring2, &emptyBstring, 0); |
| 1409 |
/* normal operation tests */ |
| 1410 |
test21_1(&shortBstring, &emptyBstring, 5); |
| 1411 |
test21_1(&longBstring, &is, 3); |
| 1412 |
test21_1(&longBstring, &ng, 5); |
| 1413 |
/* corner cases */ |
| 1414 |
test21_1(&emptyBstring, &delim, 1); |
| 1415 |
test21_1(&delim, &delim, 2); |
| 1416 |
test21_1(&beginWithDelim, &delim, 3); |
| 1417 |
test21_1(&endWithDelim, &delim, 3); |
| 1418 |
test21_1(&conseqDelim, &delim, 3); |
| 1419 |
test21_1(&oneCharLeft, &delim, 4); |
| 1420 |
test21_1(&allDelim, &delim, 4); |
| 1421 |
struct bstrList * l; |
| 1422 |
unsigned char c; |
| 1423 |
struct tagbstring t; |
| 1424 |
bstring b; |
| 1425 |
bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; |
| 1426 |
int i; |
| 1427 |
blk2tbstr (t, &c, 1); |
| 1428 |
for (i = 0; i < 3; i++) { |
| 1429 |
c = '\0'; |
| 1430 |
while (1) { |
| 1431 |
l = bsplit(list[i], c); |
| 1432 |
ck_assert(l != NULL); |
| 1433 |
b = bjoin(l, &t); |
| 1434 |
ck_assert(b != NULL); |
| 1435 |
ret = biseq(b, list[i]); |
| 1436 |
ck_assert_int_eq(ret, 1); |
| 1437 |
ret = bdestroy(b); |
| 1438 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1439 |
ret = bstrListDestroy(l); |
| 1440 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1441 |
l = bsplitstr(list[i], &t); |
| 1442 |
ck_assert(l != NULL); |
| 1443 |
b = bjoin(l, &t); |
| 1444 |
ck_assert(b != NULL); |
| 1445 |
ret = biseq(b, list[i]); |
| 1446 |
ck_assert_int_eq(ret, 1); |
| 1447 |
ret = bdestroy(b); |
| 1448 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1449 |
ret = bstrListDestroy(l); |
| 1450 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1451 |
if (UCHAR_MAX == c) { |
| 1452 |
break; |
| 1453 |
} |
| 1454 |
c++; |
| 1455 |
} |
| 1456 |
} |
| 1457 |
} |
| 1458 |
END_TEST |
| 1459 |
|
| 1460 |
static void |
| 1461 |
test22_0(const bstring b, const bstring sep, int ns, ...) |
| 1462 |
{ |
| 1463 |
va_list arglist; |
| 1464 |
struct bstrList * l; |
| 1465 |
int ret = 0; |
| 1466 |
if (b && b->data && b->slen >= 0 && |
| 1467 |
sep && sep->data && sep->slen >= 0) { |
| 1468 |
l = bsplits(b, sep); |
| 1469 |
if (l) { |
| 1470 |
int i; |
| 1471 |
va_start(arglist, ns); |
| 1472 |
for (i = 0; i < l->qty; i++) { |
| 1473 |
char *res; |
| 1474 |
res = va_arg(arglist, char *); |
| 1475 |
ck_assert(res != NULL); |
| 1476 |
ret = strlen(res); |
| 1477 |
ck_assert(ret <= l->entry[i]->slen); |
| 1478 |
ret = memcmp(l->entry[i]->data, res, |
| 1479 |
l->entry[i]->slen); |
| 1480 |
ck_assert_int_eq(ret, 0); |
| 1481 |
ck_assert_int_eq( |
| 1482 |
l->entry[i]->data[l->entry[i]->slen], |
| 1483 |
'\0'); |
| 1484 |
} |
| 1485 |
va_end(arglist); |
| 1486 |
ck_assert_int_eq(ns, l->qty); |
| 1487 |
ret = bstrListDestroy(l); |
| 1488 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1489 |
} else { |
| 1490 |
ck_assert_int_eq(ns, 0); |
| 1491 |
} |
| 1492 |
} else { |
| 1493 |
l = bsplits(b, sep); |
| 1494 |
ck_assert(l == NULL); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
START_TEST(core_022) |
| 1499 |
{ |
| 1500 |
int ret = 0; |
| 1501 |
struct tagbstring o = bsStatic("o"); |
| 1502 |
struct tagbstring s = bsStatic("s"); |
| 1503 |
struct tagbstring b = bsStatic("b"); |
| 1504 |
struct tagbstring bs = bsStatic("bs"); |
| 1505 |
struct tagbstring uo = bsStatic("uo"); |
| 1506 |
/* tests with NULL */ |
| 1507 |
test22_0(NULL, &o, 0); |
| 1508 |
test22_0(&o, NULL, 0); |
| 1509 |
/* normal operation tests */ |
| 1510 |
test22_0(&emptyBstring, &o, 1, ""); |
| 1511 |
test22_0(&emptyBstring, &uo, 1, ""); |
| 1512 |
test22_0(&shortBstring, &emptyBstring, 1, "bogus"); |
| 1513 |
test22_0(&shortBstring, &o, 2, "b", "gus"); |
| 1514 |
test22_0(&shortBstring, &s, 2, "bogu", ""); |
| 1515 |
test22_0(&shortBstring, &b, 2, "" , "ogus"); |
| 1516 |
test22_0(&shortBstring, &bs, 3, "" , "ogu", ""); |
| 1517 |
test22_0(&longBstring, &o, 9, "This is a b", "gus but reas", |
| 1518 |
"nably l", "ng string. Just l", "ng en", "ugh t", |
| 1519 |
" cause s", "me mall", "cing."); |
| 1520 |
test22_0(&shortBstring, &uo, 3, "b", "g", "s"); |
| 1521 |
struct bstrList *l; |
| 1522 |
unsigned char c; |
| 1523 |
struct tagbstring t; |
| 1524 |
bstring bb; |
| 1525 |
bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; |
| 1526 |
int i; |
| 1527 |
blk2tbstr(t, &c, 1); |
| 1528 |
for (i = 0; i < 3; i++) { |
| 1529 |
c = '\0'; |
| 1530 |
while (1) { |
| 1531 |
bb = bjoin(l = bsplits(list[i], &t), &t); |
| 1532 |
ret = biseq(bb, list[i]); |
| 1533 |
ck_assert_int_eq(ret, 1); |
| 1534 |
ret = bdestroy(bb); |
| 1535 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1536 |
bstrListDestroy(l); |
| 1537 |
if (UCHAR_MAX == c) { |
| 1538 |
break; |
| 1539 |
} |
| 1540 |
c++; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
} |
| 1544 |
END_TEST |
| 1545 |
|
| 1546 |
struct sbstr { |
| 1547 |
int ofs; |
| 1548 |
bstring b; |
| 1549 |
}; |
| 1550 |
|
| 1551 |
static size_t |
| 1552 |
test23_aux_read(void *buff, size_t elsize, size_t nelem, void *parm) |
| 1553 |
{ |
| 1554 |
struct sbstr *sb = (struct sbstr *)parm; |
| 1555 |
int els, len; |
| 1556 |
if (!parm || elsize == 0 || nelem == 0) { |
| 1557 |
return 0; |
| 1558 |
} |
| 1559 |
len = (int)(nelem * elsize); |
| 1560 |
if (len <= 0) { |
| 1561 |
return 0; |
| 1562 |
} |
| 1563 |
if (len + sb->ofs > sb->b->slen) { |
| 1564 |
len = sb->b->slen - sb->ofs; |
| 1565 |
} |
| 1566 |
els = (int)(len / elsize); |
| 1567 |
len = (int)(els * elsize); |
| 1568 |
if (len > 0) { |
| 1569 |
memcpy(buff, sb->b->data + sb->ofs, len); |
| 1570 |
sb->ofs += len; |
| 1571 |
} |
| 1572 |
return els; |
| 1573 |
} |
| 1574 |
|
| 1575 |
static int |
| 1576 |
test23_aux_open(struct sbstr * sb, bstring b) |
| 1577 |
{ |
| 1578 |
if (!sb || !b || !b->data) { |
| 1579 |
return -__LINE__; |
| 1580 |
} |
| 1581 |
sb->ofs = 0; |
| 1582 |
sb->b = b; |
| 1583 |
return 0; |
| 1584 |
} |
| 1585 |
|
| 1586 |
static int |
| 1587 |
test23_aux_splitcb(void * parm, BSTR_UNUSED int ofs, const bstring entry) |
| 1588 |
{ |
| 1589 |
bstring b = (bstring)parm; |
| 1590 |
if (b->slen > 0) { |
| 1591 |
bconchar(b, '|'); |
| 1592 |
} |
| 1593 |
bconcat(b, entry); |
| 1594 |
return 0; |
| 1595 |
} |
| 1596 |
|
| 1597 |
struct tagBss { |
| 1598 |
int first; |
| 1599 |
unsigned char sc; |
| 1600 |
bstring b; |
| 1601 |
}; |
| 1602 |
|
| 1603 |
static int |
| 1604 |
test23_aux_splitcbx(void * parm, BSTR_UNUSED int ofs, const bstring entry) |
| 1605 |
{ |
| 1606 |
struct tagBss *p = (struct tagBss *)parm; |
| 1607 |
if (!p->first) { |
| 1608 |
bconchar(p->b, (char) p->sc); |
| 1609 |
} else { |
| 1610 |
p->first = 0; |
| 1611 |
} |
| 1612 |
bconcat(p->b, entry); |
| 1613 |
return 0; |
| 1614 |
} |
| 1615 |
|
| 1616 |
START_TEST(core_023) |
| 1617 |
{ |
| 1618 |
struct tagbstring space = bsStatic(" "); |
| 1619 |
struct sbstr sb; |
| 1620 |
struct bStream *bs; |
| 1621 |
bstring b; |
| 1622 |
int l, ret = 0; |
| 1623 |
test23_aux_open(&sb, &longBstring); |
| 1624 |
bs = bsopen((bNread)NULL, &sb); |
| 1625 |
ck_assert(bs == NULL); |
| 1626 |
bs = bsopen((bNread)test23_aux_read, &sb); |
| 1627 |
ck_assert(bs != NULL); |
| 1628 |
ret = bseof(bs); |
| 1629 |
ck_assert_int_eq(ret, 0); |
| 1630 |
ret = bsbufflength(NULL, -1); |
| 1631 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1632 |
ret = bsbufflength(NULL, 1); |
| 1633 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1634 |
ret = bsbufflength(bs, -1); |
| 1635 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1636 |
ret = bsbufflength(bs, 1); |
| 1637 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1638 |
ret = bspeek(NULL, bs); |
| 1639 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1640 |
ret = bsreadln(NULL, bs,(char) '?'); |
| 1641 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1642 |
ret = bsreadln(&emptyBstring, bs,(char) '?'); |
| 1643 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1644 |
ret = bspeek(&emptyBstring, bs); |
| 1645 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1646 |
ret = bspeek(b = bfromcstr(""), bs); |
| 1647 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1648 |
ret = bsreadln(b, NULL,(char) '?'); |
| 1649 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1650 |
b->slen = 0; |
| 1651 |
ret = bsreadln(b, bs,(char) '?'); |
| 1652 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1653 |
ret = bseof(bs); |
| 1654 |
ck_assert_int_eq(ret, 1); |
| 1655 |
ret = biseq(b, &longBstring); |
| 1656 |
ck_assert_int_eq(ret, 1); |
| 1657 |
ret = bsunread(bs, b); |
| 1658 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1659 |
ret = bseof(bs); |
| 1660 |
ck_assert_int_eq(ret, 0); |
| 1661 |
b->slen = 0; |
| 1662 |
ret = bspeek(b, bs); |
| 1663 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1664 |
ret = biseq(b, &longBstring); |
| 1665 |
ck_assert_int_eq(ret, 1); |
| 1666 |
b->slen = 0; |
| 1667 |
ret = bsreadln(b, bs,(char) '?'); |
| 1668 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1669 |
ret = bseof(bs); |
| 1670 |
ck_assert_int_eq(ret, 1); |
| 1671 |
ret = biseq(b, &longBstring); |
| 1672 |
ck_assert_int_eq(ret, 1); |
| 1673 |
bs = bsclose(bs); |
| 1674 |
ck_assert(bs != NULL); |
| 1675 |
sb.ofs = 0; |
| 1676 |
bs = bsopen((bNread)test23_aux_read, &sb); |
| 1677 |
ck_assert(bs != NULL); |
| 1678 |
b->slen = 0; |
| 1679 |
ret = bsreadln(b, bs,(char) '.'); |
| 1680 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1681 |
l = b->slen; |
| 1682 |
ret = bstrncmp(b, &longBstring, l); |
| 1683 |
ck_assert_int_eq(ret, 0); |
| 1684 |
ck_assert_int_eq(longBstring.data[l-1], '.'); |
| 1685 |
ret = bsunread(bs, b); |
| 1686 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1687 |
b->slen = 0; |
| 1688 |
ret = bspeek(b, bs); |
| 1689 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1690 |
ret = biseq(b, &longBstring); |
| 1691 |
ck_assert_int_eq(ret, 1); |
| 1692 |
b->slen = 0; |
| 1693 |
ret = bsreadln(b, bs, '.'); |
| 1694 |
ck_assert_int_ne(ret, BSTR_ERR); |
| 1695 |
ck_assert_int_eq(b->slen, l); |
| 1696 |
ret = bstrncmp(b, &longBstring, l); |
| 1697 |
ck_assert_int_eq(ret, 0); |
| 1698 |
ck_assert_int_eq(longBstring.data[l-1], '.'); |
| 1699 |
bs = bsclose(bs); |
| 1700 |
ck_assert(bs != NULL); |
| 1701 |
test23_aux_open(&sb, &longBstring); |
| 1702 |
bs = bsopen((bNread) test23_aux_read, &sb); |
| 1703 |
ck_assert(bs != NULL); |
| 1704 |
ret = bseof(bs); |
| 1705 |
ck_assert_int_eq(ret, 0); |
| 1706 |
b->slen = 0; |
| 1707 |
l = bssplitscb(bs, &space, test23_aux_splitcb, b); |
| 1708 |
ret = bseof(bs); |
| 1709 |
ck_assert_int_eq(ret, 1); |
| 1710 |
bs = bsclose(bs); |
| 1711 |
ck_assert(bs != NULL); |
| 1712 |
for (l = 1; l < 4; l++) { |
| 1713 |
char *str; |
| 1714 |
for (str = (char *)longBstring.data; *str; str++) { |
| 1715 |
test23_aux_open(&sb, &longBstring); |
| 1716 |
bs = bsopen((bNread)test23_aux_read, &sb); |
| 1717 |
ck_assert(bs != NULL); |
| 1718 |
ret = bseof(bs); |
| 1719 |
ck_assert_int_eq(ret, 0); |
| 1720 |
ret = bsbufflength(bs, l); |
| 1721 |
ck_assert(0 <= ret); |
| 1722 |
b->slen = 0; |
| 1723 |
while (0 == bsreadlna(b, bs, *str)) |
| 1724 |
; |
| 1725 |
ret = biseq(b, &longBstring); |
| 1726 |
ck_assert_int_eq(ret, 1); |
| 1727 |
ret = bseof(bs); |
| 1728 |
ck_assert_int_eq(ret, 1); |
| 1729 |
bs = bsclose(bs); |
| 1730 |
ck_assert(bs != NULL); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
ret = bdestroy(b); |
| 1734 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1735 |
unsigned char c; |
| 1736 |
struct tagbstring t; |
| 1737 |
bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; |
| 1738 |
int i; |
| 1739 |
blk2tbstr(t, &c, 1); |
| 1740 |
for (i = 0; i < 3; i++) { |
| 1741 |
c = '\0'; |
| 1742 |
while (1) { |
| 1743 |
struct tagBss bss; |
| 1744 |
bss.sc = c; |
| 1745 |
bss.b = bfromcstr(""); |
| 1746 |
ck_assert(bss.b != NULL); |
| 1747 |
bss.first = 1; |
| 1748 |
test23_aux_open(&sb, list[i]); |
| 1749 |
bs = bsopen((bNread)test23_aux_read, &sb); |
| 1750 |
ck_assert(bs != NULL); |
| 1751 |
bssplitscb(bs, &t, test23_aux_splitcbx, &bss); |
| 1752 |
bs = bsclose(bs); |
| 1753 |
ck_assert(bs != NULL); |
| 1754 |
ret = biseq(bss.b, list[i]); |
| 1755 |
ck_assert_int_eq(ret, 1); |
| 1756 |
ret = bdestroy(bss.b); |
| 1757 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1758 |
if (UCHAR_MAX == c) { |
| 1759 |
break; |
| 1760 |
} |
| 1761 |
c++; |
| 1762 |
} |
| 1763 |
while (1) { |
| 1764 |
struct tagBss bss; |
| 1765 |
bss.sc = c; |
| 1766 |
bss.b = bfromcstr(""); |
| 1767 |
ck_assert(bss.b != NULL); |
| 1768 |
bss.first = 1; |
| 1769 |
test23_aux_open(&sb, list[i]); |
| 1770 |
bs = bsopen((bNread) test23_aux_read, &sb); |
| 1771 |
ck_assert(bs != NULL); |
| 1772 |
bssplitstrcb(bs, &t, test23_aux_splitcbx, &bss); |
| 1773 |
bs = bsclose(bs); |
| 1774 |
ck_assert(bs != NULL); |
| 1775 |
ret = biseq(bss.b, list[i]); |
| 1776 |
ck_assert_int_eq(ret, 1); |
| 1777 |
ret = bdestroy(bss.b); |
| 1778 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1779 |
if (UCHAR_MAX == c) { |
| 1780 |
break; |
| 1781 |
} |
| 1782 |
c++; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
END_TEST |
| 1787 |
|
| 1788 |
static void |
| 1789 |
test24_0(bstring s1, int pos, const bstring s2, int res) |
| 1790 |
{ |
| 1791 |
int ret = bninchr(s1, pos, s2); |
| 1792 |
ck_assert_int_eq(ret, res); |
| 1793 |
} |
| 1794 |
|
| 1795 |
START_TEST(core_024) |
| 1796 |
{ |
| 1797 |
bstring b; |
| 1798 |
int ret = 0; |
| 1799 |
test24_0(NULL, 0, NULL, BSTR_ERR); |
| 1800 |
test24_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 1801 |
test24_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 1802 |
test24_0(&shortBstring, 3, &badBstring1, BSTR_ERR); |
| 1803 |
test24_0(&badBstring1, 3, &shortBstring, BSTR_ERR); |
| 1804 |
test24_0(&emptyBstring, 0, &emptyBstring, BSTR_ERR); |
| 1805 |
test24_0(&shortBstring, 0, &emptyBstring, BSTR_ERR); |
| 1806 |
test24_0(&shortBstring, 0, &shortBstring, BSTR_ERR); |
| 1807 |
test24_0(&shortBstring, 1, &shortBstring, BSTR_ERR); |
| 1808 |
test24_0(&longBstring, 3, &shortBstring, 4); |
| 1809 |
b = bstrcpy(&shortBstring); |
| 1810 |
ck_assert(b != NULL); |
| 1811 |
test24_0(&longBstring, 3, b, 4); |
| 1812 |
ret = bdestroy(b); |
| 1813 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1814 |
test24_0(&longBstring, -1, &shortBstring, BSTR_ERR); |
| 1815 |
test24_0(&longBstring, 1000, &shortBstring, BSTR_ERR); |
| 1816 |
test24_0(&xxxxxBstring, 0, &shortBstring, 0); |
| 1817 |
test24_0(&xxxxxBstring, 1, &shortBstring, 1); |
| 1818 |
test24_0(&emptyBstring, 0, &shortBstring, BSTR_ERR); |
| 1819 |
test24_0(&longBstring, 0, &shortBstring, 0); |
| 1820 |
test24_0(&longBstring, 10, &shortBstring, 15); |
| 1821 |
} |
| 1822 |
END_TEST |
| 1823 |
|
| 1824 |
static void |
| 1825 |
test25_0(bstring s1, int pos, const bstring s2, int res) |
| 1826 |
{ |
| 1827 |
int ret = bninchrr(s1, pos, s2); |
| 1828 |
ck_assert_int_eq(ret, res); |
| 1829 |
} |
| 1830 |
|
| 1831 |
START_TEST(core_025) |
| 1832 |
{ |
| 1833 |
bstring b; |
| 1834 |
int ret = 0; |
| 1835 |
test25_0(NULL, 0, NULL, BSTR_ERR); |
| 1836 |
test25_0(&emptyBstring, 0, NULL, BSTR_ERR); |
| 1837 |
test25_0(NULL, 0, &emptyBstring, BSTR_ERR); |
| 1838 |
test25_0(&emptyBstring, 0, &emptyBstring, BSTR_ERR); |
| 1839 |
test25_0(&shortBstring, 0, &emptyBstring, BSTR_ERR); |
| 1840 |
test25_0(&shortBstring, 0, &badBstring1, BSTR_ERR); |
| 1841 |
test25_0(&badBstring1, 0, &shortBstring, BSTR_ERR); |
| 1842 |
test25_0(&shortBstring, 0, &shortBstring, BSTR_ERR); |
| 1843 |
test25_0(&shortBstring, 4, &shortBstring, BSTR_ERR); |
| 1844 |
test25_0(&longBstring, 10, &shortBstring, 9); |
| 1845 |
b = bstrcpy(&shortBstring); |
| 1846 |
ck_assert(b != NULL); |
| 1847 |
test25_0(&longBstring, 10, b, 9); |
| 1848 |
ret = bdestroy(b); |
| 1849 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1850 |
test25_0(&xxxxxBstring, 4, &shortBstring, 4); |
| 1851 |
test25_0(&emptyBstring, 0, &shortBstring, BSTR_ERR); |
| 1852 |
} |
| 1853 |
END_TEST |
| 1854 |
|
| 1855 |
static void |
| 1856 |
test26_0(bstring b0, int pos, int len, const bstring b1, |
| 1857 |
unsigned char fill, char *res) |
| 1858 |
{ |
| 1859 |
bstring b2; |
| 1860 |
int ret = 0; |
| 1861 |
if (b0 && b0->data && b0->slen >= 0 && |
| 1862 |
b1 && b1->data && b1->slen >= 0) { |
| 1863 |
b2 = bstrcpy(b0); |
| 1864 |
if (b2 == NULL) { |
| 1865 |
ck_abort(); |
| 1866 |
return; /* Just a safeguard */ |
| 1867 |
} |
| 1868 |
bwriteprotect(*b2); |
| 1869 |
ret = breplace(b2, pos, len, b1, fill); |
| 1870 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1871 |
ret = biseq(b0, b2); |
| 1872 |
ck_assert_int_eq(ret, 1); |
| 1873 |
bwriteallow(*b2); |
| 1874 |
ret = breplace(b2, pos, len, b1, fill); |
| 1875 |
if (b1) { |
| 1876 |
ck_assert(!(((ret == 0) != |
| 1877 |
(pos >= 0 && pos <= b2->slen)))); |
| 1878 |
} |
| 1879 |
if (res == NULL) { |
| 1880 |
ck_abort(); |
| 1881 |
return; /* Just a safeguard */ |
| 1882 |
} |
| 1883 |
ret = strlen(res); |
| 1884 |
ck_assert(b2->slen >= ret); |
| 1885 |
ret = memcmp(b2->data, res, b2->slen); |
| 1886 |
ck_assert_int_eq(ret, 0); |
| 1887 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 1888 |
ret = bdestroy(b2); |
| 1889 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1890 |
} else { |
| 1891 |
ret = breplace (b0, pos, len, b1, fill); |
| 1892 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1893 |
} |
| 1894 |
} |
| 1895 |
|
| 1896 |
START_TEST(core_026) |
| 1897 |
{ |
| 1898 |
/* tests with NULL */ |
| 1899 |
test26_0(NULL, 0, 0, NULL, '?', NULL); |
| 1900 |
test26_0(NULL, 0, 0, &emptyBstring, '?', NULL); |
| 1901 |
test26_0(&badBstring1, 1, 3, &shortBstring, '?', NULL); |
| 1902 |
test26_0(&shortBstring, 1, 3, &badBstring1, '?', NULL); |
| 1903 |
/* normal operation tests */ |
| 1904 |
test26_0(&emptyBstring, 0, 0, &emptyBstring, '?', ""); |
| 1905 |
test26_0(&emptyBstring, 5, 0, &emptyBstring, '?', "?????"); |
| 1906 |
test26_0(&emptyBstring, 5, 0, &shortBstring, '?', "?????bogus"); |
| 1907 |
test26_0(&shortBstring, 0, 0, &emptyBstring, '?', "bogus"); |
| 1908 |
test26_0(&emptyBstring, 0, 0, &shortBstring, '?', "bogus"); |
| 1909 |
test26_0(&shortBstring, 0, 0, &shortBstring, '?', "bogusbogus"); |
| 1910 |
test26_0(&shortBstring, 1, 3, &shortBstring, '?', "bboguss"); |
| 1911 |
test26_0(&shortBstring, 3, 8, &shortBstring, '?', "bogbogus"); |
| 1912 |
test26_0(&shortBstring, -1, 0, &shortBstring, '?', "bogus"); |
| 1913 |
test26_0(&shortBstring, 2, 0, &shortBstring, '?', "bobogusgus"); |
| 1914 |
test26_0(&shortBstring, 6, 0, &shortBstring, '?', "bogus?bogus"); |
| 1915 |
test26_0(&shortBstring, 6, 0, NULL, '?', "bogus"); |
| 1916 |
} |
| 1917 |
END_TEST |
| 1918 |
|
| 1919 |
static void |
| 1920 |
test27_0(bstring b0, const bstring b1, const char *res) |
| 1921 |
{ |
| 1922 |
bstring b2; |
| 1923 |
int ret = 0; |
| 1924 |
if (b0 && b0->data && b0->slen >= 0 && |
| 1925 |
b1 && b1->data && b1->slen >= 0) { |
| 1926 |
b2 = bstrcpy(b0); |
| 1927 |
if (b2 == NULL) { |
| 1928 |
ck_abort(); |
| 1929 |
return; /* Just a safeguard */ |
| 1930 |
} |
| 1931 |
bwriteprotect(*b2); |
| 1932 |
ret = bassign(b2, b1); |
| 1933 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1934 |
ret = biseq(b0, b2); |
| 1935 |
ck_assert_int_eq(ret, 1); |
| 1936 |
bwriteallow(*b2); |
| 1937 |
ret = bassign(b2, b1); |
| 1938 |
if (b1) { |
| 1939 |
ck_assert_int_eq(b2->slen, b1->slen); |
| 1940 |
} |
| 1941 |
ck_assert(!(((0 != ret) && (b1 != NULL)) || |
| 1942 |
((0 == ret) && (b1 == NULL)))); |
| 1943 |
if (res == NULL) { |
| 1944 |
ck_abort(); |
| 1945 |
return; /* Just a safeguard */ |
| 1946 |
} |
| 1947 |
ret = strlen(res); |
| 1948 |
ck_assert_int_eq(b2->slen, ret); |
| 1949 |
ret = memcmp(b2->data, res, b2->slen); |
| 1950 |
ck_assert_int_eq(ret, 0); |
| 1951 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 1952 |
ret = bdestroy(b2); |
| 1953 |
ck_assert_int_eq(ret, BSTR_OK); |
| 1954 |
} else { |
| 1955 |
ret = bassign(b0, b1); |
| 1956 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
START_TEST(core_027) |
| 1961 |
{ |
| 1962 |
/* tests with NULL */ |
| 1963 |
test27_0(NULL, NULL, NULL); |
| 1964 |
test27_0(NULL, &emptyBstring, NULL); |
| 1965 |
test27_0(&emptyBstring, NULL, ""); |
| 1966 |
test27_0(&badBstring1, &emptyBstring, NULL); |
| 1967 |
test27_0(&badBstring2, &emptyBstring, NULL); |
| 1968 |
test27_0(&emptyBstring, &badBstring1, NULL); |
| 1969 |
test27_0(&emptyBstring, &badBstring2, NULL); |
| 1970 |
/* normal operation tests on all sorts of subranges */ |
| 1971 |
test27_0(&emptyBstring, &emptyBstring, ""); |
| 1972 |
test27_0(&emptyBstring, &shortBstring, "bogus"); |
| 1973 |
test27_0(&shortBstring, &emptyBstring, ""); |
| 1974 |
test27_0(&shortBstring, &shortBstring, "bogus"); |
| 1975 |
} |
| 1976 |
END_TEST |
| 1977 |
|
| 1978 |
static void |
| 1979 |
test28_0(bstring s1, int c, int res) |
| 1980 |
{ |
| 1981 |
int ret = bstrchr (s1, c); |
| 1982 |
ck_assert_int_eq(ret, res); |
| 1983 |
} |
| 1984 |
|
| 1985 |
static void |
| 1986 |
test28_1(bstring s1, int c, int res) |
| 1987 |
{ |
| 1988 |
int ret = bstrrchr (s1, c); |
| 1989 |
ck_assert_int_eq(ret, res); |
| 1990 |
} |
| 1991 |
|
| 1992 |
static void |
| 1993 |
test28_2(bstring s1, int c, int pos, int res) |
| 1994 |
{ |
| 1995 |
int ret = bstrchrp (s1, c, pos); |
| 1996 |
ck_assert_int_eq(ret, res); |
| 1997 |
} |
| 1998 |
|
| 1999 |
static void |
| 2000 |
test28_3(bstring s1, int c, int pos, int res) |
| 2001 |
{ |
| 2002 |
int ret = bstrrchrp (s1, c, pos); |
| 2003 |
ck_assert_int_eq(ret, res); |
| 2004 |
} |
| 2005 |
|
| 2006 |
START_TEST(core_028) |
| 2007 |
{ |
| 2008 |
test28_0(NULL, 0, BSTR_ERR); |
| 2009 |
test28_0(&badBstring1, 'b', BSTR_ERR); |
| 2010 |
test28_0(&badBstring2, 's', BSTR_ERR); |
| 2011 |
test28_0(&emptyBstring, 0, BSTR_ERR); |
| 2012 |
test28_0(&shortBstring, 0, BSTR_ERR); |
| 2013 |
test28_0(&shortBstring, 'b', 0); |
| 2014 |
test28_0(&shortBstring, 's', 4); |
| 2015 |
test28_0(&shortBstring, 'q', BSTR_ERR); |
| 2016 |
test28_0(&xxxxxBstring, 0, BSTR_ERR); |
| 2017 |
test28_0(&xxxxxBstring, 'b', BSTR_ERR); |
| 2018 |
test28_0(&longBstring, 'i', 2); |
| 2019 |
test28_1(NULL, 0, BSTR_ERR); |
| 2020 |
test28_1(&badBstring1, 'b', BSTR_ERR); |
| 2021 |
test28_1(&badBstring2, 's', BSTR_ERR); |
| 2022 |
test28_1(&emptyBstring, 0, BSTR_ERR); |
| 2023 |
test28_1(&shortBstring, 0, BSTR_ERR); |
| 2024 |
test28_1(&shortBstring, 'b', 0); |
| 2025 |
test28_1(&shortBstring, 's', 4); |
| 2026 |
test28_1(&shortBstring, 'q', BSTR_ERR); |
| 2027 |
test28_1(&xxxxxBstring, 0, BSTR_ERR); |
| 2028 |
test28_1(&xxxxxBstring, 'b', BSTR_ERR); |
| 2029 |
test28_1(&longBstring, 'i', 82); |
| 2030 |
test28_2(NULL, 0, 0, BSTR_ERR); |
| 2031 |
test28_2(&badBstring1, 'b', 0, BSTR_ERR); |
| 2032 |
test28_2(&badBstring2, 's', 0, BSTR_ERR); |
| 2033 |
test28_2(&shortBstring, 'b', -1, BSTR_ERR); |
| 2034 |
test28_2(&shortBstring, 'b', shortBstring.slen, BSTR_ERR); |
| 2035 |
test28_2(&emptyBstring, 0, 0, BSTR_ERR); |
| 2036 |
test28_2(&shortBstring, 0, 0, BSTR_ERR); |
| 2037 |
test28_2(&shortBstring, 'b', 0, 0); |
| 2038 |
test28_2(&shortBstring, 'b', 1, BSTR_ERR); |
| 2039 |
test28_2(&shortBstring, 's', 0, 4); |
| 2040 |
test28_2(&shortBstring, 'q', 0, BSTR_ERR); |
| 2041 |
test28_3(NULL, 0, 0, BSTR_ERR); |
| 2042 |
test28_3(&badBstring1, 'b', 0, BSTR_ERR); |
| 2043 |
test28_3(&badBstring2, 's', 0, BSTR_ERR); |
| 2044 |
test28_3(&shortBstring, 'b', -1, BSTR_ERR); |
| 2045 |
test28_3(&shortBstring, 'b', shortBstring.slen, BSTR_ERR); |
| 2046 |
test28_3(&emptyBstring, 0, 0, BSTR_ERR); |
| 2047 |
test28_3(&shortBstring, 0, 0, BSTR_ERR); |
| 2048 |
test28_3(&shortBstring, 'b', 0, 0); |
| 2049 |
test28_3(&shortBstring, 'b', shortBstring.slen - 1, 0); |
| 2050 |
test28_3(&shortBstring, 's', shortBstring.slen - 1, 4); |
| 2051 |
test28_3(&shortBstring, 's', 0, BSTR_ERR); |
| 2052 |
} |
| 2053 |
END_TEST |
| 2054 |
|
| 2055 |
static void |
| 2056 |
test29_0(bstring b0, char *s, const char *res) |
| 2057 |
{ |
| 2058 |
bstring b2; |
| 2059 |
int ret = 0; |
| 2060 |
if (b0 && b0->data && b0->slen >= 0) { |
| 2061 |
b2 = bstrcpy(b0); |
| 2062 |
if (b2 == NULL) { |
| 2063 |
ck_abort(); |
| 2064 |
return; /* Just a safeguard */ |
| 2065 |
} |
| 2066 |
bwriteprotect(*b2); |
| 2067 |
ret = bcatcstr(b2, s); |
| 2068 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2069 |
ret = biseq(b0, b2); |
| 2070 |
ck_assert_int_eq(ret, 1); |
| 2071 |
bwriteallow(*b2); |
| 2072 |
ret = bcatcstr(b2, s); |
| 2073 |
if (s) { |
| 2074 |
ck_assert_int_eq(b2->slen, b0->slen + (int)strlen(s)); |
| 2075 |
} |
| 2076 |
ck_assert(!(((0 != ret) && (s != NULL)) || |
| 2077 |
((0 == ret) && (s == NULL)))); |
| 2078 |
ret = strlen(res); |
| 2079 |
ck_assert_int_eq(b2->slen, ret); |
| 2080 |
ret = memcmp(b2->data, res, b2->slen); |
| 2081 |
ck_assert_int_eq(ret, 0); |
| 2082 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2083 |
ret = bdestroy(b2); |
| 2084 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2085 |
} else { |
| 2086 |
ret = bcatcstr (b0, s); |
| 2087 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2088 |
} |
| 2089 |
} |
| 2090 |
|
| 2091 |
START_TEST(core_029) |
| 2092 |
{ |
| 2093 |
/* tests with NULL */ |
| 2094 |
test29_0(NULL, NULL, NULL); |
| 2095 |
test29_0(NULL, "", NULL); |
| 2096 |
test29_0(&emptyBstring, NULL, ""); |
| 2097 |
test29_0(&badBstring1, "bogus", NULL); |
| 2098 |
test29_0(&badBstring2, "bogus", NULL); |
| 2099 |
/* normal operation tests on all sorts of subranges */ |
| 2100 |
test29_0(&emptyBstring, "", ""); |
| 2101 |
test29_0(&emptyBstring, "bogus", "bogus"); |
| 2102 |
test29_0(&shortBstring, "", "bogus"); |
| 2103 |
test29_0(&shortBstring, "bogus", "bogusbogus"); |
| 2104 |
} |
| 2105 |
END_TEST |
| 2106 |
|
| 2107 |
static void |
| 2108 |
test30_0(bstring b0, const unsigned char *s, int len, const char *res) |
| 2109 |
{ |
| 2110 |
bstring b2; |
| 2111 |
int ret = 0; |
| 2112 |
if (b0 && b0->data && b0->slen >= 0) { |
| 2113 |
b2 = bstrcpy(b0); |
| 2114 |
if (b2 == NULL) { |
| 2115 |
ck_abort(); |
| 2116 |
return; /* Just a safeguard */ |
| 2117 |
} |
| 2118 |
bwriteprotect(*b2); |
| 2119 |
ret = bcatblk(b2, s, len); |
| 2120 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2121 |
ret = biseq(b0, b2); |
| 2122 |
ck_assert_int_eq(ret, 1); |
| 2123 |
bwriteallow(*b2); |
| 2124 |
ret = bcatblk(b2, s, len); |
| 2125 |
if (s) { |
| 2126 |
if (len >= 0) { |
| 2127 |
ck_assert_int_eq(b2->slen, b0->slen + len); |
| 2128 |
} else { |
| 2129 |
ck_assert_int_eq(b2->slen, b0->slen); |
| 2130 |
} |
| 2131 |
} |
| 2132 |
ck_assert(!(((0 != ret) && (s && len >= 0)) || |
| 2133 |
((0 == ret) && (s == NULL || len < 0)))); |
| 2134 |
if (res == NULL) { |
| 2135 |
ck_abort(); |
| 2136 |
return; /* Just a safeguard */ |
| 2137 |
} |
| 2138 |
ret = strlen(res); |
| 2139 |
ck_assert_int_eq(ret, b2->slen); |
| 2140 |
ret = memcmp(b2->data, res, b2->slen); |
| 2141 |
ck_assert_int_eq(ret, 0); |
| 2142 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2143 |
ret = bdestroy(b2); |
| 2144 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2145 |
} else { |
| 2146 |
ret = bcatblk (b0, s, len); |
| 2147 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2148 |
} |
| 2149 |
} |
| 2150 |
|
| 2151 |
START_TEST(core_030) |
| 2152 |
{ |
| 2153 |
/* tests with NULL */ |
| 2154 |
test30_0(NULL, NULL, 0, NULL); |
| 2155 |
test30_0(NULL, (unsigned char *) "", 0, NULL); |
| 2156 |
test30_0(&emptyBstring, NULL, 0, ""); |
| 2157 |
test30_0(&emptyBstring, NULL, -1, ""); |
| 2158 |
test30_0(&badBstring1, NULL, 0, NULL); |
| 2159 |
test30_0(&badBstring2, NULL, 0, NULL); |
| 2160 |
/* normal operation tests on all sorts of subranges */ |
| 2161 |
test30_0(&emptyBstring, (unsigned char *) "", -1, ""); |
| 2162 |
test30_0(&emptyBstring, (unsigned char *) "", 0, ""); |
| 2163 |
test30_0(&emptyBstring, (unsigned char *) "bogus", 5, "bogus"); |
| 2164 |
test30_0(&shortBstring, (unsigned char *) "", 0, "bogus"); |
| 2165 |
test30_0(&shortBstring, (unsigned char *) "bogus", 5, "bogusbogus"); |
| 2166 |
test30_0(&shortBstring, (unsigned char *) "bogus", -1, "bogus"); |
| 2167 |
} |
| 2168 |
END_TEST |
| 2169 |
|
| 2170 |
static void |
| 2171 |
test31_0(bstring b0, const bstring find, const bstring replace, |
| 2172 |
int pos, char *res) |
| 2173 |
{ |
| 2174 |
bstring b2; |
| 2175 |
int ret = 0; |
| 2176 |
if (b0 && b0->data && b0->slen >= 0 && |
| 2177 |
find && find->data && find->slen >= 0 && |
| 2178 |
replace && replace->data && replace->slen >= 0) { |
| 2179 |
b2 = bstrcpy(b0); |
| 2180 |
if (b2 == NULL) { |
| 2181 |
ck_abort(); |
| 2182 |
return; /* Just a safeguard */ |
| 2183 |
} |
| 2184 |
bwriteprotect(*b2); |
| 2185 |
ret = bfindreplace(b2, find, replace, pos); |
| 2186 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2187 |
ret = biseq (b0, b2); |
| 2188 |
ck_assert_int_eq(ret, 1); |
| 2189 |
bwriteallow(*b2); |
| 2190 |
ret = bfindreplace(b2, find, replace, pos); |
| 2191 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2192 |
ret = strlen(res); |
| 2193 |
ck_assert(b2->slen >= ret); |
| 2194 |
ret = memcmp(b2->data, res, b2->slen); |
| 2195 |
ck_assert_int_eq(ret, 0); |
| 2196 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2197 |
ret = bdestroy(b2); |
| 2198 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2199 |
} else { |
| 2200 |
ret = bfindreplace (b0, find, replace, pos); |
| 2201 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
static void |
| 2206 |
test31_1(bstring b0, const bstring find, const bstring replace, |
| 2207 |
int pos, char *res) |
| 2208 |
{ |
| 2209 |
bstring b2; |
| 2210 |
int ret = 0; |
| 2211 |
if (b0 && b0->data && b0->slen >= 0 && |
| 2212 |
find && find->data && find->slen >= 0 && |
| 2213 |
replace && replace->data && replace->slen >= 0) { |
| 2214 |
b2 = bstrcpy(b0); |
| 2215 |
if (b2 == NULL) { |
| 2216 |
ck_abort(); |
| 2217 |
return; /* Just a safeguard */ |
| 2218 |
} |
| 2219 |
bwriteprotect(*b2); |
| 2220 |
ret = bfindreplacecaseless(b2, find, replace, pos); |
| 2221 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2222 |
ret = biseq(b0, b2); |
| 2223 |
ck_assert_int_eq(ret, 1); |
| 2224 |
bwriteallow(*b2); |
| 2225 |
ret = bfindreplacecaseless(b2, find, replace, pos); |
| 2226 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2227 |
if (res == NULL) { |
| 2228 |
ck_abort(); |
| 2229 |
return; /* Just a safeguard */ |
| 2230 |
} |
| 2231 |
ret = strlen(res); |
| 2232 |
ck_assert(b2->slen >= ret); |
| 2233 |
ret = memcmp(b2->data, res, b2->slen); |
| 2234 |
ck_assert_int_eq(ret, 0); |
| 2235 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2236 |
ret = bdestroy(b2); |
| 2237 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2238 |
} else { |
| 2239 |
ret = bfindreplacecaseless (b0, find, replace, pos); |
| 2240 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
|
| 2244 |
#define LOTS_OF_S \ |
| 2245 |
"sssssssssssssssssssssssssssssssss" \ |
| 2246 |
"sssssssssssssssssssssssssssssssss" |
| 2247 |
|
| 2248 |
START_TEST(core_031) |
| 2249 |
{ |
| 2250 |
struct tagbstring t0 = bsStatic("funny"); |
| 2251 |
struct tagbstring t1 = bsStatic("weird"); |
| 2252 |
struct tagbstring t2 = bsStatic("s"); |
| 2253 |
struct tagbstring t3 = bsStatic("long"); |
| 2254 |
struct tagbstring t4 = bsStatic("big"); |
| 2255 |
struct tagbstring t5 = bsStatic("ss"); |
| 2256 |
struct tagbstring t6 = bsStatic("sstsst"); |
| 2257 |
struct tagbstring t7 = bsStatic("xx" LOTS_OF_S "xx"); |
| 2258 |
struct tagbstring t8 = bsStatic("S"); |
| 2259 |
struct tagbstring t9 = bsStatic("LONG"); |
| 2260 |
/* tests with NULL */ |
| 2261 |
test31_0(NULL, NULL, NULL, 0, NULL); |
| 2262 |
test31_0(&shortBstring, NULL, &t1, 0, (char *)shortBstring.data); |
| 2263 |
test31_0(&shortBstring, &t2, NULL, 0, (char *)shortBstring.data); |
| 2264 |
test31_0(&badBstring1, &t2, &t1, 0, NULL); |
| 2265 |
test31_0(&badBstring2, &t2, &t1, 0, NULL); |
| 2266 |
/* normal operation tests */ |
| 2267 |
test31_0(&longBstring, &shortBstring, &t0, 0, |
| 2268 |
"This is a funny but reasonably long string. " |
| 2269 |
"Just long enough to cause some mallocing."); |
| 2270 |
test31_0(&longBstring, &t2, &t1, 0, |
| 2271 |
"Thiweird iweird a boguweird but reaweirdonably " |
| 2272 |
"long weirdtring. Juweirdt long enough to cauweirde " |
| 2273 |
"weirdome mallocing."); |
| 2274 |
test31_0(&shortBstring, &t2, &t1, 0, "boguweird"); |
| 2275 |
test31_0(&shortBstring, &t8, &t1, 0, "bogus"); |
| 2276 |
test31_0(&longBstring, &t2, &t1, 27, |
| 2277 |
"This is a bogus but reasonably long weirdtring. " |
| 2278 |
"Juweirdt long enough to cauweirde weirdome mallocing."); |
| 2279 |
test31_0(&longBstring, &t3, &t4, 0, |
| 2280 |
"This is a bogus but reasonably big string. " |
| 2281 |
"Just big enough to cause some mallocing."); |
| 2282 |
test31_0(&longBstring, &t9, &t4, 0, |
| 2283 |
"This is a bogus but reasonably long string. " |
| 2284 |
"Just long enough to cause some mallocing."); |
| 2285 |
test31_0(&t6, &t2, &t5, 0, "sssstsssst"); |
| 2286 |
test31_0(&t7, &t2, &t5, 0, "xx" LOTS_OF_S LOTS_OF_S "xx"); |
| 2287 |
/* tests with NULL */ |
| 2288 |
test31_1(NULL, NULL, NULL, 0, NULL); |
| 2289 |
test31_1(&shortBstring, NULL, &t1, 0, (char *)shortBstring.data); |
| 2290 |
test31_1(&shortBstring, &t2, NULL, 0, (char *)shortBstring.data); |
| 2291 |
test31_1(&badBstring1, &t2, &t1, 0, NULL); |
| 2292 |
test31_1(&badBstring2, &t2, &t1, 0, NULL); |
| 2293 |
/* normal operation tests */ |
| 2294 |
test31_1(&longBstring, &shortBstring, &t0, 0, |
| 2295 |
"This is a funny but reasonably long string. " |
| 2296 |
"Just long enough to cause some mallocing."); |
| 2297 |
test31_1(&longBstring, &t2, &t1, 0, |
| 2298 |
"Thiweird iweird a boguweird but reaweirdonably " |
| 2299 |
"long weirdtring. Juweirdt long enough to cauweirde " |
| 2300 |
"weirdome mallocing."); |
| 2301 |
test31_1(&shortBstring, &t2, &t1, 0, "boguweird"); |
| 2302 |
test31_1(&shortBstring, &t8, &t1, 0, "boguweird"); |
| 2303 |
test31_1(&longBstring, &t2, &t1, 27, |
| 2304 |
"This is a bogus but reasonably long weirdtring. " |
| 2305 |
"Juweirdt long enough to cauweirde weirdome mallocing."); |
| 2306 |
test31_1(&longBstring, &t3, &t4, 0, |
| 2307 |
"This is a bogus but reasonably big string. " |
| 2308 |
"Just big enough to cause some mallocing."); |
| 2309 |
test31_1(&longBstring, &t9, &t4, 0, |
| 2310 |
"This is a bogus but reasonably big string. " |
| 2311 |
"Just big enough to cause some mallocing."); |
| 2312 |
test31_1(&t6, &t2, &t5, 0, "sssstsssst"); |
| 2313 |
test31_1(&t6, &t8, &t5, 0, "sssstsssst"); |
| 2314 |
test31_1(&t7, &t2, &t5, 0, "xx" LOTS_OF_S LOTS_OF_S "xx"); |
| 2315 |
} |
| 2316 |
END_TEST |
| 2317 |
|
| 2318 |
static void |
| 2319 |
test32_0(const bstring b, const char *s, int res) |
| 2320 |
{ |
| 2321 |
int ret = biseqcstr (b, s); |
| 2322 |
ck_assert_int_eq(ret, res); |
| 2323 |
} |
| 2324 |
|
| 2325 |
static void |
| 2326 |
test32_1(const bstring b, const char *s, int res) |
| 2327 |
{ |
| 2328 |
int ret = biseqcstrcaseless (b, s); |
| 2329 |
ck_assert_int_eq(ret, res); |
| 2330 |
} |
| 2331 |
|
| 2332 |
START_TEST(core_032) |
| 2333 |
{ |
| 2334 |
int ret; |
| 2335 |
bstring b; |
| 2336 |
/* tests with NULL */ |
| 2337 |
test32_0(NULL, NULL, BSTR_ERR); |
| 2338 |
test32_0(&emptyBstring, NULL, BSTR_ERR); |
| 2339 |
test32_0(NULL, "", BSTR_ERR); |
| 2340 |
test32_0(&badBstring1, "", BSTR_ERR); |
| 2341 |
test32_0(&badBstring2, "bogus", BSTR_ERR); |
| 2342 |
/* normal operation tests on all sorts of subranges */ |
| 2343 |
test32_0(&emptyBstring, "", 1); |
| 2344 |
test32_0(&shortBstring, "bogus", 1); |
| 2345 |
test32_0(&emptyBstring, "bogus", 0); |
| 2346 |
test32_0(&shortBstring, "", 0); |
| 2347 |
b = bstrcpy(&shortBstring); |
| 2348 |
ck_assert(b != NULL); |
| 2349 |
b->data[1]++; |
| 2350 |
test32_0(b, (char *)shortBstring.data, 0); |
| 2351 |
ret = bdestroy(b); |
| 2352 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2353 |
/* tests with NULL */ |
| 2354 |
test32_1(NULL, NULL, BSTR_ERR); |
| 2355 |
test32_1(&emptyBstring, NULL, BSTR_ERR); |
| 2356 |
test32_1(NULL, "", BSTR_ERR); |
| 2357 |
test32_1(&badBstring1, "", BSTR_ERR); |
| 2358 |
test32_1(&badBstring2, "bogus", BSTR_ERR); |
| 2359 |
/* normal operation tests on all sorts of subranges */ |
| 2360 |
test32_1(&emptyBstring, "", 1); |
| 2361 |
test32_1(&shortBstring, "bogus", 1); |
| 2362 |
test32_1(&shortBstring, "BOGUS", 1); |
| 2363 |
test32_1(&emptyBstring, "bogus", 0); |
| 2364 |
test32_1(&shortBstring, "", 0); |
| 2365 |
b = bstrcpy(&shortBstring); |
| 2366 |
ck_assert(b != NULL); |
| 2367 |
b->data[1]++; |
| 2368 |
test32_1(b, (char *)shortBstring.data, 0); |
| 2369 |
ret = bdestroy(b); |
| 2370 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2371 |
} |
| 2372 |
END_TEST |
| 2373 |
|
| 2374 |
static void |
| 2375 |
test33_0(bstring b0, const char *res) |
| 2376 |
{ |
| 2377 |
bstring b2; |
| 2378 |
int ret = 0; |
| 2379 |
if (b0 && b0->data && b0->slen >= 0) { |
| 2380 |
b2 = bstrcpy(b0); |
| 2381 |
if (b2 == NULL) { |
| 2382 |
ck_abort(); |
| 2383 |
return; /* Just a safeguard */ |
| 2384 |
} |
| 2385 |
bwriteprotect(*b2); |
| 2386 |
ret = btoupper(b2); |
| 2387 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2388 |
ret = biseq(b0, b2); |
| 2389 |
ck_assert_int_eq(ret, 1); |
| 2390 |
bwriteallow(*b2); |
| 2391 |
ret = btoupper(b2); |
| 2392 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2393 |
ck_assert_int_eq(b2->slen, b0->slen); |
| 2394 |
ret = strlen(res); |
| 2395 |
ck_assert_int_eq(b2->slen, ret); |
| 2396 |
ret = memcmp(b2->data, res, b2->slen); |
| 2397 |
ck_assert_int_eq(ret, 0); |
| 2398 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2399 |
ret = bdestroy(b2); |
| 2400 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2401 |
} else { |
| 2402 |
ret = btoupper(b0); |
| 2403 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2404 |
} |
| 2405 |
} |
| 2406 |
|
| 2407 |
START_TEST(core_033) |
| 2408 |
{ |
| 2409 |
/* tests with NULL */ |
| 2410 |
test33_0(NULL, NULL); |
| 2411 |
test33_0(&badBstring1, NULL); |
| 2412 |
test33_0(&badBstring2, NULL); |
| 2413 |
/* normal operation tests on all sorts of subranges */ |
| 2414 |
test33_0(&emptyBstring, ""); |
| 2415 |
test33_0(&shortBstring, "BOGUS"); |
| 2416 |
test33_0(&longBstring, |
| 2417 |
"THIS IS A BOGUS BUT REASONABLY LONG STRING. " |
| 2418 |
"JUST LONG ENOUGH TO CAUSE SOME MALLOCING."); |
| 2419 |
|
| 2420 |
} |
| 2421 |
END_TEST |
| 2422 |
|
| 2423 |
static void |
| 2424 |
test34_0(bstring b0, const char *res) |
| 2425 |
{ |
| 2426 |
bstring b2; |
| 2427 |
int ret = 0; |
| 2428 |
if (b0 != NULL && b0->data != NULL && b0->slen >= 0) { |
| 2429 |
b2 = bstrcpy(b0); |
| 2430 |
if (b2 == NULL) { |
| 2431 |
ck_abort(); |
| 2432 |
return; /* Just a safeguard */ |
| 2433 |
} |
| 2434 |
bwriteprotect(*b2); |
| 2435 |
ret = btolower(b2); |
| 2436 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2437 |
ret = biseq(b0, b2); |
| 2438 |
ck_assert_int_eq(ret, 1); |
| 2439 |
bwriteallow(*b2); |
| 2440 |
ret = btolower(b2); |
| 2441 |
ck_assert_int_eq(b2->slen, b0->slen); |
| 2442 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2443 |
if (res == NULL) { |
| 2444 |
ck_abort(); |
| 2445 |
return; /* Just a safeguard */ |
| 2446 |
} |
| 2447 |
ret = strlen(res); |
| 2448 |
ck_assert_int_eq(b2->slen, ret); |
| 2449 |
ret = memcmp(b2->data, res, b2->slen); |
| 2450 |
ck_assert_int_eq(ret, 0); |
| 2451 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2452 |
bdestroy(b2); |
| 2453 |
} else { |
| 2454 |
ret = btolower(b0); |
| 2455 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2456 |
} |
| 2457 |
} |
| 2458 |
|
| 2459 |
START_TEST(core_034) |
| 2460 |
{ |
| 2461 |
/* tests with NULL */ |
| 2462 |
test34_0(NULL, NULL); |
| 2463 |
test34_0(&badBstring1, NULL); |
| 2464 |
test34_0(&badBstring2, NULL); |
| 2465 |
/* normal operation tests on all sorts of subranges */ |
| 2466 |
test34_0(&emptyBstring, ""); |
| 2467 |
test34_0(&shortBstring, "bogus"); |
| 2468 |
test34_0(&longBstring, |
| 2469 |
"this is a bogus but reasonably long string. " |
| 2470 |
"just long enough to cause some mallocing."); |
| 2471 |
|
| 2472 |
} |
| 2473 |
END_TEST |
| 2474 |
|
| 2475 |
static void |
| 2476 |
test35_0(const bstring b0, const bstring b1, int res) |
| 2477 |
{ |
| 2478 |
int ret = bstricmp (b0, b1); |
| 2479 |
ck_assert_int_eq(ret, res); |
| 2480 |
} |
| 2481 |
|
| 2482 |
START_TEST(core_035) |
| 2483 |
{ |
| 2484 |
struct tagbstring t0 = bsStatic("bOgUs"); |
| 2485 |
struct tagbstring t1 = bsStatic("bOgUR"); |
| 2486 |
struct tagbstring t2 = bsStatic("bOgUt"); |
| 2487 |
/* tests with NULL */ |
| 2488 |
test35_0(NULL, NULL, SHRT_MIN); |
| 2489 |
test35_0(&emptyBstring, NULL, SHRT_MIN); |
| 2490 |
test35_0(NULL, &emptyBstring, SHRT_MIN); |
| 2491 |
test35_0(&emptyBstring, &badBstring1, SHRT_MIN); |
| 2492 |
test35_0(&badBstring1, &emptyBstring, SHRT_MIN); |
| 2493 |
test35_0(&shortBstring, &badBstring2, SHRT_MIN); |
| 2494 |
test35_0(&badBstring2, &shortBstring, SHRT_MIN); |
| 2495 |
/* normal operation tests on all sorts of subranges */ |
| 2496 |
test35_0(&emptyBstring, &emptyBstring, 0); |
| 2497 |
test35_0(&shortBstring, &t0, 0); |
| 2498 |
test35_0(&shortBstring, &t1, |
| 2499 |
tolower(shortBstring.data[4]) - tolower(t1.data[4])); |
| 2500 |
test35_0(&shortBstring, &t2, |
| 2501 |
tolower(shortBstring.data[4]) - tolower(t2.data[4])); |
| 2502 |
t0.slen++; |
| 2503 |
test35_0(&shortBstring, &t0, -(UCHAR_MAX+1)); |
| 2504 |
test35_0(&t0, &shortBstring, (UCHAR_MAX+1)); |
| 2505 |
} |
| 2506 |
END_TEST |
| 2507 |
|
| 2508 |
static void |
| 2509 |
test36_0(const bstring b0, const bstring b1, int n, int res) |
| 2510 |
{ |
| 2511 |
int ret = bstrnicmp(b0, b1, n); |
| 2512 |
ck_assert_int_eq(ret, res); |
| 2513 |
} |
| 2514 |
|
| 2515 |
START_TEST(core_036) |
| 2516 |
{ |
| 2517 |
struct tagbstring t0 = bsStatic("bOgUs"); |
| 2518 |
struct tagbstring t1 = bsStatic("bOgUR"); |
| 2519 |
struct tagbstring t2 = bsStatic("bOgUt"); |
| 2520 |
/* tests with NULL */ |
| 2521 |
test36_0(NULL, NULL, 0, SHRT_MIN); |
| 2522 |
test36_0(&emptyBstring, NULL, 0, SHRT_MIN); |
| 2523 |
test36_0(NULL, &emptyBstring, 0, SHRT_MIN); |
| 2524 |
test36_0(&emptyBstring, &badBstring1, 0, SHRT_MIN); |
| 2525 |
test36_0(&badBstring1, &emptyBstring, 0, SHRT_MIN); |
| 2526 |
test36_0(&shortBstring, &badBstring2, 5, SHRT_MIN); |
| 2527 |
test36_0(&badBstring2, &shortBstring, 5, SHRT_MIN); |
| 2528 |
/* normal operation tests on all sorts of subranges */ |
| 2529 |
test36_0(&emptyBstring, &emptyBstring, 0, 0); |
| 2530 |
test36_0(&shortBstring, &t0, 0, 0); |
| 2531 |
test36_0(&shortBstring, &t0, 5, 0); |
| 2532 |
test36_0(&shortBstring, &t0, 4, 0); |
| 2533 |
test36_0(&shortBstring, &t0, 6, 0); |
| 2534 |
test36_0(&shortBstring, &t1, 5, shortBstring.data[4] - t1.data[4]); |
| 2535 |
test36_0(&shortBstring, &t1, 4, 0); |
| 2536 |
test36_0(&shortBstring, &t1, 6, shortBstring.data[4] - t1.data[4]); |
| 2537 |
test36_0(&shortBstring, &t2, 5, shortBstring.data[4] - t2.data[4]); |
| 2538 |
test36_0(&shortBstring, &t2, 4, 0); |
| 2539 |
test36_0(&shortBstring, &t2, 6, shortBstring.data[4] - t2.data[4]); |
| 2540 |
t0.slen++; |
| 2541 |
test36_0(&shortBstring, &t0, 5, 0); |
| 2542 |
test36_0(&shortBstring, &t0, 6, -(UCHAR_MAX+1)); |
| 2543 |
test36_0(&t0, &shortBstring, 6, (UCHAR_MAX+1)); |
| 2544 |
} |
| 2545 |
END_TEST |
| 2546 |
|
| 2547 |
static void |
| 2548 |
test37_0(const bstring b0, const bstring b1, int res) |
| 2549 |
{ |
| 2550 |
int ret = biseqcaseless (b0, b1); |
| 2551 |
ck_assert_int_eq(ret, res); |
| 2552 |
} |
| 2553 |
|
| 2554 |
START_TEST(core_037) |
| 2555 |
{ |
| 2556 |
struct tagbstring t0 = bsStatic("bOgUs"); |
| 2557 |
struct tagbstring t1 = bsStatic("bOgUR"); |
| 2558 |
struct tagbstring t2 = bsStatic("bOgUt"); |
| 2559 |
/* tests with NULL */ |
| 2560 |
test37_0(NULL, NULL, BSTR_ERR); |
| 2561 |
test37_0(&emptyBstring, NULL, BSTR_ERR); |
| 2562 |
test37_0(NULL, &emptyBstring, BSTR_ERR); |
| 2563 |
test37_0(&emptyBstring, &badBstring1, BSTR_ERR); |
| 2564 |
test37_0(&badBstring1, &emptyBstring, BSTR_ERR); |
| 2565 |
test37_0(&shortBstring, &badBstring2, BSTR_ERR); |
| 2566 |
test37_0(&badBstring2, &shortBstring, BSTR_ERR); |
| 2567 |
/* normal operation tests on all sorts of subranges */ |
| 2568 |
test37_0(&emptyBstring, &emptyBstring, 1); |
| 2569 |
test37_0(&shortBstring, &t0, 1); |
| 2570 |
test37_0(&shortBstring, &t1, 0); |
| 2571 |
test37_0(&shortBstring, &t2, 0); |
| 2572 |
} |
| 2573 |
END_TEST |
| 2574 |
|
| 2575 |
struct emuFile { |
| 2576 |
int ofs; |
| 2577 |
bstring contents; |
| 2578 |
}; |
| 2579 |
|
| 2580 |
static int |
| 2581 |
test38_aux_bngetc(struct emuFile * f) |
| 2582 |
{ |
| 2583 |
int v = EOF; |
| 2584 |
if (f) { |
| 2585 |
v = bchare(f->contents, f->ofs, EOF); |
| 2586 |
if (EOF != v) { |
| 2587 |
f->ofs++; |
| 2588 |
} |
| 2589 |
} |
| 2590 |
return v; |
| 2591 |
} |
| 2592 |
|
| 2593 |
static size_t |
| 2594 |
test38_aux_bnread (void *buff, size_t elsize, size_t nelem, struct emuFile *f) |
| 2595 |
{ |
| 2596 |
char * b = (char *) buff; |
| 2597 |
int v; |
| 2598 |
size_t i, j, c = 0; |
| 2599 |
if (!f || !b) { |
| 2600 |
return c; |
| 2601 |
} |
| 2602 |
for (i = 0; i < nelem; i++) { |
| 2603 |
for (j = 0; j < elsize; j++) { |
| 2604 |
v = test38_aux_bngetc(f); |
| 2605 |
if (EOF == v) { |
| 2606 |
*b = '\0'; |
| 2607 |
return c; |
| 2608 |
} else { |
| 2609 |
*b = v; |
| 2610 |
b++; |
| 2611 |
c++; |
| 2612 |
} |
| 2613 |
} |
| 2614 |
} |
| 2615 |
return c; |
| 2616 |
} |
| 2617 |
|
| 2618 |
static int |
| 2619 |
test38_aux_bnopen(struct emuFile *f, bstring b) { |
| 2620 |
if (!f || !b) { |
| 2621 |
return -__LINE__; |
| 2622 |
} |
| 2623 |
f->ofs = 0; |
| 2624 |
f->contents = b; |
| 2625 |
return 0; |
| 2626 |
} |
| 2627 |
|
| 2628 |
START_TEST(core_038) |
| 2629 |
{ |
| 2630 |
struct emuFile f; |
| 2631 |
bstring b0, b1, b2, b3; |
| 2632 |
int ret = test38_aux_bnopen(&f, &shortBstring); |
| 2633 |
ck_assert_int_eq(ret, 0); |
| 2634 |
/* Creation/reads */ |
| 2635 |
#if defined(HAVE_BGETS) |
| 2636 |
b0 = bgetstream((bNgetc)test38_aux_bngetc, &f, 'b'); |
| 2637 |
#else |
| 2638 |
b0 = bgets((bNgetc)test38_aux_bngetc, &f, 'b'); |
| 2639 |
#endif |
| 2640 |
ck_assert(b0 != NULL); |
| 2641 |
b1 = bread((bNread)test38_aux_bnread, &f); |
| 2642 |
ck_assert(b1 != NULL); |
| 2643 |
#if defined(HAVE_BGETS) |
| 2644 |
b2 = bgetstream((bNgetc)test38_aux_bngetc, &f, '\0'); |
| 2645 |
#else |
| 2646 |
b2 = bgets((bNgetc)test38_aux_bngetc, &f, '\0'); |
| 2647 |
#endif |
| 2648 |
ck_assert(b2 == NULL); |
| 2649 |
b3 = bread((bNread)test38_aux_bnread, &f); |
| 2650 |
ck_assert(b3 != NULL); |
| 2651 |
ret = biseqcstr(b0, "b"); |
| 2652 |
ck_assert_int_eq(ret, 1); |
| 2653 |
ret = biseqcstr(b1, "ogus"); |
| 2654 |
ck_assert_int_eq(ret, 1); |
| 2655 |
ret = biseqcstr(b3, ""); |
| 2656 |
ck_assert_int_eq(ret, 1); |
| 2657 |
/* Bogus accumulations */ |
| 2658 |
f.ofs = 0; |
| 2659 |
ret = bgetsa(NULL, (bNgetc)test38_aux_bngetc, &f, 'o'); |
| 2660 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2661 |
ret = breada(NULL, (bNread)test38_aux_bnread, &f); |
| 2662 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2663 |
ret = bgetsa(&shortBstring, (bNgetc)test38_aux_bngetc, &f, 'o'); |
| 2664 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2665 |
ret = breada(&shortBstring, (bNread)test38_aux_bnread, &f); |
| 2666 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2667 |
/* Normal accumulations */ |
| 2668 |
ret = bgetsa(b0, (bNgetc) test38_aux_bngetc, &f, 'o'); |
| 2669 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2670 |
ret = breada(b1, (bNread) test38_aux_bnread, &f); |
| 2671 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2672 |
ret = biseqcstr(b0, "bbo"); |
| 2673 |
ck_assert_int_eq(ret, 1); |
| 2674 |
ret = biseqcstr(b1, "ogusgus"); |
| 2675 |
ck_assert_int_eq(ret, 1); |
| 2676 |
/* Attempt to append past end should do nothing */ |
| 2677 |
ret = bgetsa(b0, (bNgetc)test38_aux_bngetc, &f, 'o'); |
| 2678 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2679 |
ret = breada(b1, (bNread)test38_aux_bnread, &f); |
| 2680 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2681 |
ret = biseqcstr(b0, "bbo"); |
| 2682 |
ck_assert_int_eq(ret, 1); |
| 2683 |
ret = biseqcstr(b1, "ogusgus"); |
| 2684 |
ck_assert_int_eq(ret, 1); |
| 2685 |
ret = bdestroy(b0); |
| 2686 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2687 |
ret = bdestroy(b1); |
| 2688 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2689 |
ret = bdestroy(b2); |
| 2690 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2691 |
bdestroy(b3); |
| 2692 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2693 |
} |
| 2694 |
END_TEST |
| 2695 |
|
| 2696 |
static void |
| 2697 |
test39_0(const bstring b, const bstring lt, const bstring rt, const bstring t) |
| 2698 |
{ |
| 2699 |
bstring r; |
| 2700 |
int ret = 0; |
| 2701 |
ret = bltrimws(NULL); |
| 2702 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2703 |
ret = brtrimws(NULL); |
| 2704 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2705 |
ret = btrimws(NULL); |
| 2706 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2707 |
r = bstrcpy(b); |
| 2708 |
if (r == NULL) { |
| 2709 |
ck_abort(); |
| 2710 |
return; /* Just a safeguard */ |
| 2711 |
} |
| 2712 |
bwriteprotect(*r); |
| 2713 |
ret = bltrimws(r); |
| 2714 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2715 |
ret = brtrimws(r); |
| 2716 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2717 |
ret = btrimws(r); |
| 2718 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2719 |
bwriteallow(*r); |
| 2720 |
ret = bltrimws(r); |
| 2721 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2722 |
ret = biseq(r, lt); |
| 2723 |
ck_assert_int_eq(ret, 1); |
| 2724 |
ret = bdestroy(r); |
| 2725 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2726 |
r = bstrcpy(b); |
| 2727 |
ck_assert(r != NULL); |
| 2728 |
ret = brtrimws(r); |
| 2729 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2730 |
ret = biseq(r, rt); |
| 2731 |
ck_assert_int_eq(ret, 1); |
| 2732 |
ret = bdestroy(r); |
| 2733 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2734 |
r = bstrcpy(b); |
| 2735 |
ck_assert(r != NULL); |
| 2736 |
ret = btrimws(r); |
| 2737 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2738 |
ret = biseq(r, t); |
| 2739 |
ck_assert_int_eq(ret, 1); |
| 2740 |
ret = bdestroy(r); |
| 2741 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2742 |
} |
| 2743 |
|
| 2744 |
START_TEST(core_039) |
| 2745 |
{ |
| 2746 |
struct tagbstring t0 = bsStatic(" bogus string "); |
| 2747 |
struct tagbstring t1 = bsStatic("bogus string "); |
| 2748 |
struct tagbstring t2 = bsStatic(" bogus string"); |
| 2749 |
struct tagbstring t3 = bsStatic("bogus string"); |
| 2750 |
struct tagbstring t4 = bsStatic(" "); |
| 2751 |
struct tagbstring t5 = bsStatic(""); |
| 2752 |
test39_0(&t0, &t1, &t2, &t3); |
| 2753 |
test39_0(&t1, &t1, &t3, &t3); |
| 2754 |
test39_0(&t2, &t3, &t2, &t3); |
| 2755 |
test39_0(&t3, &t3, &t3, &t3); |
| 2756 |
test39_0(&t4, &t5, &t5, &t5); |
| 2757 |
test39_0(&t5, &t5, &t5, &t5); |
| 2758 |
} |
| 2759 |
END_TEST |
| 2760 |
|
| 2761 |
static void |
| 2762 |
test40_0(bstring b0, const bstring b1, int left, int len, const char *res) |
| 2763 |
{ |
| 2764 |
bstring b2; |
| 2765 |
int ret = 0; |
| 2766 |
if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && |
| 2767 |
b1 != NULL && b1->data != NULL && b1->slen >= 0) { |
| 2768 |
b2 = bstrcpy(b0); |
| 2769 |
if (b2 == NULL) { |
| 2770 |
ck_abort(); |
| 2771 |
return; /* Just a safeguard */ |
| 2772 |
} |
| 2773 |
bwriteprotect(*b2); |
| 2774 |
ret = bassignmidstr(b2, b1, left, len); |
| 2775 |
ck_assert_int_ne(ret, 0); |
| 2776 |
ret = biseq(b0, b2); |
| 2777 |
ck_assert_int_eq(ret, 1); |
| 2778 |
bwriteallow(*b2); |
| 2779 |
ret = bassignmidstr(b2, b1, left, len); |
| 2780 |
if (b1) { |
| 2781 |
ck_assert(!((b2->slen > len) | (b2->slen < 0))); |
| 2782 |
} |
| 2783 |
ck_assert(!(((0 != ret) && (b1 != NULL)) || |
| 2784 |
((0 == ret) && (b1 == NULL)))); |
| 2785 |
if (res == NULL) { |
| 2786 |
ck_abort(); |
| 2787 |
return; /* Just a safeguard */ |
| 2788 |
} |
| 2789 |
ret = strlen(res); |
| 2790 |
ck_assert_int_eq(b2->slen, ret); |
| 2791 |
ret = memcmp(b2->data, res, b2->slen); |
| 2792 |
ck_assert_int_eq(ret, 0); |
| 2793 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 2794 |
ret = bdestroy(b2); |
| 2795 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2796 |
} else { |
| 2797 |
ret = bassignmidstr(b0, b1, left, len); |
| 2798 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2799 |
} |
| 2800 |
} |
| 2801 |
|
| 2802 |
START_TEST(core_040) |
| 2803 |
{ |
| 2804 |
/* tests with NULL */ |
| 2805 |
test40_0(NULL, NULL, 0, 1, NULL); |
| 2806 |
test40_0(NULL, &emptyBstring, 0, 1, NULL); |
| 2807 |
test40_0(&emptyBstring, NULL, 0, 1, ""); |
| 2808 |
test40_0(&badBstring1, &emptyBstring, 0, 1, NULL); |
| 2809 |
test40_0(&badBstring2, &emptyBstring, 0, 1, NULL); |
| 2810 |
test40_0(&emptyBstring, &badBstring1, 0, 1, NULL); |
| 2811 |
test40_0(&emptyBstring, &badBstring2, 0, 1, NULL); |
| 2812 |
/* normal operation tests on all sorts of subranges */ |
| 2813 |
test40_0(&emptyBstring, &emptyBstring, 0, 1, ""); |
| 2814 |
test40_0(&emptyBstring, &shortBstring, 1, 3, "ogu"); |
| 2815 |
test40_0(&shortBstring, &emptyBstring, 0, 1, ""); |
| 2816 |
test40_0(&shortBstring, &shortBstring, 1, 3, "ogu"); |
| 2817 |
test40_0(&shortBstring, &shortBstring, -1, 4, "bog"); |
| 2818 |
test40_0(&shortBstring, &shortBstring, 1, 9, "ogus"); |
| 2819 |
test40_0(&shortBstring, &shortBstring, 9, 1, ""); |
| 2820 |
} |
| 2821 |
END_TEST |
| 2822 |
|
| 2823 |
static void |
| 2824 |
test41_0(bstring b1, int left, int len) |
| 2825 |
{ |
| 2826 |
struct tagbstring t; |
| 2827 |
bstring b2, b3; |
| 2828 |
int ret = 0; |
| 2829 |
if (b1 && b1->data && b1->slen >= 0) { |
| 2830 |
b2 = bfromcstr(""); |
| 2831 |
ck_assert(b2 != NULL); |
| 2832 |
bassignmidstr(b2, b1, left, len); |
| 2833 |
bmid2tbstr(t, b1, left, len); |
| 2834 |
b3 = bstrcpy(&t); |
| 2835 |
ck_assert(b3 != NULL); |
| 2836 |
ret = biseq(&t, b2); |
| 2837 |
ck_assert_int_eq(ret, 1); |
| 2838 |
ret = bdestroy(b2); |
| 2839 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2840 |
ret = bdestroy(b3); |
| 2841 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2842 |
} else { |
| 2843 |
bmid2tbstr(t, b1, left, len); |
| 2844 |
b3 = bstrcpy(&t); |
| 2845 |
ck_assert(b3 != NULL); |
| 2846 |
ck_assert_int_eq(t.slen, 0); |
| 2847 |
ret = bdestroy(b3); |
| 2848 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2849 |
} |
| 2850 |
} |
| 2851 |
|
| 2852 |
START_TEST(core_041) |
| 2853 |
{ |
| 2854 |
/* tests with NULL */ |
| 2855 |
test41_0(NULL, 0, 1); |
| 2856 |
test41_0(&emptyBstring, 0, 1); |
| 2857 |
test41_0(NULL, 0, 1); |
| 2858 |
test41_0(&emptyBstring, 0, 1); |
| 2859 |
test41_0(&emptyBstring, 0, 1); |
| 2860 |
test41_0(&badBstring1, 0, 1); |
| 2861 |
test41_0(&badBstring2, 0, 1); |
| 2862 |
/* normal operation tests on all sorts of subranges */ |
| 2863 |
test41_0(&emptyBstring, 0, 1); |
| 2864 |
test41_0(&shortBstring, 1, 3); |
| 2865 |
test41_0(&emptyBstring, 0, 1); |
| 2866 |
test41_0(&shortBstring, 1, 3); |
| 2867 |
test41_0(&shortBstring, -1, 4); |
| 2868 |
test41_0(&shortBstring, 1, 9); |
| 2869 |
test41_0(&shortBstring, 9, 1); |
| 2870 |
} |
| 2871 |
END_TEST |
| 2872 |
|
| 2873 |
static void |
| 2874 |
test42_0(const bstring bi, int len, const char *res) |
| 2875 |
{ |
| 2876 |
bstring b; |
| 2877 |
int ret = 0; |
| 2878 |
ret = btrunc(b = bstrcpy (bi), len); |
| 2879 |
if (len >= 0) { |
| 2880 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2881 |
} else { |
| 2882 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2883 |
} |
| 2884 |
if (res) { |
| 2885 |
ret = biseqcstr(b, res); |
| 2886 |
ck_assert_int_eq(ret, 1); |
| 2887 |
} |
| 2888 |
ret = bdestroy(b); |
| 2889 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2890 |
} |
| 2891 |
|
| 2892 |
START_TEST(core_042) |
| 2893 |
{ |
| 2894 |
int ret = 0; |
| 2895 |
/* tests with NULL */ |
| 2896 |
ret = btrunc(NULL, 2); |
| 2897 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2898 |
ret = btrunc (NULL, 0); |
| 2899 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2900 |
ret = btrunc (NULL, -1); |
| 2901 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2902 |
/* write protected */ |
| 2903 |
ret = btrunc (&shortBstring, 2); |
| 2904 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2905 |
ret = btrunc (&shortBstring, 0); |
| 2906 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2907 |
ret = btrunc (&shortBstring, -1); |
| 2908 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2909 |
test42_0(&emptyBstring, 10, ""); |
| 2910 |
test42_0(&emptyBstring, 0, ""); |
| 2911 |
test42_0(&emptyBstring, -1, NULL); |
| 2912 |
test42_0(&shortBstring, 10, "bogus"); |
| 2913 |
test42_0(&shortBstring, 3, "bog"); |
| 2914 |
test42_0(&shortBstring, 0, ""); |
| 2915 |
test42_0(&shortBstring, -1, NULL); |
| 2916 |
} |
| 2917 |
END_TEST |
| 2918 |
|
| 2919 |
START_TEST(core_043) |
| 2920 |
{ |
| 2921 |
static struct tagbstring ts0 = bsStatic(""); |
| 2922 |
static struct tagbstring ts1 = bsStatic(" "); |
| 2923 |
static struct tagbstring ts2 = bsStatic(" abc"); |
| 2924 |
static struct tagbstring ts3 = bsStatic("abc "); |
| 2925 |
static struct tagbstring ts4 = bsStatic(" abc "); |
| 2926 |
static struct tagbstring ts5 = bsStatic("abc"); |
| 2927 |
bstring tstrs[6] = { &ts0, &ts1, &ts2, &ts3, &ts4, &ts5 }; |
| 2928 |
int ret = 0; |
| 2929 |
for (int i = 0; i < 6; i++) { |
| 2930 |
struct tagbstring t; |
| 2931 |
bstring b; |
| 2932 |
btfromblkltrimws(t, tstrs[i]->data, tstrs[i]->slen); |
| 2933 |
bltrimws(b = bstrcpy (tstrs[i])); |
| 2934 |
ret = biseq(b, &t); |
| 2935 |
ck_assert_int_eq(ret, 1); |
| 2936 |
ret = bdestroy(b); |
| 2937 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2938 |
btfromblkrtrimws(t, tstrs[i]->data, tstrs[i]->slen); |
| 2939 |
brtrimws(b = bstrcpy(tstrs[i])); |
| 2940 |
ret = biseq(b, &t); |
| 2941 |
ck_assert_int_eq(ret, 1); |
| 2942 |
ret = bdestroy(b); |
| 2943 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2944 |
btfromblktrimws(t, tstrs[i]->data, tstrs[i]->slen); |
| 2945 |
btrimws(b = bstrcpy (tstrs[i])); |
| 2946 |
ret = biseq(b, &t); |
| 2947 |
ck_assert_int_eq(ret, 1); |
| 2948 |
ret = bdestroy(b); |
| 2949 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2950 |
} |
| 2951 |
} |
| 2952 |
END_TEST |
| 2953 |
|
| 2954 |
static void |
| 2955 |
test44_0(const char *str) |
| 2956 |
{ |
| 2957 |
int ret = 0; |
| 2958 |
bstring b = bfromcstr(""); |
| 2959 |
if (b == NULL) { |
| 2960 |
ck_abort(); |
| 2961 |
return; /* Just a safeguard */ |
| 2962 |
} |
| 2963 |
if (NULL == str) { |
| 2964 |
ret = bassigncstr(NULL, "test"); |
| 2965 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2966 |
ret = bassigncstr(b, NULL); |
| 2967 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2968 |
ret = bassigncstr(&shortBstring, NULL); |
| 2969 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2970 |
} else { |
| 2971 |
ret = bassigncstr(NULL, str); |
| 2972 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2973 |
ret = bassigncstr(b, str); |
| 2974 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2975 |
ret = strcmp(bdatae(b, ""), str); |
| 2976 |
ck_assert_int_eq(ret, 0); |
| 2977 |
ret = strlen(str); |
| 2978 |
ck_assert_int_eq(b->slen, ret); |
| 2979 |
ret = bassigncstr(b, "xxxxx"); |
| 2980 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2981 |
bwriteprotect(*b); |
| 2982 |
ret = bassigncstr (b, str); |
| 2983 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2984 |
ret = strcmp(bdatae(b, ""), "xxxxx"); |
| 2985 |
ck_assert_int_eq(ret, 0); |
| 2986 |
ret = strlen("xxxxx"); |
| 2987 |
ck_assert_int_eq(b->slen, ret); |
| 2988 |
bwriteallow(*b); |
| 2989 |
ret = bassigncstr(&shortBstring, str); |
| 2990 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2991 |
ret = bassigncstr(&shortBstring, str); |
| 2992 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 2993 |
} |
| 2994 |
ret = bdestroy(b); |
| 2995 |
ck_assert_int_eq(ret, BSTR_OK); |
| 2996 |
} |
| 2997 |
|
| 2998 |
START_TEST(core_044) |
| 2999 |
{ |
| 3000 |
/* tests with NULL */ |
| 3001 |
test44_0(NULL); |
| 3002 |
test44_0(EMPTY_STRING); |
| 3003 |
test44_0(SHORT_STRING); |
| 3004 |
test44_0(LONG_STRING); |
| 3005 |
} |
| 3006 |
END_TEST |
| 3007 |
|
| 3008 |
static void |
| 3009 |
test45_0(const char *str) |
| 3010 |
{ |
| 3011 |
int ret = 0, len; |
| 3012 |
bstring b = bfromcstr(""); |
| 3013 |
if (b == NULL) { |
| 3014 |
ck_abort(); |
| 3015 |
return; /* Just a safeguard */ |
| 3016 |
} |
| 3017 |
if (!str) { |
| 3018 |
ret = bassignblk(NULL, "test", 4); |
| 3019 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3020 |
ret = bassignblk(b, NULL, 1); |
| 3021 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3022 |
ret = bassignblk(&shortBstring, NULL, 1); |
| 3023 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3024 |
} else { |
| 3025 |
len = strlen(str); |
| 3026 |
ret = bassignblk(NULL, str, len); |
| 3027 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3028 |
ret = bassignblk(b, str, len); |
| 3029 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3030 |
ret = strcmp(bdatae(b, ""), str); |
| 3031 |
ck_assert_int_eq(ret, 0); |
| 3032 |
ck_assert_int_eq(b->slen, len); |
| 3033 |
ret = bassigncstr(b, "xxxxx"); |
| 3034 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3035 |
bwriteprotect(*b); |
| 3036 |
ret = bassignblk(b, str, len); |
| 3037 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3038 |
ret = strcmp(bdatae(b, ""), "xxxxx"); |
| 3039 |
ck_assert_int_eq(ret, 0); |
| 3040 |
ret = strlen("xxxxx"); |
| 3041 |
ck_assert_int_eq(b->slen, ret); |
| 3042 |
bwriteallow(*b); |
| 3043 |
ret = bassignblk(&shortBstring, str, len); |
| 3044 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3045 |
ret = bassignblk (&shortBstring, str, len); |
| 3046 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3047 |
} |
| 3048 |
ret = bdestroy(b); |
| 3049 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3050 |
} |
| 3051 |
|
| 3052 |
START_TEST(core_045) |
| 3053 |
{ |
| 3054 |
/* tests with NULL */ |
| 3055 |
test45_0(NULL); |
| 3056 |
test45_0(EMPTY_STRING); |
| 3057 |
test45_0(SHORT_STRING); |
| 3058 |
test45_0(LONG_STRING); |
| 3059 |
} |
| 3060 |
END_TEST |
| 3061 |
|
| 3062 |
static void |
| 3063 |
test46_0(const bstring r, bstring b, int count, const char *fmt, ...) |
| 3064 |
{ |
| 3065 |
int ret; |
| 3066 |
va_list arglist; |
| 3067 |
va_start(arglist, fmt); |
| 3068 |
ret = bvcformata(b, count, fmt, arglist); |
| 3069 |
va_end(arglist); |
| 3070 |
if (ret < 0) { |
| 3071 |
ck_assert(r == NULL); |
| 3072 |
} else { |
| 3073 |
ret = biseq(r, b); |
| 3074 |
ck_assert_int_eq(ret, 1); |
| 3075 |
} |
| 3076 |
} |
| 3077 |
|
| 3078 |
static void |
| 3079 |
test46_1(bstring b, const char * fmt, const bstring r, ...) |
| 3080 |
{ |
| 3081 |
int ret; |
| 3082 |
bvformata(ret, b, fmt, r); |
| 3083 |
if (ret < 0) { |
| 3084 |
ck_assert(r == NULL); |
| 3085 |
} else { |
| 3086 |
ret = biseq(r, b); |
| 3087 |
ck_assert_int_eq(ret, 1); |
| 3088 |
} |
| 3089 |
} |
| 3090 |
|
| 3091 |
START_TEST(core_046) |
| 3092 |
{ |
| 3093 |
bstring b, b2; |
| 3094 |
int ret = 0; |
| 3095 |
test46_0(NULL, NULL, 8, "[%d]", 15); |
| 3096 |
test46_0(NULL, &shortBstring, 8, "[%d]", 15); |
| 3097 |
test46_0(NULL, &badBstring1, 8, "[%d]", 15); |
| 3098 |
test46_0(NULL, &badBstring2, 8, "[%d]", 15); |
| 3099 |
test46_0(NULL, &badBstring3, 8, "[%d]", 15); |
| 3100 |
b = bfromcstr(""); |
| 3101 |
ck_assert(b != NULL); |
| 3102 |
test46_0(&shortBstring, b, shortBstring.slen, "%s", |
| 3103 |
(char *)shortBstring.data); |
| 3104 |
b->slen = 0; |
| 3105 |
test46_0(&shortBstring, b, shortBstring.slen + 1, "%s", |
| 3106 |
(char *)shortBstring.data); |
| 3107 |
b->slen = 0; |
| 3108 |
test46_0(NULL, b, shortBstring.slen-1, "%s", |
| 3109 |
(char *)shortBstring.data); |
| 3110 |
test46_1(NULL, "[%d]", NULL, 15); |
| 3111 |
test46_1(&shortBstring, "[%d]", NULL, 15); |
| 3112 |
test46_1(&badBstring1, "[%d]", NULL, 15); |
| 3113 |
test46_1(&badBstring2, "[%d]", NULL, 15); |
| 3114 |
test46_1(&badBstring3, "[%d]", NULL, 15); |
| 3115 |
b->slen = 0; |
| 3116 |
test46_1(b, "%s", &shortBstring, (char *)shortBstring.data); |
| 3117 |
b->slen = 0; |
| 3118 |
test46_1(b, "%s", &longBstring, (char *)longBstring.data); |
| 3119 |
b->slen = 0; |
| 3120 |
b2 = bfromcstr(EIGHT_CHAR_STRING); |
| 3121 |
bconcat(b2, b2); |
| 3122 |
bconcat(b2, b2); |
| 3123 |
bconcat(b2, b2); |
| 3124 |
test46_1(b, "%s%s%s%s%s%s%s%s", b2, |
| 3125 |
EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, |
| 3126 |
EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING); |
| 3127 |
bdestroy(b2); |
| 3128 |
ret = bdestroy(b); |
| 3129 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3130 |
} |
| 3131 |
END_TEST |
| 3132 |
|
| 3133 |
/* biseqblk */ |
| 3134 |
|
| 3135 |
static void |
| 3136 |
test47_0(const bstring b, const unsigned char *blk, int len, int res) |
| 3137 |
{ |
| 3138 |
int ret = biseqblk(b, blk, len); |
| 3139 |
ck_assert_int_eq(ret, res); |
| 3140 |
} |
| 3141 |
|
| 3142 |
START_TEST(core_047) |
| 3143 |
{ |
| 3144 |
/* tests with NULL */ |
| 3145 |
test47_0(NULL, NULL, 0, BSTR_ERR); |
| 3146 |
test47_0(&emptyBstring, NULL, 0, BSTR_ERR); |
| 3147 |
test47_0(NULL, emptyBstring.data, 0, BSTR_ERR); |
| 3148 |
test47_0(&shortBstring, NULL, shortBstring.slen, BSTR_ERR); |
| 3149 |
test47_0(NULL, shortBstring.data, 0, BSTR_ERR); |
| 3150 |
test47_0(&badBstring1, badBstring1.data, badBstring1.slen, BSTR_ERR); |
| 3151 |
test47_0(&badBstring2, badBstring2.data, badBstring2.slen, BSTR_ERR); |
| 3152 |
test47_0(&shortBstring, badBstring2.data, badBstring2.slen, BSTR_ERR); |
| 3153 |
test47_0(&badBstring2, shortBstring.data, shortBstring.slen, BSTR_ERR); |
| 3154 |
/* normal operation tests */ |
| 3155 |
test47_0(&emptyBstring, emptyBstring.data, emptyBstring.slen, 1); |
| 3156 |
test47_0(&shortBstring, emptyBstring.data, emptyBstring.slen, 0); |
| 3157 |
test47_0(&emptyBstring, shortBstring.data, shortBstring.slen, 0); |
| 3158 |
test47_0(&shortBstring, shortBstring.data, shortBstring.slen, 1); |
| 3159 |
{ |
| 3160 |
bstring b = bstrcpy(&shortBstring); |
| 3161 |
ck_assert(b != NULL); |
| 3162 |
b->data[1]++; |
| 3163 |
test47_0(b, shortBstring.data, shortBstring.slen, 0); |
| 3164 |
bdestroy(b); |
| 3165 |
} |
| 3166 |
test47_0(&shortBstring, longBstring.data, longBstring.slen, 0); |
| 3167 |
test47_0(&longBstring, shortBstring.data, shortBstring.slen, 0); |
| 3168 |
} |
| 3169 |
END_TEST |
| 3170 |
|
| 3171 |
/* biseqcaselessblk */ |
| 3172 |
|
| 3173 |
static void |
| 3174 |
test48_0(const bstring b, const unsigned char *blk, int len, int res) |
| 3175 |
{ |
| 3176 |
int ret = biseqcaselessblk(b, blk, len); |
| 3177 |
ck_assert_int_eq(ret, res); |
| 3178 |
} |
| 3179 |
|
| 3180 |
START_TEST(core_048) |
| 3181 |
{ |
| 3182 |
struct tagbstring t0 = bsStatic("bOgUs"); |
| 3183 |
struct tagbstring t1 = bsStatic("bOgUR"); |
| 3184 |
struct tagbstring t2 = bsStatic("bOgUt"); |
| 3185 |
/* tests with NULL */ |
| 3186 |
test48_0(NULL, NULL, 0, BSTR_ERR); |
| 3187 |
test48_0(&emptyBstring, NULL, 0, BSTR_ERR); |
| 3188 |
test48_0(NULL, emptyBstring.data, 0, BSTR_ERR); |
| 3189 |
test48_0(&emptyBstring, badBstring1.data, emptyBstring.slen, BSTR_ERR); |
| 3190 |
test48_0(&badBstring1, emptyBstring.data, badBstring1.slen, BSTR_ERR); |
| 3191 |
test48_0(&shortBstring, badBstring2.data, badBstring2.slen, BSTR_ERR); |
| 3192 |
test48_0(&badBstring2, shortBstring.data, badBstring2.slen, BSTR_ERR); |
| 3193 |
/* normal operation tests */ |
| 3194 |
test48_0(&emptyBstring, emptyBstring.data, emptyBstring.slen, 1); |
| 3195 |
test48_0(&shortBstring, t0.data, t0.slen, 1); |
| 3196 |
test48_0(&shortBstring, t1.data, t1.slen, 0); |
| 3197 |
test48_0(&shortBstring, t2.data, t2.slen, 0); |
| 3198 |
} |
| 3199 |
END_TEST |
| 3200 |
|
| 3201 |
/* binsertblk */ |
| 3202 |
|
| 3203 |
static void |
| 3204 |
test49_0(bstring b0, int pos, const void *blk, int len, |
| 3205 |
unsigned char fill, const char *res, int reslen) |
| 3206 |
{ |
| 3207 |
bstring b2; |
| 3208 |
int ret = 0; |
| 3209 |
if (b0 && b0->data && b0->slen >= 0 && |
| 3210 |
blk && len >= 0) { |
| 3211 |
b2 = bstrcpy(b0); |
| 3212 |
if (b2 == NULL) { |
| 3213 |
ck_abort(); |
| 3214 |
return; |
| 3215 |
} |
| 3216 |
bwriteprotect(*b2); |
| 3217 |
ret = binsertblk(b2, pos, blk, len, fill); |
| 3218 |
ck_assert_int_ne(ret, 0); |
| 3219 |
ret = biseq(b0, b2); |
| 3220 |
ck_assert_int_eq(ret, 1); |
| 3221 |
bwriteallow(*b2); |
| 3222 |
ret = binsertblk(b2, pos, blk, len, fill); |
| 3223 |
if (res == NULL) { |
| 3224 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3225 |
} else { |
| 3226 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3227 |
ck_assert_int_ge(reslen, 0); |
| 3228 |
ck_assert_int_eq(b2->slen, reslen); |
| 3229 |
ret = memcmp(b2->data, res, reslen); |
| 3230 |
ck_assert_int_eq(ret, 0); |
| 3231 |
ck_assert_int_eq(b2->data[b2->slen], '\0'); |
| 3232 |
} |
| 3233 |
ret = bdestroy(b2); |
| 3234 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3235 |
} else { |
| 3236 |
ret = binsertblk(b0, pos, blk, len, fill); |
| 3237 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 3238 |
} |
| 3239 |
} |
| 3240 |
|
| 3241 |
START_TEST(core_049) |
| 3242 |
{ |
| 3243 |
/* tests with NULL */ |
| 3244 |
test49_0(NULL, 0, NULL, 0, '?', NULL, -1); |
| 3245 |
test49_0(NULL, 0, "", 0, '?', NULL, -1); |
| 3246 |
test49_0(&badBstring1, 0, NULL, 0, '?', NULL, -1); |
| 3247 |
test49_0(&badBstring1, 0, badBstring1.data, 4, '?', NULL, -1); |
| 3248 |
test49_0(&badBstring2, 0, NULL, 0, '?', NULL, -1); |
| 3249 |
test49_0(&badBstring2, 0, badBstring2.data, 5, '?', NULL, -1); |
| 3250 |
/* normal operation tests */ |
| 3251 |
test49_0(&emptyBstring, 0, "", 0, '?', "", (int)(sizeof("") - 1)); |
| 3252 |
test49_0(&emptyBstring, 5, "", 0, '?', "?????", (int)(sizeof("?????") - 1)); |
| 3253 |
test49_0(&emptyBstring, 5, SHORT_STRING, 5, '?', "?????bogus", |
| 3254 |
(int)(sizeof("?????bogus") - 1)); |
| 3255 |
test49_0(&shortBstring, 0, "", 0, '?', "bogus", (int)(sizeof("bogus") - 1)); |
| 3256 |
test49_0(&emptyBstring, 0, SHORT_STRING, 5, '?', "bogus", |
| 3257 |
(int)(sizeof("bogus") - 1)); |
| 3258 |
test49_0(&shortBstring, 0, SHORT_STRING, 5, '?', "bogusbogus", |
| 3259 |
(int)(sizeof("bogusbogus") - 1)); |
| 3260 |
test49_0(&shortBstring, -1, SHORT_STRING, 5, '?', NULL, -1); |
| 3261 |
test49_0(&shortBstring, 2, SHORT_STRING, 5, '?', "bobogusgus", |
| 3262 |
(int)(sizeof("bobogusgus") - 1)); |
| 3263 |
test49_0(&shortBstring, 6, SHORT_STRING, 5, '?', "bogus?bogus", |
| 3264 |
(int)(sizeof("bogus?bogus") - 1)); |
| 3265 |
/* alias test: insert substring of self */ |
| 3266 |
{ |
| 3267 |
bstring b0 = bfromcstr("aaaaabbbbb"); |
| 3268 |
ck_assert(b0 != NULL); |
| 3269 |
b0->slen = 6; |
| 3270 |
int ret = binsertblk(b0, 2, b0->data + 4, 4, '?'); |
| 3271 |
ck_assert_int_eq(ret, BSTR_OK); |
| 3272 |
ck_assert_int_eq(biseqcstr(b0, "aaabbbaaab"), 1); |
| 3273 |
bdestroy(b0); |
| 3274 |
} |
| 3275 |
} |
| 3276 |
END_TEST |
| 3277 |
|
| 3278 |
/* bjoinblk */ |
| 3279 |
|
| 3280 |
START_TEST(core_050) |
| 3281 |
{ |
| 3282 |
struct bstrList *l; |
| 3283 |
bstring b; |
| 3284 |
bstring c; |
| 3285 |
int ret; |
| 3286 |
/* error cases */ |
| 3287 |
ck_assert(bjoinblk(NULL, ",", 1) == NULL); |
| 3288 |
ck_assert(bjoinblk(NULL, NULL, 0) == NULL); |
| 3289 |
/* empty list */ |
| 3290 |
l = bstrListCreate(); |
| 3291 |
ck_assert(l != NULL); |
| 3292 |
c = bjoinblk(l, ",", 1); |
| 3293 |
ck_assert(c != NULL); |
| 3294 |
ck_assert_int_eq(c->slen, 0); |
| 3295 |
bdestroy(c); |
| 3296 |
bstrListDestroy(l); |
| 3297 |
/* split and rejoin with blk separator */ |
| 3298 |
{ |
| 3299 |
b = bfromcstr("one,two,three"); |
| 3300 |
ck_assert(b != NULL); |
| 3301 |
l = bsplit(b, ','); |
| 3302 |
ck_assert(l != NULL); |
| 3303 |
ck_assert_int_eq(l->qty, 3); |
| 3304 |
c = bjoin(l, NULL); |
| 3305 |
ck_assert(c != NULL); |
| 3306 |
ck_assert_int_eq(biseqcstr(c, "onetwothree"), 1); |
| 3307 |
ck_assert_int_eq(c->data[c->slen], '\0'); |
| 3308 |
bdestroy(c); |
| 3309 |
c = bjoinblk(l, ",", 1); |
| 3310 |
ck_assert(c != NULL); |
| 3311 |
ret = biseq(c, b); |
| 3312 |
ck_assert_int_eq(ret, 1); |
| 3313 |
ck_assert_int_eq(c->data[c->slen], '\0'); |
| 3314 |
bdestroy(c); |
| 3315 |
/* rejoin with zero-length separator */ |
| 3316 |
c = bjoinblk(l, "", 0); |
| 3317 |
ck_assert(c != NULL); |
| 3318 |
ck_assert_int_eq(biseqcstr(c, "onetwothree"), 1); |
| 3319 |
ck_assert_int_eq(c->data[c->slen], '\0'); |
| 3320 |
bdestroy(c); |
| 3321 |
/* rejoin with multi-char separator */ |
| 3322 |
c = bjoinblk(l, "-->", 3); |
| 3323 |
ck_assert(c != NULL); |
| 3324 |
ck_assert_int_eq(biseqcstr(c, "one-->two-->three"), 1); |
| 3325 |
ck_assert_int_eq(c->data[c->slen], '\0'); |
| 3326 |
bdestroy(c); |
| 3327 |
bstrListDestroy(l); |
| 3328 |
bdestroy(b); |
| 3329 |
} |
| 3330 |
/* single entry list */ |
| 3331 |
{ |
| 3332 |
l = bstrListCreate(); |
| 3333 |
ck_assert(l != NULL); |
| 3334 |
bstrListAlloc(l, 2); |
| 3335 |
l->entry[0] = bfromcstr("only"); |
| 3336 |
l->qty = 1; |
| 3337 |
c = bjoinblk(l, ",", 1); |
| 3338 |
ck_assert(c != NULL); |
| 3339 |
ck_assert_int_eq(biseqcstr(c, "only"), 1); |
| 3340 |
ck_assert_int_eq(c->data[c->slen], '\0'); |
| 3341 |
bdestroy(c); |
| 3342 |
bdestroy(l->entry[0]); |
| 3343 |
l->qty = 0; |
| 3344 |
bstrListDestroy(l); |
| 3345 |
} |
| 3346 |
} |
| 3347 |
END_TEST |
| 3348 |
|
| 3349 |
int |
| 3350 |
main(void) |
| 3351 |
{ |
| 3352 |
/* Build test suite */ |
| 3353 |
Suite *suite = suite_create("bstr-core"); |
| 3354 |
/* Core tests */ |
| 3355 |
TCase *core = tcase_create("Core"); |
| 3356 |
tcase_add_test(core, core_000); |
| 3357 |
tcase_add_test(core, core_001); |
| 3358 |
tcase_add_test(core, core_002); |
| 3359 |
tcase_add_test(core, core_003); |
| 3360 |
tcase_add_test(core, core_004); |
| 3361 |
tcase_add_test(core, core_005); |
| 3362 |
tcase_add_test(core, core_006); |
| 3363 |
tcase_add_test(core, core_007); |
| 3364 |
tcase_add_test(core, core_008); |
| 3365 |
tcase_add_test(core, core_009); |
| 3366 |
tcase_add_test(core, core_010); |
| 3367 |
tcase_add_test(core, core_011); |
| 3368 |
tcase_add_test(core, core_012); |
| 3369 |
tcase_add_test(core, core_013); |
| 3370 |
tcase_add_test(core, core_014); |
| 3371 |
tcase_add_test(core, core_015); |
| 3372 |
tcase_add_test(core, core_016); |
| 3373 |
tcase_add_test(core, core_017); |
| 3374 |
tcase_add_test(core, core_018); |
| 3375 |
tcase_add_test(core, core_019); |
| 3376 |
tcase_add_test(core, core_020); |
| 3377 |
tcase_add_test(core, core_021); |
| 3378 |
tcase_add_test(core, core_022); |
| 3379 |
tcase_add_test(core, core_023); |
| 3380 |
tcase_add_test(core, core_024); |
| 3381 |
tcase_add_test(core, core_025); |
| 3382 |
tcase_add_test(core, core_026); |
| 3383 |
tcase_add_test(core, core_027); |
| 3384 |
tcase_add_test(core, core_028); |
| 3385 |
tcase_add_test(core, core_029); |
| 3386 |
tcase_add_test(core, core_030); |
| 3387 |
tcase_add_test(core, core_031); |
| 3388 |
tcase_add_test(core, core_032); |
| 3389 |
tcase_add_test(core, core_033); |
| 3390 |
tcase_add_test(core, core_034); |
| 3391 |
tcase_add_test(core, core_035); |
| 3392 |
tcase_add_test(core, core_036); |
| 3393 |
tcase_add_test(core, core_037); |
| 3394 |
tcase_add_test(core, core_038); |
| 3395 |
tcase_add_test(core, core_039); |
| 3396 |
tcase_add_test(core, core_040); |
| 3397 |
tcase_add_test(core, core_041); |
| 3398 |
tcase_add_test(core, core_042); |
| 3399 |
tcase_add_test(core, core_043); |
| 3400 |
tcase_add_test(core, core_044); |
| 3401 |
tcase_add_test(core, core_045); |
| 3402 |
tcase_add_test(core, core_046); |
| 3403 |
tcase_add_test(core, core_047); |
| 3404 |
tcase_add_test(core, core_048); |
| 3405 |
tcase_add_test(core, core_049); |
| 3406 |
tcase_add_test(core, core_050); |
| 3407 |
suite_add_tcase(suite, core); |
| 3408 |
/* Run tests */ |
| 3409 |
SRunner *runner = srunner_create(suite); |
| 3410 |
srunner_run_all(runner, CK_ENV); |
| 3411 |
int number_failed = srunner_ntests_failed(runner); |
| 3412 |
srunner_free(runner); |
| 3413 |
return (0 == number_failed) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 3414 |
} |