1 /* 2 * Copyright (C) 2018 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 #ifndef SRC_BASE_TEST_UTILS_H_ 18 #define SRC_BASE_TEST_UTILS_H_ 19 20 #include <string> 21 22 #include "perfetto/base/logging.h" 23 24 #if PERFETTO_DCHECK_IS_ON() 25 26 #define EXPECT_DCHECK_DEATH(statement) \ 27 EXPECT_DEATH_IF_SUPPORTED(statement, "PERFETTO_CHECK") 28 #define ASSERT_DCHECK_DEATH(statement) \ 29 ASSERT_DEATH_IF_SUPPORTED(statement, "PERFETTO_CHECK") 30 31 #else // PERFETTO_DCHECK_IS_ON() 32 33 // Since PERFETTO_DCHECK_IS_ON() is false these statements should not die (if 34 // they should/do we should use EXPECT/ASSERT DEATH_TEST_IF_SUPPORTED directly). 35 // Therefore if the platform supports DEATH_TESTS we can use the handy 36 // GTEST_EXECUTE_STATEMENT_ which prevents optimizing the code away, and if not 37 // we just fall back on executing the code directly. 38 #if defined(GTEST_EXECUTE_STATEMENT_) 39 #define EXPECT_DCHECK_DEATH(statement) \ 40 GTEST_EXECUTE_STATEMENT_(statement, "PERFETTO_CHECK") 41 #define ASSERT_DCHECK_DEATH(statement) \ 42 GTEST_EXECUTE_STATEMENT_(statement, "PERFETTO_CHECK") 43 #else 44 #define EXPECT_DCHECK_DEATH(statement) [&]() { statement }() 45 #define ASSERT_DCHECK_DEATH(statement) [&]() { statement }() 46 #endif // defined(GTEST_EXECUTE_STATEMENT_) 47 48 #endif // PERFETTO_DCHECK_IS_ON() 49 50 namespace perfetto { 51 namespace base { 52 53 std::string GetCurExecutableDir(); 54 std::string GetTestDataPath(const std::string& path); 55 56 // Returns a xxd-style hex dump (hex + ascii chars) of the input data. 57 std::string HexDump(const void* data, size_t len, size_t bytes_per_line = 16); 58 inline std::string HexDump(const std::string& data, 59 size_t bytes_per_line = 16) { 60 return HexDump(data.data(), data.size(), bytes_per_line); 61 } 62 63 } // namespace base 64 } // namespace perfetto 65 66 #endif // SRC_BASE_TEST_UTILS_H_ 67