Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
TestTArray.cpp File Reference
#include <stdlib.h>
#include <stdio.h>
#include "nsTArray.h"
#include "nsMemory.h"
#include "nsAutoPtr.h"
#include "nsString.h"
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsXPCOM.h"
#include "nsILocalFile.h"

Go to the source code of this file.

Classes

class  Object
class  nsFileNameComparator
class  RefcountedObject
struct  Test

Defines

#define DECL_TEST(name)   { #name, name }

Typedefs

typedef nsCOMPtr< nsIFileFilePointer
typedef PRBool(* TestFunc )()

Functions

template<class T >
bool operator< (const nsCOMPtr< T > &lhs, const nsCOMPtr< T > &rhs)
template<class ElementType >
static PRBool test_basic_array (ElementType *data, PRUint32 dataLen, const ElementType &extra)
static PRBool test_int_array ()
static PRBool test_int64_array ()
static PRBool test_char_array ()
static PRBool test_uint32_array ()
static PRBool test_object_array ()
static PRBool operator== (const nsCString &a, const char *b)
static PRBool test_string_array ()
static PRBool test_comptr_array ()
static PRBool test_refptr_array ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static struct Test tests []

Define Documentation

#define DECL_TEST (   name)    { #name, name }

Definition at line 369 of file TestTArray.cpp.


Typedef Documentation

Definition at line 288 of file TestTArray.cpp.

typedef PRBool(* TestFunc)()

Definition at line 368 of file TestTArray.cpp.


Function Documentation

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 386 of file TestTArray.cpp.

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

  if (NS_FAILED(NS_InitXPCOM2(nsnull, nsnull, nsnull)))
    return -1;

  while (count--) {
    for (const Test* t = tests; t->name != nsnull; ++t) {
      printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
    }
  }
  
  NS_ShutdownXPCOM(nsnull);
  return 0;
}

Here is the call graph for this function:

template<class T >
bool operator< ( const nsCOMPtr< T > &  lhs,
const nsCOMPtr< T > &  rhs 
) [inline]

Definition at line 53 of file TestTArray.cpp.

                                                                      {
  return lhs.get() < rhs.get();
}

Here is the call graph for this function:

static PRBool operator== ( const nsCString a,
const char *  b 
) [static]

Definition at line 244 of file TestTArray.cpp.

                                                            {
  return a.Equals(b);
}
template<class ElementType >
static PRBool test_basic_array ( ElementType *  data,
PRUint32  dataLen,
const ElementType &  extra 
) [static]

Definition at line 60 of file TestTArray.cpp.

                                                         {
  nsTArray<ElementType> ary;
  ary.AppendElements(data, dataLen);
  PRUint32 i;
  for (i = 0; i < ary.Length(); ++i) {
    if (ary[i] != data[i])
      return PR_FALSE;
  }
  // ensure sort results in ascending order
  ary.Sort();
  for (i = 1; i < ary.Length(); ++i) {
    if (ary[i] < ary[i-1])
      return PR_FALSE;
  }
  PRUint32 oldLen = ary.Length();
  ary.RemoveElement(data[dataLen / 2]);
  if (ary.Length() != (oldLen - 1))
    return PR_FALSE;

  PRUint32 index = ary.Length() / 2;
  if (!ary.InsertElementAt(index, extra))
    return PR_FALSE;
  if (ary[index] != extra)
    return PR_FALSE;
  if (ary.IndexOf(extra) == PR_UINT32_MAX)
    return PR_FALSE;
  if (ary.LastIndexOf(extra) == PR_UINT32_MAX)
    return PR_FALSE;
  // ensure proper searching
  if (ary.IndexOf(extra) > ary.LastIndexOf(extra))
    return PR_FALSE;
  if (ary.IndexOf(extra, index) != ary.LastIndexOf(extra, index))
    return PR_FALSE;

  nsTArray<ElementType> copy(ary);
  for (i = 0; i < copy.Length(); ++i) {
    if (ary[i] != copy[i])
      return PR_FALSE;
  }
  if (!ary.AppendElements(copy))
    return PR_FALSE;
  PRUint32 cap = ary.Capacity();
  ary.RemoveElementsAt(copy.Length(), copy.Length());
  ary.Compact();
  if (ary.Capacity() == cap)
    return PR_FALSE;

  ary.Clear();
  if (!ary.IsEmpty() || ary.Elements() == nsnull)
    return PR_FALSE;

  ary = copy;
  for (i = 0; i < copy.Length(); ++i) {
    if (ary[i] != copy[i])
      return PR_FALSE;
  }

  if (!ary.InsertElementsAt(0, copy))
    return PR_FALSE;
  ary.RemoveElementsAt(0, copy.Length());
  for (i = 0; i < copy.Length(); ++i) {
    if (ary[i] != copy[i])
      return PR_FALSE;
  }

  // These shouldn't crash!
  nsTArray<ElementType> empty;
  ary.AppendElements(NS_REINTERPRET_CAST(ElementType *, 0), 0);
  ary.AppendElements(empty);

  // See bug 324981
  ary.RemoveElement(extra);
  ary.RemoveElement(extra);

  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRBool test_char_array ( ) [static]

Definition at line 149 of file TestTArray.cpp.

                                {
  char data[] = {4,6,8,2,4,1,5,7,3};
  return test_basic_array(data, NS_ARRAY_LENGTH(data), char(14));
}

Here is the call graph for this function:

static PRBool test_comptr_array ( ) [static]

Definition at line 299 of file TestTArray.cpp.

                                  {
  FilePointer tmpDir;
  NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpDir));
  if (!tmpDir)
    return PR_FALSE;
  const char *kNames[] = {
    "foo.txt", "bar.html", "baz.gif"
  };
  nsTArray<FilePointer> fileArray;
  PRUint32 i;
  for (i = 0; i < NS_ARRAY_LENGTH(kNames); ++i) {
    FilePointer f;
    tmpDir->Clone(getter_AddRefs(f));
    if (!f)
      return PR_FALSE;
    if (NS_FAILED(f->AppendNative(nsDependentCString(kNames[i]))))
      return PR_FALSE;
    fileArray.AppendElement(f);
  }

  if (fileArray.IndexOf(kNames[1], 0, nsFileNameComparator()) != 1)
    return PR_FALSE;

  // It's unclear what 'operator<' means for nsCOMPtr, but whatever...
  return test_basic_array(fileArray.Elements(), fileArray.Length(), 
                          tmpDir);
}

