|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "mozilla/DebugOnly.h" |
|
6 |
|
7 #include "jsapi-tests/tests.h" |
|
8 |
|
9 BEGIN_TEST(testOOM) |
|
10 { |
|
11 JS::RootedValue v(cx, JS::Int32Value(9)); |
|
12 JS::RootedString jsstr(cx, JS::ToString(cx, v)); |
|
13 mozilla::DebugOnly<const jschar *> s = JS_GetStringCharsZ(cx, jsstr); |
|
14 JS_ASSERT(s[0] == '9' && s[1] == '\0'); |
|
15 return true; |
|
16 } |
|
17 |
|
18 virtual JSRuntime * createRuntime() |
|
19 { |
|
20 JSRuntime *rt = JS_NewRuntime(0, JS_USE_HELPER_THREADS); |
|
21 if (!rt) |
|
22 return nullptr; |
|
23 JS_SetGCParameter(rt, JSGC_MAX_BYTES, (uint32_t)-1); |
|
24 setNativeStackQuota(rt); |
|
25 return rt; |
|
26 } |
|
27 END_TEST(testOOM) |
|
28 |
|
29 #ifdef DEBUG // OOM_maxAllocations is only available in debug builds. |
|
30 |
|
31 const uint32_t maxAllocsPerTest = 100; |
|
32 |
|
33 #define START_OOM_TEST(name) \ |
|
34 testName = name; \ |
|
35 printf("Test %s: started\n", testName); \ |
|
36 for (oomAfter = 1; oomAfter < maxAllocsPerTest; ++oomAfter) { \ |
|
37 setOOMAfter(oomAfter) |
|
38 |
|
39 #define OOM_TEST_FINISHED \ |
|
40 { \ |
|
41 printf("Test %s: finished with %d allocations\n", \ |
|
42 testName, oomAfter - 1); \ |
|
43 break; \ |
|
44 } |
|
45 |
|
46 #define END_OOM_TEST \ |
|
47 } \ |
|
48 cancelOOMAfter(); \ |
|
49 CHECK(oomAfter != maxAllocsPerTest) |
|
50 |
|
51 BEGIN_TEST(testNewRuntime) |
|
52 { |
|
53 uninit(); // Get rid of test harness' original JSRuntime. |
|
54 |
|
55 JSRuntime *rt; |
|
56 START_OOM_TEST("new runtime"); |
|
57 rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS); |
|
58 if (rt) |
|
59 OOM_TEST_FINISHED; |
|
60 END_OOM_TEST; |
|
61 JS_DestroyRuntime(rt); |
|
62 return true; |
|
63 } |
|
64 |
|
65 const char* testName; |
|
66 uint32_t oomAfter; |
|
67 |
|
68 void |
|
69 setOOMAfter(uint32_t numAllocs) |
|
70 { |
|
71 OOM_maxAllocations = OOM_counter + numAllocs; |
|
72 } |
|
73 |
|
74 void |
|
75 cancelOOMAfter() |
|
76 { |
|
77 OOM_maxAllocations = UINT32_MAX; |
|
78 } |
|
79 END_TEST(testNewRuntime) |
|
80 |
|
81 #endif |