DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 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 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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Util.h"

#include "nsIAtom.h"
#include "nsString.h"
#include "UTFStrings.h"
#include "nsIServiceManager.h"
#include "nsStaticAtom.h"

using namespace mozilla;

namespace TestAtoms {

bool
test_basic()
{
  for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
    nsDependentString str16(ValidStrings[i].m16);
    nsDependentCString str8(ValidStrings[i].m8);

    nsCOMPtr<nsIAtom> atom = do_GetAtom(str16);
    
    if (!atom->Equals(str16) || !atom->EqualsUTF8(str8))
      return false;

    nsString tmp16;
    nsCString tmp8;
    atom->ToString(tmp16);
    atom->ToUTF8String(tmp8);
    if (!str16.Equals(tmp16) || !str8.Equals(tmp8))
      return false;

    if (!nsDependentString(atom->GetUTF16String()).Equals(str16))
      return false;

    if (!nsAtomString(atom).Equals(str16) ||
        !nsDependentAtomString(atom).Equals(str16) ||
        !nsAtomCString(atom).Equals(str8))
      return false;
  }
  
  return true;
}

bool
test_16vs8()
{
  for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
    nsCOMPtr<nsIAtom> atom16 = do_GetAtom(ValidStrings[i].m16);
    nsCOMPtr<nsIAtom> atom8 = do_GetAtom(ValidStrings[i].m8);
    if (atom16 != atom8)
      return false;
  }
  
  return true;
}

bool
test_buffersharing()
{
  nsString unique;
  unique.AssignLiteral("this is a unique string !@#$");
  
  nsCOMPtr<nsIAtom> atom = do_GetAtom(unique);
  
  return unique.get() == atom->GetUTF16String();
}

bool
test_null()
{
  nsAutoString str(NS_LITERAL_STRING("string with a \0 char"));
  nsDependentString strCut(str.get());

  if (str.Equals(strCut))
    return false;
  
  nsCOMPtr<nsIAtom> atomCut = do_GetAtom(strCut);
  nsCOMPtr<nsIAtom> atom = do_GetAtom(str);
  
  return atom->GetLength() == str.Length() &&
         atom->Equals(str) &&
         atom->EqualsUTF8(NS_ConvertUTF16toUTF8(str)) &&
         atom != atomCut &&
         atomCut->Equals(strCut);
}

bool
test_invalid()
{
  for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
    nsrefcnt count = NS_GetNumberOfAtoms();

    {
      nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid16Strings[i].m16);
      if (!atom16->Equals(nsDependentString(Invalid16Strings[i].m16)))
        return false;
    }
    
    if (count != NS_GetNumberOfAtoms())
      return false;
  }

  for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
    nsrefcnt count = NS_GetNumberOfAtoms();

    {
      nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Invalid8Strings[i].m8);
      nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid8Strings[i].m16);
      if (atom16 != atom8 ||
          !atom16->Equals(nsDependentString(Invalid8Strings[i].m16)))
        return false;
    }
    
    if (count != NS_GetNumberOfAtoms())
      return false;
  }

// Don't run this test in debug builds as that intentionally asserts.
#ifndef DEBUG
  nsCOMPtr<nsIAtom> emptyAtom = do_GetAtom("");

  for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
    nsrefcnt count = NS_GetNumberOfAtoms();

    nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Malformed8Strings[i]);
    if (atom8 != emptyAtom ||
        count != NS_GetNumberOfAtoms())
      return false;
  }
#endif

  return true;
}

#define FIRST_ATOM_STR "first static atom. Hello!"
#define SECOND_ATOM_STR "second static atom. @World!"
#define THIRD_ATOM_STR "third static atom?!"

