Back to index

lightning-sunbird  0.9+nobinonly
Classes | Typedefs | Functions | Variables
TestStrings.cpp File Reference
#include <stdio.h>
#include "nsString.h"
#include "nsStringBuffer.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"

Go to the source code of this file.

Classes

struct  Test

Typedefs

typedef PRBool(* TestFunc )()

Functions

void test_assign_helper (const nsACString &in, nsACString &_retval)
PRBool test_assign ()
PRBool test_assign_c ()
PRBool test1 ()
PRBool test2 ()
PRBool test_find ()
PRBool test_rfind ()
PRBool test_rfind_2 ()
PRBool test_rfind_3 ()
PRBool test_rfind_4 ()
PRBool test_distance ()
PRBool test_length ()
PRBool test_trim ()
PRBool test_replace_substr ()
PRBool test_replace_substr_2 ()
PRBool test_strip_ws ()
PRBool test_equals_ic ()
PRBool test_fixed_string ()
PRBool test_concat ()
PRBool test_concat_2 ()
PRBool test_xpidl_string ()
PRBool test_empty_assign ()
PRBool test_set_length ()
PRBool test_substring ()
PRBool test_appendint64 ()
PRBool test_findcharinset ()
PRBool test_rfindcharinset ()
PRBool test_stringbuffer ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static struct Test tests []

Typedef Documentation

typedef PRBool(* TestFunc)()

Definition at line 618 of file TestStrings.cpp.


Function Documentation

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 659 of file TestStrings.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;
  }

Definition at line 68 of file TestStrings.cpp.

  {
    NS_NAMED_LITERAL_STRING(empty, "");
    const nsAString& aStr = empty;

    nsAutoString buf(aStr);

    PRInt32 n = buf.FindChar(',');

    n = buf.Length();

    buf.Cut(0, n + 1);
    n = buf.FindChar(',');

    if (n != kNotFound)
      printf("n=%d\n", n);

    return n == kNotFound;
  }

Definition at line 88 of file TestStrings.cpp.

  {
    nsCString data("hello world");
    const nsACString& aStr = data;

    nsCString temp(aStr);
    temp.Cut(0, 6);

    PRBool r = strcmp(temp.get(), "world") == 0;
    if (!r)
      printf("[temp=%s]\n", temp.get());
    return r;
  }

Here is the call graph for this function:

Definition at line 468 of file TestStrings.cpp.

  {
    nsCString str;

    PRInt64 max = LL_MaxInt();
    static const char max_expected[] = "9223372036854775807";
    PRInt64 min = LL_MinInt();
    static const char min_expected[] = "-9223372036854775808";
    static const char min_expected_oct[] = "1000000000000000000000";
    PRInt64 maxint_plus1 = LL_INIT(1, 0);
    static const char maxint_plus1_expected[] = "4294967296";
    static const char maxint_plus1_expected_x[] = "100000000";

    str.AppendInt(max);

    if (!str.Equals(max_expected)) {
      fprintf(stderr, "Error appending LL_MaxInt(): Got %s\n", str.get());
      return PR_FALSE;
    }

    str.Truncate();
    str.AppendInt(min);
    if (!str.Equals(min_expected)) {
      fprintf(stderr, "Error appending LL_MinInt(): Got %s\n", str.get());
      return PR_FALSE;
    }
    str.Truncate();
    str.AppendInt(min, 8);
    if (!str.Equals(min_expected_oct)) {
      fprintf(stderr, "Error appending LL_MinInt() (oct): Got %s\n", str.get());
      return PR_FALSE;
    }


    str.Truncate();
    str.AppendInt(maxint_plus1);
    if (!str.Equals(maxint_plus1_expected)) {
      fprintf(stderr, "Error appending PR_UINT32_MAX + 1: Got %s\n", str.get());
      return PR_FALSE;
    }
    str.Truncate();
    str.AppendInt(maxint_plus1, 16);
    if (!str.Equals(maxint_plus1_expected_x)) {
      fprintf(stderr, "Error appending PR_UINT32_MAX + 1 (hex): Got %s\n", str.get());
      return PR_FALSE;
    }


    return PR_TRUE;
  }

Here is the call graph for this function:

