From 87f3f5af2e03e1291019701670b80287d3c368db Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 10 May 2018 22:18:36 +0200 Subject: [PATCH] test: plug AliasedBuffer cctest memory leak No need to heap-allocate values here. PR-URL: https://github.com/nodejs/node/pull/20665 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Daniel Bevenius --- test/cctest/test_aliased_buffer.cc | 79 ++++++++++++++---------------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/test/cctest/test_aliased_buffer.cc b/test/cctest/test_aliased_buffer.cc index 7afa466133b757..b2f29d3b7cb1ab 100644 --- a/test/cctest/test_aliased_buffer.cc +++ b/test/cctest/test_aliased_buffer.cc @@ -8,28 +8,26 @@ using node::AliasedBuffer; class AliasBufferTest : public NodeTestFixture {}; template -void CreateOracleValues(NativeT* buf, size_t count) { - for (size_t i = 0, j = count; i < count; i++, j--) { - buf[i] = static_cast(j); +void CreateOracleValues(std::vector* buf) { + for (size_t i = 0, j = buf->size(); i < buf->size(); i++, j--) { + (*buf)[i] = static_cast(j); } } template void WriteViaOperator(AliasedBuffer* aliasedBuffer, - size_t size, - NativeT* oracle) { + const std::vector& oracle) { // write through the API - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < oracle.size(); i++) { (*aliasedBuffer)[i] = oracle[i]; } } template void WriteViaSetValue(AliasedBuffer* aliasedBuffer, - size_t size, - NativeT* oracle) { + const std::vector& oracle) { // write through the API - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < oracle.size(); i++) { aliasedBuffer->SetValue(i, oracle[i]); } } @@ -38,10 +36,9 @@ template void ReadAndValidate(v8::Isolate* isolate, v8::Local context, AliasedBuffer* aliasedBuffer, - size_t size, - NativeT* oracle) { + const std::vector& oracle) { // read through the API - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < oracle.size(); i++) { NativeT v1 = (*aliasedBuffer)[i]; NativeT v2 = aliasedBuffer->GetValue(i); EXPECT_TRUE(v1 == oracle[i]); @@ -49,16 +46,16 @@ void ReadAndValidate(v8::Isolate* isolate, } // validate size of JS Buffer - EXPECT_TRUE(aliasedBuffer->GetJSArray()->Length() == size); + EXPECT_TRUE(aliasedBuffer->GetJSArray()->Length() == oracle.size()); EXPECT_TRUE( aliasedBuffer->GetJSArray()->ByteLength() == - (size * sizeof(NativeT))); + (oracle.size() * sizeof(NativeT))); // validate operator * and GetBuffer are the same EXPECT_TRUE(aliasedBuffer->GetNativeBuffer() == *(*aliasedBuffer)); // read through the JS API - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < oracle.size(); i++) { v8::Local v8TypedArray = aliasedBuffer->GetJSArray(); v8::MaybeLocal v = v8TypedArray->Get(context, i); EXPECT_TRUE(v.IsEmpty() == false); @@ -80,21 +77,19 @@ void ReadWriteTest(v8::Isolate* isolate) { const size_t size = 100; AliasedBuffer ab(isolate, size); - NativeT* oracle = new NativeT[size]; - CreateOracleValues(oracle, size); - WriteViaOperator(&ab, size, oracle); - ReadAndValidate(isolate, context, &ab, size, oracle); + std::vector oracle(size); + CreateOracleValues(&oracle); + WriteViaOperator(&ab, oracle); + ReadAndValidate(isolate, context, &ab, oracle); - WriteViaSetValue(&ab, size, oracle); + WriteViaSetValue(&ab, oracle); // validate copy constructor { AliasedBuffer ab2(ab); - ReadAndValidate(isolate, context, &ab2, size, oracle); + ReadAndValidate(isolate, context, &ab2, oracle); } - ReadAndValidate(isolate, context, &ab, size, oracle); - - delete[] oracle; + ReadAndValidate(isolate, context, &ab, oracle); } template< @@ -124,28 +119,28 @@ void SharedBufferTest( AliasedBuffer ab_C( isolate, sizeInBytes_A + sizeInBytes_B, count_C, rootBuffer); - NativeT_A* oracle_A = new NativeT_A[count_A]; - NativeT_B* oracle_B = new NativeT_B[count_B]; - NativeT_C* oracle_C = new NativeT_C[count_C]; - CreateOracleValues(oracle_A, count_A); - CreateOracleValues(oracle_B, count_B); - CreateOracleValues(oracle_C, count_C); + std::vector oracle_A(count_A); + std::vector oracle_B(count_B); + std::vector oracle_C(count_C); + CreateOracleValues(&oracle_A); + CreateOracleValues(&oracle_B); + CreateOracleValues(&oracle_C); - WriteViaOperator(&ab_A, count_A, oracle_A); - WriteViaOperator(&ab_B, count_B, oracle_B); - WriteViaOperator(&ab_C, count_C, oracle_C); + WriteViaOperator(&ab_A, oracle_A); + WriteViaOperator(&ab_B, oracle_B); + WriteViaOperator(&ab_C, oracle_C); - ReadAndValidate(isolate, context, &ab_A, count_A, oracle_A); - ReadAndValidate(isolate, context, &ab_B, count_B, oracle_B); - ReadAndValidate(isolate, context, &ab_C, count_C, oracle_C); + ReadAndValidate(isolate, context, &ab_A, oracle_A); + ReadAndValidate(isolate, context, &ab_B, oracle_B); + ReadAndValidate(isolate, context, &ab_C, oracle_C); - WriteViaSetValue(&ab_A, count_A, oracle_A); - WriteViaSetValue(&ab_B, count_B, oracle_B); - WriteViaSetValue(&ab_C, count_C, oracle_C); + WriteViaSetValue(&ab_A, oracle_A); + WriteViaSetValue(&ab_B, oracle_B); + WriteViaSetValue(&ab_C, oracle_C); - ReadAndValidate(isolate, context, &ab_A, count_A, oracle_A); - ReadAndValidate(isolate, context, &ab_B, count_B, oracle_B); - ReadAndValidate(isolate, context, &ab_C, count_C, oracle_C); + ReadAndValidate(isolate, context, &ab_A, oracle_A); + ReadAndValidate(isolate, context, &ab_B, oracle_B); + ReadAndValidate(isolate, context, &ab_C, oracle_C); } TEST_F(AliasBufferTest, Uint8Array) {