diff options
Diffstat (limited to 'xpcom/glue/tests')
-rw-r--r-- | xpcom/glue/tests/gtest/TestArray.cpp | 169 | ||||
-rw-r--r-- | xpcom/glue/tests/gtest/TestFileUtils.cpp | 283 | ||||
-rw-r--r-- | xpcom/glue/tests/gtest/TestGCPostBarriers.cpp | 140 | ||||
-rw-r--r-- | xpcom/glue/tests/gtest/TestNsDeque.cpp | 342 | ||||
-rw-r--r-- | xpcom/glue/tests/gtest/TestThreadUtils.cpp | 937 | ||||
-rw-r--r-- | xpcom/glue/tests/gtest/moz.build | 22 |
6 files changed, 1893 insertions, 0 deletions
diff --git a/xpcom/glue/tests/gtest/TestArray.cpp b/xpcom/glue/tests/gtest/TestArray.cpp new file mode 100644 index 000000000..72d28b4df --- /dev/null +++ b/xpcom/glue/tests/gtest/TestArray.cpp @@ -0,0 +1,169 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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 <stdio.h> +#include <stdlib.h> +#include "gtest/gtest.h" + +// Disable deprecation warnings generated by nsISupportsArray and associated +// classes. +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#elif defined(_MSC_VER) +#pragma warning (push) +#pragma warning (disable : 4996) +#endif + +#include "nsISupportsArray.h" + +// {9e70a320-be02-11d1-8031-006008159b5a} +#define NS_IFOO_IID \ + {0x9e70a320, 0xbe02, 0x11d1, \ + {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}} + +namespace TestArray { + +class IFoo : public nsISupports { +public: + + NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) + + NS_IMETHOD_(nsrefcnt) RefCnt() = 0; + NS_IMETHOD_(int32_t) ID() = 0; +}; + +NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID) + +class Foo final : public IFoo { +public: + + explicit Foo(int32_t aID); + + // nsISupports implementation + NS_DECL_ISUPPORTS + + // IFoo implementation + NS_IMETHOD_(nsrefcnt) RefCnt() override { return mRefCnt; } + NS_IMETHOD_(int32_t) ID() override { return mID; } + + static int32_t gCount; + + int32_t mID; + +private: + ~Foo(); +}; + +int32_t Foo::gCount; + +Foo::Foo(int32_t aID) +{ + mID = aID; + ++gCount; +} + +Foo::~Foo() +{ + --gCount; +} + +NS_IMPL_ISUPPORTS(Foo, IFoo) + +void CheckArray(nsISupportsArray* aArray, int32_t aExpectedCount, int32_t aElementIDs[], int32_t aExpectedTotal) +{ + uint32_t cnt = 0; +#ifdef DEBUG + nsresult rv = +#endif + aArray->Count(&cnt); + NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); + int32_t count = cnt; + int32_t index; + + EXPECT_EQ(Foo::gCount, aExpectedTotal); + EXPECT_EQ(count, aExpectedCount); + + for (index = 0; (index < count) && (index < aExpectedCount); index++) { + nsCOMPtr<IFoo> foo = do_QueryElementAt(aArray, index); + EXPECT_EQ(foo->ID(), aElementIDs[index]); + } +} + +void FillArray(nsISupportsArray* aArray, int32_t aCount) +{ + int32_t index; + for (index = 0; index < aCount; index++) { + nsCOMPtr<IFoo> foo = new Foo(index); + aArray->AppendElement(foo); + } +} + +} // namespace TestArray + +using namespace TestArray; + +TEST(Array, main) +{ + nsISupportsArray* array; + nsresult rv; + + if (NS_OK == (rv = NS_NewISupportsArray(&array))) { + FillArray(array, 10); + int32_t fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + CheckArray(array, 10, fillResult, 10); + + // test insert + nsCOMPtr<IFoo> foo = do_QueryElementAt(array, 3); + array->InsertElementAt(foo, 5); + int32_t insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; + CheckArray(array, 11, insertResult, 10); + array->InsertElementAt(foo, 0); + int32_t insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; + CheckArray(array, 12, insertResult2, 10); + array->AppendElement(foo); + int32_t appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3}; + CheckArray(array, 13, appendResult, 10); + + + // test IndexOf + int32_t expectedIndex = 0; + int32_t index = array->IndexOf(foo); + EXPECT_EQ(index, expectedIndex); + + // test ReplaceElementAt + array->ReplaceElementAt(foo, 8); + int32_t replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; + CheckArray(array, 13, replaceResult, 9); + + // test RemoveElementAt, RemoveElement + array->RemoveElementAt(0); + int32_t removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; + CheckArray(array, 12, removeResult, 9); + array->RemoveElementAt(7); + int32_t removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3}; + CheckArray(array, 11, removeResult2, 9); + array->RemoveElement(foo); + int32_t removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3}; + CheckArray(array, 10, removeResult3, 9); + + foo = nullptr; + + // test clear + array->Clear(); + FillArray(array, 4); + CheckArray(array, 4, fillResult, 4); + + // test delete + NS_RELEASE(array); + } +} + +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) +#pragma warning (pop) +#endif diff --git a/xpcom/glue/tests/gtest/TestFileUtils.cpp b/xpcom/glue/tests/gtest/TestFileUtils.cpp new file mode 100644 index 000000000..55106c6c5 --- /dev/null +++ b/xpcom/glue/tests/gtest/TestFileUtils.cpp @@ -0,0 +1,283 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +/* Test ReadSysFile() */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> + +#include "FileUtils.h" + +#include "gtest/gtest.h" + +namespace mozilla { + +#ifdef ReadSysFile_PRESENT + +/** + * Create a file with the specified contents. + */ +static bool +WriteFile( + const char* aFilename, + const void* aContents, + size_t aContentsLen) +{ + int fd; + ssize_t ret; + size_t offt; + + fd = MOZ_TEMP_FAILURE_RETRY( + open(aFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + if (fd == -1) { + fprintf(stderr, "open(): %s: %s\n", aFilename, strerror(errno)); + return false; + } + + offt = 0; + do { + ret = MOZ_TEMP_FAILURE_RETRY( + write(fd, (char*)aContents + offt, aContentsLen - offt)); + if (ret == -1) { + fprintf(stderr, "write(): %s: %s\n", aFilename, strerror(errno)); + close(fd); + return false; + } + offt += ret; + } while (offt < aContentsLen); + + ret = MOZ_TEMP_FAILURE_RETRY(close(fd)); + if (ret == -1) { + fprintf(stderr, "close(): %s: %s\n", aFilename, strerror(errno)); + return false; + } + return true; +} + +TEST(ReadSysFile, Nonexistent) { + bool ret; + int errno_saved; + + ret = ReadSysFile("/nonexistent", nullptr, 0); + errno_saved = errno; + + ASSERT_FALSE(ret); + ASSERT_EQ(errno_saved, ENOENT); +} + +TEST(ReadSysFile, Main) { + /* Use a different file name for each test since different tests could be + executed concurrently. */ + static const char* fn = "TestReadSysFileMain"; + /* If we have a file which contains "abcd" and we read it with ReadSysFile(), + providing a buffer of size 10 bytes, we would expect 5 bytes to be written + to that buffer: "abcd\0". */ + struct + { + /* input (file contents), e.g. "abcd" */ + const char* input; + /* pretended output buffer size, e.g. 10; the actual buffer is larger + and we check if anything was written past the end of the allowed length */ + size_t output_size; + /* expected number of bytes written to the output buffer, including the + terminating '\0', e.g. 5 */ + size_t output_len; + /* expected output buffer contents, e.g. "abcd\0", the first output_len + bytes of the output buffer should match the first 'output_len' bytes from + 'output', the rest of the output buffer should be untouched. */ + const char* output; + } tests[] = { + /* No new lines */ + {"", 0, 0, ""}, + {"", 1, 1, "\0"}, /* \0 is redundant, but we write it for clarity */ + {"", 9, 1, "\0"}, + + {"a", 0, 0, ""}, + {"a", 1, 1, "\0"}, + {"a", 2, 2, "a\0"}, + {"a", 9, 2, "a\0"}, + + {"abcd", 0, 0, ""}, + {"abcd", 1, 1, "\0"}, + {"abcd", 2, 2, "a\0"}, + {"abcd", 3, 3, "ab\0"}, + {"abcd", 4, 4, "abc\0"}, + {"abcd", 5, 5, "abcd\0"}, + {"abcd", 9, 5, "abcd\0"}, + + /* A single trailing new line */ + {"\n", 0, 0, ""}, + {"\n", 1, 1, "\0"}, + {"\n", 2, 1, "\0"}, + {"\n", 9, 1, "\0"}, + + {"a\n", 0, 0, ""}, + {"a\n", 1, 1, "\0"}, + {"a\n", 2, 2, "a\0"}, + {"a\n", 3, 2, "a\0"}, + {"a\n", 9, 2, "a\0"}, + + {"abcd\n", 0, 0, ""}, + {"abcd\n", 1, 1, "\0"}, + {"abcd\n", 2, 2, "a\0"}, + {"abcd\n", 3, 3, "ab\0"}, + {"abcd\n", 4, 4, "abc\0"}, + {"abcd\n", 5, 5, "abcd\0"}, + {"abcd\n", 6, 5, "abcd\0"}, + {"abcd\n", 9, 5, "abcd\0"}, + + /* Multiple trailing new lines */ + {"\n\n", 0, 0, ""}, + {"\n\n", 1, 1, "\0"}, + {"\n\n", 2, 2, "\n\0"}, + {"\n\n", 3, 2, "\n\0"}, + {"\n\n", 9, 2, "\n\0"}, + + {"a\n\n", 0, 0, ""}, + {"a\n\n", 1, 1, "\0"}, + {"a\n\n", 2, 2, "a\0"}, + {"a\n\n", 3, 3, "a\n\0"}, + {"a\n\n", 4, 3, "a\n\0"}, + {"a\n\n", 9, 3, "a\n\0"}, + + {"abcd\n\n", 0, 0, ""}, + {"abcd\n\n", 1, 1, "\0"}, + {"abcd\n\n", 2, 2, "a\0"}, + {"abcd\n\n", 3, 3, "ab\0"}, + {"abcd\n\n", 4, 4, "abc\0"}, + {"abcd\n\n", 5, 5, "abcd\0"}, + {"abcd\n\n", 6, 6, "abcd\n\0"}, + {"abcd\n\n", 7, 6, "abcd\n\0"}, + {"abcd\n\n", 9, 6, "abcd\n\0"}, + + /* New line in the middle */ + {"ab\ncd", 9, 6, "ab\ncd\0"}, + }; + + for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); + /* Leave the file to exist if some of the assertions fail. */ + + char buf[128]; + static const char unmodified = 'X'; + + memset(buf, unmodified, sizeof(buf)); + + ASSERT_TRUE(ReadSysFile(fn, buf, tests[i].output_size)); + + if (tests[i].output_size == 0) { + /* The buffer must be unmodified. We check only the first byte. */ + ASSERT_EQ(unmodified, buf[0]); + } else { + ASSERT_EQ(tests[i].output_len, strlen(buf) + 1); + ASSERT_STREQ(tests[i].output, buf); + /* Check that the first byte after the trailing '\0' has not been + modified. */ + ASSERT_EQ(unmodified, buf[tests[i].output_len]); + } + } + + unlink(fn); +} + +TEST(ReadSysFile, Int) { + static const char* fn = "TestReadSysFileInt"; + struct + { + /* input (file contents), e.g. "5" */ + const char* input; + /* expected return value, if false, then the output is not checked */ + bool ret; + /* expected result */ + int output; + } tests[] = { + {"0", true, 0}, + {"00", true, 0}, + {"1", true, 1}, + {"5", true, 5}, + {"55", true, 55}, + + {" 123", true, 123}, + {"123 ", true, 123}, + {" 123 ", true, 123}, + {"123\n", true, 123}, + + {"", false, 0}, + {" ", false, 0}, + {"a", false, 0}, + + {"-1", true, -1}, + {" -456 ", true, -456}, + {" -78.9 ", true, -78}, + }; + + for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); + /* Leave the file to exist if some of the assertions fail. */ + + bool ret; + int output = 424242; + + ret = ReadSysFile(fn, &output); + + ASSERT_EQ(tests[i].ret, ret); + + if (ret) { + ASSERT_EQ(tests[i].output, output); + } + } + + unlink(fn); +} + +TEST(ReadSysFile, Bool) { + static const char* fn = "TestReadSysFileBool"; + struct + { + /* input (file contents), e.g. "1" */ + const char* input; + /* expected return value */ + bool ret; + /* expected result */ + bool output; + } tests[] = { + {"0", true, false}, + {"00", true, false}, + {"1", true, true}, + {"5", true, true}, + {"23", true, true}, + {"-1", true, true}, + + {"", false, true /* unused */}, + }; + + for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); + /* Leave the file to exist if some of the assertions fail. */ + + bool ret; + bool output; + + ret = ReadSysFile(fn, &output); + + ASSERT_EQ(tests[i].ret, ret); + + if (ret) { + ASSERT_EQ(tests[i].output, output); + } + } + + unlink(fn); +} + +#endif /* ReadSysFile_PRESENT */ + +} // namespace mozilla diff --git a/xpcom/glue/tests/gtest/TestGCPostBarriers.cpp b/xpcom/glue/tests/gtest/TestGCPostBarriers.cpp new file mode 100644 index 000000000..5bf10ab05 --- /dev/null +++ b/xpcom/glue/tests/gtest/TestGCPostBarriers.cpp @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +/* + * Tests that generational garbage collection post-barriers are correctly + * implemented for nsTArrays that contain JavaScript Values. + */ + +#include "jsapi.h" +#include "nsTArray.h" + +#include "gtest/gtest.h" + +#include "js/TracingAPI.h" +#include "js/HeapAPI.h" + +#include "mozilla/CycleCollectedJSContext.h" + +using namespace JS; +using namespace mozilla; + +template<class ArrayT> +static void +TraceArray(JSTracer* trc, void* data) +{ + ArrayT* array = static_cast<ArrayT *>(data); + for (unsigned i = 0; i < array->Length(); ++i) + JS::TraceEdge(trc, &array->ElementAt(i), "array-element"); +} + +/* + * Use arrays with initial size much smaller than the final number of elements + * to test that moving Heap<T> elements works correctly. + */ +const size_t ElementCount = 100; +const size_t InitialElements = ElementCount / 10; + +template<class ArrayT> +static void +RunTest(JSContext* cx, ArrayT* array) +{ + JS_GC(cx); + + ASSERT_TRUE(array != nullptr); + JS_AddExtraGCRootsTracer(cx, TraceArray<ArrayT>, array); + + /* + * Create the array and fill it with new JS objects. With GGC these will be + * allocated in the nursery. + */ + RootedValue value(cx); + const char* property = "foo"; + for (size_t i = 0; i < ElementCount; ++i) { + RootedObject obj(cx, JS_NewPlainObject(cx)); + ASSERT_FALSE(JS::ObjectIsTenured(obj)); + value = Int32Value(i); + ASSERT_TRUE(JS_SetProperty(cx, obj, property, value)); + ASSERT_TRUE(array->AppendElement(obj, fallible)); + } + + /* + * If postbarriers are not working, we will crash here when we try to mark + * objects that have been moved to the tenured heap. + */ + JS_GC(cx); + + /* + * Sanity check that our array contains what we expect. + */ + for (size_t i = 0; i < ElementCount; ++i) { + RootedObject obj(cx, array->ElementAt(i)); + ASSERT_TRUE(JS::ObjectIsTenured(obj)); + ASSERT_TRUE(JS_GetProperty(cx, obj, property, &value)); + ASSERT_TRUE(value.isInt32()); + ASSERT_EQ(static_cast<int32_t>(i), value.toInt32()); + } + + JS_RemoveExtraGCRootsTracer(cx, TraceArray<ArrayT>, array); +} + +static void +CreateGlobalAndRunTest(JSContext* cx) +{ + static const JSClassOps GlobalClassOps = { + nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + JS_GlobalObjectTraceHook + }; + + static const JSClass GlobalClass = { + "global", JSCLASS_GLOBAL_FLAGS, + &GlobalClassOps + }; + + JS::CompartmentOptions options; + options.behaviors().setVersion(JSVERSION_LATEST); + JS::PersistentRootedObject global(cx); + global = JS_NewGlobalObject(cx, &GlobalClass, nullptr, JS::FireOnNewGlobalHook, options); + ASSERT_TRUE(global != nullptr); + + JSCompartment *oldCompartment = JS_EnterCompartment(cx, global); + + typedef Heap<JSObject*> ElementT; + + { + nsTArray<ElementT>* array = new nsTArray<ElementT>(InitialElements); + RunTest(cx, array); + delete array; + } + + { + FallibleTArray<ElementT>* array = new FallibleTArray<ElementT>(InitialElements); + RunTest(cx, array); + delete array; + } + + { + AutoTArray<ElementT, InitialElements> array; + RunTest(cx, &array); + } + + JS_LeaveCompartment(cx, oldCompartment); +} + +TEST(GCPostBarriers, nsTArray) { + CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::Get(); + ASSERT_TRUE(ccjscx != nullptr); + JSContext* cx = ccjscx->Context(); + ASSERT_TRUE(cx != nullptr); + + JS_BeginRequest(cx); + + CreateGlobalAndRunTest(cx); + + JS_EndRequest(cx); +} diff --git a/xpcom/glue/tests/gtest/TestNsDeque.cpp b/xpcom/glue/tests/gtest/TestNsDeque.cpp new file mode 100644 index 000000000..b84e1b781 --- /dev/null +++ b/xpcom/glue/tests/gtest/TestNsDeque.cpp @@ -0,0 +1,342 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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 "gtest/gtest.h" +#include "nsDeque.h" +#include "nsCRT.h" +#include <stdio.h> + +/************************************************************** + Now define the token deallocator class... + **************************************************************/ +namespace TestNsDeque { + + class _Dealloc: public nsDequeFunctor + { + virtual void* operator()(void* aObject) { + return 0; + } + }; + + static bool VerifyContents(const nsDeque& aDeque, const int* aContents, size_t aLength) + { + for (size_t i=0; i<aLength; ++i) { + if (*(int*)aDeque.ObjectAt(i) != aContents[i]) { + return false; + } + } + return true; + } + + class Deallocator: public nsDequeFunctor + { + virtual void* operator()(void* aObject) + { + if (aObject) + { + // Set value to -1, to use in test function. + *((int*)aObject) = -1; + } + + return nullptr; + } + }; + + class ForEachAdder: public nsDequeFunctor + { + virtual void* operator()(void* aObject) + { + if (aObject) + { + sum += *(int*)aObject; + } + + return aObject; + } + + private: + int sum = 0; + + public: + int GetSum() { return sum; } + + }; +} + +using namespace TestNsDeque; + +TEST(NsDeque, OriginalTest) +{ + const size_t size = 200; + int ints[size]; + size_t i=0; + int temp; + nsDeque theDeque(new _Dealloc); //construct a simple one... + + // ints = [0...199] + for (i=0;i<size;i++) { //initialize'em + ints[i]=static_cast<int>(i); + } + // queue = [0...69] + for (i=0;i<70;i++) { + theDeque.Push(&ints[i]); + temp=*(int*)theDeque.Peek(); + EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #1"; + EXPECT_EQ(i + 1, theDeque.GetSize()) << "Verify size after push #1"; + } + + EXPECT_EQ(70u,theDeque.GetSize()) << "Verify overall size after pushes #1"; + + // queue = [0...14] + for (i=1;i<=55;i++) { + temp=*(int*)theDeque.Pop(); + EXPECT_EQ(70-static_cast<int>(i),temp) << "Verify end after pop # 1"; + EXPECT_EQ(70u - i,theDeque.GetSize()) << "Verify size after pop # 1"; + } + EXPECT_EQ(15u,theDeque.GetSize()) << "Verify overall size after pops"; + + // queue = [0...14,0...54] + for (i=0;i<55;i++) { + theDeque.Push(&ints[i]); + temp=*(int*)theDeque.Peek(); + EXPECT_EQ(static_cast<int>(i),temp) << "Verify end after push #2"; + EXPECT_EQ(i + 15u + 1,theDeque.GetSize()) << "Verify size after push # 2"; + } + EXPECT_EQ(70u,theDeque.GetSize()) << "Verify size after end of all pushes #2"; + + // queue = [0...14,0...19] + for (i=1;i<=35;i++) { + temp=*(int*)theDeque.Pop(); + EXPECT_EQ(55-static_cast<int>(i),temp ) << "Verify end after pop # 2"; + EXPECT_EQ(70u - i,theDeque.GetSize()) << "Verify size after pop #2"; + } + EXPECT_EQ(35u,theDeque.GetSize()) << "Verify overall size after end of all pops #2"; + + // queue = [0...14,0...19,0...34] + for (i=0;i<35;i++) { + theDeque.Push(&ints[i]); + temp = *(int*)theDeque.Peek(); + EXPECT_EQ(static_cast<int>(i),temp) << "Verify end after push # 3"; + EXPECT_EQ(35u + 1u + i,theDeque.GetSize()) << "Verify size after push #3"; + } + + // queue = [0...14,0...19] + for (i=0;i<35;i++) { + temp=*(int*)theDeque.Pop(); + EXPECT_EQ(34 - static_cast<int>(i), temp) << "Verify end after pop # 3"; + } + + // queue = [0...14] + for (i=0;i<20;i++) { + temp=*(int*)theDeque.Pop(); + EXPECT_EQ(19 - static_cast<int>(i),temp) << "Verify end after pop # 4"; + } + + // queue = [] + for (i=0;i<15;i++) { + temp=*(int*)theDeque.Pop(); + EXPECT_EQ(14 - static_cast<int>(i),temp) << "Verify end after pop # 5"; + } + + EXPECT_EQ(0u,theDeque.GetSize()) << "Deque should finish empty."; +} + +TEST(NsDeque, OriginalFlaw) +{ + int ints[200]; + int i=0; + int temp; + nsDeque d(new _Dealloc); + /** + * Test 1. Origin near end, semi full, call Peek(). + * you start, mCapacity is 8 + */ + for (i=0; i<30; i++) + ints[i]=i; + + for (i=0; i<6; i++) { + d.Push(&ints[i]); + temp = *(int*)d.Peek(); + EXPECT_EQ(i, temp) << "OriginalFlaw push #1"; + } + EXPECT_EQ(6u, d.GetSize()) << "OriginalFlaw size check #1"; + + for (i=0; i<4; i++) { + temp=*(int*)d.PopFront(); + EXPECT_EQ(i, temp) << "PopFront test"; + } + // d = [4,5] + EXPECT_EQ(2u, d.GetSize()) << "OriginalFlaw size check #2"; + + for (i=0; i<4; i++) { + d.Push(&ints[6 + i]); + } + + // d = [4...9] + for (i=4; i<=9; i++) { + temp=*(int*)d.PopFront(); + EXPECT_EQ(i, temp) << "OriginalFlaw empty check"; + } +} + +TEST(NsDeque, TestObjectAt) +{ + nsDeque d; + const int count = 10; + int ints[count]; + for (int i=0; i<count; i++) { + ints[i] = i; + } + + for (int i=0; i<6; i++) { + d.Push(&ints[i]); + } + // d = [0...5] + d.PopFront(); + d.PopFront(); + + // d = [2..5] + for (size_t i=2; i<=5; i++) { + int t = *(int*)d.ObjectAt(i-2); + EXPECT_EQ(static_cast<int>(i),t) << "Verify ObjectAt()"; + } +} + +TEST(NsDeque, TestPushFront) +{ + // PushFront has some interesting corner cases, primarily we're interested in whether: + // - wrapping around works properly + // - growing works properly + + nsDeque d; + + const int kPoolSize = 10; + const size_t kMaxSizeBeforeGrowth = 8; + + int pool[kPoolSize]; + for (int i = 0; i < kPoolSize; i++) { + pool[i] = i; + } + + for (size_t i = 0; i < kMaxSizeBeforeGrowth; i++) { + d.PushFront(pool + i); + } + + EXPECT_EQ(kMaxSizeBeforeGrowth, d.GetSize()) << "verify size"; + + static const int t1[] = {7,6,5,4,3,2,1,0}; + EXPECT_TRUE(VerifyContents(d, t1, kMaxSizeBeforeGrowth)) << "verify pushfront 1"; + + // Now push one more so it grows + d.PushFront(pool + kMaxSizeBeforeGrowth); + EXPECT_EQ(kMaxSizeBeforeGrowth + 1, d.GetSize()) << "verify size"; + + static const int t2[] = {8,7,6,5,4,3,2,1,0}; + EXPECT_TRUE(VerifyContents(d, t2, kMaxSizeBeforeGrowth + 1)) << "verify pushfront 2"; + + // And one more so that it wraps again + d.PushFront(pool + kMaxSizeBeforeGrowth + 1); + EXPECT_EQ(kMaxSizeBeforeGrowth + 2, d.GetSize()) << "verify size"; + + static const int t3[] = {9,8,7,6,5,4,3,2,1,0}; + EXPECT_TRUE(VerifyContents(d, t3, kMaxSizeBeforeGrowth + 2)) <<"verify pushfront 3"; +} + +void CheckIfQueueEmpty(nsDeque& d) +{ + EXPECT_EQ(0u, d.GetSize()) << "Size should be 0"; + EXPECT_EQ(nullptr, d.Pop()) << "Invalid operation should return nullptr"; + EXPECT_EQ(nullptr, d.PopFront()) << "Invalid operation should return nullptr"; + EXPECT_EQ(nullptr, d.Peek()) << "Invalid operation should return nullptr"; + EXPECT_EQ(nullptr, d.PeekFront()) << "Invalid operation should return nullptr"; + EXPECT_EQ(nullptr, d.ObjectAt(0u)) << "Invalid operation should return nullptr"; +} + +TEST(NsDeque,TestEmpty) +{ + // Make sure nsDeque gives sane results if it's empty. + nsDeque d; + size_t numberOfEntries = 8; + + CheckIfQueueEmpty(d); + + // Fill it up and drain it. + for (size_t i = 0; i < numberOfEntries; i++) { + d.Push((void*)0xAA); + } + + EXPECT_EQ(numberOfEntries, d.GetSize()); + + for (size_t i = 0; i < numberOfEntries; i++) { + (void)d.Pop(); + } + + // Now check it again. + CheckIfQueueEmpty(d); +} + +TEST(NsDeque,TestEraseMethod) +{ + nsDeque d; + const size_t numberOfEntries = 8; + + // Fill it up before calling Erase + for (size_t i = 0; i < numberOfEntries; i++) { + d.Push((void*)0xAA); + } + + // Call Erase + d.Erase(); + + // Now check it again. + CheckIfQueueEmpty(d); +} + +TEST(NsDeque,TestEraseShouldCallDeallocator) +{ + nsDeque d(new Deallocator()); + const size_t NumTestValues = 8; + + int* testArray[NumTestValues]; + for (size_t i=0; i < NumTestValues; i++) + { + testArray[i] = new int(); + *(testArray[i]) = i; + d.Push((void*)testArray[i]); + } + + d.Erase(); + + // Now check it again. + CheckIfQueueEmpty(d); + + for (size_t i=0; i < NumTestValues; i++) + { + EXPECT_EQ(-1, *(testArray[i])) << "Erase should call deallocator: " << *(testArray[i]); + } +} + +TEST(NsDeque, TestForEach) +{ + nsDeque d(new Deallocator()); + const size_t NumTestValues = 8; + int sum = 0; + + int* testArray[NumTestValues]; + for (size_t i=0; i < NumTestValues; i++) + { + testArray[i] = new int(); + *(testArray[i]) = i; + sum += i; + d.Push((void*)testArray[i]); + } + + ForEachAdder adder; + d.ForEach(adder); + EXPECT_EQ(sum, adder.GetSum()) << "For each should iterate over values"; + + d.Erase(); +} diff --git a/xpcom/glue/tests/gtest/TestThreadUtils.cpp b/xpcom/glue/tests/gtest/TestThreadUtils.cpp new file mode 100644 index 000000000..728bae612 --- /dev/null +++ b/xpcom/glue/tests/gtest/TestThreadUtils.cpp @@ -0,0 +1,937 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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 <stdio.h> +#include <stdlib.h> +#include "nsThreadUtils.h" +#include "gtest/gtest.h" + +// {9e70a320-be02-11d1-8031-006008159b5a} +#define NS_IFOO_IID \ + {0x9e70a320, 0xbe02, 0x11d1, \ + {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}} + +namespace TestThreadUtils { + +static bool gDebug = false; +static int gAlive, gZombies; +static int gAllConstructions, gConstructions, gCopyConstructions, + gMoveConstructions, gDestructions, gAssignments, gMoves; +struct Spy +{ + static void ClearActions() + { + gAllConstructions = gConstructions = gCopyConstructions + = gMoveConstructions = gDestructions = gAssignments = gMoves = 0; + } + static void ClearAll() + { + ClearActions(); + gAlive = 0; + } + + explicit Spy(int aID) : mID(aID) + { + ++gAlive; ++gAllConstructions; ++gConstructions; + if (gDebug) { printf("Spy[%d@%p]()\n", mID, this); } + } + Spy(const Spy& o) : mID(o.mID) + { + ++gAlive; ++gAllConstructions; ++gCopyConstructions; + if (gDebug) { printf("Spy[%d@%p](&[%d@%p])\n", mID, this, o.mID, &o); } + } + Spy(Spy&& o) : mID(o.mID) + { + o.mID = -o.mID; + ++gZombies; ++gAllConstructions; ++gMoveConstructions; + if (gDebug) { printf("Spy[%d@%p](&&[%d->%d@%p])\n", mID, this, -o.mID, o.mID, &o); } + } + ~Spy() + { + if (mID >= 0) { --gAlive; } else { --gZombies; } ++gDestructions; + if (gDebug) { printf("~Spy[%d@%p]()\n", mID, this); } + mID = 0; + } + Spy& operator=(const Spy& o) + { + ++gAssignments; + if (gDebug) { printf("Spy[%d->%d@%p] = &[%d@%p]\n", mID, o.mID, this, o.mID, &o); } + mID = o.mID; + return *this; + }; + Spy& operator=(Spy&& o) + { + --gAlive; ++gZombies; + ++gMoves; + if (gDebug) { printf("Spy[%d->%d@%p] = &&[%d->%d@%p]\n", mID, o.mID, this, o.mID, -o.mID, &o); } + mID = o.mID; o.mID = -o.mID; + return *this; + }; + + int mID; // ID given at construction, or negation if was moved from; 0 when destroyed. +}; + +struct ISpyWithISupports : public nsISupports +{ + NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) + NS_IMETHOD_(nsrefcnt) RefCnt() = 0; + NS_IMETHOD_(int32_t) ID() = 0; +}; +NS_DEFINE_STATIC_IID_ACCESSOR(ISpyWithISupports, NS_IFOO_IID) +struct SpyWithISupports : public ISpyWithISupports, public Spy +{ +private: + virtual ~SpyWithISupports() = default; +public: + explicit SpyWithISupports(int aID) : Spy(aID) {}; + NS_DECL_ISUPPORTS + NS_IMETHOD_(nsrefcnt) RefCnt() override { return mRefCnt; } + NS_IMETHOD_(int32_t) ID() override { return mID; } +}; +NS_IMPL_ISUPPORTS(SpyWithISupports, ISpyWithISupports) + + +class IThreadUtilsObject : public nsISupports +{ +public: + NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) + + NS_IMETHOD_(nsrefcnt) RefCnt() = 0; + NS_IMETHOD_(int32_t) ID() = 0; +}; + +NS_DEFINE_STATIC_IID_ACCESSOR(IThreadUtilsObject, NS_IFOO_IID) + +struct ThreadUtilsObject : public IThreadUtilsObject +{ + // nsISupports implementation + NS_DECL_ISUPPORTS + + // IThreadUtilsObject implementation + NS_IMETHOD_(nsrefcnt) RefCnt() override { return mRefCnt; } + NS_IMETHOD_(int32_t) ID() override { return 0; } + + int mCount; // Number of calls + arguments processed. + int mA0, mA1, mA2, mA3; + Spy mSpy; const Spy* mSpyPtr; + ThreadUtilsObject() + : mCount(0) + , mA0(0), mA1(0), mA2(0), mA3(0) + , mSpy(1), mSpyPtr(nullptr) + {} +private: + virtual ~ThreadUtilsObject() = default; +public: + void Test0() { mCount += 1; } + void Test1i(int a0) { mCount += 2; mA0 = a0; } + void Test2i(int a0, int a1) { mCount += 3; mA0 = a0; mA1 = a1; } + void Test3i(int a0, int a1, int a2) + { + mCount += 4; mA0 = a0; mA1 = a1; mA2 = a2; + } + void Test4i(int a0, int a1, int a2, int a3) + { + mCount += 5; mA0 = a0; mA1 = a1; mA2 = a2; mA3 = a3; + } + void Test1pi(int* ap) + { + mCount += 2; mA0 = ap ? *ap : -1; + } + void Test1pci(const int* ap) + { + mCount += 2; mA0 = ap ? *ap : -1; + } + void Test1ri(int& ar) + { + mCount += 2; mA0 = ar; + } + void Test1rri(int&& arr) + { + mCount += 2; mA0 = arr; + } + void Test1upi(mozilla::UniquePtr<int> aup) + { + mCount += 2; mA0 = aup ? *aup : -1; + } + void Test1rupi(mozilla::UniquePtr<int>& aup) + { + mCount += 2; mA0 = aup ? *aup : -1; + } + void Test1rrupi(mozilla::UniquePtr<int>&& aup) + { + mCount += 2; mA0 = aup ? *aup : -1; + } + + void Test1s(Spy) { mCount += 2; } + void Test1ps(Spy*) { mCount += 2; } + void Test1rs(Spy&) { mCount += 2; } + void Test1rrs(Spy&&) { mCount += 2; } + void Test1ups(mozilla::UniquePtr<Spy>) { mCount += 2; } + void Test1rups(mozilla::UniquePtr<Spy>&) { mCount += 2; } + void Test1rrups(mozilla::UniquePtr<Spy>&&) { mCount += 2; } + + // Possible parameter passing styles: + void TestByValue(Spy s) + { + if (gDebug) { printf("TestByValue(Spy[%d@%p])\n", s.mID, &s); } + mSpy = s; + }; + void TestByConstLRef(const Spy& s) + { + if (gDebug) { printf("TestByConstLRef(Spy[%d@%p]&)\n", s.mID, &s); } + mSpy = s; + }; + void TestByRRef(Spy&& s) + { + if (gDebug) { printf("TestByRRef(Spy[%d@%p]&&)\n", s.mID, &s); } + mSpy = mozilla::Move(s); + }; + void TestByLRef(Spy& s) + { + if (gDebug) { printf("TestByLRef(Spy[%d@%p]&)\n", s.mID, &s); } + mSpy = s; + mSpyPtr = &s; + }; + void TestByPointer(Spy* p) + { + if (p) { + if (gDebug) { printf("TestByPointer(&Spy[%d@%p])\n", p->mID, p); } + mSpy = *p; + } else { + if (gDebug) { printf("TestByPointer(nullptr)\n"); } + } + mSpyPtr = p; + }; + void TestByPointerToConst(const Spy* p) + { + if (p) { + if (gDebug) { printf("TestByPointerToConst(&Spy[%d@%p])\n", p->mID, p); } + mSpy = *p; + } else { + if (gDebug) { printf("TestByPointerToConst(nullptr)\n"); } + } + mSpyPtr = p; + }; +}; + +NS_IMPL_ISUPPORTS(ThreadUtilsObject, IThreadUtilsObject) + +class ThreadUtilsRefCountedFinal final +{ +public: + ThreadUtilsRefCountedFinal() : m_refCount(0) {} + ~ThreadUtilsRefCountedFinal() {} + // 'AddRef' and 'Release' methods with different return types, to verify + // that the return type doesn't influence storage selection. + long AddRef(void) { return ++m_refCount; } + void Release(void) { --m_refCount; } +private: + long m_refCount; +}; + +class ThreadUtilsRefCountedBase +{ +public: + ThreadUtilsRefCountedBase() : m_refCount(0) {} + virtual ~ThreadUtilsRefCountedBase() {} + // 'AddRef' and 'Release' methods with different return types, to verify + // that the return type doesn't influence storage selection. + virtual void AddRef(void) { ++m_refCount; } + virtual MozExternalRefCountType Release(void) { return --m_refCount; } +private: + MozExternalRefCountType m_refCount; +}; + +class ThreadUtilsRefCountedDerived + : public ThreadUtilsRefCountedBase +{}; + +class ThreadUtilsNonRefCounted +{}; + +} // namespace TestThreadUtils + +TEST(ThreadUtils, main) +{ +#ifndef XPCOM_GLUE_AVOID_NSPR + using namespace TestThreadUtils; + + static_assert(!IsParameterStorageClass<int>::value, + "'int' should not be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByValue<int>>::value, + "StoreCopyPassByValue<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByConstLRef<int>>::value, + "StoreCopyPassByConstLRef<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByLRef<int>>::value, + "StoreCopyPassByLRef<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByRRef<int>>::value, + "StoreCopyPassByRRef<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreRefPassByLRef<int>>::value, + "StoreRefPassByLRef<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreConstRefPassByConstLRef<int>>::value, + "StoreConstRefPassByConstLRef<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StorensRefPtrPassByPtr<int>>::value, + "StorensRefPtrPassByPtr<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StorePtrPassByPtr<int>>::value, + "StorePtrPassByPtr<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreConstPtrPassByConstPtr<int>>::value, + "StoreConstPtrPassByConstPtr<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByConstPtr<int>>::value, + "StoreCopyPassByConstPtr<int> should be recognized as Storage Class"); + static_assert(IsParameterStorageClass<StoreCopyPassByPtr<int>>::value, + "StoreCopyPassByPtr<int> should be recognized as Storage Class"); + + RefPtr<ThreadUtilsObject> rpt(new ThreadUtilsObject); + int count = 0; + + // Test legacy functions. + + nsCOMPtr<nsIRunnable> r1 = + NewRunnableMethod(rpt, &ThreadUtilsObject::Test0); + r1->Run(); + EXPECT_EQ(count += 1, rpt->mCount); + + r1 = NewRunnableMethod<int>(rpt, &ThreadUtilsObject::Test1i, 11); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(11, rpt->mA0); + + // Test variadic function with simple POD arguments. + + r1 = NewRunnableMethod(rpt, &ThreadUtilsObject::Test0); + r1->Run(); + EXPECT_EQ(count += 1, rpt->mCount); + + static_assert( + mozilla::IsSame< ::detail::ParameterStorage<int>::Type, + StoreCopyPassByValue<int>>::value, + "detail::ParameterStorage<int>::Type should be StoreCopyPassByValue<int>"); + static_assert( + mozilla::IsSame< ::detail::ParameterStorage<StoreCopyPassByValue<int>>::Type, + StoreCopyPassByValue<int>>::value, + "detail::ParameterStorage<StoreCopyPassByValue<int>>::Type should be StoreCopyPassByValue<int>"); + + r1 = NewRunnableMethod<int>(rpt, &ThreadUtilsObject::Test1i, 12); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(12, rpt->mA0); + + r1 = NewRunnableMethod<int, int>( + rpt, &ThreadUtilsObject::Test2i, 21, 22); + r1->Run(); + EXPECT_EQ(count += 3, rpt->mCount); + EXPECT_EQ(21, rpt->mA0); + EXPECT_EQ(22, rpt->mA1); + + r1 = NewRunnableMethod<int, int, int>( + rpt, &ThreadUtilsObject::Test3i, 31, 32, 33); + r1->Run(); + EXPECT_EQ(count += 4, rpt->mCount); + EXPECT_EQ(31, rpt->mA0); + EXPECT_EQ(32, rpt->mA1); + EXPECT_EQ(33, rpt->mA2); + + r1 = NewRunnableMethod<int, int, int, int>( + rpt, &ThreadUtilsObject::Test4i, 41, 42, 43, 44); + r1->Run(); + EXPECT_EQ(count += 5, rpt->mCount); + EXPECT_EQ(41, rpt->mA0); + EXPECT_EQ(42, rpt->mA1); + EXPECT_EQ(43, rpt->mA2); + EXPECT_EQ(44, rpt->mA3); + + // More interesting types of arguments. + + // Passing a short to make sure forwarding works with an inexact type match. + short int si = 11; + r1 = NewRunnableMethod<int>(rpt, &ThreadUtilsObject::Test1i, si); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(si, rpt->mA0); + + // Raw pointer, possible cv-qualified. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int*>::Type, + StorePtrPassByPtr<int>>::value, + "detail::ParameterStorage<int*>::Type should be StorePtrPassByPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int* const>::Type, + StorePtrPassByPtr<int>>::value, + "detail::ParameterStorage<int* const>::Type should be StorePtrPassByPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int* volatile>::Type, + StorePtrPassByPtr<int>>::value, + "detail::ParameterStorage<int* volatile>::Type should be StorePtrPassByPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int* const volatile>::Type, + StorePtrPassByPtr<int>>::value, + "detail::ParameterStorage<int* const volatile>::Type should be StorePtrPassByPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int*>::Type::stored_type, + int*>::value, + "detail::ParameterStorage<int*>::Type::stored_type should be int*"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int*>::Type::passed_type, + int*>::value, + "detail::ParameterStorage<int*>::Type::passed_type should be int*"); + { + int i = 12; + r1 = NewRunnableMethod<int*>(rpt, &ThreadUtilsObject::Test1pi, &i); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(i, rpt->mA0); + } + + // Raw pointer to const. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int*>::Type, + StoreConstPtrPassByConstPtr<int>>::value, + "detail::ParameterStorage<const int*>::Type should be StoreConstPtrPassByConstPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int* const>::Type, + StoreConstPtrPassByConstPtr<int>>::value, + "detail::ParameterStorage<const int* const>::Type should be StoreConstPtrPassByConstPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int* volatile>::Type, + StoreConstPtrPassByConstPtr<int>>::value, + "detail::ParameterStorage<const int* volatile>::Type should be StoreConstPtrPassByConstPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int* const volatile>::Type, + StoreConstPtrPassByConstPtr<int>>::value, + "detail::ParameterStorage<const int* const volatile>::Type should be StoreConstPtrPassByConstPtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int*>::Type::stored_type, + const int*>::value, + "detail::ParameterStorage<const int*>::Type::stored_type should be const int*"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<const int*>::Type::passed_type, + const int*>::value, + "detail::ParameterStorage<const int*>::Type::passed_type should be const int*"); + { + int i = 1201; + r1 = NewRunnableMethod<const int*>(rpt, &ThreadUtilsObject::Test1pci, &i); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(i, rpt->mA0); + } + + // Raw pointer to copy. + static_assert(mozilla::IsSame<StoreCopyPassByPtr<int>::stored_type, + int>::value, + "StoreCopyPassByPtr<int>::stored_type should be int"); + static_assert(mozilla::IsSame<StoreCopyPassByPtr<int>::passed_type, + int*>::value, + "StoreCopyPassByPtr<int>::passed_type should be int*"); + { + int i = 1202; + r1 = NewRunnableMethod<StoreCopyPassByPtr<int>>( + rpt, &ThreadUtilsObject::Test1pi, i); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(i, rpt->mA0); + } + + // Raw pointer to const copy. + static_assert(mozilla::IsSame<StoreCopyPassByConstPtr<int>::stored_type, + int>::value, + "StoreCopyPassByConstPtr<int>::stored_type should be int"); + static_assert(mozilla::IsSame<StoreCopyPassByConstPtr<int>::passed_type, + const int*>::value, + "StoreCopyPassByConstPtr<int>::passed_type should be const int*"); + { + int i = 1203; + r1 = NewRunnableMethod<StoreCopyPassByConstPtr<int>>( + rpt, &ThreadUtilsObject::Test1pci, i); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(i, rpt->mA0); + } + + // nsRefPtr to pointer. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<StorensRefPtrPassByPtr<SpyWithISupports>>::Type, + StorensRefPtrPassByPtr<SpyWithISupports>>::value, + "ParameterStorage<StorensRefPtrPassByPtr<SpyWithISupports>>::Type should be StorensRefPtrPassByPtr<SpyWithISupports>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<SpyWithISupports*>::Type, + StorensRefPtrPassByPtr<SpyWithISupports>>::value, + "ParameterStorage<SpyWithISupports*>::Type should be StorensRefPtrPassByPtr<SpyWithISupports>"); + static_assert(mozilla::IsSame<StorensRefPtrPassByPtr<SpyWithISupports>::stored_type, + RefPtr<SpyWithISupports>>::value, + "StorensRefPtrPassByPtr<SpyWithISupports>::stored_type should be RefPtr<SpyWithISupports>"); + static_assert(mozilla::IsSame<StorensRefPtrPassByPtr<SpyWithISupports>::passed_type, + SpyWithISupports*>::value, + "StorensRefPtrPassByPtr<SpyWithISupports>::passed_type should be SpyWithISupports*"); + // (more nsRefPtr tests below) + + // nsRefPtr for ref-countable classes that do not derive from ISupports. + static_assert(::detail::HasRefCountMethods<ThreadUtilsRefCountedFinal>::value, + "ThreadUtilsRefCountedFinal has AddRef() and Release()"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<ThreadUtilsRefCountedFinal*>::Type, + StorensRefPtrPassByPtr<ThreadUtilsRefCountedFinal>>::value, + "ParameterStorage<ThreadUtilsRefCountedFinal*>::Type should be StorensRefPtrPassByPtr<ThreadUtilsRefCountedFinal>"); + static_assert(::detail::HasRefCountMethods<ThreadUtilsRefCountedBase>::value, + "ThreadUtilsRefCountedBase has AddRef() and Release()"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<ThreadUtilsRefCountedBase*>::Type, + StorensRefPtrPassByPtr<ThreadUtilsRefCountedBase>>::value, + "ParameterStorage<ThreadUtilsRefCountedBase*>::Type should be StorensRefPtrPassByPtr<ThreadUtilsRefCountedBase>"); + static_assert(::detail::HasRefCountMethods<ThreadUtilsRefCountedDerived>::value, + "ThreadUtilsRefCountedDerived has AddRef() and Release()"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<ThreadUtilsRefCountedDerived*>::Type, + StorensRefPtrPassByPtr<ThreadUtilsRefCountedDerived>>::value, + "ParameterStorage<ThreadUtilsRefCountedDerived*>::Type should be StorensRefPtrPassByPtr<ThreadUtilsRefCountedDerived>"); + + static_assert(!::detail::HasRefCountMethods<ThreadUtilsNonRefCounted>::value, + "ThreadUtilsNonRefCounted doesn't have AddRef() and Release()"); + static_assert(!mozilla::IsSame< ::detail::ParameterStorage<ThreadUtilsNonRefCounted*>::Type, + StorensRefPtrPassByPtr<ThreadUtilsNonRefCounted>>::value, + "ParameterStorage<ThreadUtilsNonRefCounted*>::Type should NOT be StorensRefPtrPassByPtr<ThreadUtilsNonRefCounted>"); + + // Lvalue reference. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&>::Type, + StoreRefPassByLRef<int>>::value, + "ParameterStorage<int&>::Type should be StoreRefPassByLRef<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&>::Type::stored_type, + StoreRefPassByLRef<int>::stored_type>::value, + "ParameterStorage<int&>::Type::stored_type should be StoreRefPassByLRef<int>::stored_type"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&>::Type::stored_type, + int&>::value, + "ParameterStorage<int&>::Type::stored_type should be int&"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&>::Type::passed_type, + int&>::value, + "ParameterStorage<int&>::Type::passed_type should be int&"); + { + int i = 13; + r1 = NewRunnableMethod<int&>(rpt, &ThreadUtilsObject::Test1ri, i); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(i, rpt->mA0); + } + + // Rvalue reference -- Actually storing a copy and then moving it. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&&>::Type, + StoreCopyPassByRRef<int>>::value, + "ParameterStorage<int&&>::Type should be StoreCopyPassByRRef<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&&>::Type::stored_type, + StoreCopyPassByRRef<int>::stored_type>::value, + "ParameterStorage<int&&>::Type::stored_type should be StoreCopyPassByRRef<int>::stored_type"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&&>::Type::stored_type, + int>::value, + "ParameterStorage<int&&>::Type::stored_type should be int"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<int&&>::Type::passed_type, + int&&>::value, + "ParameterStorage<int&&>::Type::passed_type should be int&&"); + { + int i = 14; + r1 = NewRunnableMethod<int&&>( + rpt, &ThreadUtilsObject::Test1rri, mozilla::Move(i)); + } + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(14, rpt->mA0); + + // Null unique pointer, by semi-implicit store&move with "T&&" syntax. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<mozilla::UniquePtr<int>&&>::Type, + StoreCopyPassByRRef<mozilla::UniquePtr<int>>>::value, + "ParameterStorage<UniquePtr<int>&&>::Type should be StoreCopyPassByRRef<UniquePtr<int>>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<mozilla::UniquePtr<int>&&>::Type::stored_type, + StoreCopyPassByRRef<mozilla::UniquePtr<int>>::stored_type>::value, + "ParameterStorage<UniquePtr<int>&&>::Type::stored_type should be StoreCopyPassByRRef<UniquePtr<int>>::stored_type"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<mozilla::UniquePtr<int>&&>::Type::stored_type, + mozilla::UniquePtr<int>>::value, + "ParameterStorage<UniquePtr<int>&&>::Type::stored_type should be UniquePtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<mozilla::UniquePtr<int>&&>::Type::passed_type, + mozilla::UniquePtr<int>&&>::value, + "ParameterStorage<UniquePtr<int>&&>::Type::passed_type should be UniquePtr<int>&&"); + { + mozilla::UniquePtr<int> upi; + r1 = NewRunnableMethod<mozilla::UniquePtr<int>&&>( + rpt, &ThreadUtilsObject::Test1upi, mozilla::Move(upi)); + } + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(-1, rpt->mA0); + rpt->mA0 = 0; + + // Null unique pointer, by explicit store&move with "StoreCopyPassByRRef<T>" syntax. + static_assert(mozilla::IsSame< ::detail::ParameterStorage<StoreCopyPassByRRef<mozilla::UniquePtr<int>>>::Type::stored_type, + StoreCopyPassByRRef<mozilla::UniquePtr<int>>::stored_type>::value, + "ParameterStorage<StoreCopyPassByRRef<UniquePtr<int>>>::Type::stored_type should be StoreCopyPassByRRef<UniquePtr<int>>::stored_type"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<StoreCopyPassByRRef<mozilla::UniquePtr<int>>>::Type::stored_type, + StoreCopyPassByRRef<mozilla::UniquePtr<int>>::stored_type>::value, + "ParameterStorage<StoreCopyPassByRRef<UniquePtr<int>>>::Type::stored_type should be StoreCopyPassByRRef<UniquePtr<int>>::stored_type"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<StoreCopyPassByRRef<mozilla::UniquePtr<int>>>::Type::stored_type, + mozilla::UniquePtr<int>>::value, + "ParameterStorage<StoreCopyPassByRRef<UniquePtr<int>>>::Type::stored_type should be UniquePtr<int>"); + static_assert(mozilla::IsSame< ::detail::ParameterStorage<StoreCopyPassByRRef<mozilla::UniquePtr<int>>>::Type::passed_type, + mozilla::UniquePtr<int>&&>::value, + "ParameterStorage<StoreCopyPassByRRef<UniquePtr<int>>>::Type::passed_type should be UniquePtr<int>&&"); + { + mozilla::UniquePtr<int> upi; + r1 = NewRunnableMethod + <StoreCopyPassByRRef<mozilla::UniquePtr<int>>>( + rpt, &ThreadUtilsObject::Test1upi, mozilla::Move(upi)); + } + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(-1, rpt->mA0); + + // Unique pointer as xvalue. + { + mozilla::UniquePtr<int> upi = mozilla::MakeUnique<int>(1); + r1 = NewRunnableMethod<mozilla::UniquePtr<int>&&>( + rpt, &ThreadUtilsObject::Test1upi, mozilla::Move(upi)); + } + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(1, rpt->mA0); + + { + mozilla::UniquePtr<int> upi = mozilla::MakeUnique<int>(1); + r1 = NewRunnableMethod + <StoreCopyPassByRRef<mozilla::UniquePtr<int>>> + (rpt, &ThreadUtilsObject::Test1upi, mozilla::Move(upi)); + } + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(1, rpt->mA0); + + // Unique pointer as prvalue. + r1 = NewRunnableMethod<mozilla::UniquePtr<int>&&>( + rpt, &ThreadUtilsObject::Test1upi, mozilla::MakeUnique<int>(2)); + r1->Run(); + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(2, rpt->mA0); + + // Unique pointer as lvalue to lref. + { + mozilla::UniquePtr<int> upi; + r1 = NewRunnableMethod<mozilla::UniquePtr<int>&>( + rpt, &ThreadUtilsObject::Test1rupi, upi); + // Passed as lref, so Run() must be called while local upi is still alive! + r1->Run(); + } + EXPECT_EQ(count += 2, rpt->mCount); + EXPECT_EQ(-1, rpt->mA0); + + // Verify copy/move assumptions. + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from lvalue, pass by value\n", __LINE__); } + { // Block around nsCOMPtr lifetime. + nsCOMPtr<nsIRunnable> r2; + { // Block around Spy lifetime. + if (gDebug) { printf("%d - Spy s(10)\n", __LINE__); } + Spy s(10); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + if (gDebug) { printf("%d - r2 = NewRunnableMethod<StoreCopyPassByValue<Spy>>(&TestByValue, s)\n", __LINE__); } + r2 = NewRunnableMethod<StoreCopyPassByValue<Spy>>( + rpt, &ThreadUtilsObject::TestByValue, s); + EXPECT_EQ(2, gAlive); + EXPECT_LE(1, gCopyConstructions); // At least 1 copy-construction. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with Spy s(10)\n", __LINE__); } + } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r2->Run(); + EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. + EXPECT_EQ(10, rpt->mSpy.mID); + EXPECT_LE(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from prvalue, pass by value\n", __LINE__); } + { + if (gDebug) { printf("%d - r3 = NewRunnableMethod<StoreCopyPassByValue<Spy>>(&TestByValue, Spy(11))\n", __LINE__); } + nsCOMPtr<nsIRunnable> r3 = + NewRunnableMethod<StoreCopyPassByValue<Spy>>( + rpt, &ThreadUtilsObject::TestByValue, Spy(11)); + EXPECT_EQ(1, gAlive); + EXPECT_EQ(1, gConstructions); + EXPECT_LE(1, gMoveConstructions); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r3->Run(); + EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. + EXPECT_EQ(11, rpt->mSpy.mID); + EXPECT_LE(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + { // Store copy from xvalue, pass by value. + nsCOMPtr<nsIRunnable> r4; + { + Spy s(12); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + r4 = NewRunnableMethod<StoreCopyPassByValue<Spy>>( + rpt, &ThreadUtilsObject::TestByValue, mozilla::Move(s)); + EXPECT_LE(1, gMoveConstructions); + EXPECT_EQ(1, gAlive); + EXPECT_EQ(1, gZombies); + Spy::ClearActions(); + } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(1, gAlive); + EXPECT_EQ(0, gZombies); + Spy::ClearActions(); + r4->Run(); + EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. + EXPECT_EQ(12, rpt->mSpy.mID); + EXPECT_LE(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + // Won't test xvalues anymore, prvalues are enough to verify all rvalues. + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from lvalue, pass by const lvalue ref\n", __LINE__); } + { // Block around nsCOMPtr lifetime. + nsCOMPtr<nsIRunnable> r5; + { // Block around Spy lifetime. + if (gDebug) { printf("%d - Spy s(20)\n", __LINE__); } + Spy s(20); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + if (gDebug) { printf("%d - r5 = NewRunnableMethod<StoreCopyPassByConstLRef<Spy>>(&TestByConstLRef, s)\n", __LINE__); } + r5 = NewRunnableMethod<StoreCopyPassByConstLRef<Spy>>( + rpt, &ThreadUtilsObject::TestByConstLRef, s); + EXPECT_EQ(2, gAlive); + EXPECT_LE(1, gCopyConstructions); // At least 1 copy-construction. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with Spy s(20)\n", __LINE__); } + } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r5->Run(); + EXPECT_EQ(0, gCopyConstructions); // No copies in call. + EXPECT_EQ(20, rpt->mSpy.mID); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from prvalue, pass by const lvalue ref\n", __LINE__); } + { + if (gDebug) { printf("%d - r6 = NewRunnableMethod<StoreCopyPassByConstLRef<Spy>>(&TestByConstLRef, Spy(21))\n", __LINE__); } + nsCOMPtr<nsIRunnable> r6 = + NewRunnableMethod<StoreCopyPassByConstLRef<Spy>>( + rpt, &ThreadUtilsObject::TestByConstLRef, Spy(21)); + EXPECT_EQ(1, gAlive); + EXPECT_EQ(1, gConstructions); + EXPECT_LE(1, gMoveConstructions); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r6->Run(); + EXPECT_EQ(0, gCopyConstructions); // No copies in call. + EXPECT_EQ(21, rpt->mSpy.mID); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from lvalue, pass by rvalue ref\n", __LINE__); } + { // Block around nsCOMPtr lifetime. + nsCOMPtr<nsIRunnable> r7; + { // Block around Spy lifetime. + if (gDebug) { printf("%d - Spy s(30)\n", __LINE__); } + Spy s(30); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + if (gDebug) { printf("%d - r7 = NewRunnableMethod<StoreCopyPassByRRef<Spy>>(&TestByRRef, s)\n", __LINE__); } + r7 = NewRunnableMethod<StoreCopyPassByRRef<Spy>>( + rpt, &ThreadUtilsObject::TestByRRef, s); + EXPECT_EQ(2, gAlive); + EXPECT_LE(1, gCopyConstructions); // At least 1 copy-construction. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with Spy s(30)\n", __LINE__); } + } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r7->Run(); + EXPECT_LE(1, gMoves); // Move in call. + EXPECT_EQ(30, rpt->mSpy.mID); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(0, gAlive); // Spy inside Test is not counted. + EXPECT_EQ(1, gZombies); // Our local spy should now be a zombie. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store copy from prvalue, pass by rvalue ref\n", __LINE__); } + { + if (gDebug) { printf("%d - r8 = NewRunnableMethod<StoreCopyPassByRRef<Spy>>(&TestByRRef, Spy(31))\n", __LINE__); } + nsCOMPtr<nsIRunnable> r8 = + NewRunnableMethod<StoreCopyPassByRRef<Spy>>( + rpt, &ThreadUtilsObject::TestByRRef, Spy(31)); + EXPECT_EQ(1, gAlive); + EXPECT_EQ(1, gConstructions); + EXPECT_LE(1, gMoveConstructions); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r8->Run(); + EXPECT_LE(1, gMoves); // Move in call. + EXPECT_EQ(31, rpt->mSpy.mID); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(0, gAlive); // Spy inside Test is not counted. + EXPECT_EQ(1, gZombies); // Our local spy should now be a zombie. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store lvalue ref, pass lvalue ref\n", __LINE__); } + { + if (gDebug) { printf("%d - Spy s(40)\n", __LINE__); } + Spy s(40); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - r9 = NewRunnableMethod<Spy&>(&TestByLRef, s)\n", __LINE__); } + nsCOMPtr<nsIRunnable> r9 = + NewRunnableMethod<Spy&>( + rpt, &ThreadUtilsObject::TestByLRef, s); + EXPECT_EQ(0, gAllConstructions); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r9->Run(); + EXPECT_LE(1, gAssignments); // Assignment from reference in call. + EXPECT_EQ(40, rpt->mSpy.mID); + EXPECT_EQ(&s, rpt->mSpyPtr); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); // Spy inside Test is not counted. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store nsRefPtr, pass by pointer\n", __LINE__); } + { // Block around nsCOMPtr lifetime. + nsCOMPtr<nsIRunnable> r10; + SpyWithISupports* ptr = 0; + { // Block around RefPtr<Spy> lifetime. + if (gDebug) { printf("%d - RefPtr<SpyWithISupports> s(new SpyWithISupports(45))\n", __LINE__); } + RefPtr<SpyWithISupports> s(new SpyWithISupports(45)); + ptr = s.get(); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + if (gDebug) { printf("%d - r10 = NewRunnableMethod<StorensRefPtrPassByPtr<Spy>>(&TestByRRef, s.get())\n", __LINE__); } + r10 = NewRunnableMethod<StorensRefPtrPassByPtr<SpyWithISupports>>( + rpt, &ThreadUtilsObject::TestByPointer, s.get()); + EXPECT_LE(0, gAllConstructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with RefPtr<Spy> s\n", __LINE__); } + } + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r10->Run(); + EXPECT_LE(1, gAssignments); // Assignment from pointee in call. + EXPECT_EQ(45, rpt->mSpy.mID); + EXPECT_EQ(ptr, rpt->mSpyPtr); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); // Spy inside Test is not counted. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store pointer to lvalue, pass by pointer\n", __LINE__); } + { + if (gDebug) { printf("%d - Spy s(55)\n", __LINE__); } + Spy s(55); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - r11 = NewRunnableMethod<Spy*>(&TestByPointer, s)\n", __LINE__); } + nsCOMPtr<nsIRunnable> r11 = + NewRunnableMethod<Spy*>( + rpt, &ThreadUtilsObject::TestByPointer, &s); + EXPECT_EQ(0, gAllConstructions); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r11->Run(); + EXPECT_LE(1, gAssignments); // Assignment from pointee in call. + EXPECT_EQ(55, rpt->mSpy.mID); + EXPECT_EQ(&s, rpt->mSpyPtr); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); // Spy inside Test is not counted. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); + + Spy::ClearAll(); + if (gDebug) { printf("%d - Test: Store pointer to const lvalue, pass by pointer\n", __LINE__); } + { + if (gDebug) { printf("%d - Spy s(60)\n", __LINE__); } + Spy s(60); + EXPECT_EQ(1, gConstructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - r12 = NewRunnableMethod<Spy*>(&TestByPointer, s)\n", __LINE__); } + nsCOMPtr<nsIRunnable> r12 = + NewRunnableMethod<const Spy*>( + rpt, &ThreadUtilsObject::TestByPointerToConst, &s); + EXPECT_EQ(0, gAllConstructions); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); + Spy::ClearActions(); + if (gDebug) { printf("%d - Run()\n", __LINE__); } + r12->Run(); + EXPECT_LE(1, gAssignments); // Assignment from pointee in call. + EXPECT_EQ(60, rpt->mSpy.mID); + EXPECT_EQ(&s, rpt->mSpyPtr); + EXPECT_EQ(0, gDestructions); + EXPECT_EQ(1, gAlive); // Spy inside Test is not counted. + Spy::ClearActions(); + if (gDebug) { printf("%d - End block with r\n", __LINE__); } + } + if (gDebug) { printf("%d - After end block with r\n", __LINE__); } + EXPECT_EQ(1, gDestructions); + EXPECT_EQ(0, gAlive); +#endif // XPCOM_GLUE_AVOID_NSPR +} diff --git a/xpcom/glue/tests/gtest/moz.build b/xpcom/glue/tests/gtest/moz.build new file mode 100644 index 000000000..9f4d83a3e --- /dev/null +++ b/xpcom/glue/tests/gtest/moz.build @@ -0,0 +1,22 @@ +# -*- Mode: python; 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 += [ + 'TestArray.cpp', + 'TestFileUtils.cpp', + 'TestGCPostBarriers.cpp', + 'TestNsDeque.cpp', + 'TestThreadUtils.cpp', +] + +LOCAL_INCLUDES = [ + '../..', +] + +FINAL_LIBRARY = 'xul-gtest' + +if CONFIG['GNU_CXX']: + CXXFLAGS += ['-Wno-error=shadow'] |