Browse Source

test: add buffer alignment regression tests

Buffers instances can have arbitrary alignment. `node-ffi` depends on
this. Add some regression tests to ensure we don't break this in the
future.

PR-URL: https://github.com/nodejs/node/pull/5752
Reviewed-By: trevnorris - Trevor Norris <trev.norris@gmail.com>
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
process-exit-stdio-flushing
Anna Henningsen 9 years ago
committed by Ali Ijaz Sheikh
parent
commit
be97db92af
  1. 9
      test/addons/buffer-free-callback/binding.cc
  2. 21
      test/addons/buffer-free-callback/test.js

9
test/addons/buffer-free-callback/binding.cc

@ -13,9 +13,16 @@ static void FreeCallback(char* data, void* hint) {
void Alloc(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
alive++;
uintptr_t alignment = args[1]->IntegerValue();
uintptr_t offset = args[2]->IntegerValue();
uintptr_t static_offset = reinterpret_cast<uintptr_t>(buf) % alignment;
char* aligned = buf + (alignment - static_offset) + offset;
args.GetReturnValue().Set(node::Buffer::New(
isolate,
buf,
aligned,
args[0]->IntegerValue(),
FreeCallback,
nullptr).ToLocalChecked());

21
test/addons/buffer-free-callback/test.js

@ -4,8 +4,8 @@
require('../../common');
var binding = require('./build/Release/binding');
function check(size) {
var buf = binding.alloc(size);
function check(size, alignment, offset) {
var buf = binding.alloc(size, alignment, offset);
var slice = buf.slice(size >>> 1);
buf = null;
@ -16,7 +16,20 @@ function check(size) {
gc();
}
check(64);
check(64, 1, 0);
// Buffers can have weird sizes.
check(97, 1, 0);
// Buffers can be unaligned
check(64, 8, 0);
check(64, 16, 0);
check(64, 8, 1);
check(64, 16, 1);
check(97, 8, 1);
check(97, 16, 1);
check(97, 8, 3);
check(97, 16, 3);
// Empty ArrayBuffer does not allocate data, worth checking
check(0);
check(0, 1, 0);

Loading…
Cancel
Save