Back to index

lightning-sunbird  0.9+nobinonly
Classes | Typedefs | Functions | Variables
TestMinStringAPI.cpp File Reference
#include <stdio.h>
#include "nsStringAPI.h"
#include "nsCRT.h"

Go to the source code of this file.

Classes

struct  Test

Typedefs

typedef PRBool(* TestFunc )()

Functions

static PRBool test_basic_1 ()
static PRBool test_basic_2 ()
static PRBool test_convert ()
static PRBool test_append ()
static void ReplaceSubstring (nsACString &str, const nsACString &matchVal, const nsACString &newVal)
static PRBool test_replace_driver (const char *strVal, const char *matchVal, const char *newVal, const char *finalVal)
static PRBool test_replace ()
static void CompressWhitespace (nsACString &str)
static PRBool test_compress_ws ()
static PRBool test_depend ()
static PRBool test_depend_sub ()
static PRBool test_adopt ()
static PRBool test_adopt_sub ()
static PRBool test_mutation ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static const char kAsciiData [] = "hello world"
static const PRUnichar kUnicodeData []
static const char * kWhitespace = "\b\t\r\n "
static struct Test tests []

Typedef Documentation

typedef PRBool(* TestFunc)()

Definition at line 469 of file TestMinStringAPI.cpp.


Function Documentation

static void CompressWhitespace ( nsACString &  str) [static]

