test_Arguments.cpp 4.0 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. */

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#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) {
Y
Stash  
Yu Yang 已提交
31
  paddle_arguments args = paddle_arguments_create_none();
32
  uint64_t size;
Y
Stash  
Yu Yang 已提交
33
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_size(args, &size));
34
  ASSERT_EQ(0UL, size);
Y
Stash  
Yu Yang 已提交
35
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(args));
36 37 38
}

TEST(CAPIArguments, value) {
Y
Stash  
Yu Yang 已提交
39 40
  paddle_arguments args = paddle_arguments_create_none();
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_resize(args, 1));
41

Y
Yu Yang 已提交
42
  paddle_matrix mat = paddle_matrix_create(128, 64, false);
43 44
  for (size_t i = 0; i < 128; ++i) {
    std::vector<pd_real> sampleBuf = randomBuffer(64);
Y
Yu Yang 已提交
45
    paddle_matrix_set_row(mat, i, sampleBuf.data());
46
  }
Y
Stash  
Yu Yang 已提交
47
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_set_value(args, 0, mat));
48

Y
Yu Yang 已提交
49
  paddle_matrix val = paddle_matrix_create_none();
50

Y
Stash  
Yu Yang 已提交
51
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_value(args, 0, val));
52 53 54 55 56

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

Y
Yu Yang 已提交
57 58
    ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, i, &row1));
    ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(val, i, &row2));
59 60
    ASSERT_EQ(row1, row2);
  }
Y
Yu Yang 已提交
61

Y
Yu Yang 已提交
62 63 64 65
  paddle_ivector ivec = paddle_ivector_create_none();
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(val));
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
Y
Stash  
Yu Yang 已提交
66
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(args));
67
}
68 69

TEST(CAPIArguments, ids) {
Y
Stash  
Yu Yang 已提交
70 71
  paddle_arguments args = paddle_arguments_create_none();
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_resize(args, 1));
72

Y
Yu Yang 已提交
73
  paddle_ivector ivec;
74
  int array[3] = {1, 2, 3};
Y
Yu Yang 已提交
75
  ivec = paddle_ivector_create(array, 3, true, false);
Y
Stash  
Yu Yang 已提交
76
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_set_ids(args, 0, ivec));
77

Y
Yu Yang 已提交
78
  paddle_ivector val = paddle_ivector_create_none();
Y
Stash  
Yu Yang 已提交
79
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_ids(args, 0, val));
Y
Yu Yang 已提交
80 81
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
Y
Stash  
Yu Yang 已提交
82
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(args));
83 84
}

Y
Yu Yang 已提交
85 86
template <typename T1, typename T2>
void testSequenceHelper(T1 setter, T2 getter) {
Y
Stash  
Yu Yang 已提交
87 88
  paddle_arguments args = paddle_arguments_create_none();
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_resize(args, 1));
89

Y
Yu Yang 已提交
90
  paddle_ivector ivec;
91
  int array[3] = {1, 2, 3};
Y
Yu Yang 已提交
92
  ivec = paddle_ivector_create(array, 3, true, false);
Y
Yu Yang 已提交
93
  ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec));
94

Y
Yu Yang 已提交
95
  paddle_ivector val = paddle_ivector_create_none();
Y
Yu Yang 已提交
96 97
  ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val));
  uint64_t size;
Y
Yu Yang 已提交
98
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get_size(val, &size));
Y
Yu Yang 已提交
99 100

  int* rawBuf;
Y
Yu Yang 已提交
101
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get(val, &rawBuf));
Y
Yu Yang 已提交
102 103 104 105
  for (size_t i = 0; i < size; ++i) {
    ASSERT_EQ(array[i], rawBuf[i]);
  }

Y
Yu Yang 已提交
106 107
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
  ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
Y
Stash  
Yu Yang 已提交
108
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(args));
109
}
Y
Yu Yang 已提交
110 111

TEST(CAPIArguments, Sequence) {
Y
Stash  
Yu Yang 已提交
112 113 114 115
  testSequenceHelper(paddle_arguments_set_sequence_start_pos,
                     paddle_arguments_sequence_start_pos);
  testSequenceHelper(paddle_arguments_set_sub_sequence_start_pos,
                     paddle_arguments_sub_sequence_start_pos);
Y
Yu Yang 已提交
116
}
新手
引导
客服 返回
顶部