Steven Moreland | e695c3e | 2019-08-21 15:36:22 -0700 | [diff] [blame] | 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 | |
Dan Shi | f32b8dd | 2019-11-04 23:22:06 -0800 | [diff] [blame] | 22 | static inline std::string Sanitize(std::string name) { |
Steven Moreland | e695c3e | 2019-08-21 15:36:22 -0700 | [diff] [blame] | 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 | } |
Steven Moreland | e695c3e | 2019-08-21 15:36:22 -0700 | [diff] [blame] | 27 | return name; |
| 28 | } |
| 29 | |
Dan Shi | f32b8dd | 2019-11-04 23:22:06 -0800 | [diff] [blame] | 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> |
| 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 | |
Steven Moreland | e695c3e | 2019-08-21 15:36:22 -0700 | [diff] [blame] | 57 | } // namespace hardware |
| 58 | } // namespace android |