Here is the call graph for this function:

static PRBool test_int64_array ( ) [static]

Definition at line 144 of file TestTArray.cpp.

                                 {
  PRInt64 data[] = {4,6,8,2,4,1,5,7,3};
  return test_basic_array(data, NS_ARRAY_LENGTH(data), PRInt64(14));
}

Here is the call graph for this function:

static PRBool test_int_array ( ) [static]

Definition at line 139 of file TestTArray.cpp.

                               {
  int data[] = {4,6,8,2,4,1,5,7,3};
  return test_basic_array(data, NS_ARRAY_LENGTH(data), int(14));
}

Here is the call graph for this function:

static PRBool test_object_array ( ) [static]

Definition at line 194 of file TestTArray.cpp.

                                  {
  nsTArray<Object> objArray;
  const char kdata[] = "hello world";
  PRUint32 i;
  for (i = 0; i < NS_ARRAY_LENGTH(kdata); ++i) {
    char x[] = {kdata[i],'\0'};
    if (!objArray.AppendElement(Object(x, i)))
      return PR_FALSE;
  }
  for (i = 0; i < NS_ARRAY_LENGTH(kdata); ++i) {
    if (objArray[i].Str()[0] != kdata[i])
      return PR_FALSE;
    if (objArray[i].Num() != i)
      return PR_FALSE;
  }
  objArray.Sort();
  const char ksorted[] = "\0 dehllloorw";
  for (i = 0; i < NS_ARRAY_LENGTH(kdata)-1; ++i) {
    if (objArray[i].Str()[0] != ksorted[i])
      return PR_FALSE;
  }
  return PR_TRUE;
}

Here is the call graph for this function:

static PRBool test_refptr_array ( ) [static]

Definition at line 344 of file TestTArray.cpp.

                                  {
  PRBool rv = PR_TRUE;

  nsTArray< nsRefPtr<RefcountedObject> > objArray;

  RefcountedObject *a = new RefcountedObject(); a->AddRef();
  RefcountedObject *b = new RefcountedObject(); b->AddRef();
  RefcountedObject *c = new RefcountedObject(); c->AddRef();

  objArray.AppendElement(a);
  objArray.AppendElement(b);
  objArray.AppendElement(c);

  if (objArray.IndexOf(b) != 1)
    rv = PR_FALSE;

  a->Release();
  b->Release();
  c->Release();
  return rv;
}

Here is the call graph for this function:

static PRBool test_string_array ( ) [static]

Definition at line 248 of file TestTArray.cpp.

                                  {
  nsTArray<nsCString> strArray;
  const char kdata[] = "hello world";
  PRUint32 i;
  for (i = 0; i < NS_ARRAY_LENGTH(kdata); ++i) {
    if (!strArray.AppendElement(nsCString(kdata[i])))
      return PR_FALSE;
  }
  for (i = 0; i < NS_ARRAY_LENGTH(kdata); ++i) {
    if (strArray[i].CharAt(0) != kdata[i])
      return PR_FALSE;
  }

  const char kextra[] = "foo bar";
  PRUint32 oldLen = strArray.Length();
  if (!strArray.AppendElement(kextra))
    return PR_FALSE;
  strArray.RemoveElement(kextra);
  if (oldLen != strArray.Length())
    return PR_FALSE;

  if (strArray.IndexOf("e") != 1)
    return PR_FALSE;

  strArray.Sort();
  const char ksorted[] = "\0 dehllloorw";
  for (i = 0; i < NS_ARRAY_LENGTH(kdata)-1; ++i) {
    if (strArray[i].CharAt(0) != ksorted[i])
      return PR_FALSE;
  }

  nsCString rawArray[NS_ARRAY_LENGTH(kdata)-1];
  for (i = 0; i < NS_ARRAY_LENGTH(rawArray); ++i)
    rawArray[i].Assign(kdata + i);  // substrings of kdata
  return test_basic_array(rawArray, NS_ARRAY_LENGTH(rawArray),
                          nsCString("foopy"));
}

Here is the call graph for this function:

static PRBool test_uint32_array ( ) [static]

Definition at line 154 of file TestTArray.cpp.

                                  {
  PRUint32 data[] = {4,6,8,2,4,1,5,7,3};
  return test_basic_array(data, NS_ARRAY_LENGTH(data), PRUint32(14));
}

Here is the call graph for this function:


Variable Documentation

struct Test tests[] [static]