Bug 1126295 - Move TestAtoms.cpp to gtest and enable it; r=froydnj
author<sajitk>
Tue, 27 Jan 2015 09:28:45 -0500
changeset 268006 7cf5a24145ac2f3904818538972cd56f1e2b6aa5
parent 267991 92dc9ab424bdde398c107b8ce4f91869b81a87ae
child 268007 2bdf262fcf4fa15e6e092b36e126521d66fc5e34
push id29538
push userkwierso@gmail.com
push dateFri, 16 Oct 2015 18:52:30 +0000
treeherdermozilla-central@0d5765f295a8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1126295
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1126295 - Move TestAtoms.cpp to gtest and enable it; r=froydnj commit 7d4543037c618cc0e95b6f1801c2187e3361c82a (HEAD, explicit) Author: Ehsan Akhgari <ehsan@mozilla.com> Bug 1126295 - Move TestAtoms.cpp to gtest and enable it; r=froydnj
xpcom/tests/TestAtoms.cpp
xpcom/tests/gtest/TestAtoms.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
rename from xpcom/tests/TestAtoms.cpp
rename to xpcom/tests/gtest/TestAtoms.cpp
--- a/xpcom/tests/TestAtoms.cpp
+++ b/xpcom/tests/gtest/TestAtoms.cpp
@@ -6,140 +6,122 @@
 #include "mozilla/ArrayUtils.h"
 
 #include "nsIAtom.h"
 #include "nsString.h"
 #include "UTFStrings.h"
 #include "nsIServiceManager.h"
 #include "nsStaticAtom.h"
 
+#include "gtest/gtest.h"
+
 using namespace mozilla;
 
 namespace TestAtoms {
 
-bool
-test_basic()
+TEST(Atoms, 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;
+
+    EXPECT_TRUE(atom->Equals(str16));
+    EXPECT_TRUE(atom->EqualsUTF8(str8));
 
     nsString tmp16;
     nsCString tmp8;
     atom->ToString(tmp16);
     atom->ToUTF8String(tmp8);
-    if (!str16.Equals(tmp16) || !str8.Equals(tmp8))
-      return false;
+    EXPECT_TRUE(str16.Equals(tmp16));
+    EXPECT_TRUE(str8.Equals(tmp8));
 
-    if (!nsDependentString(atom->GetUTF16String()).Equals(str16))
-      return false;
+    EXPECT_TRUE(nsDependentString(atom->GetUTF16String()).Equals(str16));
 
-    if (!nsAtomString(atom).Equals(str16) ||
-        !nsDependentAtomString(atom).Equals(str16) ||
-        !nsAtomCString(atom).Equals(str8))
-      return false;
+    EXPECT_TRUE(nsAtomString(atom).Equals(str16));
+    EXPECT_TRUE(nsDependentAtomString(atom).Equals(str16));
+    EXPECT_TRUE(nsAtomCString(atom).Equals(str8));
   }
-  
-  return true;
 }
 
-bool
-test_16vs8()
+TEST(Atoms, 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;
+    EXPECT_EQ(atom16, atom8);
   }
-  
-  return true;
 }
 
-bool
-test_buffersharing()
+TEST(Atoms, BufferSharing)
 {
   nsString unique;
   unique.AssignLiteral("this is a unique string !@#$");
-  
+
   nsCOMPtr<nsIAtom> atom = do_GetAtom(unique);
-  
-  return unique.get() == atom->GetUTF16String();
+
+  EXPECT_EQ(unique.get(), atom->GetUTF16String());
 }
 
-bool
-test_null()
+TEST(Atoms, NUll)
 {
   nsAutoString str(NS_LITERAL_STRING("string with a \0 char"));
   nsDependentString strCut(str.get());
 
-  if (str.Equals(strCut))
-    return false;
-  
+  EXPECT_FALSE(str.Equals(strCut));
+
   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);
+
+  EXPECT_EQ(atom->GetLength(), str.Length());
+  EXPECT_TRUE(atom->Equals(str));
+  EXPECT_TRUE(atom->EqualsUTF8(NS_ConvertUTF16toUTF8(str)));
+  EXPECT_NE(atom, atomCut);
+  EXPECT_TRUE(atomCut->Equals(strCut));
 }
 
-bool
-test_invalid()
+TEST(Atoms, 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;
+      EXPECT_TRUE(atom16->Equals(nsDependentString(Invalid16Strings[i].m16)));
     }
-    
-    if (count != NS_GetNumberOfAtoms())
-      return false;
+
+    EXPECT_EQ(count, NS_GetNumberOfAtoms());
   }
 
   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;
+      EXPECT_EQ(atom16, atom8);
+      EXPECT_TRUE(atom16->Equals(nsDependentString(Invalid8Strings[i].m16)));
     }
