tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

abort_test.cc (3423B)


      1 // Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
      2 // SPDX-License-Identifier: Apache-2.0
      3 // SPDX-License-Identifier: BSD-3-Clause
      4 
      5 #include "hwy/abort.h"
      6 
      7 #include <stdio.h>
      8 
      9 #include <string>
     10 
     11 #include "hwy/base.h"
     12 #include "hwy/tests/hwy_gtest.h"
     13 #include "hwy/tests/test_util-inl.h"  // HWY_ASSERT_EQ
     14 
     15 namespace hwy {
     16 namespace {
     17 
     18 TEST(AbortTest, WarnOverrideChain) {
     19  WarnFunc FirstHandler = [](const char* file, int line,
     20                             const char* formatted_err) -> void {
     21    fprintf(stderr, "%s from %d of %s", formatted_err, line, file);
     22  };
     23  WarnFunc SecondHandler = [](const char* file, int line,
     24                              const char* formatted_err) -> void {
     25    fprintf(stderr, "%s from %d of %s", formatted_err, line, file);
     26  };
     27 
     28  // Do not check that the first SetWarnFunc returns nullptr, because it is
     29  // not guaranteed to be the first call - other TEST may come first.
     30  (void)SetWarnFunc(FirstHandler);
     31  HWY_ASSERT(GetWarnFunc() == FirstHandler);
     32  HWY_ASSERT(SetWarnFunc(SecondHandler) == FirstHandler);
     33  HWY_ASSERT(GetWarnFunc() == SecondHandler);
     34  HWY_ASSERT(SetWarnFunc(nullptr) == SecondHandler);
     35  HWY_ASSERT(GetWarnFunc() == nullptr);
     36 }
     37 
     38 #ifdef GTEST_HAS_DEATH_TEST
     39 
     40 std::string GetBaseName(std::string const& file_name) {
     41  auto last_slash = file_name.find_last_of("/\\");
     42  return file_name.substr(last_slash + 1);
     43 }
     44 
     45 TEST(AbortDeathTest, AbortDefault) {
     46  std::string expected = std::string("Abort at ") + GetBaseName(__FILE__) +
     47                         ":" + std::to_string(__LINE__ + 1) + ": Test Abort";
     48  ASSERT_DEATH(HWY_ABORT("Test %s", "Abort"), expected);
     49 }
     50 
     51 TEST(AbortDeathTest, AbortOverride) {
     52  const AbortFunc CustomAbortHandler = [](const char* file, int line,
     53                                          const char* formatted_err) -> void {
     54    fprintf(stderr, "%s from %02d of %s", formatted_err, line,
     55            GetBaseName(file).data());
     56  };
     57 
     58  SetAbortFunc(CustomAbortHandler);
     59 
     60  // googletest regex does not support `+` for digits on Windows?!
     61  // https://google.github.io/googletest/advanced.html#regular-expression-syntax
     62  // Hence we insert the expected line number manually.
     63  char buf[100];
     64  const std::string file = GetBaseName(__FILE__);
     65  const int line = __LINE__ + 2;  // from which HWY_ABORT is called
     66  snprintf(buf, sizeof(buf), "Test Abort from %02d of %s", line, file.c_str());
     67  ASSERT_DEATH({ HWY_ABORT("Test %s", "Abort"); }, buf);
     68 }
     69 #endif  // GTEST_HAS_DEATH_TEST
     70 
     71 TEST(AbortTest, AbortOverrideChain) {
     72  AbortFunc FirstHandler = [](const char* file, int line,
     73                              const char* formatted_err) -> void {
     74    fprintf(stderr, "%s from %d of %s", formatted_err, line, file);
     75  };
     76  AbortFunc SecondHandler = [](const char* file, int line,
     77                               const char* formatted_err) -> void {
     78    fprintf(stderr, "%s from %d of %s", formatted_err, line, file);
     79  };
     80 
     81  // Do not check that the first SetAbortFunc returns nullptr, because it is
     82  // not guaranteed to be the first call - other TEST may come first.
     83  (void)SetAbortFunc(FirstHandler);
     84  HWY_ASSERT(GetAbortFunc() == FirstHandler);
     85  HWY_ASSERT(SetAbortFunc(SecondHandler) == FirstHandler);
     86  HWY_ASSERT(GetAbortFunc() == SecondHandler);
     87  HWY_ASSERT(SetAbortFunc(nullptr) == SecondHandler);
     88  HWY_ASSERT(GetAbortFunc() == nullptr);
     89 }
     90 
     91 }  // namespace
     92 }  // namespace hwy
     93 
     94 HWY_TEST_MAIN();