Util.c 19.1 KB
Newer Older
1
/*
2
 *  BCUnit - 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
 *  Generic (internal) utility functions used across BCUnit.
 *  These were originally distributed across the other BCUnit
24
 *  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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>

52
#include "BCUnit.h"
53 54 55
#include "TestDB.h"
#include "Util.h"

56
extern bcunit_trace_handler_t CU_trace_handler;
57
#ifdef _WIN32
58

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

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

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

91
#include <android/log.h>
92

93 94 95 96 97 98 99 100 101 102 103

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 {
104
		__android_log_vprint((error)?ANDROID_LOG_ERROR:ANDROID_LOG_INFO, "BCUnit", fmt, args);
105 106 107
	}
	va_end(args);
}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#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);
}

123 124
#endif

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/*=================================================================
 *  Time management, prevents to add a dependency towards bctoolbox
 *=================================================================*/

//  Windows
#ifdef _WIN32
  #include <Windows.h>
  double CU_get_wall_time(void){
      LARGE_INTEGER time,freq;
      if (!QueryPerformanceFrequency(&freq)) {
         //  Handle error
         return 0;
      }
      else if (!QueryPerformanceCounter(&time)) {
         //  Handle error
         return 0;
      }
      else {
        return (double)time.QuadPart / freq.QuadPart;
      }
  }
#else
  //#include <time.h>
  #include <sys/time.h>
  double CU_get_wall_time(void) {
      struct timeval time;
      if (gettimeofday(&time,NULL)) {
          //  Handle error
          return 0;
      }
      //tv_usec is divided by a million to get as a double [seconds].[milliseconds]
      return (double)time.tv_sec + (double)time.tv_usec * .000001;
  }
#endif
159

160
/*------------------------------------------------------------------------*/
tlh2000's avatar
tlh2000 committed
161
/**
162 163
 *  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
164
 *  to CU_translate_special_characters().  Add additional chars/replacements
165
 *  or modify existing ones to change the behavior upon translation.
166
 */
167
static const struct bindings {
168 169 170
	const char special_char;    /**< Special character. */
	const char *replacement;    /**< Entity code for special character. */
} CU_bindings [] = {
171 172 173 174 175 176
    {'&', "&amp;"},
    {'>', "&gt;"},
    {'<', "&lt;"}
};

/*------------------------------------------------------------------------*/
tlh2000's avatar
tlh2000 committed
177
/**
178
 *  Checks whether a character is a special xml character.
179
 *  This function performs a lookup of the specified character in
180 181 182
 *  the CU_bindings structure.  If it is a special character, its
 *  index into the CU_bindings array is returned.  If not, -1 is returned.
 *
183
 *  @param ch The character to check
184
 *  @return Index into CU_bindings if a special character, -1 otherwise.
185
 */
186
static int get_index(const char ch)
187
{
188
	int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]);
189 190
	int counter;

191
	for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) {
192
		;
193
	}
194

195 196
	return (counter < length ? counter : -1);
}
197

198
size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen)
199
{
200 201
/* old implementation
  size_t count = 0;
202 203
	size_t src = 0;
	size_t dest = 0;
204
	size_t length = 0;
205 206
	int conv_index;

207 208
  assert(NULL != szSrc);
  assert(NULL != szDest);
209

210
	length = strlen(szSrc);
211
	memset(szDest, 0, maxlen);
212
	while ((dest < maxlen) && (src < length)) {
213

214
		if ((-1 != (conv_index = get_index(szSrc[src]))) &&
215 216 217
        ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) {
			strcat(szDest, CU_bindings[conv_index].replacement);
			dest += strlen(CU_bindings[conv_index].replacement);
218
			++count;
219 220 221 222 223 224 225
		} else {
			szDest[dest++] = szSrc[src];
		}

		++src;
	}

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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	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;
  }
284 285
	return count;
}
286

287 288
/*------------------------------------------------------------------------*/
int CU_compare_strings(const char* szSrc, const char* szDest)
289
{
290 291 292 293
  assert(NULL != szSrc);
  assert(NULL != szDest);

	while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) {
294 295 296
		szSrc++;
		szDest++;
	}
297

298
	return (int)(*szSrc - *szDest);
299 300
}

