Util.c 18.1 KB
Newer Older
1 2
/*
 *  CUnit - A Unit testing framework library for C.
3 4
 *  Copyright (C) 2001       Anil Kumar
 *  Copyright (C) 2004-2006  Anil Kumar, Jerry St.Clair
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
22 23 24
 *  Generic (internal) utility functions used across CUnit.
 *  These were originally distributed across the other CUnit
 *  source files, but were consolidated here for consistency.
25
 *
26
 *  13/Oct/2001   Initial implementation (AK)
27
 *
28 29
 *  26/Jul/2003   Added a function to convert a string containing special
 *                characters into escaped character for XML/HTML usage. (AK)
30
 *
31
 *  16-Jul-2004   New interface, doxygen comments. (JDS)
32
 *
33
 *  17-Apr-2006   Added CU_translated_strlen() and CU_number_width().
tlh2000's avatar
tlh2000 committed
34 35
 *                Fixed off-by-1 error in CU_translate_special_characters(),
 *                modifying implementation & results in some cases.  User can
36
 *                now tell if conversion failed. (JDS)
37 38
 */

39 40 41 42 43 44 45
/** @file
 *  Utility functions (implementation).
 */
/** @addtogroup Framework
 @{
*/

46 47 48 49 50 51 52 53 54 55
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>

#include "CUnit.h"
#include "TestDB.h"
#include "Util.h"

56
extern cunit_trace_handler_t CU_trace_handler;
57
#ifdef _WIN32
58

59 60 61
#include <stdarg.h>
#include <winsock2.h>

62

Simon Morlat's avatar
Simon Morlat committed
63
void OutputDebugStringPrintf(FILE *file, const char *fmt, ...) {
64 65
	va_list args;

66 67 68 69
	va_start(args, fmt);
	if (CU_trace_handler) {
		CU_trace_handler(1, fmt, args);
	} else {
70
#ifdef _MSC_VER
Simon Morlat's avatar
Simon Morlat committed
71 72
		char msg[512];
		int len;
73 74
		len = vsnprintf(msg, sizeof(msg), fmt, args);
		if (len > 0) {
75
#ifndef _UNICODE
76
			OutputDebugString(msg);
77
#else
78 79 80 81
			wchar_t *tmp = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
			mbstowcs(tmp, msg, len);
			OutputDebugString(tmp);
			free(tmp);
82
#endif
83
		}
Simon Morlat's avatar
Simon Morlat committed
84 85 86
#else
		fprintf(file, fmt, args);
#endif
87
	}
88
	va_end(args);
89
}
90
#elif defined(ANDROID)
91

92
#include <android/log.h>
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

void AndroidPrintf(FILE *file, const char *fmt, ...) {
	char msg[512];
	va_list args;
	int len;
	int error = (file == stderr);

	va_start(args, fmt);
	if (CU_trace_handler) {
		CU_trace_handler((error)?1:0, fmt, args);
	} else {
		__android_log_vprint((error)?ANDROID_LOG_ERROR:ANDROID_LOG_INFO, "CUnit", fmt, args);
	}
	va_end(args);
}
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#else

void otherPrintf(FILE *file, const char *fmt, ...){
	va_list args;
	int error = (file == stderr);

	va_start(args, fmt);
	if (CU_trace_handler) {
		CU_trace_handler((error)?1:0, fmt, args);
	} else {
		vfprintf(file, fmt, args);
	}
	va_end(args);
}

124 125
#endif

126

127
/*------------------------------------------------------------------------*/
tlh2000's avatar
tlh2000 committed
128
/**
129 130
 *  Structure containing mappings of special characters to xml entity codes.
 *  special_char's in the CU_bindings array will be translated during calls
tlh2000's avatar
tlh2000 committed
131
 *  to CU_translate_special_characters().  Add additional chars/replacements
132
 *  or modify existing ones to change the behavior upon translation.
133
 */
