array_ref_test.cc 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "paddle/utils/array_ref.h"

#include <cstdlib>
#include <ctime>

#include "glog/logging.h"
#include "gtest/gtest.h"

TEST(array_ref, array_ref) {
C
Chen Weihang 已提交
24
  paddle::array_ref<int> a;
25 26 27
  CHECK_EQ(a.size(), size_t(0));
  CHECK_EQ(a.data(), static_cast<int*>(nullptr));

C
Chen Weihang 已提交
28
  paddle::array_ref<int> b(paddle::none);
29 30 31 32
  CHECK_EQ(b.size(), size_t(0));
  CHECK_EQ(b.data(), static_cast<int*>(nullptr));

  int v = 1;
C
Chen Weihang 已提交
33
  paddle::array_ref<int> c(v);
34 35
  CHECK_EQ(c.size(), size_t(1));
  CHECK_EQ(c.data(), &v);
C
Chen Weihang 已提交
36
  CHECK_EQ(c.equals(paddle::make_array_ref(v)), true);
37 38

  int v1[5] = {1, 2, 3, 4, 5};
C
Chen Weihang 已提交
39
  paddle::array_ref<int> d(v1, 5);
40 41
  CHECK_EQ(d.size(), size_t(5));
  CHECK_EQ(d.data(), v1);
C
Chen Weihang 已提交
42
  CHECK_EQ(d.equals(paddle::make_array_ref(v1, 5)), true);
43

C
Chen Weihang 已提交
44
  paddle::array_ref<int> e(&v1[0], &v1[4]);
45 46
  CHECK_EQ(e.size(), size_t(4));
  CHECK_EQ(e.data(), v1);
C
Chen Weihang 已提交
47
  CHECK_EQ(e.equals(paddle::make_array_ref(&v1[0], &v1[4])), true);
48

C
Chen Weihang 已提交
49 50
  paddle::small_vector<int, 3> small_vector{1, 2, 3};
  paddle::array_ref<int> f(small_vector);
51 52
  CHECK_EQ(f.size(), size_t(3));
  CHECK_EQ(f.data(), small_vector.data());
C
Chen Weihang 已提交
53
  CHECK_EQ(f.equals(paddle::make_array_ref(small_vector)), true);
54 55

  std::vector<int> vector{1, 2, 3};
C
Chen Weihang 已提交
56
  paddle::array_ref<int> g(vector);
57 58
  CHECK_EQ(g.size(), size_t(3));
  CHECK_EQ(g.data(), vector.data());
C
Chen Weihang 已提交
59
  CHECK_EQ(g.equals(paddle::make_array_ref(vector)), true);
60 61

  std::initializer_list<int> list = {1, 2, 3};
C
Chen Weihang 已提交
62
  paddle::array_ref<int> h(list);
63 64 65
  CHECK_EQ(h.size(), size_t(3));
  CHECK_EQ(h.data(), list.begin());

C
Chen Weihang 已提交
66
  paddle::array_ref<int> i(h);
67 68 69
  CHECK_EQ(i.size(), size_t(3));
  CHECK_EQ(i.data(), list.begin());
  CHECK_EQ(i.equals(h), true);
C
Chen Weihang 已提交
70
  CHECK_EQ(i.equals(paddle::make_array_ref(h)), true);
71 72 73 74 75 76 77 78 79 80

  auto slice = i.slice(1, 2);
  CHECK_EQ(slice.size(), size_t(2));
  CHECK_EQ(slice[0], 2);
  CHECK_EQ(slice[1], 3);

  auto drop = i.drop_front(2);
  CHECK_EQ(drop.size(), size_t(1));
  CHECK_EQ(drop[0], 3);

C
Chen Weihang 已提交
81
  paddle::array_ref<int> nums = {1, 2, 3, 4, 5, 6, 7, 8};
82 83 84 85 86 87 88 89 90 91 92
  auto front = nums.take_front(3);
  CHECK_EQ(front.size(), size_t(3));
  for (size_t i = 0; i < 3; ++i) {
    CHECK_EQ(front[i], nums[i]);
  }
  auto back = nums.take_back(3);
  CHECK_EQ(back.size(), size_t(3));
  for (size_t i = 0; i < 3; ++i) {
    CHECK_EQ(back[i], nums[i + 5]);
  }
}