blob: 4b5ac2dd781d77a0578ec2990b53daa7321f3429 [file] [log] [blame]
Steven Morelande695c3e2019-08-21 15:36:22 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19namespace android {
20namespace hardware {
21
Dan Shif32b8dd2019-11-04 23:22:06 -080022static inline std::string Sanitize(std::string name) {
Steven Morelande695c3e2019-08-21 15:36:22 -070023 for (size_t i = 0; i < name.size(); i++) {
24 // gtest test names must only contain alphanumeric characters
25 if (!std::isalnum(name[i])) name[i] = '_';
26 }
Steven Morelande695c3e2019-08-21 15:36:22 -070027 return name;
28}
29
Dan Shif32b8dd2019-11-04 23:22:06 -080030static inline std::string PrintInstanceNameToString(
31 const testing::TestParamInfo<std::string>& info) {
32 // test names need to be unique -> index prefix
33 std::string name = std::to_string(info.index) + "/" + info.param;
34 return Sanitize(name);
35}
36
37template <typename... T>
38static inline std::string PrintInstanceTupleNameToString(
39 const testing::TestParamInfo<std::tuple<T...>>& info) {
40 std::vector<std::string> instances = std::apply(
41 [](auto&&... elems) {
42 std::vector<std::string> instances;
43 instances.reserve(sizeof...(elems));
44 (instances.push_back(std::forward<decltype(elems)>(elems)), ...);
45 return instances;
46 },
47 info.param);
48 std::string param_string;
49 for (const std::string& instance : instances) {
50 param_string += instance + "_";
51 }
52 param_string += std::to_string(info.index);
53
54 return Sanitize(param_string);
55}
56
Steven Morelande695c3e2019-08-21 15:36:22 -070057} // namespace hardware
58} // namespace android