134
static const struct bindings {
135 136 137
	const char special_char;    /**< Special character. */
	const char *replacement;    /**< Entity code for special character. */
} CU_bindings [] = {
138 139 140 141 142 143
    {'&', "&amp;"},
    {'>', "&gt;"},
    {'<', "&lt;"}
};

/*------------------------------------------------------------------------*/
tlh2000's avatar
tlh2000 committed
144
/**
145
 *  Checks whether a character is a special xml character.
146
 *  This function performs a lookup of the specified character in
147 148 149
 *  the CU_bindings structure.  If it is a special character, its
 *  index into the CU_bindings array is returned.  If not, -1 is returned.
 *
150
 *  @param ch The character to check
151
 *  @return Index into CU_bindings if a special character, -1 otherwise.
152
 */
153
static int get_index(const char ch)
154
{
155
	int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]);
156 157
	int counter;

158
	for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) {
159
		;
160
	}
161

162 163
	return (counter < length ? counter : -1);
}
164

165
size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen)
166
{
167 168
/* old implementation
  size_t count = 0;
169 170
	size_t src = 0;
	size_t dest = 0;
171
	size_t length = 0;
172 173
	int conv_index;

174 175
  assert(NULL != szSrc);
  assert(NULL != szDest);
176

177
	length = strlen(szSrc);
178
	memset(szDest, 0, maxlen);
179
	while ((dest < maxlen) && (src < length)) {
180

181
		if ((-1 != (conv_index = get_index(szSrc[src]))) &&
182 183 184
        ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) {
			strcat(szDest, CU_bindings[conv_index].replacement);
			dest += strlen(CU_bindings[conv_index].replacement);
185
			++count;
186 187 188 189 190 191 192
		} else {
			szDest[dest++] = szSrc[src];
		}

		++src;
	}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	return count;
*/
  size_t count = 0;
  size_t repl_len;
  int conv_index;
  char *dest_start = szDest;

  assert(NULL != szSrc);
  assert(NULL != szDest);

  /* only process if destination buffer not 0-length */
  if (maxlen > 0) {

    while ((maxlen > 0) && (*szSrc != '\0')) {
      conv_index = get_index(*szSrc);
      if (-1 != conv_index) {
        if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) {
			    memcpy(szDest, CU_bindings[conv_index].replacement, repl_len);
			    szDest += repl_len;
          maxlen -= repl_len;
			    ++count;
        } else {
          maxlen = 0;   /* ran out of room - abort conversion */
          break;
        }
		  } else {
			  *szDest++ = *szSrc;
        --maxlen;
		  }
		  ++szSrc;
	  }

    if (0 == maxlen) {
      *dest_start = '\0';   /* ran out of room - return empty string in szDest */
      count = 0;
    } else {
      *szDest = '\0';       /* had room - make sure szDest has a terminating \0 */
    }
  }
	return count;
}

/*------------------------------------------------------------------------*/
size_t CU_translated_strlen(const char* szSrc)
{
	size_t count = 0;
  int conv_index;

  assert(NULL != szSrc);

	while (*szSrc != '\0') {
    if (-1 != (conv_index = get_index(*szSrc))) {
      count += strlen(CU_bindings[conv_index].replacement);
    } else {
      ++count;
    }
    ++szSrc;
  }
251 252
	return count;
}
253

254 255
/*------------------------------------------------------------------------*/
int CU_compare_strings(const char* szSrc, const char* szDest)
256
{
257 258 259 260
  assert(NULL != szSrc);
  assert(NULL != szDest);

	while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) {
261 262 263
		szSrc++;
		szDest++;
	}
264

265
	return (int)(*szSrc - *szDest);
266 267
}

268 269
/*------------------------------------------------------------------------*/
void CU_trim(char* szString)
270
{
271 272
	CU_trim_left(szString);
	CU_trim_right(szString);
273 274
}

