Skip to content

Commit 11965bc

Browse files
add llvm::SmallVector to paddle (#34832)
* add llvm::SmallVector to paddle * rename small vector file * merge paddle small vector to one file * add small_vector_test * modify smallvector test argument type * add string header
1 parent 3d9603d commit 11965bc

File tree

3 files changed

+1559
-0
lines changed

3 files changed

+1559
-0
lines changed

paddle/testing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
if(WITH_TESTING)
44
cc_library(paddle_gtest_main SRCS paddle_gtest_main.cc DEPS device_context memory gtest gflags)
55
endif()
6+
cc_test(small_vector_test SRCS small_vector_test.cc DEPS gtest gflags)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/utils/small_vector.h"
16+
17+
#include <cstdlib>
18+
#include <ctime>
19+
20+
#include "glog/logging.h"
21+
#include "gtest/gtest.h"
22+
23+
template <typename T, unsigned N>
24+
static std::vector<T> ToStdVector(const paddle::SmallVector<T, N> &vec) {
25+
std::vector<T> std_vec;
26+
std_vec.reserve(vec.size());
27+
for (size_t i = 0; i < vec.size(); ++i) {
28+
std_vec.emplace_back(vec[i]);
29+
}
30+
return std_vec;
31+
}
32+
33+
template <size_t N>
34+
void SmallVectorCheck(size_t n) {
35+
std::srand(std::time(nullptr));
36+
37+
std::vector<int> std_vec;
38+
paddle::SmallVector<int, N> vec;
39+
40+
for (size_t i = 0; i < n; ++i) {
41+
int value = rand(); // NOLINT
42+
43+
std_vec.emplace_back(value);
44+
vec.emplace_back(value);
45+
46+
CHECK_EQ(std_vec.size(), vec.size());
47+
CHECK_EQ(std_vec.back(), vec.back());
48+
49+
CHECK_EQ(vec.back(), value);
50+
}
51+
52+
bool is_equal = (std_vec == ToStdVector(vec));
53+
54+
CHECK_EQ(is_equal, true);
55+
56+
for (size_t i = 0; i < n; ++i) {
57+
CHECK_EQ(std_vec.size(), vec.size());
58+
CHECK_EQ(std_vec.back(), vec.back());
59+
std_vec.pop_back();
60+
vec.pop_back();
61+
CHECK_EQ(std_vec.size(), vec.size());
62+
}
63+
64+
CHECK_EQ(std_vec.size(), static_cast<size_t>(0));
65+
CHECK_EQ(vec.size(), static_cast<size_t>(0));
66+
}
67+
68+
TEST(samll_vector, small_vector) {
69+
for (size_t i = 0; i < 20; ++i) {
70+
SmallVectorCheck<1>(i);
71+
SmallVectorCheck<10>(i);
72+
SmallVectorCheck<15>(i);
73+
SmallVectorCheck<20>(i);
74+
SmallVectorCheck<21>(i);
75+
SmallVectorCheck<25>(i);
76+
}
77+
}

0 commit comments

Comments
 (0)