GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../test/cctest/test_aliased_buffer.cc Lines: 137 137 100.0 %
Date: 2019-01-07 12:15:22 Branches: 169 258 65.5 %

Line Branch Exec Source
1
2
#include "v8.h"
3
#include "aliased_buffer.h"
4
#include "node_test_fixture.h"
5
6
using node::AliasedBuffer;
7
8
28
class AliasBufferTest : public NodeTestFixture {};
9
10
template <class NativeT>
11
20
void CreateOracleValues(std::vector<NativeT>* buf) {
12




1145
  for (size_t i = 0, j = buf->size(); i < buf->size(); i++, j--) {
13
1125
    (*buf)[i] = static_cast<NativeT>(j);
14
  }
15
20
}
16
17
template <class NativeT, class V8T>
18
20
void WriteViaOperator(AliasedBuffer<NativeT, V8T>* aliasedBuffer,
19
                      const std::vector<NativeT>& oracle) {
20
  // write through the API
21




1145
  for (size_t i = 0; i < oracle.size(); i++) {
22
1125
    (*aliasedBuffer)[i] = oracle[i];
23
  }
24
20
}
25
26
template <class NativeT, class V8T>
27
20
void WriteViaSetValue(AliasedBuffer<NativeT, V8T>* aliasedBuffer,
28
                      const std::vector<NativeT>& oracle) {
29
  // write through the API
30




1145
  for (size_t i = 0; i < oracle.size(); i++) {
31
1125
    aliasedBuffer->SetValue(i, oracle[i]);
32
  }
33
20
}
34
35
template <class NativeT, class V8T>
36
48
void ReadAndValidate(v8::Isolate* isolate,
37
                     v8::Local<v8::Context> context,
38
                     AliasedBuffer<NativeT, V8T>* aliasedBuffer,
39
                     const std::vector<NativeT>& oracle) {
40
  // read through the API
41




3098
  for (size_t i = 0; i < oracle.size(); i++) {
42
3050
    NativeT v1 = (*aliasedBuffer)[i];
43
3050
    NativeT v2 = aliasedBuffer->GetValue(i);
44




3050
    EXPECT_TRUE(v1 == oracle[i]);
45




3050
    EXPECT_TRUE(v2 == oracle[i]);
46
  }
47
48
  // validate size of JS Buffer
49




96
  EXPECT_TRUE(aliasedBuffer->GetJSArray()->Length() == oracle.size());
50




96
  EXPECT_TRUE(
51
    aliasedBuffer->GetJSArray()->ByteLength() ==
52
    (oracle.size() * sizeof(NativeT)));
53
54
  // validate operator * and GetBuffer are the same
55




48
  EXPECT_TRUE(aliasedBuffer->GetNativeBuffer() == *(*aliasedBuffer));
56
57
  // read through the JS API
58




3098
  for (size_t i = 0; i < oracle.size(); i++) {
59
3050
    v8::Local<V8T> v8TypedArray = aliasedBuffer->GetJSArray();
60
6100
    v8::MaybeLocal<v8::Value> v = v8TypedArray->Get(context, i);
61




3050
    EXPECT_TRUE(v.IsEmpty() == false);
62
3050
    v8::Local<v8::Value> v2 = v.ToLocalChecked();
63




3050
    EXPECT_TRUE(v2->IsNumber());
64
3050
    v8::MaybeLocal<v8::Number> v3 = v2->ToNumber(context);
65
3050
    v8::Local<v8::Number> v4 = v3.ToLocalChecked();
66
3050
    NativeT actualValue = static_cast<NativeT>(v4->Value());
67




3050
    EXPECT_TRUE(actualValue == oracle[i]);
68
  }
69
48
}
70
71
template <class NativeT, class V8T>
72
8
void ReadWriteTest(v8::Isolate* isolate) {
73
8
  v8::Isolate::Scope isolate_scope(isolate);
74
16
  v8::HandleScope handle_scope(isolate);
75
16
  v8::Local<v8::Context> context = v8::Context::New(isolate);
76
  v8::Context::Scope context_scope(context);
77
78
8
  const size_t size = 100;
79
16
  AliasedBuffer<NativeT, V8T> ab(isolate, size);
80
16
  std::vector<NativeT> oracle(size);
81
8
  CreateOracleValues(&oracle);
82
8
  WriteViaOperator(&ab, oracle);
83
8
  ReadAndValidate(isolate, context, &ab, oracle);
84
85
8
  WriteViaSetValue(&ab, oracle);
86
87
  // validate copy constructor
88
  {
89
8
    AliasedBuffer<NativeT, V8T> ab2(ab);
90
8
    ReadAndValidate(isolate, context, &ab2, oracle);
91
  }
92
16
  ReadAndValidate(isolate, context, &ab, oracle);
93
8
}
94
95
template <
96
    class NativeT_A, class V8T_A,