275 276
/*------------------------------------------------------------------------*/
void CU_trim_left(char* szString)
277 278 279 280
{
	int nOffset = 0;
	char* szSrc = szString;
	char* szDest = szString;
281

282
	assert(NULL != szString);
283

284
	/* Scan for the spaces in the starting of string. */
285 286
	for (; '\0' != *szSrc; szSrc++, nOffset++) {
		if (!isspace(*szSrc)) {
287
			break;
288
		}
289 290
	}

291
	for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) {
292
		;
293
	}
294 295
}

296 297
/*------------------------------------------------------------------------*/
void CU_trim_right(char* szString)
298
{
299
	size_t nLength;
300
	char* szSrc = szString;
301

302
	assert(NULL != szString);
303 304 305 306
	nLength = strlen(szString);
	/*
	 * Scan for specs in the end of string.
	 */
307
	for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) {
308
		;
309
	}
310 311 312 313

	*(szSrc + nLength) = '\0';
}

314 315 316
/*------------------------------------------------------------------------*/
size_t CU_number_width(int number)
{
anilsaharan's avatar
anilsaharan committed
317
	char buf[33];
318

anilsaharan's avatar
anilsaharan committed
319 320 321
	snprintf(buf, 33, "%d", number);
	buf[32] = '\0';
	return (strlen(buf));
322 323
}

324 325 326 327 328
/** @} */

#ifdef CUNIT_BUILD_TESTS
#include "test_cunit.h"

329 330 331
/* Keep BUF_LEN even or trouble ensues below... */
#define BUF_LEN 1000
#define MAX_LEN BUF_LEN/2
332 333

static void test_CU_translate_special_characters(void)
334
{
335 336 337
  char dest_buf[BUF_LEN];
  char *dest = dest_buf + MAX_LEN;
  char ref_buf[BUF_LEN];
338
  const int mask_char = 0x01;   /* char written to buffer  */
tlh2000's avatar
tlh2000 committed
339

340 341
  /* set up reference buffer for testing of translated strings */
  memset(ref_buf, mask_char, BUF_LEN);
tlh2000's avatar
tlh2000 committed
342

343
  /* empty src */
344 345 346 347 348
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));
349

350
  /* 1 char src */
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("#", dest, 0));
  TEST(!strncmp(dest_buf, ref_buf, BUF_LEN));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("#", dest, 1));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 2));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 4));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 5));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(1 == CU_translate_special_characters("&", dest, 6));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "&amp;\0", 6));
  TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6));
385 386

  /* maxlen=0 */
387
  memset(dest_buf, mask_char, BUF_LEN);
388
  strcpy(dest, "random initialized string");
389 390
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 0));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
391
  TEST(!strcmp(dest, "random initialized string"));
392
  TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1));
393 394

  /* maxlen < len(converted szSrc) */
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 1));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 2));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 5));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 10));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 20));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 24));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 25));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 37));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37));
442 443

  /* maxlen > len(converted szSrc) */