Definition at line 49 of file TestStrings.cpp.

  {
    nsCString result;
    test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
    PRBool r = strcmp(result.get(), "ab") == 0;
    if (!r)
      printf("[result=%s]\n", result.get());
    return r;
  }

Here is the call graph for this function:

Definition at line 59 of file TestStrings.cpp.

  {
    nsCString c; c.Assign('c');
    PRBool r = strcmp(c.get(), "c") == 0;
    if (!r)
      printf("[result=%s]\n", c.get());
    return r;
  }

Here is the call graph for this function:

void test_assign_helper ( const nsACString &  in,
nsACString &  _retval 
)

Definition at line 44 of file TestStrings.cpp.

  {
    _retval = in;
  }

Here is the caller graph for this function:

Definition at line 330 of file TestStrings.cpp.

  {
    nsCString bar("bar");
    const nsACString& barRef = bar;

    const nsPromiseFlatCString& result =
        PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
                           NS_LITERAL_CSTRING(",") +
                           barRef);
    if (strcmp(result.get(), "foo,bar") == 0)
      return PR_TRUE;

    printf("[result=%s]\n", result.get());
    return PR_FALSE;
  }

Here is the call graph for this function:

Definition at line 346 of file TestStrings.cpp.

  {
    nsCString fieldTextStr("xyz");
    nsCString text("text");
    const nsACString& aText = text;

    nsCAutoString result( fieldTextStr + aText );

    if (strcmp(result.get(), "xyztext") == 0)
      return PR_TRUE;
    
    printf("[result=%s]\n", result.get());
    return PR_FALSE;
  }

Here is the call graph for this function:

Definition at line 189 of file TestStrings.cpp.

  {
    const char text[] = "abc-xyz";
    nsCString s(text);
    nsCString::const_iterator begin, end;
    s.BeginReading(begin);
    s.EndReading(end);
    size_t d = Distance(begin, end);
    PRBool r = (d == sizeof(text)-1);
    if (!r)
      printf("d=%u\n", d);
    return r;
  }

Here is the call graph for this function:

Definition at line 425 of file TestStrings.cpp.

  {
    nsCString a;
    a.AssignLiteral("");

    a.AppendLiteral("");

    nsCString b;
    b.SetCapacity(0);
    return PR_TRUE;
  }

Definition at line 302 of file TestStrings.cpp.

  {
    nsCString s;
    PRBool r = s.LowerCaseEqualsLiteral("view-source");
    if (r)
      printf("[r=%d]\n", r);
    return !r;
  }

Definition at line 102 of file TestStrings.cpp.

  {
    nsCString src("<!DOCTYPE blah blah blah>");

    PRInt32 i = src.Find("DOCTYPE", PR_TRUE, 2, 1);
    if (i == 2)
      return PR_TRUE;

    printf("i=%d\n", i);
    return PR_FALSE;
  }

Definition at line 519 of file TestStrings.cpp.

  {
    nsCString buf("hello, how are you?");

    PRInt32 index = buf.FindCharInSet(",?", 5);
    if (index != 5)
      return PR_FALSE;

    index = buf.FindCharInSet("helo", 0);
    if (index != 0)
      return PR_FALSE;

    index = buf.FindCharInSet("z?", 6);
    if (index != (PRInt32) buf.Length()-1)
      return PR_FALSE;

    return PR_TRUE;
  }

Definition at line 311 of file TestStrings.cpp.

  {
    char buf[256] = "hello world";

    nsFixedCString s(buf, sizeof(buf));

    if (s.Length() != strlen(buf))
      return PR_FALSE;

    if (strcmp(s.get(), buf) != 0)
      return PR_FALSE;

    s.Assign("foopy doopy doo");
    if (s.get() != buf)
      return PR_FALSE;
    
    return PR_TRUE;
  }

Definition at line 203 of file TestStrings.cpp.

  {
    const char text[] = "abc-xyz";
    nsCString s(text);
    size_t d = s.Length();
    PRBool r = (d == sizeof(text)-1);
    if (!r)
      printf("d=%u\n", d);
    return r;
  }

