/// @file
/// Test creation, use, and destruction of an interlaced multicomponent element restriction
/// \test Test creation, use, and destruction of an interlaced multicomponent element restriction
#include <ceed.h>
#include <ceed/backend.h>

int main(int argc, char **argv) {
  Ceed ceed;
  CeedVector x, y;
  CeedInt num_elem = 3;
  CeedInt ind[2*num_elem];
  CeedInt layout[3];
  CeedScalar a[2*(num_elem+1)];
  const CeedScalar *yy;
  CeedElemRestriction r;

  CeedInit(argv[1], &ceed);

  // Setup
  CeedVectorCreate(ceed, 2*(num_elem+1), &x);
  for (CeedInt i=0; i<num_elem+1; i++) {
    a[2*i] = 10 + i;
    a[2*i+1] = 20 + i;
  }
  CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);

  for (CeedInt i=0; i<num_elem; i++) {
    ind[2*i+0] = 2*i;
    ind[2*i+1] = 2*(i+1);
  }
  CeedElemRestrictionCreate(ceed, num_elem, 2, 2, 1, 2*(num_elem+1),
                            CEED_MEM_HOST,
                            CEED_USE_POINTER, ind, &r);
  CeedVectorCreate(ceed, 2*(num_elem*2), &y);
  CeedVectorSetValue(y, 0); // Allocates array

  // Restrict
  CeedElemRestrictionApply(r, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);

  // Check
  CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
  CeedElemRestrictionGetELayout(r, &layout);
  for (CeedInt i=0; i<2; i++)       // Node
    for (CeedInt j=0; j<2; j++)     // Component
      for (CeedInt k=0; k<num_elem; k++)  // Element
        if (yy[i*layout[0]+j*layout[1]+k*layout[2]] != a[ind[i+k*2]+j])
          // LCOV_EXCL_START
          printf("Error in restricted array y[%d][%d][%d] = %f != %f\n",
                 i, j, k, (double)yy[i*layout[0]+j*layout[1]+k*layout[2]],
                 a[ind[i+k*2]+j*(num_elem+1)]);
  // LCOV_EXCL_STOP

  CeedVectorRestoreArrayRead(y, &yy);
  CeedVectorDestroy(&x);
  CeedVectorDestroy(&y);
  CeedElemRestrictionDestroy(&r);
  CeedDestroy(&ceed);
  return 0;
}
