test_Matrix.cpp 962 字节
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "PaddleCAPI.h"
#include "gtest/gtest.h"

TEST(CAPIMatrix, create) {
  PD_Matrix mat;
  ASSERT_EQ(PD_NO_ERROR, PDMatCreate(&mat, 128, 32, false));
  std::vector<pd_real> sampleRow;
  sampleRow.resize(32);
  for (size_t i = 0; i < sampleRow.size(); ++i) {
    sampleRow[i] = 1.0 / (i + 1.0);
  }
  ASSERT_EQ(PD_NO_ERROR, PDMatCopyToRow(mat, 0, sampleRow.data()));
  ASSERT_EQ(PD_OUT_OF_RANGE, PDMatCopyToRow(mat, 128, sampleRow.data()));

  pd_real* arrayPtr;

  ASSERT_EQ(PD_NO_ERROR, PDMatGetRow(mat, 0, &arrayPtr));
  for (size_t i = 0; i < sampleRow.size(); ++i) {
    ASSERT_NEAR(sampleRow[i], arrayPtr[i], 1e-5);
  }

  uint64_t height, width;
  ASSERT_EQ(PD_NO_ERROR, PDMatGetShape(mat, &height, &width));
  ASSERT_EQ(128, height);
  ASSERT_EQ(32, width);
  ASSERT_EQ(PD_NO_ERROR, PDMatDestroy(mat));
}

TEST(CAPIMatrix, createNone) {
  PD_Matrix mat;
  ASSERT_EQ(PD_NO_ERROR, PDMatCreateNone(&mat));
  ASSERT_EQ(PD_NO_ERROR, PDMatDestroy(mat));
}