xpcom/tests/TestAtoms.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestAtoms.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,278 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/ArrayUtils.h"
    1.10 +
    1.11 +#include "nsIAtom.h"
    1.12 +#include "nsString.h"
    1.13 +#include "UTFStrings.h"
    1.14 +#include "nsIServiceManager.h"
    1.15 +#include "nsStaticAtom.h"
    1.16 +
    1.17 +using namespace mozilla;
    1.18 +
    1.19 +namespace TestAtoms {
    1.20 +
    1.21 +bool
    1.22 +test_basic()
    1.23 +{
    1.24 +  for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
    1.25 +    nsDependentString str16(ValidStrings[i].m16);
    1.26 +    nsDependentCString str8(ValidStrings[i].m8);
    1.27 +
    1.28 +    nsCOMPtr<nsIAtom> atom = do_GetAtom(str16);
    1.29 +    
    1.30 +    if (!atom->Equals(str16) || !atom->EqualsUTF8(str8))
    1.31 +      return false;
    1.32 +
    1.33 +    nsString tmp16;
    1.34 +    nsCString tmp8;
    1.35 +    atom->ToString(tmp16);
    1.36 +    atom->ToUTF8String(tmp8);
    1.37 +    if (!str16.Equals(tmp16) || !str8.Equals(tmp8))
    1.38 +      return false;
    1.39 +
    1.40 +    if (!nsDependentString(atom->GetUTF16String()).Equals(str16))
    1.41 +      return false;
    1.42 +
    1.43 +    if (!nsAtomString(atom).Equals(str16) ||
    1.44 +        !nsDependentAtomString(atom).Equals(str16) ||
    1.45 +        !nsAtomCString(atom).Equals(str8))
    1.46 +      return false;
    1.47 +  }
    1.48 +  
    1.49 +  return true;
    1.50 +}
    1.51 +
    1.52 +bool
    1.53 +test_16vs8()
    1.54 +{
    1.55 +  for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
    1.56 +    nsCOMPtr<nsIAtom> atom16 = do_GetAtom(ValidStrings[i].m16);
    1.57 +    nsCOMPtr<nsIAtom> atom8 = do_GetAtom(ValidStrings[i].m8);
    1.58 +    if (atom16 != atom8)
    1.59 +      return false;
    1.60 +  }
    1.61 +  
    1.62 +  return true;
    1.63 +}
    1.64 +
    1.65 +bool
    1.66 +test_buffersharing()
    1.67 +{
    1.68 +  nsString unique;
    1.69 +  unique.AssignLiteral("this is a unique string !@#$");
    1.70 +  
    1.71 +  nsCOMPtr<nsIAtom> atom = do_GetAtom(unique);
    1.72 +  
    1.73 +  return unique.get() == atom->GetUTF16String();
    1.74 +}
    1.75 +
    1.76 +bool
    1.77 +test_null()
    1.78 +{
    1.79 +  nsAutoString str(NS_LITERAL_STRING("string with a \0 char"));
    1.80 +  nsDependentString strCut(str.get());
    1.81 +
    1.82 +  if (str.Equals(strCut))
    1.83 +    return false;
    1.84 +  
    1.85 +  nsCOMPtr<nsIAtom> atomCut = do_GetAtom(strCut);
    1.86 +  nsCOMPtr<nsIAtom> atom = do_GetAtom(str);
    1.87 +  
    1.88 +  return atom->GetLength() == str.Length() &&
    1.89 +         atom->Equals(str) &&
    1.90 +         atom->EqualsUTF8(NS_ConvertUTF16toUTF8(str)) &&
    1.91 +         atom != atomCut &&
    1.92 +         atomCut->Equals(strCut);
    1.93 +}
    1.94 +
    1.95 +bool
    1.96 +test_invalid()
    1.97 +{
    1.98 +  for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
    1.99 +    nsrefcnt count = NS_GetNumberOfAtoms();
   1.100 +
   1.101 +    {
   1.102 +      nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid16Strings[i].m16);
   1.103 +      if (!atom16->Equals(nsDependentString(Invalid16Strings[i].m16)))
   1.104 +        return false;
   1.105 +    }
   1.106 +    
   1.107 +    if (count != NS_GetNumberOfAtoms())
   1.108 +      return false;
   1.109 +  }
   1.110 +
   1.111 +  for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
   1.112 +    nsrefcnt count = NS_GetNumberOfAtoms();
   1.113 +
   1.114 +    {
   1.115 +      nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Invalid8Strings[i].m8);
   1.116 +      nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid8Strings[i].m16);
   1.117 +      if (atom16 != atom8 ||
   1.118 +          !atom16->Equals(nsDependentString(Invalid8Strings[i].m16)))
   1.119 +        return false;
   1.120 +    }
   1.121 +    
   1.122 +    if (count != NS_GetNumberOfAtoms())
   1.123 +      return false;
   1.124 +  }
   1.125 +
   1.126 +// Don't run this test in debug builds as that intentionally asserts.
   1.127 +#ifndef DEBUG
   1.128 +  nsCOMPtr<nsIAtom> emptyAtom = do_GetAtom("");
   1.129 +
   1.130 +  for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
   1.131 +    nsrefcnt count = NS_GetNumberOfAtoms();
   1.132 +
   1.133 +    nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Malformed8Strings[i]);
   1.134 +    if (atom8 != emptyAtom ||
   1.135 +        count != NS_GetNumberOfAtoms())
   1.136 +      return false;
   1.137 +  }
   1.138 +#endif
   1.139 +
   1.140 +  return true;
   1.141 +}
   1.142 +
   1.143 +#define FIRST_ATOM_STR "first static atom. Hello!"
   1.144 +#define SECOND_ATOM_STR "second static atom. @World!"
   1.145 +#define THIRD_ATOM_STR "third static atom?!"
   1.146 +
   1.147 +static nsIAtom* sAtom1 = 0;
   1.148 +static nsIAtom* sAtom2 = 0;
   1.149 +static nsIAtom* sAtom3 = 0;
   1.150 +NS_STATIC_ATOM_BUFFER(sAtom1_buffer, FIRST_ATOM_STR)
   1.151 +NS_STATIC_ATOM_BUFFER(sAtom2_buffer, SECOND_ATOM_STR)
   1.152 +NS_STATIC_ATOM_BUFFER(sAtom3_buffer, THIRD_ATOM_STR)
   1.153 +static const nsStaticAtom sAtoms_info[] = {
   1.154 +  NS_STATIC_ATOM(sAtom1_buffer, &sAtom1),
   1.155 +  NS_STATIC_ATOM(sAtom2_buffer, &sAtom2),
   1.156 +  NS_STATIC_ATOM(sAtom3_buffer, &sAtom3),
   1.157 +};
   1.158 +
   1.159 +bool
   1.160 +isStaticAtom(nsIAtom* atom)
   1.161 +{
   1.162 +  // Don't use logic && in order to ensure that all addrefs/releases are always
   1.163 +  // run, even if one of the tests fail. This allows us to run this code on a
   1.164 +  // non-static atom without affecting its refcount.
   1.165 +  return (atom->AddRef() == 2) &
   1.166 +         (atom->AddRef() == 2) &
   1.167 +         (atom->AddRef() == 2) &
   1.168 +         (atom->Release() == 1) &
   1.169 +         (atom->Release() == 1) &
   1.170 +         (atom->Release() == 1);
   1.171 +}
   1.172 +
   1.173 +bool
   1.174 +test_atomtable()
   1.175 +{
   1.176 +  nsrefcnt count = NS_GetNumberOfAtoms();
   1.177 +  
   1.178 +  nsCOMPtr<nsIAtom> thirdNonPerm = do_GetAtom(THIRD_ATOM_STR);
   1.179 +  
   1.180 +  if (isStaticAtom(thirdNonPerm))
   1.181 +    return false;
   1.182 +
   1.183 +  if (!thirdNonPerm || NS_GetNumberOfAtoms() != count + 1)
   1.184 +    return false;
   1.185 +
   1.186 +  NS_RegisterStaticAtoms(sAtoms_info);
   1.187 +
   1.188 +  return sAtom1 &&
   1.189 +         sAtom1->Equals(NS_LITERAL_STRING(FIRST_ATOM_STR)) &&
   1.190 +         isStaticAtom(sAtom1) &&
   1.191 +         sAtom2 &&
   1.192 +         sAtom2->Equals(NS_LITERAL_STRING(SECOND_ATOM_STR)) &&
   1.193 +         isStaticAtom(sAtom2) &&
   1.194 +         sAtom3 &&
   1.195 +         sAtom3->Equals(NS_LITERAL_STRING(THIRD_ATOM_STR)) &&
   1.196 +         isStaticAtom(sAtom3) &&
   1.197 +         NS_GetNumberOfAtoms() == count + 3 &&
   1.198 +         thirdNonPerm == sAtom3;
   1.199 +}
   1.200 +
   1.201 +#define FIRST_PERM_ATOM_STR "first permanent atom. Hello!"
   1.202 +#define SECOND_PERM_ATOM_STR "second permanent atom. @World!"
   1.203 +
   1.204 +bool
   1.205 +test_permanent()
   1.206 +{
   1.207 +  nsrefcnt count = NS_GetNumberOfAtoms();
   1.208 +
   1.209 +  {
   1.210 +    nsCOMPtr<nsIAtom> first = do_GetAtom(FIRST_PERM_ATOM_STR);
   1.211 +    if (!first->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) ||
   1.212 +        isStaticAtom(first))
   1.213 +      return false;
   1.214 +  
   1.215 +    nsCOMPtr<nsIAtom> first_p =
   1.216 +      NS_NewPermanentAtom(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR));
   1.217 +    if (!first_p->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) ||
   1.218 +        !isStaticAtom(first_p) ||
   1.219 +        first != first_p)
   1.220 +      return false;
   1.221 +  
   1.222 +    nsCOMPtr<nsIAtom> second_p =
   1.223 +      NS_NewPermanentAtom(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR));
   1.224 +    if (!second_p->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) ||
   1.225 +        !isStaticAtom(second_p))
   1.226 +      return false;
   1.227 +  
   1.228 +    nsCOMPtr<nsIAtom> second = do_GetAtom(SECOND_PERM_ATOM_STR);
   1.229 +    if (!second->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) ||
   1.230 +        !isStaticAtom(second) ||
   1.231 +        second != second_p)
   1.232 +      return false;
   1.233 +  }
   1.234 +
   1.235 +  return NS_GetNumberOfAtoms() == count + 2;
   1.236 +}
   1.237 +
   1.238 +typedef bool (*TestFunc)();
   1.239 +
   1.240 +static const struct Test
   1.241 +  {
   1.242 +    const char* name;
   1.243 +    TestFunc    func;
   1.244 +  }
   1.245 +tests[] =
   1.246 +  {
   1.247 +    { "test_basic", test_basic },
   1.248 +    { "test_16vs8", test_16vs8 },
   1.249 +    { "test_buffersharing", test_buffersharing },
   1.250 +    { "test_null", test_null },
   1.251 +    { "test_invalid", test_invalid },
   1.252 +// FIXME: Bug 577500 TestAtoms fails when run in dist/bin due to
   1.253 +// static atom table already being closed. TestStaticAtoms has similar
   1.254 +// failure.
   1.255 +#if 0
   1.256 +    { "test_atomtable", test_atomtable },
   1.257 +    { "test_permanent", test_permanent },
   1.258 +#endif
   1.259 +    { nullptr, nullptr }
   1.260 +  };
   1.261 +
   1.262 +}
   1.263 +
   1.264 +using namespace TestAtoms;
   1.265 +
   1.266 +int main()
   1.267 +  {
   1.268 +    {
   1.269 +      nsCOMPtr<nsIServiceManager> servMan;
   1.270 +      NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
   1.271 +  
   1.272 +      for (const Test* t = tests; t->name != nullptr; ++t)
   1.273 +        {
   1.274 +          printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
   1.275 +        }
   1.276 +    }
   1.277 +
   1.278 +    NS_ShutdownXPCOM(nullptr);
   1.279 +
   1.280 +    return 0;
   1.281 +  }

mercurial