Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testArrayBufferWithUserOwnedContents.cpp
blob9a360e38be027c174de480dd01b01d9fb1041c59
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
5 #include <stdint.h> // uint32_t
7 #include "js/ArrayBuffer.h" // JS::{DetachArrayBuffer,GetArrayBuffer{ByteLength,Data},IsArrayBufferObject,NewArrayBufferWithUserOwnedContents}
8 #include "js/GCAPI.h" // JS::AutoCheckCannotGC, JS_GC
9 #include "js/RootingAPI.h" // JS::Rooted
10 #include "jsapi-tests/tests.h"
11 #include "vm/ArrayBufferObject.h"
13 char testData[] =
14 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
16 constexpr size_t testDataLength = sizeof(testData);
18 static void GC(JSContext* cx) {
19 JS_GC(cx);
20 // Trigger another to wait for background finalization to end.
21 JS_GC(cx);
24 BEGIN_TEST(testArrayBufferWithUserOwnedContents) {
25 JS::Rooted<JSObject*> obj(cx, JS::NewArrayBufferWithUserOwnedContents(
26 cx, testDataLength, testData));
27 GC(cx);
28 CHECK(VerifyObject(obj, testDataLength));
29 GC(cx);
30 JS::DetachArrayBuffer(cx, obj);
31 GC(cx);
32 CHECK(VerifyObject(obj, 0));
34 return true;
37 bool VerifyObject(JS::HandleObject obj, uint32_t length) {
38 JS::AutoCheckCannotGC nogc;
40 CHECK(obj);
41 CHECK(JS::IsArrayBufferObject(obj));
42 CHECK_EQUAL(JS::GetArrayBufferByteLength(obj), length);
43 bool sharedDummy;
44 const char* data = reinterpret_cast<const char*>(
45 JS::GetArrayBufferData(obj, &sharedDummy, nogc));
46 if (length == testDataLength) {
47 CHECK(data);
48 CHECK(testData == data);
51 return true;
54 END_TEST(testArrayBufferWithUserOwnedContents)