Definition at line 227 of file TestStrings.cpp.

  {
    const char text[] = "abc-ppp-qqq-ppp-xyz";
    nsCString s(text);
    s.ReplaceSubstring("ppp", "www");
    PRBool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
    if (!r)
      {
        printf("[s=%s]\n", s.get());
        return PR_FALSE;
      }

    s.Assign("foobar");
    s.ReplaceSubstring("foo", "bar");
    s.ReplaceSubstring("bar", "");
    r = strcmp(s.get(), "") == 0;
    if (!r)
      {
        printf("[s=%s]\n", s.get());
        return PR_FALSE;
      }

    s.Assign("foofoofoo");
    s.ReplaceSubstring("foo", "foo");
    r = strcmp(s.get(), "foofoofoo") == 0;
    if (!r)
      {
        printf("[s=%s]\n", s.get());
        return PR_FALSE;
      }

    s.Assign("foofoofoo");
    s.ReplaceSubstring("of", "fo");
    r = strcmp(s.get(), "fofoofooo") == 0;
    if (!r)
      {
        printf("[s=%s]\n", s.get());
        return PR_FALSE;
      }

    return PR_TRUE;
  }

Here is the call graph for this function:

Definition at line 270 of file TestStrings.cpp.

  {
    const char *oldName = nsnull;
    const char *newName = "user";
    nsString acctName; acctName.AssignLiteral("forums.foo.com");
    nsAutoString newAcctName, oldVal, newVal;
    oldVal.AssignWithConversion(oldName);
    newVal.AssignWithConversion(newName);
    newAcctName.Assign(acctName);

    // here, oldVal is empty.  we are testing that this function
    // does not hang.  see bug 235355.
    newAcctName.ReplaceSubstring(oldVal, newVal);

    // we expect that newAcctName will be unchanged.
    if (!newAcctName.Equals(acctName))
      return PR_FALSE;

    return PR_TRUE;
  }

Definition at line 114 of file TestStrings.cpp.

  {
    const char text[] = "<!DOCTYPE blah blah blah>";
    const char term[] = "bLaH";
    nsCString src(text);
    PRInt32 i;

    i = src.RFind(term, PR_TRUE, 3, -1); 
    if (i != kNotFound)
      {
        printf("unexpected result searching from offset=3, i=%d\n", i);
        return PR_FALSE;
      }

    i = src.RFind(term, PR_TRUE, -1, -1);
    if (i != 20)
      {
        printf("unexpected result searching from offset=-1, i=%d\n", i);
        return PR_FALSE;
      }

    i = src.RFind(term, PR_TRUE, 13, -1);
    if (i != 10)
      {
        printf("unexpected result searching from offset=13, i=%d\n", i);
        return PR_FALSE;
      }

    i = src.RFind(term, PR_TRUE, 22, 3);
    if (i != 20)
      {
        printf("unexpected result searching from offset=22, i=%d\n", i);
        return PR_FALSE;
      }

    return PR_TRUE;
  }

Definition at line 152 of file TestStrings.cpp.

  {
    const char text[] = "<!DOCTYPE blah blah blah>";
    nsCString src(text);
    PRInt32 i = src.RFind("TYPE", PR_FALSE, 5, -1); 
    if (i == 5)
      return PR_TRUE;

    printf("i=%d\n", i);
    return PR_FALSE;
  }

Definition at line 164 of file TestStrings.cpp.

  {
    const char text[] = "urn:mozilla:locale:en-US:necko";
    nsCAutoString value(text);
    PRInt32 i = value.RFind(":");
    if (i == 24)
      return PR_TRUE;

    printf("i=%d\n", i);
    return PR_FALSE;
  }

Definition at line 176 of file TestStrings.cpp.

  {
    nsCString value("a.msf");
    PRInt32 i = value.RFind(".msf");
    if (i != 1)
      {
        printf("i=%d\n", i);
        return PR_FALSE;
      }

    return PR_TRUE;
  }

Definition at line 538 of file TestStrings.cpp.

  {
    nsCString buf("hello, how are you?");

    PRInt32 index = buf.RFindCharInSet(",?", 5);
    if (index != 5)
      return PR_FALSE;

    index = buf.RFindCharInSet("helo", 0);
    if (index != 0)
      return PR_FALSE;

    index = buf.RFindCharInSet("z?", 6);
    if (index != kNotFound)
      return PR_FALSE;

    index = buf.RFindCharInSet("l", 5);
    if (index != 3)
      return PR_FALSE;

    buf.Assign("abcdefghijkabc");

    index = buf.RFindCharInSet("ab");
    if (index != 12)
      return PR_FALSE;

    index = buf.RFindCharInSet("ab", 11);
    if (index != 11)
      return PR_FALSE;

    index = buf.RFindCharInSet("ab", 10);
    if (index != 1)
      return PR_FALSE;

    index = buf.RFindCharInSet("ab", 0);
    if (index != 0)
      return PR_FALSE;

    index = buf.RFindCharInSet("cd", 1);
    if (index != kNotFound)
      return PR_FALSE;

    index = buf.RFindCharInSet("h");
    if (index != 7)
      return PR_FALSE;

    return PR_TRUE;
  }

