test-log.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright 2022 Xiaomi Corporation (authors: Fangjun Kuang)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "gtest/gtest.h"
  19. #include "log.h"
  20. namespace knf {
  21. #if KNF_ENABLE_CHECK
  22. TEST(Log, TestLog) {
  23. KNF_LOG(TRACE) << "this is a trace message";
  24. KNF_LOG(DEBUG) << "this is a debug message";
  25. KNF_LOG(INFO) << "this is an info message";
  26. KNF_LOG(WARNING) << "this is a warning message";
  27. KNF_LOG(ERROR) << "this is an error message";
  28. ASSERT_THROW(KNF_LOG(FATAL) << "This will crash the program",
  29. std::runtime_error);
  30. // For debug build
  31. KNF_DLOG(TRACE) << "this is a trace message for debug build";
  32. KNF_DLOG(DEBUG) << "this is a trace message for debug build";
  33. KNF_DLOG(INFO) << "this is a trace message for debug build";
  34. KNF_DLOG(ERROR) << "this is an error message for debug build";
  35. KNF_DLOG(WARNING) << "this is a trace message for debug build";
  36. #if !defined(NDEBUG)
  37. ASSERT_THROW(KNF_DLOG(FATAL) << "this is a trace message for debug build",
  38. std::runtime_error);
  39. #endif
  40. }
  41. TEST(Log, TestCheck) {
  42. KNF_CHECK_EQ(1, 1) << "ok";
  43. KNF_CHECK_LE(1, 3) << "ok";
  44. KNF_CHECK_LT(1, 2) << "ok";
  45. KNF_CHECK_GT(2, 1) << "ok";
  46. KNF_CHECK_GE(2, 1) << "ok";
  47. ASSERT_THROW(KNF_CHECK_EQ(2, 1) << "bad things happened", std::runtime_error);
  48. // for debug build
  49. KNF_DCHECK_EQ(1, 1) << "ok";
  50. KNF_DCHECK_LE(1, 3) << "ok";
  51. KNF_DCHECK_LT(1, 2) << "ok";
  52. KNF_DCHECK_GT(2, 1) << "ok";
  53. KNF_DCHECK_GE(2, 1) << "ok";
  54. #if !defined(NDEBUG)
  55. ASSERT_THROW(KNF_CHECK_EQ(2, 1) << "bad things happened", std::runtime_error);
  56. #endif
  57. }
  58. #endif
  59. } // namespace knf