/*
 * Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
 *
 * This source code and/or documentation ("Licensed Deliverables") are
 * subject to NVIDIA intellectual property rights under U.S. and
 * international Copyright laws.
 */

#pragma once

#include <utils/cudart.hpp>

#include <catch2/catch.hpp>

#include <string>
#include <vector>

namespace cugraph::ops::catch2 {

/**
 * @brief Custom matcher class for comparing device array in `std::iota` order
 *
 * @tparam DataT data type of the arrays
 */
template <typename DataT>
class device_iota_t : public Catch::MatcherBase<DataT*> {
  const DataT start_;
  const size_t len_;

 public:
  explicit device_iota_t(DataT _start, size_t _len) : start_(_start), len_(_len) {}

  bool match(DataT* const& d_computed) const override
  {
    std::vector<DataT> h_computed(len_);
    utils::copy(h_computed.data(), d_computed, len_, cudaMemcpyDeviceToHost);
    for (size_t i = 0; i < len_; ++i) {
      if (static_cast<DataT>(start_ + i) != h_computed[i]) return false;
    }
    return true;
  }

  [[nodiscard]] std::string describe() const override { return "device iota matcher"; }
};  // class device_iota_t

/**
 * @brief Builder function for the custom device array `iota()` matcher
 *
 * @tparam DataT data type of the arrays
 *
 * @param[in] start iota start value
 * @param[in] len   length of the array
 *
 * @return the matcher object
 */
template <typename DataT>
device_iota_t<DataT> device_iota(DataT start, size_t len)
{
  return device_iota_t<DataT>(start, len);
}

}  // namespace cugraph::ops::catch2
