Commit 1e20aa47 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added unit tests for ref counting



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 9b81267b
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -6,3 +6,8 @@ target_link_libraries(QirQrtTester ${XACC_TEST_LIBRARIES} xacc::xacc qir-qrt qrt

# Test compile a QASM -> QIR lowering
add_test(NAME mlir_qrt_compile COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/bell_qasm.qasm)

add_executable(QirQrtRefCountTester QirQrtRefCountTester.cpp)
add_test(NAME qcor_QirQrtRefCountTester COMMAND QirQrtRefCountTester)
target_include_directories(QirQrtRefCountTester PRIVATE . .. ${CMAKE_BINARY_DIR} ${XACC_ROOT}/include/gtest)
target_link_libraries(QirQrtRefCountTester ${XACC_TEST_LIBRARIES} xacc::xacc qir-qrt qrt)
+48 −0
Original line number Diff line number Diff line
#if __GNUC__ >= 5
// Disable GCC 5's -Wsuggest-override and -Wsign-compare warnings in gtest
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsuggest-override"
# pragma GCC diagnostic ignored "-Wsign-compare"
#endif
#include "gtest/gtest.h"
#include "qir-qrt.hpp"

TEST(QirQrtRefCountTester, checkArray) {
  auto test_array = __quantum__rt__array_create_1d(8, 2);
  EXPECT_EQ(test_array->size(), 2);
  EXPECT_EQ(test_array->element_size(), 8);
  EXPECT_EQ(test_array->ref_count(), 1);
  // Tracker should detect the array is leaking (no dealloc)
  EXPECT_TRUE(qcor::internal::AllocationTracker::get().checkLeak());
  __quantum__rt__array_update_reference_count(test_array, 3);
  EXPECT_EQ(test_array->ref_count(), 4);
  for (int i = 0; i < 4; ++i) {
    __quantum__rt__array_update_reference_count(test_array, -1);
  }

  // Should be dealloc'ed by now...
  EXPECT_FALSE(qcor::internal::AllocationTracker::get().checkLeak());
}

TEST(QirQrtRefCountTester, checkTuple) {
  // No leak
  EXPECT_FALSE(qcor::internal::AllocationTracker::get().checkLeak());
  auto tuple = __quantum__rt__tuple_create(sizeof(double) + sizeof(void*));
  auto tuple_header = TupleHeader::getHeader(tuple);
  EXPECT_EQ(tuple_header->ref_count(), 1);
  // Leak
  EXPECT_TRUE(qcor::internal::AllocationTracker::get().checkLeak());
  __quantum__rt__tuple_update_reference_count(tuple, -1);
  // Gone by now
  EXPECT_FALSE(qcor::internal::AllocationTracker::get().checkLeak());
}

#if __GNUC__ >= 5
# pragma GCC diagnostic pop
#endif

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}
 No newline at end of file