deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / test-typedarrays.cc
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdlib.h>
6
7 #include "src/v8.h"
8 #include "test/cctest/cctest.h"
9
10 #include "src/api.h"
11 #include "src/heap/heap.h"
12 #include "src/objects.h"
13 #include "src/v8.h"
14
15 using namespace v8::internal;
16
17 void TestArrayBufferViewContents(LocalContext& env, bool should_use_buffer) {
18   v8::Local<v8::Object> obj_a =
19       v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
20   CHECK(obj_a->IsArrayBufferView());
21   v8::Local<v8::ArrayBufferView> array_buffer_view =
22       v8::Local<v8::ArrayBufferView>::Cast(obj_a);
23   CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
24   unsigned char contents[4] = {23, 23, 23, 23};
25   CHECK_EQ(sizeof(contents),
26            array_buffer_view->CopyContents(contents, sizeof(contents)));
27   CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
28   for (size_t i = 0; i < sizeof(contents); ++i) {
29     CHECK_EQ(i, contents[i]);
30   }
31 }
32
33
34 TEST(CopyContentsTypedArray) {
35   LocalContext env;
36   v8::HandleScope scope(env->GetIsolate());
37   CompileRun(
38       "var a = new Uint8Array(4);"
39       "a[0] = 0;"
40       "a[1] = 1;"
41       "a[2] = 2;"
42       "a[3] = 3;");
43   TestArrayBufferViewContents(env, false);
44 }
45
46
47 TEST(CopyContentsArray) {
48   LocalContext env;
49   v8::HandleScope scope(env->GetIsolate());
50   CompileRun("var a = new Uint8Array([0, 1, 2, 3]);");
51   TestArrayBufferViewContents(env, true);
52 }
53
54
55 TEST(CopyContentsView) {
56   LocalContext env;
57   v8::HandleScope scope(env->GetIsolate());
58   CompileRun(
59       "var b = new ArrayBuffer(6);"
60       "var c = new Uint8Array(b);"
61       "c[0] = -1;"
62       "c[1] = -1;"
63       "c[2] = 0;"
64       "c[3] = 1;"
65       "c[4] = 2;"
66       "c[5] = 3;"
67       "var a = new DataView(b, 2);");
68   TestArrayBufferViewContents(env, true);
69 }
70
71
72 TEST(AllocateNotExternal) {
73   LocalContext env;
74   v8::HandleScope scope(env->GetIsolate());
75   void* memory = V8::ArrayBufferAllocator()->Allocate(1024);
76   v8::Local<v8::ArrayBuffer> buffer =
77       v8::ArrayBuffer::New(env->GetIsolate(), memory, 1024,
78                            v8::ArrayBufferCreationMode::kInternalized);
79   CHECK(!buffer->IsExternal());
80   CHECK_EQ(memory, buffer->GetContents().Data());
81 }