97
    class NativeT_B, class V8T_B,
98
    class NativeT_C, class V8T_C>
99
4
void SharedBufferTest(
100
    v8::Isolate* isolate,
101
    size_t count_A,
102
    size_t count_B,
103
    size_t count_C) {
104
4
  v8::Isolate::Scope isolate_scope(isolate);
105
8
  v8::HandleScope handle_scope(isolate);
106
8
  v8::Local<v8::Context> context = v8::Context::New(isolate);
107
  v8::Context::Scope context_scope(context);
108
109
4
  size_t sizeInBytes_A = count_A * sizeof(NativeT_A);
110
4
  size_t sizeInBytes_B = count_B * sizeof(NativeT_B);
111
4
  size_t sizeInBytes_C = count_C * sizeof(NativeT_C);
112
113
  AliasedBuffer<uint8_t, v8::Uint8Array> rootBuffer(
114
8
      isolate, sizeInBytes_A + sizeInBytes_B + sizeInBytes_C);
115
  AliasedBuffer<NativeT_A, V8T_A> ab_A(
116
8
      isolate, 0, count_A, rootBuffer);
117
  AliasedBuffer<NativeT_B, V8T_B> ab_B(
118
8
      isolate, sizeInBytes_A, count_B, rootBuffer);
119
  AliasedBuffer<NativeT_C, V8T_C> ab_C(
120
8
      isolate, sizeInBytes_A + sizeInBytes_B, count_C, rootBuffer);
121
122
8
  std::vector<NativeT_A> oracle_A(count_A);
123
8
  std::vector<NativeT_B> oracle_B(count_B);
124
8
  std::vector<NativeT_C> oracle_C(count_C);
125
4
  CreateOracleValues(&oracle_A);
126
4
  CreateOracleValues(&oracle_B);
127
4
  CreateOracleValues(&oracle_C);
128
129
4
  WriteViaOperator(&ab_A, oracle_A);
130
4
  WriteViaOperator(&ab_B, oracle_B);
131
4
  WriteViaOperator(&ab_C, oracle_C);
132
133
4
  ReadAndValidate(isolate, context, &ab_A, oracle_A);
134
4
  ReadAndValidate(isolate, context, &ab_B, oracle_B);
135
4
  ReadAndValidate(isolate, context, &ab_C, oracle_C);
136
137
4
  WriteViaSetValue(&ab_A, oracle_A);
138
4
  WriteViaSetValue(&ab_B, oracle_B);
139
4
  WriteViaSetValue(&ab_C, oracle_C);
140
141
4
  ReadAndValidate(isolate, context, &ab_A, oracle_A);
142
4
  ReadAndValidate(isolate, context, &ab_B, oracle_B);
143
8
  ReadAndValidate(isolate, context, &ab_C, oracle_C);
144
4
}
145
146
5
TEST_F(AliasBufferTest, Uint8Array) {
147
1
  ReadWriteTest<uint8_t, v8::Uint8Array>(isolate_);
148
1
}
149
150
5
TEST_F(AliasBufferTest, Int8Array) {
151
1
  ReadWriteTest<int8_t, v8::Int8Array>(isolate_);
152
1
}
153
154
5
TEST_F(AliasBufferTest, Uint16Array) {
155
1
  ReadWriteTest<uint16_t, v8::Uint16Array>(isolate_);
156
1
}
157
158
5
TEST_F(AliasBufferTest, Int16Array) {
159
1
  ReadWriteTest<int16_t, v8::Int16Array>(isolate_);
160
1
}
161
162
5
TEST_F(AliasBufferTest, Uint32Array) {
163
1
  ReadWriteTest<uint32_t, v8::Uint32Array>(isolate_);
164
1
}
165
166
5
TEST_F(AliasBufferTest, Int32Array) {
167
1
  ReadWriteTest<int32_t, v8::Int32Array>(isolate_);
168
1
}
169
170
5
TEST_F(AliasBufferTest, Float32Array) {
171
1
  ReadWriteTest<float, v8::Float32Array>(isolate_);
172
1
}
173
174
5
TEST_F(AliasBufferTest, Float64Array) {
175
1
  ReadWriteTest<double, v8::Float64Array>(isolate_);
176
1
}
177
178
5
TEST_F(AliasBufferTest, SharedArrayBuffer1) {
179
  SharedBufferTest<
180
      uint32_t, v8::Uint32Array,
181
      double, v8::Float64Array,
182
1
      int8_t, v8::Int8Array>(isolate_, 100, 80, 8);
183
1
}
184
185
5
TEST_F(AliasBufferTest, SharedArrayBuffer2) {
186
  SharedBufferTest<
187
      double, v8::Float64Array,
188
      int8_t, v8::Int8Array,
189
1
      double, v8::Float64Array>(isolate_, 100, 8, 8);
190
1
}
191
192
5
TEST_F(AliasBufferTest, SharedArrayBuffer3) {
193
  SharedBufferTest<
194
      int8_t, v8::Int8Array,
195
      int8_t, v8::Int8Array,
196
1
      double, v8::Float64Array>(isolate_, 1, 7, 8);
197
1
}
198
199
5
TEST_F(AliasBufferTest, SharedArrayBuffer4) {
200
  SharedBufferTest<
201
      int8_t, v8::Int8Array,
202
      int8_t, v8::Int8Array,
203
1
      int32_t, v8::Int32Array>(isolate_, 1, 3, 1);
204
1
}
205
206
5
TEST_F(AliasBufferTest, OperatorOverloads) {
207
1
  v8::Isolate::Scope isolate_scope(isolate_);
208
2
  v8::HandleScope handle_scope(isolate_);
209
2
  v8::Local<v8::Context> context = v8::Context::New(isolate_);
210
  v8::Context::Scope context_scope(context);
211
1
  const size_t size = 10;
212
2
  AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, size};