301 302
/*------------------------------------------------------------------------*/
void CU_trim(char* szString)
303
{
304 305
	CU_trim_left(szString);
	CU_trim_right(szString);
306 307
}

308 309
/*------------------------------------------------------------------------*/
void CU_trim_left(char* szString)
310 311 312 313
{
	int nOffset = 0;
	char* szSrc = szString;
	char* szDest = szString;
314

315
	assert(NULL != szString);
316

317
	/* Scan for the spaces in the starting of string. */
318 319
	for (; '\0' != *szSrc; szSrc++, nOffset++) {
		if (!isspace(*szSrc)) {
320
			break;
321
		}
322 323
	}

324
	for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) {
325
		;
326
	}
327 328
}

329 330
/*------------------------------------------------------------------------*/
void CU_trim_right(char* szString)
331
{
332
	size_t nLength;
333
	char* szSrc = szString;
334

335
	assert(NULL != szString);
336 337 338 339
	nLength = strlen(szString);
	/*
	 * Scan for specs in the end of string.
	 */
340
	for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) {
341
		;
342
	}
343 344 345 346

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

347 348 349
/*------------------------------------------------------------------------*/
size_t CU_number_width(int number)
{
anilsaharan's avatar
anilsaharan committed
350
	char buf[33];
351

anilsaharan's avatar
anilsaharan committed
352 353 354
	snprintf(buf, 33, "%d", number);
	buf[32] = '\0';
	return (strlen(buf));
355 356
}

357 358
/** @} */

359 360
#ifdef BCUNIT_BUILD_TESTS
#include "test_bcunit.h"
361

362 363 364
/* Keep BUF_LEN even or trouble ensues below... */
#define BUF_LEN 1000
#define MAX_LEN BUF_LEN/2
365 366

static void test_CU_translate_special_characters(void)
367
{
368 369 370
  char dest_buf[BUF_LEN];
  char *dest = dest_buf + MAX_LEN;
  char ref_buf[BUF_LEN];
371
  const int mask_char = 0x01;   /* char written to buffer  */
tlh2000's avatar
tlh2000 committed
372

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

376
  /* empty src */
377 378 379 380 381
  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));
382

383
  /* 1 char src */
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  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));
418 419

  /* maxlen=0 */
420
  memset(dest_buf, mask_char, BUF_LEN);
421
  strcpy(dest, "random initialized string");
422 423
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 0));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
424
  TEST(!strcmp(dest, "random initialized string"));
425
  TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1));
426 427

  /* maxlen < len(converted szSrc) */
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  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));
475 476

  /* maxlen > len(converted szSrc) */
477 478 479 480 481 482 483 484 485 486 487
  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));
488 489

  /* no special characters */
490 491 492 493
  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));
494

495
  /* only special characters */
496 497 498
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
499
  TEST(!strcmp(dest, "&lt;&gt;&lt;&gt;&lt;&lt;&gt;&gt;&amp;&amp;&amp;"));
500 501
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
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("<><><<>>&&&"));
}

525
static void test_CU_compare_strings(void)
526
{
527 528 529 530 531 532 533 534 535 536
  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"));
}
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
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));
573

574 575 576 577 578 579 580 581 582 583 584
  strcpy(string, "~ & ^ ( ^  ");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));

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

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

587
static void test_CU_trim_left(void)
588
{
589
  char string[MAX_LEN];
590

591 592 593 594 595 596 597 598 599 600 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
  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));
634
}
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

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));
}

685 686 687 688 689 690 691 692 693 694 695
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));
}

696
void test_bcunit_Util(void)
697 698
{

699
  test_bcunit_start_tests("Util.c");
700 701

  test_CU_translate_special_characters();
702
  test_CU_translated_strlen();
703 704 705 706
  test_CU_compare_strings();
  test_CU_trim();
  test_CU_trim_left();
  test_CU_trim_right();
707
  test_CU_number_width();
708

709
  test_bcunit_end_tests();
710 711
}

712
#endif    /* BCUNIT_BUILD_TESTS */