1 /*
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 
19 namespace android {
20 namespace hardware {
21 
Sanitize(std::string name)22 static inline std::string Sanitize(std::string name) {
23     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     }
27     return name;
28 }
29 
PrintInstanceNameToString(const testing::TestParamInfo<std::string> & info)30 static 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 
37 template <typename... T>
PrintInstanceTupleNameToString(const testing::TestParamInfo<std::tuple<T...>> & info)38 static 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 
57 }  // namespace hardware
58 }  // namespace android
59