213
214
1
  EXPECT_EQ(static_cast<uint32_t>(1), ab[0] = 1);
215
1
  EXPECT_EQ(static_cast<uint32_t>(4), ab[0] += 3);
216
1
  EXPECT_EQ(static_cast<uint32_t>(2), ab[0] -= 2);
217
2
  EXPECT_EQ(static_cast<uint32_t>(-2), -ab[0]);
218
1
}
219
220
5
TEST_F(AliasBufferTest, OperatorOverloadsRefs) {
221
1
  v8::Isolate::Scope isolate_scope(isolate_);
222
2
  v8::HandleScope handle_scope(isolate_);
223
2
  v8::Local<v8::Context> context = v8::Context::New(isolate_);
224
  v8::Context::Scope context_scope(context);
225
2
  AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, 2};
226
  using Reference = AliasedBuffer<uint32_t, v8::Uint32Array>::Reference;
227
1
  Reference ref = ab[0];
228
1
  Reference ref_value = ab[1] = 2;
229
230
1
  EXPECT_EQ(static_cast<uint32_t>(2), ref = ref_value);
231
1
  EXPECT_EQ(static_cast<uint32_t>(4), ref += ref_value);
232
1
  EXPECT_EQ(static_cast<uint32_t>(2), ref -= ref_value);
233
2
  EXPECT_EQ(static_cast<uint32_t>(-2), -ref);
234

4
}