
// RUN: %hc %s -o %t.out && %t.out

#include <iostream>
#include <hc.hpp>

// added for checking HSA profile
#include <hc.hpp>

// test C++AMP with fine-grained SVM
// requires HSA Full Profile to operate successfully

struct S {
  unsigned int bit : 3;
};

class C {
public:
  unsigned int bit : 3;
};

union U {
  unsigned int bit : 3;
};

bool test() {

  const int vecSize = 16;

  int ans[vecSize];
  int *p_ans = &ans[0];

  parallel_for_each(
    hc::extent<1>(vecSize),
    [=](hc::index<1> idx) [[hc]] {

    S s;
    s.bit = 7;
    ++s.bit;
    C c;
    c.bit = 7;
    ++c.bit;
    U u;
    u.bit = 7;
    ++u.bit;
    p_ans[idx[0]] = (int)s.bit + (int)c.bit + (int)u.bit;
  });

  // Verify
  int error = 0;
  for(int i = 0; i < vecSize; i++) {
    error += abs(ans[i]);
  }
  if (error == 0) {
    std::cout << "Verify success!\n";
  } else {
    std::cout << "Verify failed!\n";
  }
  return (error == 0);
}

int main() {
  bool ret = true;

  // only conduct the test in case we are running on a HSA full profile stack
  hc::accelerator acc;
  if (acc.is_hsa_accelerator() &&
      acc.get_profile() == hc::hcAgentProfileFull) {
    ret &= test();
  }

  return !(ret == true);
}

