<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<canvas id="canvas" width="40" height="40"></canvas>
<script>
description("Make sure that reading and writing WebGL2 buffers works as expected.");

var arrayBuffer = new ArrayBuffer(40);
var fullArrayView = new Float32Array(arrayBuffer); // [15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 40 bytes
var partialArrayView = new Float32Array(arrayBuffer, 4, 5); // [16, 17, 18, 19, 20], 20 bytes
var receiver = new Float32Array(20); // 20 floats, 80 bytes
for (var i = 0; i < 10; ++i)
	fullArrayView[i] = 15 + i;

var sentinel = 100;
function fillReceiver() {
	for (var i = 0; i < receiver.length; ++i)
		receiver[i] = sentinel;
}

var canvas = document.getElementById("canvas");
var context = canvas.getContext("webgl2");

runTests(context.ARRAY_BUFFER);
runTests(context.COPY_READ_BUFFER);
runTests(context.COPY_WRITE_BUFFER);
runTests(context.ELEMENT_ARRAY_BUFFER);
runTests(context.PIXEL_PACK_BUFFER);
runTests(context.PIXEL_UNPACK_BUFFER);
runTests(context.TRANSFORM_FEEDBACK_BUFFER);
runTests(context.UNIFORM_BUFFER);

function runTests(target) {
	var buffer = context.createBuffer();
	shouldBe("context.getError()", "context.NO_ERROR");
	context.bindBuffer(target, buffer);
	shouldBe("context.getError()", "context.NO_ERROR");



	context.bufferData(target, arrayBuffer, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 10);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + i + "]");

	fillReceiver();
	context.bufferData(target, arrayBuffer, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 10);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + i + "]");

	context.bufferData(target, arrayBuffer, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 9);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 9; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferData(target, arrayBuffer, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 8);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 8; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferData(target, partialArrayView, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferData(target, partialArrayView, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 3) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 3) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 3) + "]");

	fillReceiver();
	context.bufferData(target, partialArrayView, context.STATIC_DRAW, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 2);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 2; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 4) + "]");



	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	// Give the buffer a size which is big enough to hold the entire buffer
	fillReceiver();
	context.bufferData(target, receiver, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");



	context.bufferSubData(target, 0, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 10);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + i + "]");

	fillReceiver();
	context.bufferSubData(target, 0, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 10);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + i + "]");

	context.bufferSubData(target, 0, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 9);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 9; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 8);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 8; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 0, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferSubData(target, 0, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 3) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 0, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferSubData(target, 0, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 3) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 0, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	context.bufferSubData(target, 0, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 3) + "]");

	fillReceiver();
	context.bufferSubData(target, 0, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 2);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 2; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 4) + "]");



	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	// Give the buffer a size which is big enough to hold the entire buffer
	fillReceiver();
	context.bufferData(target, receiver, context.STATIC_DRAW);
	shouldBe("context.getError()", "context.NO_ERROR");



	context.bufferSubData(target, 4, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 11);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + i + "]");

	fillReceiver();
	context.bufferSubData(target, 4, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 11);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	shouldBe("receiver[1]", "sentinel");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + (i + 2) + "]", "fullArrayView[" + i + "]");

	context.bufferSubData(target, 4, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 10);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 10; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + i + "]");

	fillReceiver();
	context.bufferSubData(target, 4, arrayBuffer);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 8);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 8; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 4, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 6);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 6);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	shouldBe("receiver[1]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 2) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferSubData(target, 4, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 4, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 6);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 6);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	shouldBe("receiver[1]", "sentinel");
	for (var i = 0; i < 5; ++i)
		shouldBe("receiver[" + (i + 2) + "]", "fullArrayView[" + (i + 1) + "]");

	context.bufferSubData(target, 4, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 1) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	////////////////////////////////////////////////////////////////////////////////////////////////

	context.bufferSubData(target, 4, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 0, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 0, receiver, 1, 5);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	shouldBe("receiver[1]", "sentinel");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + (i + 2) + "]", "fullArrayView[" + (i + 2) + "]");

	context.bufferSubData(target, 4, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 4, receiver, 0, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	for (var i = 0; i < 4; ++i)
		shouldBe("receiver[" + i + "]", "fullArrayView[" + (i + 2) + "]");

	fillReceiver();
	context.bufferSubData(target, 4, partialArrayView, 1, 4);
	shouldBe("context.getError()", "context.NO_ERROR");
	context.getBufferSubData(target, 8, receiver, 1, 3);
	shouldBe("context.getError()", "context.NO_ERROR");
	shouldBe("receiver[0]", "sentinel");
	for (var i = 0; i < 3; ++i)
		shouldBe("receiver[" + (i + 1) + "]", "fullArrayView[" + (i + 3) + "]");



	context.deleteBuffer(buffer);
	shouldBe("context.getError()", "context.NO_ERROR");
}
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>