Definition at line 437 of file TestStrings.cpp.

  {
    const char kText[] = "Default Plugin";
    nsCString buf;
    buf.SetCapacity(sizeof(kText)-1);
    buf.Assign(kText);
    buf.SetLength(sizeof(kText)-1);
    if (strcmp(buf.get(), kText) != 0)
      return PR_FALSE;
    return PR_TRUE;
  }

Here is the call graph for this function:

Definition at line 587 of file TestStrings.cpp.

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

    nsStringBuffer *buf;
    
    buf = nsStringBuffer::Alloc(sizeof(kData));
    if (!buf)
      return PR_FALSE;
    buf->Release();
 
    buf = nsStringBuffer::Alloc(sizeof(kData));
    if (!buf)
      return PR_FALSE;
    char *data = (char *) buf->Data();
    memcpy(data, kData, sizeof(kData));

    nsCString str;
    buf->ToString(sizeof(kData)-1, str);

    nsStringBuffer *buf2;
    buf2 = nsStringBuffer::FromString(str);

    PRBool rv = (buf == buf2);

    buf->Release();
    return rv;
  }

Here is the call graph for this function:

Definition at line 291 of file TestStrings.cpp.

  {
    const char text[] = " a    $   ";
    nsCString s(text);
    s.StripWhitespace();
    PRBool r = strcmp(s.get(), "a$") == 0;
    if (!r)
      printf("[s=%s]\n", s.get());
    return r;
  }

Here is the call graph for this function:

Definition at line 449 of file TestStrings.cpp.

  {
    nsCString super("hello world"), sub("hello");

    // this tests that |super| starts with |sub|,
    
    PRBool r = sub.Equals(StringHead(super, sub.Length()));
    if (!r)
      return PR_FALSE;

    // and verifies that |sub| does not start with |super|.

    r = super.Equals(StringHead(sub, super.Length()));
    if (r)
      return PR_FALSE;

    return PR_TRUE;
  }

Here is the call graph for this function:

Definition at line 214 of file TestStrings.cpp.

  {
    const char text[] = " a\t    $   ";
    const char set[] = " \t$";

    nsCString s(text);
    s.Trim(set);
    PRBool r = strcmp(s.get(), "a") == 0;
    if (!r)
      printf("[s=%s]\n", s.get());
    return r;
  }

Here is the call graph for this function:

Definition at line 373 of file TestStrings.cpp.

  {
    nsXPIDLCString a, b;
    a = b;
    if (a != b)
      return PR_FALSE;

    a.Adopt(0);
    if (a != b)
      return PR_FALSE;

    a.Append("foopy");
    a.Assign(b);
    if (a != b)
      return PR_FALSE;

    a.Insert("", 0);
    a.Assign(b);
    if (a != b)
      return PR_FALSE;

    const char text[] = "hello world";
    *getter_Copies(a) = nsCRT::strdup(text);
    if (strcmp(a, text) != 0)
      return PR_FALSE;

    b = a;
    if (strcmp(a, b) != 0)
      return PR_FALSE;

    a.Adopt(0);
    nsACString::const_iterator begin, end;
    a.BeginReading(begin);
    a.EndReading(end);
    char *r = ToNewCString(Substring(begin, end));
    if (strcmp(r, "") != 0)
      return PR_FALSE;
    nsMemory::Free(r);

    a.Adopt(0);
    if (a != (const char*) 0)
      return PR_FALSE;

    /*
    PRInt32 index = a.FindCharInSet("xyz");
    if (index != kNotFound)
      return PR_FALSE;
    */

    return PR_TRUE;
  }

Here is the call graph for this function:


Variable Documentation

struct Test tests[] [static]