Definition at line 304 of file TestMinStringAPI.cpp.

  {
    const char *p;
    PRInt32 i, len = (PRInt32) NS_CStringGetData(str, &p);

    // trim leading whitespace

    for (i=0; i<len; ++i)
      {
        if (!strchr(kWhitespace, (char) p[i]))
          break;
      }

    if (i>0)
      {
        NS_CStringCutData(str, 0, i);
        len = (PRInt32) NS_CStringGetData(str, &p);
      }

    // trim trailing whitespace

    for (i=len-1; i>=0; --i)
      {
        if (!strchr(kWhitespace, (char) p[i]))
          break;
      }

    if (++i < len)
      NS_CStringCutData(str, i, len - i);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 494 of file TestMinStringAPI.cpp.

  {
    int count = 1;
    if (argc > 1)
      count = atoi(argv[1]);

    while (count--)
      {
        for (const Test* t = tests; t->name != nsnull; ++t)
          {
            printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
          }
      }
    
    return 0;
  }
static void ReplaceSubstring ( nsACString &  str,
const nsACString &  matchVal,
const nsACString &  newVal 
) [static]

Definition at line 210 of file TestMinStringAPI.cpp.

  {
    const char* sp, *mp, *np;
    PRUint32 sl, ml, nl;

    sl = NS_CStringGetData(str, &sp);
    ml = NS_CStringGetData(matchVal, &mp);
    nl = NS_CStringGetData(newVal, &np);

    for (const char* iter = sp; iter <= sp + sl - ml; ++iter)
      {
        if (memcmp(iter, mp, ml) == 0)
          {
            PRUint32 offset = iter - sp;

            NS_CStringSetDataRange(str, offset, ml, np, nl);

            sl = NS_CStringGetData(str, &sp);

            iter = sp + offset + nl - 1;
          }
      }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static PRBool test_adopt ( ) [static]

Definition at line 388 of file TestMinStringAPI.cpp.

  {
    static const char kData[] = "hello world";

    char *data = (char *) nsMemory::Clone(kData, sizeof(kData));
    if (!data)
      return PR_FALSE;

    nsCStringContainer s;
    NS_ENSURE_SUCCESS(
        NS_CStringContainerInit2(s, data, PR_UINT32_MAX,
                                 NS_CSTRING_CONTAINER_INIT_ADOPT),
        PR_FALSE); // leaks data on failure *shrug*

    const char *sd;
    NS_CStringGetData(s, &sd);

    PRBool rv = (sd == data);
    NS_CStringContainerFinish(s);
    return rv;
  }

Here is the call graph for this function:

static PRBool test_adopt_sub ( ) [static]

Definition at line 410 of file TestMinStringAPI.cpp.

  {
    static const char kData[] = "hello world";

    char *data = (char *) nsMemory::Clone(kData, sizeof(kData)-1);
    if (!data)
      return PR_FALSE;

    nsCStringContainer s;
    NS_ENSURE_SUCCESS(
        NS_CStringContainerInit2(s, data, sizeof(kData)-1,
                                 NS_CSTRING_CONTAINER_INIT_ADOPT |
                                 NS_CSTRING_CONTAINER_INIT_SUBSTRING),
        PR_FALSE); // leaks data on failure *shrug*

    PRBool terminated;
    const char *sd;
    PRUint32 len = NS_CStringGetData(s, &sd, &terminated);

    PRBool rv = (sd == data && len == sizeof(kData)-1 && !terminated);
    NS_CStringContainerFinish(s);
    return rv;
  }

Here is the call graph for this function:

static PRBool test_append ( ) [static]

Definition at line 197 of file TestMinStringAPI.cpp.

  {
    nsCStringContainer s;
    NS_CStringContainerInit(s);

    NS_CStringSetData(s, "foo");
    NS_CStringAppendData(s, "bar");

    NS_CStringContainerFinish(s);
    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_basic_1 ( ) [static]

Definition at line 51 of file TestMinStringAPI.cpp.

  {
    nsCStringContainer s;
    NS_CStringContainerInit(s);

    const char *ptr;
    PRUint32 len;
    char *clone;

    NS_CStringGetData(s, &ptr);
    if (ptr == nsnull || *ptr != '\0')
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    NS_CStringSetData(s, kAsciiData, PR_UINT32_MAX);
    len = NS_CStringGetData(s, &ptr);
    if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    if (len != sizeof(kAsciiData)-1)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    clone = NS_CStringCloneData(s);
    if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    nsMemory::Free(clone);

    nsCStringContainer temp;
    NS_CStringContainerInit(temp);
    NS_CStringCopy(temp, s);

    len = NS_CStringGetData(temp, &ptr);
    if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    if (len != sizeof(kAsciiData)-1)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    NS_CStringContainerFinish(temp);

    NS_CStringContainerFinish(s);
    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_basic_2 ( ) [static]

Definition at line 110 of file TestMinStringAPI.cpp.

  {
    nsStringContainer s;
    NS_StringContainerInit(s);

    const PRUnichar *ptr;
    PRUint32 len;
    PRUnichar *clone;

    NS_StringGetData(s, &ptr);
    if (ptr == nsnull || *ptr != '\0')
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    NS_StringSetData(s, kUnicodeData, PR_UINT32_MAX);
    len = NS_StringGetData(s, &ptr);
    if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    if (len != sizeof(kUnicodeData)/2 - 1)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    clone = NS_StringCloneData(s);
    if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    nsMemory::Free(clone);

    nsStringContainer temp;
    NS_StringContainerInit(temp);
    NS_StringCopy(temp, s);

    len = NS_StringGetData(temp, &ptr);
    if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }
    if (len != sizeof(kUnicodeData)/2 - 1)
      {
        NS_ERROR("unexpected result");
        return PR_FALSE;
      }

    NS_StringContainerFinish(temp);

    NS_StringContainerFinish(s);

    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_compress_ws ( ) [static]

Definition at line 335 of file TestMinStringAPI.cpp.

  {
    nsCStringContainer s;
    NS_CStringContainerInit(s);
    NS_CStringSetData(s, " \thello world\r  \n");
    CompressWhitespace(s);
    const char *d;
    NS_CStringGetData(s, &d);
    PRBool rv = !strcmp(d, "hello world");
    if (!rv)
      printf("=> \"%s\"\n", d);
    NS_CStringContainerFinish(s);
    return rv;
  }

Here is the call graph for this function:

static PRBool test_convert ( void  ) [static]

Definition at line 170 of file TestMinStringAPI.cpp.

  {
    nsStringContainer s;
    NS_StringContainerInit(s);
    NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1);

    nsCStringContainer temp;
    NS_CStringContainerInit(temp);

    const char *data;

    NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp);
    NS_CStringGetData(temp, &data);
    if (strcmp(data, kAsciiData) != 0)
      return PR_FALSE;

    NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp);
    NS_CStringGetData(temp, &data);
    if (strcmp(data, kAsciiData) != 0)
      return PR_FALSE;

    NS_CStringContainerFinish(temp);

    NS_StringContainerFinish(s);
    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_depend ( ) [static]

Definition at line 350 of file TestMinStringAPI.cpp.

  {
    static const char kData[] = "hello world";

    nsCStringContainer s;
    NS_ENSURE_SUCCESS(
        NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
                                 NS_CSTRING_CONTAINER_INIT_DEPEND),
        PR_FALSE);

    const char *sd;
    NS_CStringGetData(s, &sd);

    PRBool rv = (sd == kData);
    NS_CStringContainerFinish(s);
    return rv;
  }

Here is the call graph for this function:

static PRBool test_depend_sub ( ) [static]

Definition at line 368 of file TestMinStringAPI.cpp.

  {
    static const char kData[] = "hello world";

    nsCStringContainer s;
    NS_ENSURE_SUCCESS(
        NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
                                 NS_CSTRING_CONTAINER_INIT_DEPEND |
                                 NS_CSTRING_CONTAINER_INIT_SUBSTRING),
        PR_FALSE);

    PRBool terminated;
    const char *sd;
    PRUint32 len = NS_CStringGetData(s, &sd, &terminated);

    PRBool rv = (sd == kData && len == sizeof(kData)-1 && !terminated);
    NS_CStringContainerFinish(s);
    return rv;
  }

