TensorTypeTest.cpp 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#include "TensorType.h"
#include <gtest/gtest.h>

namespace paddle {

H
hedaoyuan 已提交
20 21
TEST(TensorType, Matrix) {
  Tensor<real, DEVICE_TYPE_CPU>::Matrix matrix(100, 200);
22 23 24
  EXPECT_EQ(matrix.getHeight(), 100U);
  EXPECT_EQ(matrix.getWidth(), 200U);
  EXPECT_EQ(matrix.getElementCnt(), 100U * 200U);
H
hedaoyuan 已提交
25 26 27 28
  EXPECT_EQ(matrix.useGpu(), false);

  Tensor<real, DEVICE_TYPE_GPU>::Matrix testGpu(100, 200);
  EXPECT_EQ(testGpu.useGpu(), true);
29 30
}

H
hedaoyuan 已提交
31 32 33 34 35
TEST(TensorType, Vector) {
  Tensor<real, DEVICE_TYPE_CPU>::Vector cpuVector(100);
  Tensor<real, DEVICE_TYPE_GPU>::Vector gpuVector(100);
  EXPECT_EQ(cpuVector.useGpu(), false);
  EXPECT_EQ(gpuVector.useGpu(), true);
36 37
  EXPECT_EQ(cpuVector.getSize(), 100U);
  EXPECT_EQ(gpuVector.getSize(), 100U);
H
hedaoyuan 已提交
38 39 40 41 42

  Tensor<int, DEVICE_TYPE_CPU>::Vector cpuIVector(100);
  Tensor<int, DEVICE_TYPE_GPU>::Vector gpuIVector(100);
  EXPECT_EQ(cpuIVector.useGpu(), false);
  EXPECT_EQ(gpuIVector.useGpu(), true);
43 44
  EXPECT_EQ(cpuIVector.getSize(), 100U);
  EXPECT_EQ(gpuIVector.getSize(), 100U);
45 46
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
TEST(TensorType, EmptyMatrix) {
  CpuMatrix empty(nullptr, 0, 0);
  CpuMatrix nonEmpty(10, 10);
  EXPECT_EQ(empty.isEmpty(), true);
  EXPECT_EQ(nonEmpty.isEmpty(), false);
  CHECK(nonEmpty);
  auto function = [](const CpuMatrix& matrix) {
    if (matrix) {
      EXPECT_NE(matrix.getData(), nullptr);
    } else {
      EXPECT_EQ(matrix.getData(), nullptr);
    }
  };
  function(empty);
  function(nonEmpty);
}

64
}  // namespace paddle