test_Arguments.cpp 3.9 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

Y
Yu Yang 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "PaddleCAPI.h"
#include "gtest/gtest.h"
#include "paddle/utils/ThreadLocal.h"

static std::vector<pd_real> randomBuffer(size_t bufSize) {
  auto& eng = paddle::ThreadLocalRandomEngine::get();
  std::uniform_real_distribution<pd_real> dist(-1.0, 1.0);
  std::vector<pd_real> retv;
  retv.reserve(bufSize);
  for (size_t i = 0; i < bufSize; ++i) {
    retv.push_back(dist(eng));
  }
  return retv;
}

TEST(CAPIArguments, create) {
  PD_Arguments args;
Y
Yu Yang 已提交
32
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
Y
Yu Yang 已提交
33
  uint64_t size;
Y
Yu Yang 已提交
34
  ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSize(args, &size));
Y
Yu Yang 已提交
35
  ASSERT_EQ(0UL, size);
Y
Yu Yang 已提交
36
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
Y
Yu Yang 已提交
37 38 39 40
}

TEST(CAPIArguments, value) {
  PD_Arguments args;
Y
Yu Yang 已提交
41 42
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
Y
Yu Yang 已提交
43 44

  PD_Matrix mat;
Y
Yu Yang 已提交
45
  ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 128, 64, false));
Y
Yu Yang 已提交
46 47 48 49
  for (size_t i = 0; i < 128; ++i) {
    std::vector<pd_real> sampleBuf = randomBuffer(64);
    PDMatCopyToRow(mat, i, sampleBuf.data());
  }
Y
Yu Yang 已提交
50
  ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat));
Y
Yu Yang 已提交
51 52

  PD_Matrix val;
Y
Yu Yang 已提交
53
  ASSERT_EQ(kPD_NO_ERROR, PDMatCreateNone(&val));
Y
Yu Yang 已提交
54

Y
Yu Yang 已提交
55
  ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val));
Y
Yu Yang 已提交
56 57 58 59 60

  for (size_t i = 0; i < 128; ++i) {
    pd_real* row1;
    pd_real* row2;

Y
Yu Yang 已提交
61 62
    ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, i, &row1));
    ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(val, i, &row2));
Y
Yu Yang 已提交
63 64
    ASSERT_EQ(row1, row2);
  }
Y
Yu Yang 已提交
65 66 67 68 69 70 71

  PD_IVector ivec;
  ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&ivec));
  ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(val));
  ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
Y
Yu Yang 已提交
72
}
L
livc 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

TEST(CAPIArguments, ids) {
  PD_Arguments args;
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));

  PD_IVector ivec;
  int array[3] = {1, 2, 3};
  ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec));

  PD_IVector val;
  ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val));
  ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}

Y
Yu Yang 已提交
92 93
template <typename T1, typename T2>
void testSequenceHelper(T1 setter, T2 getter) {
L
livc 已提交
94 95 96 97 98 99 100
  PD_Arguments args;
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));

  PD_IVector ivec;
  int array[3] = {1, 2, 3};
  ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true));
Y
Yu Yang 已提交
101
  ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec));
L
livc 已提交
102 103 104

  PD_IVector val;
  ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
Y
Yu Yang 已提交
105 106 107 108 109 110 111 112 113 114
  ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val));
  uint64_t size;
  ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size));

  int* rawBuf;
  ASSERT_EQ(kPD_NO_ERROR, PDIVectorGet(val, &rawBuf));
  for (size_t i = 0; i < size; ++i) {
    ASSERT_EQ(array[i], rawBuf[i]);
  }

L
livc 已提交
115 116 117 118
  ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
Y
Yu Yang 已提交
119 120 121 122 123 124

TEST(CAPIArguments, Sequence) {
  testSequenceHelper(PDArgsSetSequenceStartPos, PDArgsGetSequenceStartPos);
  testSequenceHelper(PDArgsSetSubSequenceStartPos,
                     PDArgsGetSubSequenceStartPos);
}