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>
This commit is contained in:
Anna Henningsen 2016-03-17 02:56:15 +01:00 committed by Ali Ijaz Sheikh
parent 73fc440870
commit be97db92af
2 changed files with 25 additions and 5 deletions

View File

@ -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());

View File

@ -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);