bh_log_test.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "bh_log.h"
  8. #include "stdio.h"
  9. class bh_log_test_suite : public testing::Test
  10. {
  11. protected:
  12. // You should make the members protected s.t. they can be
  13. // accessed from sub-classes.
  14. // virtual void SetUp() will be called before each test is run. You
  15. // should define it if you need to initialize the variables.
  16. // Otherwise, this can be skipped.
  17. virtual void SetUp() {}
  18. // virtual void TearDown() will be called after each test is run.
  19. // You should define it if there is cleanup work to do. Otherwise,
  20. // you don't have to provide it.
  21. //
  22. virtual void TearDown() {}
  23. };
  24. #define TEST_STR "This is a test."
  25. TEST_F(bh_log_test_suite, bh_log_set_verbose_level)
  26. {
  27. bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
  28. }
  29. TEST_F(bh_log_test_suite, bh_print_time)
  30. {
  31. std::string captured;
  32. bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
  33. bh_print_time(TEST_STR);
  34. bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
  35. testing::internal::CaptureStdout();
  36. bh_print_time(TEST_STR);
  37. captured = testing::internal::GetCapturedStdout();
  38. EXPECT_EQ(0, strncmp(TEST_STR, captured.c_str(), strlen(TEST_STR)));
  39. testing::internal::CaptureStdout();
  40. bh_print_time(TEST_STR);
  41. captured = testing::internal::GetCapturedStdout();
  42. EXPECT_EQ(0, strncmp(TEST_STR, captured.c_str(), strlen(TEST_STR)));
  43. }
  44. TEST_F(bh_log_test_suite, bh_log)
  45. {
  46. std::string captured;
  47. bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
  48. testing::internal::CaptureStdout();
  49. bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, TEST_STR);
  50. captured = testing::internal::GetCapturedStdout();
  51. EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
  52. testing::internal::CaptureStdout();
  53. bh_log(BH_LOG_LEVEL_ERROR, __FILE__, __LINE__, TEST_STR);
  54. captured = testing::internal::GetCapturedStdout();
  55. EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
  56. testing::internal::CaptureStdout();
  57. bh_log(BH_LOG_LEVEL_WARNING, __FILE__, __LINE__, TEST_STR);
  58. captured = testing::internal::GetCapturedStdout();
  59. EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
  60. testing::internal::CaptureStdout();
  61. bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, TEST_STR);
  62. captured = testing::internal::GetCapturedStdout();
  63. EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
  64. // log_verbose_level == BH_LOG_LEVEL_DEBUG, so BH_LOG_LEVEL_VERBOSE is not
  65. // printed.
  66. testing::internal::CaptureStdout();
  67. bh_log(BH_LOG_LEVEL_VERBOSE, __FILE__, __LINE__, TEST_STR);
  68. captured = testing::internal::GetCapturedStdout();
  69. EXPECT_EQ(nullptr, strstr(captured.c_str(), TEST_STR));
  70. // After set log_verbose_level = BH_LOG_LEVEL_VERBOSE, BH_LOG_LEVEL_VERBOSE
  71. // can be printed.
  72. bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
  73. testing::internal::CaptureStdout();
  74. bh_log(BH_LOG_LEVEL_VERBOSE, __FILE__, __LINE__, TEST_STR);
  75. captured = testing::internal::GetCapturedStdout();
  76. EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
  77. }