michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: BEGIN_TEST(testOOM) michael@0: { michael@0: JS::RootedValue v(cx, JS::Int32Value(9)); michael@0: JS::RootedString jsstr(cx, JS::ToString(cx, v)); michael@0: mozilla::DebugOnly s = JS_GetStringCharsZ(cx, jsstr); michael@0: JS_ASSERT(s[0] == '9' && s[1] == '\0'); michael@0: return true; michael@0: } michael@0: michael@0: virtual JSRuntime * createRuntime() michael@0: { michael@0: JSRuntime *rt = JS_NewRuntime(0, JS_USE_HELPER_THREADS); michael@0: if (!rt) michael@0: return nullptr; michael@0: JS_SetGCParameter(rt, JSGC_MAX_BYTES, (uint32_t)-1); michael@0: setNativeStackQuota(rt); michael@0: return rt; michael@0: } michael@0: END_TEST(testOOM) michael@0: michael@0: #ifdef DEBUG // OOM_maxAllocations is only available in debug builds. michael@0: michael@0: const uint32_t maxAllocsPerTest = 100; michael@0: michael@0: #define START_OOM_TEST(name) \ michael@0: testName = name; \ michael@0: printf("Test %s: started\n", testName); \ michael@0: for (oomAfter = 1; oomAfter < maxAllocsPerTest; ++oomAfter) { \ michael@0: setOOMAfter(oomAfter) michael@0: michael@0: #define OOM_TEST_FINISHED \ michael@0: { \ michael@0: printf("Test %s: finished with %d allocations\n", \ michael@0: testName, oomAfter - 1); \ michael@0: break; \ michael@0: } michael@0: michael@0: #define END_OOM_TEST \ michael@0: } \ michael@0: cancelOOMAfter(); \ michael@0: CHECK(oomAfter != maxAllocsPerTest) michael@0: michael@0: BEGIN_TEST(testNewRuntime) michael@0: { michael@0: uninit(); // Get rid of test harness' original JSRuntime. michael@0: michael@0: JSRuntime *rt; michael@0: START_OOM_TEST("new runtime"); michael@0: rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS); michael@0: if (rt) michael@0: OOM_TEST_FINISHED; michael@0: END_OOM_TEST; michael@0: JS_DestroyRuntime(rt); michael@0: return true; michael@0: } michael@0: michael@0: const char* testName; michael@0: uint32_t oomAfter; michael@0: michael@0: void michael@0: setOOMAfter(uint32_t numAllocs) michael@0: { michael@0: OOM_maxAllocations = OOM_counter + numAllocs; michael@0: } michael@0: michael@0: void michael@0: cancelOOMAfter() michael@0: { michael@0: OOM_maxAllocations = UINT32_MAX; michael@0: } michael@0: END_TEST(testNewRuntime) michael@0: michael@0: #endif