static nsIAtom* sAtom1 = 0;
static nsIAtom* sAtom2 = 0;
static nsIAtom* sAtom3 = 0;
NS_STATIC_ATOM_BUFFER(sAtom1_buffer, FIRST_ATOM_STR)
NS_STATIC_ATOM_BUFFER(sAtom2_buffer, SECOND_ATOM_STR)
NS_STATIC_ATOM_BUFFER(sAtom3_buffer, THIRD_ATOM_STR)
static const nsStaticAtom sAtoms_info[] = {
  NS_STATIC_ATOM(sAtom1_buffer, &sAtom1),
  NS_STATIC_ATOM(sAtom2_buffer, &sAtom2),
  NS_STATIC_ATOM(sAtom3_buffer, &sAtom3),
};

bool
isStaticAtom(nsIAtom* atom)
{
  // Don't use logic && in order to ensure that all addrefs/releases are always
  // run, even if one of the tests fail. This allows us to run this code on a
  // non-static atom without affecting its refcount.
  return (atom->AddRef() == 2) &
         (atom->AddRef() == 2) &
         (atom->AddRef() == 2) &
         (atom->Release() == 1) &
         (atom->Release() == 1) &
         (atom->Release() == 1);
}

bool
test_atomtable()
{
  nsrefcnt count = NS_GetNumberOfAtoms();
  
  nsCOMPtr<nsIAtom> thirdNonPerm = do_GetAtom(THIRD_ATOM_STR);
  
  if (isStaticAtom(thirdNonPerm))
    return false;

  if (!thirdNonPerm || NS_GetNumberOfAtoms() != count + 1)
    return false;

  NS_RegisterStaticAtoms(sAtoms_info);

  return sAtom1 &&
         sAtom1->Equals(NS_LITERAL_STRING(FIRST_ATOM_STR)) &&
         isStaticAtom(sAtom1) &&
         sAtom2 &&
         sAtom2->Equals(NS_LITERAL_STRING(SECOND_ATOM_STR)) &&
         isStaticAtom(sAtom2) &&
         sAtom3 &&
         sAtom3->Equals(NS_LITERAL_STRING(THIRD_ATOM_STR)) &&
         isStaticAtom(sAtom3) &&
         NS_GetNumberOfAtoms() == count + 3 &&
         thirdNonPerm == sAtom3;
}

#define FIRST_PERM_ATOM_STR "first permanent atom. Hello!"
#define SECOND_PERM_ATOM_STR "second permanent atom. @World!"

bool
test_permanent()
{
  nsrefcnt count = NS_GetNumberOfAtoms();

  {
    nsCOMPtr<nsIAtom> first = do_GetAtom(FIRST_PERM_ATOM_STR);
    if (!first->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) ||
        isStaticAtom(first))
      return false;
  
    nsCOMPtr<nsIAtom> first_p =
      NS_NewPermanentAtom(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR));
    if (!first_p->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) ||
        !isStaticAtom(first_p) ||
        first != first_p)
      return false;
  
    nsCOMPtr<nsIAtom> second_p =
      NS_NewPermanentAtom(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR));
    if (!second_p->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) ||
        !isStaticAtom(second_p))
      return false;
  
    nsCOMPtr<nsIAtom> second = do_GetAtom(SECOND_PERM_ATOM_STR);
    if (!second->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) ||
        !isStaticAtom(second) ||
        second != second_p)
      return false;
  }

  return NS_GetNumberOfAtoms() == count + 2;
}

typedef bool (*TestFunc)();

static const struct Test
  {
    const char* name;
    TestFunc    func;
  }
tests[] =
  {
    { "test_basic", test_basic },
    { "test_16vs8", test_16vs8 },
    { "test_buffersharing", test_buffersharing },
    { "test_null", test_null },
    { "test_invalid", test_invalid },
// FIXME: Bug 577500 TestAtoms fails when run in dist/bin due to
// static atom table already being closed. TestStaticAtoms has similar
// failure.
#if 0
    { "test_atomtable", test_atomtable },
    { "test_permanent", test_permanent },
#endif
    { nullptr, nullptr }
  };

}

using namespace TestAtoms;

int main()
  {
    {
      nsCOMPtr<nsIServiceManager> servMan;
      NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  
      for (const Test* t = tests; t->name != nullptr; ++t)
        {
          printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
        }
    }

    NS_ShutdownXPCOM(nullptr);

    return 0;
  }