提交 c7088da5 编写于 作者: S storypku 提交者: Liu Jiaming

cyber: add unit tests for shared_library

上级 6ebdd0de
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
......@@ -18,4 +18,37 @@ cc_library(
hdrs = ["exceptions.h"],
)
cc_binary(
name = "libsample.so",
testonly = True,
linkshared = True,
linkstatic = False,
visibility = ["//visibility:private"],
deps = [
":sample_lib",
],
)
cc_library(
name = "sample_lib",
testonly = True,
srcs = ["sample.cc"],
hdrs = ["sample.h"],
linkopts = ["-lm"],
visibility = ["//visibility:private"],
)
cc_test(
name = "shared_library_test",
size = "small",
srcs = ["shared_library_test.cc"],
data = [
":libsample.so",
],
deps = [
":shared_library",
"@com_google_googletest//:gtest_main",
],
)
cpplint()
/******************************************************************************
* Copyright 2020 The Apollo 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 "cyber/class_loader/shared_library/sample.h"
#include <cmath>
int sample_add(int x, int y) { return x + y; }
double sample_sin(double x) { return std::sin(x); }
/******************************************************************************
* Copyright 2020 The Apollo 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.
*****************************************************************************/
#ifndef CYBER_CLASS_LOADER_SHARED_LIBRARY_SAMPLE_H_
#define CYBER_CLASS_LOADER_SHARED_LIBRARY_SAMPLE_H_
extern "C" int sample_add(int a, int b);
extern "C" double sample_sin(double x);
#endif // CYBER_CLASS_LOADER_SHARED_LIBRARY_SAMPLE_H_
......@@ -19,7 +19,7 @@
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
// SPDX-License-Identifier: BSL-1.0
//
#include "cyber/class_loader/shared_library/shared_library.h"
......
/******************************************************************************
* Copyright 2020 The Apollo 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 "cyber/class_loader/shared_library/shared_library.h"
#include <cmath>
#include "gtest/gtest.h"
namespace {
const std::string& SAMPLE_LIB =
"/apollo/bazel-bin/cyber/class_loader/shared_library/libsample.so";
constexpr double epsilon = 1.0e-8;
} // namespace
namespace apollo {
namespace cyber {
namespace class_loader {
TEST(SharedLibraryTest, symbol_test_1) {
auto shared_lib = std::make_shared<SharedLibrary>();
EXPECT_TRUE(shared_lib->GetPath().empty());
EXPECT_FALSE(shared_lib->IsLoaded());
EXPECT_NO_THROW(shared_lib->Load(SAMPLE_LIB));
EXPECT_TRUE(shared_lib->IsLoaded());
EXPECT_THROW(shared_lib->Load(SAMPLE_LIB), LibraryAlreadyLoadedException);
EXPECT_TRUE(shared_lib->HasSymbol("sample_add"));
auto symbol = shared_lib->GetSymbol("sample_add");
EXPECT_TRUE(symbol != nullptr);
typedef int (*BinaryFunc)(int, int);
auto pf = reinterpret_cast<BinaryFunc>(symbol);
EXPECT_EQ(pf(3, 5), 8);
shared_lib->Unload();
}
TEST(SharedLibraryTest, symbol_test_2) {
auto shared_lib = std::make_shared<SharedLibrary>(SAMPLE_LIB);
EXPECT_TRUE(shared_lib->IsLoaded());
typedef double (*UnaryFunc)(double);
auto symbol = shared_lib->GetSymbol("sample_sin");
EXPECT_TRUE(symbol != nullptr);
auto sample_sin = reinterpret_cast<UnaryFunc>(symbol);
EXPECT_NEAR(sample_sin(M_PI / 2), 1.0, epsilon);
shared_lib->Unload();
}
} // namespace class_loader
} // namespace cyber
} // namespace apollo
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册