<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<link rel="stylesheet" type="text/css" href="../unit.css" />
<script type="application/javascript" src="../unit.js"></script>
<script type="application/javascript" src="../util.js"></script>

<script type="application/javascript">

function assertIdxs(name, arr, length) {
//   assertOk(name+": Read with negative idx should work", function(){ return arr[-1] });
//   assertOk(name+": Read with too large idx should work", function(){ return arr[length] });
//   assertOk(name+": Write with negative idx should work", function(){ arr[-1] = 0 });
//   assertOk(name+": Write with too large idx should work", function(){ arr[length] = 0 });
//   arr[0] = 2;
//   assertEquals(name+": Test that write worked", 2, arr[0]);
//   assertOk(name+": Write with bad value should work", function(){ arr[0] = {x:"foo"} });
//   assertEquals(name+": Test that bad write didn't work", 2, arr[0]);
  assertOk(name+": Read and writes with OK idxs should work", function(){
    for (var i=0; i<length; i++) arr[i] = i + 1;
    for (var i=0; i<length; i++) arr[i] = arr[i] + 1;
    for (var i=0; i<length; i++) assertEquals(name+": Test that reads and writes work", i+2, arr[i]);
  });
}

Tests.startUnit = function () {
  var canvas = document.getElementById('gl');
  var gl = wrapGLContext(getGLContext(canvas));
  prog = new Shader(gl, 'vert', 'frag');
  prog.use();
  prog.uniform4f('c', 255, 0, 0, 255);
  va = prog.attrib('Vertex');
  buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  return [gl];
}

Tests.endUnit = function() {
  prog.destroy();
}

Tests.testCreateFromArray = function() {
  var a = new Float32Array([1,2,3,4,5,6]);
  assertIdxs('Float', a, 6);
  var a = new Int32Array([1,2,3,4,5,6]);
  assertIdxs('Int', a, 6);
  var a = new Int16Array([1,2,3,4,5,6]);
  assertIdxs('Short', a, 6);
  var a = new Int8Array([1,2,3,4,5,6]);
  assertIdxs('Byte', a, 6);
  var a = new Uint32Array([1,2,3,4,5,6]);
  assertIdxs('UInt', a, 6);
  var a = new Uint16Array([1,2,3,4,5,6]);
  assertIdxs('UShort', a, 6);
  var a = new Uint8Array([1,2,3,4,5,6]);
  assertIdxs('UByte', a, 6);
}
Tests.testCreateFromCount = function() {
  var a = new Float32Array(6);
  assertIdxs('Float', a, 6);
  var a = new Int32Array(6);
  assertIdxs('Int', a, 6);
  var a = new Int16Array(6);
  assertIdxs('Short', a, 6);
  var a = new Int8Array(6);
  assertIdxs('Byte', a, 6);
  var a = new Uint32Array(6);
  assertIdxs('UInt', a, 6);
  var a = new Uint16Array(6);
  assertIdxs('UShort', a, 6);
  var a = new Uint8Array(6);
  assertIdxs('UByte', a, 6);
}
Tests.testCreateFromBuffer = function() {
  var sz = 24;
  var b = new ArrayBuffer(sz);
  var a = new Float32Array(b);
  assertIdxs('Float', a, sz/4);
  var a = new Int32Array(b);
  assertIdxs('Int', a, sz/4);
  var a = new Int16Array(b);
  assertIdxs('Short', a, sz/2);
  var a = new Int8Array(b);
  assertIdxs('Byte', a, sz/1);
  var a = new Uint32Array(b);
  assertIdxs('UInt', a, sz/4);
  var a = new Uint16Array(b);
  assertIdxs('UShort', a, sz/2);
  var a = new Uint8Array(b);
  assertIdxs('UByte', a, sz/1);
}

Tests.testThatWritesChangeDrawing = function(gl) {
  var verts = [
    0,0,
    1,0,
    1,1,

    0,0,
    1,1,
    0,1
  ];
  var a = new Float32Array(verts);
  var arr = [];
  for (var i=0; i<12; i++)
    arr[i] = a[i];
  assertEquals("Test that reads work from an array-initialized Float32Array", arr, verts);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  gl.bufferData(gl.ARRAY_BUFFER, a, gl.STATIC_DRAW);
  gl.vertexAttribPointer(va, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(va);

  var id = new Uint8Array(4);
  gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]);

  gl.drawArrays(gl.TRIANGLES, 0, 6);

  gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals([255, 0, 0, 255], [id[0], id[1], id[2], id[3]]);
  gl.readPixels(0,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]);

  a[0] = a[6] = a[10] = -1;
  gl.bufferData(gl.ARRAY_BUFFER, a, gl.STATIC_DRAW);
  gl.vertexAttribPointer(va, 2, gl.FLOAT, false, 0, 0);

  gl.drawArrays(gl.TRIANGLES, 0, 6);

  gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals([255, 0, 0, 255], [id[0], id[1], id[2], id[3]]);
  gl.readPixels(0,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals("Test that Float32Array#[]= worked and drawArrays drew a full-width rectangle",
               [255, 0, 0, 255], [id[0], id[1], id[2], id[3]]);
  gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id);
  assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]);
}

</script>
<script id="vert" type="x-shader/x-vertex">
  attribute vec2 Vertex;
  void main()
  {
    gl_Position = vec4(Vertex, 0.0, 1.0);
  }
</script>
<script id="frag" type="x-shader/x-fragment">
  precision mediump float;

  uniform vec4 c;
  void main()
  {
    gl_FragColor = c;
  }
</script>
<style>canvas{border: 1px solid black}</style>
</head><body>
<canvas id="gl" width="16" height="16"></canvas>
</body></html>
