js/src/jsapi-tests/testOOM.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testOOM.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "mozilla/DebugOnly.h"
     1.9 +
    1.10 +#include "jsapi-tests/tests.h"
    1.11 +
    1.12 +BEGIN_TEST(testOOM)
    1.13 +{
    1.14 +    JS::RootedValue v(cx, JS::Int32Value(9));
    1.15 +    JS::RootedString jsstr(cx, JS::ToString(cx, v));
    1.16 +    mozilla::DebugOnly<const jschar *> s = JS_GetStringCharsZ(cx, jsstr);
    1.17 +    JS_ASSERT(s[0] == '9' && s[1] == '\0');
    1.18 +    return true;
    1.19 +}
    1.20 +
    1.21 +virtual JSRuntime * createRuntime()
    1.22 +{
    1.23 +    JSRuntime *rt = JS_NewRuntime(0, JS_USE_HELPER_THREADS);
    1.24 +    if (!rt)
    1.25 +        return nullptr;
    1.26 +    JS_SetGCParameter(rt, JSGC_MAX_BYTES, (uint32_t)-1);
    1.27 +    setNativeStackQuota(rt);
    1.28 +    return rt;
    1.29 +}
    1.30 +END_TEST(testOOM)
    1.31 +
    1.32 +#ifdef DEBUG  // OOM_maxAllocations is only available in debug builds.
    1.33 +
    1.34 +const uint32_t maxAllocsPerTest = 100;
    1.35 +
    1.36 +#define START_OOM_TEST(name)                                                  \
    1.37 +    testName = name;                                                          \
    1.38 +    printf("Test %s: started\n", testName);                                   \
    1.39 +    for (oomAfter = 1; oomAfter < maxAllocsPerTest; ++oomAfter) {             \
    1.40 +        setOOMAfter(oomAfter)
    1.41 +
    1.42 +#define OOM_TEST_FINISHED                                                     \
    1.43 +    {                                                                         \
    1.44 +        printf("Test %s: finished with %d allocations\n",                     \
    1.45 +               testName, oomAfter - 1);                                       \
    1.46 +        break;                                                                \
    1.47 +    }
    1.48 +
    1.49 +#define END_OOM_TEST                                                          \
    1.50 +    }                                                                         \
    1.51 +    cancelOOMAfter();                                                         \
    1.52 +    CHECK(oomAfter != maxAllocsPerTest)
    1.53 +
    1.54 +BEGIN_TEST(testNewRuntime)
    1.55 +{
    1.56 +    uninit(); // Get rid of test harness' original JSRuntime.
    1.57 +
    1.58 +    JSRuntime *rt;
    1.59 +    START_OOM_TEST("new runtime");
    1.60 +    rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS);
    1.61 +    if (rt)
    1.62 +        OOM_TEST_FINISHED;
    1.63 +    END_OOM_TEST;
    1.64 +    JS_DestroyRuntime(rt);
    1.65 +    return true;
    1.66 +}
    1.67 +
    1.68 +const char* testName;
    1.69 +uint32_t oomAfter;
    1.70 +
    1.71 +void
    1.72 +setOOMAfter(uint32_t numAllocs)
    1.73 +{
    1.74 +    OOM_maxAllocations = OOM_counter + numAllocs;
    1.75 +}
    1.76 +
    1.77 +void
    1.78 +cancelOOMAfter()
    1.79 +{
    1.80 +    OOM_maxAllocations = UINT32_MAX;
    1.81 +}
    1.82 +END_TEST(testNewRuntime)
    1.83 +
    1.84 +#endif

mercurial