444 445 446 447 448 449 450 451 452 453 454
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(4 == CU_translate_special_characters("some <<string & another>", dest, 38));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some &lt;&lt;string &amp; another&gt;\0", 38));
  TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38));

  /* maxlen > len(converted szSrc) */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(4 == CU_translate_special_characters("some <<string & another>", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some &lt;&lt;string &amp; another&gt;\0", 38));
455 456

  /* no special characters */
457 458 459 460
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some string or another\0", 23));
461

462
  /* only special characters */
463 464 465
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
466
  TEST(!strcmp(dest, "&lt;&gt;&lt;&gt;&lt;&lt;&gt;&gt;&amp;&amp;&amp;"));
467 468
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
static void test_CU_translated_strlen(void)
{
  /* empty src */
  TEST(0 == CU_translated_strlen(""));

  /* 1 char src */
  TEST(1 == CU_translated_strlen("#"));
  TEST(5 == CU_translated_strlen("&"));
  TEST(4 == CU_translated_strlen("<"));
  TEST(4 == CU_translated_strlen(">"));
  TEST(1 == CU_translated_strlen("?"));

  /* 2 char src */
  TEST(2 == CU_translated_strlen("#@"));
  TEST(10 == CU_translated_strlen("&&"));
  TEST(9 == CU_translated_strlen(">&"));

  /* longer src */
  TEST(37 == CU_translated_strlen("some <<string & another>"));
  TEST(22 == CU_translated_strlen("some string or another"));
  TEST(47 == CU_translated_strlen("<><><<>>&&&"));
}

492
static void test_CU_compare_strings(void)
493
{
494 495 496 497 498 499 500 501 502 503
  TEST(0 == CU_compare_strings("",""));
  TEST(0 == CU_compare_strings("@","@"));
  TEST(0 == CU_compare_strings("D","d"));
  TEST(0 == CU_compare_strings("s1","s1"));
  TEST(0 == CU_compare_strings("s1","S1"));
  TEST(0 != CU_compare_strings("s1","s12"));
  TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1"));
  TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!"));
  TEST(0 != CU_compare_strings("not the same"," not the same"));
}
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
static void test_CU_trim(void)
{
  char string[MAX_LEN];

  strcpy(string, "");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim(string);
  TEST(!strcmp("b", string));

  strcpy(string, "  B");
  CU_trim(string);
  TEST(!strcmp("B", string));

  strcpy(string, "s ");
  CU_trim(string);
  TEST(!strcmp("s", string));

  strcpy(string, "S  ");
  CU_trim(string);
  TEST(!strcmp("S", string));

  strcpy(string, "  5   ");
  CU_trim(string);
  TEST(!strcmp("5", string));
540

541 542 543 544 545 546 547 548 549 550 551
  strcpy(string, "~ & ^ ( ^  ");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));
552
}
553

554
static void test_CU_trim_left(void)
555
{
556
  char string[MAX_LEN];
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
  strcpy(string, "");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim_left(string);
  TEST(!strcmp("b", string));

  strcpy(string, "  B");
  CU_trim_left(string);
  TEST(!strcmp("B", string));

  strcpy(string, "s ");
  CU_trim_left(string);
  TEST(!strcmp("s ", string));

  strcpy(string, "S  ");
  CU_trim_left(string);
  TEST(!strcmp("S  ", string));

  strcpy(string, "  5   ");
  CU_trim_left(string);
  TEST(!strcmp("5   ", string));

  strcpy(string, "~ & ^ ( ^  ");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^  ", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^  ", string));
601
}
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

static void test_CU_trim_right(void)
{
  char string[MAX_LEN];

  strcpy(string, "");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim_right(string);
  TEST(!strcmp(" b", string));

  strcpy(string, "  B");
  CU_trim_right(string);
  TEST(!strcmp("  B", string));

  strcpy(string, "s ");
  CU_trim_right(string);
  TEST(!strcmp("s", string));

  strcpy(string, "S  ");
  CU_trim_right(string);
  TEST(!strcmp("S", string));

  strcpy(string, "  5   ");
  CU_trim_right(string);
  TEST(!strcmp("  5", string));

  strcpy(string, "~ & ^ ( ^  ");
  CU_trim_right(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim_right(string);
  TEST(!strcmp("  ~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim_right(string);
  TEST(!strcmp("  ~ & ^ ( ^", string));
}

652 653 654 655 656 657 658 659 660 661 662
static void test_CU_number_width(void)
{
  TEST(1 == CU_number_width(0));
  TEST(1 == CU_number_width(1));
  TEST(2 == CU_number_width(-1));
  TEST(4 == CU_number_width(2346));
  TEST(7 == CU_number_width(-257265));
  TEST(9 == CU_number_width(245723572));
  TEST(9 == CU_number_width(-45622572));
}

663 664 665 666 667 668
void test_cunit_Util(void)
{

  test_cunit_start_tests("Util.c");

  test_CU_translate_special_characters();
669
  test_CU_translated_strlen();
670 671 672 673
  test_CU_compare_strings();
  test_CU_trim();
  test_CU_trim_left();
  test_CU_trim_right();
674
  test_CU_number_width();
675 676 677 678 679

  test_cunit_end_tests();
}

#endif    /* CUNIT_BUILD_TESTS */