-    
-    if (count != NS_GetNumberOfAtoms())
-      return false;
+
+    EXPECT_EQ(count, NS_GetNumberOfAtoms());
   }
 
 // 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;
+    EXPECT_EQ(atom8, emptyAtom);
+    EXPECT_EQ(count, NS_GetNumberOfAtoms());
   }
 #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;
@@ -154,125 +136,77 @@ static const nsStaticAtom sAtoms_info[] 
 };
 
 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 rv = (atom->AddRef() == 2);
+  rv &= (atom->AddRef() == 2);
+  rv &= (atom->AddRef() == 2);
+
+  rv &= (atom->Release() == 1);
+  rv &= (atom->Release() == 1);
+  rv &= (atom->Release() == 1);
+  return rv;
 }
 
-bool
-test_atomtable()
+TEST(Atoms, Table)
 {
   nsrefcnt count = NS_GetNumberOfAtoms();
-  
+
   nsCOMPtr<nsIAtom> thirdNonPerm = do_GetAtom(THIRD_ATOM_STR);
-  
-  if (isStaticAtom(thirdNonPerm))
-    return false;
+
+  EXPECT_FALSE(isStaticAtom(thirdNonPerm));
 
-  if (!thirdNonPerm || NS_GetNumberOfAtoms() != count + 1)
-    return false;
+  EXPECT_TRUE(thirdNonPerm);
+  EXPECT_EQ(NS_GetNumberOfAtoms(), count + 1);
 
   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;
+  EXPECT_TRUE(sAtom1);
+  EXPECT_TRUE(sAtom1->Equals(NS_LITERAL_STRING(FIRST_ATOM_STR)));
+  EXPECT_TRUE(isStaticAtom(sAtom1));
+  EXPECT_TRUE(sAtom2);
+  EXPECT_TRUE(sAtom2->Equals(NS_LITERAL_STRING(SECOND_ATOM_STR)));
+  EXPECT_TRUE(isStaticAtom(sAtom2));
+  EXPECT_TRUE(sAtom3);
+  EXPECT_TRUE(sAtom3->Equals(NS_LITERAL_STRING(THIRD_ATOM_STR)));
+  EXPECT_TRUE(isStaticAtom(sAtom3));
+  EXPECT_EQ(NS_GetNumberOfAtoms(), count + 3);
+  EXPECT_EQ(thirdNonPerm, sAtom3);
 }
 
 #define FIRST_PERM_ATOM_STR "first permanent atom. Hello!"
 #define SECOND_PERM_ATOM_STR "second permanent atom. @World!"
 
-bool
-test_permanent()
+TEST(Atoms, 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;
-  
+    EXPECT_TRUE(first->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)));
+    EXPECT_FALSE(isStaticAtom(first));
+
     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;
-  
+    EXPECT_TRUE(first_p->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)));
+    EXPECT_TRUE(isStaticAtom(first_p));
+    EXPECT_EQ(first, first_p);
+
     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;
-  
+    EXPECT_TRUE(second_p->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)));
+    EXPECT_TRUE(isStaticAtom(second_p));
+
     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;
+    EXPECT_TRUE(second->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)));
+    EXPECT_TRUE(isStaticAtom(second));
+    EXPECT_EQ(second, second_p);
   }
 
-  return NS_GetNumberOfAtoms() == count + 2;
+  EXPECT_EQ(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;
-  }
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -1,16 +1,17 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 UNIFIED_SOURCES += [
     'Helpers.cpp',
+	'TestAtoms.cpp',
     'TestCloneInputStream.cpp',
     'TestCRT.cpp',
     'TestEncoding.cpp',
     'TestExpirationTracker.cpp',
     'TestPipes.cpp',
     'TestPLDHash.cpp',
     'TestPriorityQueue.cpp',
     'TestSnappyStreams.cpp',
--- a/xpcom/tests/moz.build
+++ b/xpcom/tests/moz.build
@@ -70,21 +70,16 @@ GeckoCppUnitTests([
     'TestThreadUtils'
 ])
 
 if CONFIG['MOZ_MEMORY']:
     GeckoCppUnitTests([
         'TestJemalloc',
     ])
 
-# XXX Make these tests work in libxul builds.
-#CPP_UNIT_TESTS += [
-#    'TestAtoms',
-#]
-
 # FIXME: bug 577500 TestStaticAtoms fails when run in dist/bin
 #CPP_UNIT_TESTS += [
 #    'TestStaticAtoms',
 #]
 
 if CONFIG['MOZ_DEBUG'] and CONFIG['OS_ARCH'] not in ('WINNT'):
     # FIXME bug 523392: TestDeadlockDetector doesn't like Windows
     # FIXME bug 523378: also fails on OS X