Here is the call graph for this function:

static PRBool test_mutation ( ) [static]

Definition at line 434 of file TestMinStringAPI.cpp.

  {
    nsCStringContainer s;
    NS_CStringContainerInit(s);

    const char kText[] = "Every good boy does fine.";

    char *buf;
    PRUint32 len = NS_CStringGetMutableData(s, sizeof(kText) - 1, &buf);
    if (!buf || len != sizeof(kText) - 1)
      return PR_FALSE;
    memcpy(buf, kText, sizeof(kText));

    const char *data;
    NS_CStringGetData(s, &data);
    if (strcmp(data, kText) != 0)
      return PR_FALSE;

    PRUint32 newLen = len + 1;
    len = NS_CStringGetMutableData(s, newLen, &buf);
    if (!buf || len != newLen)
      return PR_FALSE;

    buf[len - 1] = '.';

    NS_CStringGetData(s, &data);
    if (strncmp(data, kText, len - 1) != 0 || data[len - 1] != '.')
      return PR_FALSE;

    NS_CStringContainerFinish(s);
    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_replace ( ) [static]

Definition at line 266 of file TestMinStringAPI.cpp.

  {
    PRBool rv;

    rv = test_replace_driver("hello world, hello again!",
                             "hello",
                             "goodbye",
                             "goodbye world, goodbye again!");
    if (!rv)
      return rv;

    rv = test_replace_driver("foofoofoofoo!",
                             "foo",
                             "bar",
                             "barbarbarbar!");
    if (!rv)
      return rv;

    rv = test_replace_driver("foo bar systems",
                             "xyz",
                             "crazy",
                             "foo bar systems");
    if (!rv)
      return rv;

    rv = test_replace_driver("oh",
                             "xyz",
                             "crazy",
                             "oh");
    if (!rv)
      return rv;

    return PR_TRUE;
  }

Here is the call graph for this function:

static PRBool test_replace_driver ( const char *  strVal,
const char *  matchVal,
const char *  newVal,
const char *  finalVal 
) [static]

Definition at line 236 of file TestMinStringAPI.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char kAsciiData[] = "hello world" [static]

Definition at line 46 of file TestMinStringAPI.cpp.

Initial value:
  {'h','e','l','l','o',' ','w','o','r','l','d','\0'}

Definition at line 48 of file TestMinStringAPI.cpp.

const char* kWhitespace = "\b\t\r\n " [static]

Definition at line 301 of file TestMinStringAPI.cpp.

struct Test tests[] [static]