rename from xpcom/tests/TestThreadUtils.cpp
rename to xpcom/tests/gtest/TestThreadUtils.cpp
--- a/xpcom/tests/TestThreadUtils.cpp
+++ b/xpcom/tests/gtest/TestThreadUtils.cpp
@@ -1,15 +1,16 @@
/* 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 "TestHarness.h"
#include "nsThreadUtils.h"
+#include "gtest/gtest.h"
+
using namespace mozilla;
enum {
TEST_CALL_VOID_ARG_VOID_RETURN,
TEST_CALL_VOID_ARG_VOID_RETURN_CONST,
TEST_CALL_VOID_ARG_NONVOID_RETURN,
TEST_CALL_NONVOID_ARG_VOID_RETURN,
TEST_CALL_NONVOID_ARG_NONVOID_RETURN,
@@ -152,163 +153,156 @@ struct TestCopyMove
TestCopyMove(const TestCopyMove& a) : mCopyCounter(a.mCopyCounter), mMoveCounter(a.mMoveCounter) { ++mCopyCounter; };
TestCopyMove(TestCopyMove&& a) : mCopyCounter(a.mCopyCounter), mMoveCounter(a.mMoveCounter) { a.mMoveCounter = nullptr; ++mMoveCounter; }
~TestCopyMove() { mCopyCounter = nullptr; mMoveCounter = nullptr; }
void operator()() { MOZ_RELEASE_ASSERT(mCopyCounter); MOZ_RELEASE_ASSERT(mMoveCounter); }
int* mCopyCounter;
int* mMoveCounter;
};
-static int Expect(const char* aContext, int aCounter, int aMaxExpected)
+static void Expect(const char* aContext, int aCounter, int aMaxExpected)
{
- if (aCounter > aMaxExpected) {
- fail("%s: expected %d max, got %d", aContext, aMaxExpected, aCounter);
- return 1;
- }
- passed("%s: got %d <= %d as expected", aContext, aCounter, aMaxExpected);
- return 0;
+ EXPECT_LE(aCounter, aMaxExpected) << aContext;
}
-int TestNS_NewRunnableFunction()
+TEST(ThreadUtils, NewRunnableFunction)
{
- int result = 0;
-
// Test NS_NewRunnableFunction with copyable-only function object.
{
int copyCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyWithNoMove tracker(©Counter);
trackedRunnable = NS_NewRunnableFunction(tracker);
// Original 'tracker' is destroyed here.
}
// Verify that the runnable contains a non-destroyed function object.
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) function, copies",
- copyCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable-only (and no move) function, copies",
+ copyCounter, 1);
}
{
int copyCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
// Passing as rvalue, but using copy.
// (TestCopyWithDeletedMove wouldn't allow this.)
trackedRunnable = NS_NewRunnableFunction(TestCopyWithNoMove(©Counter));
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) function rvalue, copies",
- copyCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable-only (and no move) function rvalue, copies",
+ copyCounter, 1);
}
{
int copyCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyWithDeletedMove tracker(©Counter);
trackedRunnable = NS_NewRunnableFunction(tracker);
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable-only (and deleted move) function, copies",
- copyCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable-only (and deleted move) function, copies",
+ copyCounter, 1);
}
// Test NS_NewRunnableFunction with movable-only function object.
{
int moveCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestMove tracker(&moveCounter);
trackedRunnable = NS_NewRunnableFunction(Move(tracker));
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with movable-only function, moves",
- moveCounter, 1);
+ Expect("NS_NewRunnableFunction with movable-only function, moves",
+ moveCounter, 1);
}
{
int moveCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
trackedRunnable = NS_NewRunnableFunction(TestMove(&moveCounter));
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with movable-only function rvalue, moves",
- moveCounter, 1);
+ Expect("NS_NewRunnableFunction with movable-only function rvalue, moves",
+ moveCounter, 1);
}
// Test NS_NewRunnableFunction with copyable&movable function object.
{
int copyCounter = 0;
int moveCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyMove tracker(©Counter, &moveCounter);
trackedRunnable = NS_NewRunnableFunction(Move(tracker));
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable&movable function, copies",
- copyCounter, 0);
- result |= Expect("NS_NewRunnableFunction with copyable&movable function, moves",
- moveCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable&movable function, copies",
+ copyCounter, 0);
+ Expect("NS_NewRunnableFunction with copyable&movable function, moves",
+ moveCounter, 1);
}
{
int copyCounter = 0;
int moveCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
trackedRunnable =
NS_NewRunnableFunction(TestCopyMove(©Counter, &moveCounter));
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable&movable function rvalue, copies",
- copyCounter, 0);
- result |= Expect("NS_NewRunnableFunction with copyable&movable function rvalue, moves",
- moveCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable&movable function rvalue, copies",
+ copyCounter, 0);
+ Expect("NS_NewRunnableFunction with copyable&movable function rvalue, moves",
+ moveCounter, 1);
}
// Test NS_NewRunnableFunction with copyable-only lambda capture.
{
int copyCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyWithNoMove tracker(©Counter);
// Expect 2 copies (here -> local lambda -> runnable lambda).
trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) capture, copies",
- copyCounter, 2);
+ Expect("NS_NewRunnableFunction with copyable-only (and no move) capture, copies",
+ copyCounter, 2);
}
{
int copyCounter = 0;
{
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyWithDeletedMove tracker(©Counter);
// Expect 2 copies (here -> local lambda -> runnable lambda).
trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable-only (and deleted move) capture, copies",
- copyCounter, 2);
+ Expect("NS_NewRunnableFunction with copyable-only (and deleted move) capture, copies",
+ copyCounter, 2);
}
// Note: Not possible to use move-only captures.
// (Until we can use C++14 generalized lambda captures)
// Test NS_NewRunnableFunction with copyable&movable lambda capture.
{
int copyCounter = 0;
@@ -317,32 +311,25 @@ int TestNS_NewRunnableFunction()
nsCOMPtr<nsIRunnable> trackedRunnable;
{
TestCopyMove tracker(©Counter, &moveCounter);
trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
// Expect 1 copy (here -> local lambda) and 1 move (local -> runnable lambda).
}
trackedRunnable->Run();
}
- result |= Expect("NS_NewRunnableFunction with copyable&movable capture, copies",
- copyCounter, 1);
- result |= Expect("NS_NewRunnableFunction with copyable&movable capture, moves",
- moveCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable&movable capture, copies",
+ copyCounter, 1);
+ Expect("NS_NewRunnableFunction with copyable&movable capture, moves",
+ moveCounter, 1);
}
-
- return result;
}
-int main(int argc, char** argv)
+TEST(ThreadUtils, RunnableMethod)
{
- int result = TestNS_NewRunnableFunction();
-
- ScopedXPCOM xpcom("ThreadUtils");
- NS_ENSURE_FALSE(xpcom.failed(), 1);
-
memset(gRunnableExecuted, false, MAX_TESTS * sizeof(bool));
// Scope the smart ptrs so that the runnables need to hold on to whatever they need
{
RefPtr<nsFoo> foo = new nsFoo();
RefPtr<nsBar> bar = new nsBar();
RefPtr<const nsBar> constBar = bar;
// This pointer will be freed at the end of the block
@@ -374,25 +361,18 @@ int main(int argc, char** argv)
}
// Spin the event loop
NS_ProcessPendingEvents(nullptr);
// Now test a suicidal event in NS_New(Named)Thread
nsCOMPtr<nsIThread> thread;
NS_NewNamedThread("SuicideThread", getter_AddRefs(thread), new TestSuicide());
- MOZ_RELEASE_ASSERT(thread);
+ ASSERT_TRUE(thread);
while (!gRunnableExecuted[TEST_CALL_NEWTHREAD_SUICIDAL]) {
NS_ProcessPendingEvents(nullptr);
}
for (uint32_t i = 0; i < MAX_TESTS; i++) {
- if (gRunnableExecuted[i]) {
- passed("Test %d passed",i);
- } else {
- fail("Error in test %d", i);
- result = 1;
- }
+ EXPECT_TRUE(gRunnableExecuted[i]) << "Error in test " << i;
}
-
- return result;
}