/* Testing for bundle pack and unpack which invloves a flexible way of 
   reading and writing arrays. This example may still not cover all possible 
   cases. Anyone can add more send and receive pairs for different cases.
 */
#include <civlc.cvh>
#include <bundle.cvh>
#include <stdlib.h>

$input int N;
$assume(N > 4);
$input int inputs1[3][N];
$input int inputs2[N][3];
$input int inputs3[2 * N];

void main(){
  int * p;
  int * p2;
  double ** pp;
  
  p = (int *)malloc(sizeof(int) * 3 * N);
  p2 = (int *)malloc(sizeof(int) * 2 * N);
  pp = (double **)malloc(sizeof(double *) * 2);
  for (int i = 0; i < 2; i++) {
      pp[i] = (double *)malloc(sizeof(double) * 10);
      for (int j = 0; j < 10; j++) pp[i][j] = i + 1;
  }
  
  $bundle bun;

  bun = $bundle_pack(pp[0], sizeof(double) * 10);

  $bundle_unpack_apply(bun, pp[1], _MAXLOC, 5, pp[0]);
  for (int j = 0; j < 10; j++) $assert(pp[0][j] == 2);
  
  $bundle_unpack_apply(bun, pp[1], _SUM, 10, pp[0]);
  for (int j = 0; j < 10; j++) $assert(pp[0][j] == 3);

  
  free(p);
  free(p2);
  free(pp[0]);
  free(pp[1]);
  free(pp);
}
