test_misc.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <string>
  22. #include "cnis/infer_server.h"
  23. #include "cnis/processor.h"
  24. #ifdef CNIS_WITH_CONTRIB
  25. #include "cnis/contrib/opencv_frame.h"
  26. #include "cnis/contrib/video_helper.h"
  27. namespace infer_server {
  28. using video::PixelFmt;
  29. using video::VideoFrame;
  30. TEST(InferServer, PlaneNum) {
  31. EXPECT_EQ(GetPlaneNum(PixelFmt::NV12), 2u);
  32. EXPECT_EQ(GetPlaneNum(PixelFmt::NV21), 2u);
  33. EXPECT_EQ(GetPlaneNum(PixelFmt::I420), 3u);
  34. EXPECT_EQ(GetPlaneNum(PixelFmt::RGBA), 1u);
  35. EXPECT_EQ(GetPlaneNum(PixelFmt::BGRA), 1u);
  36. EXPECT_EQ(GetPlaneNum(PixelFmt::ARGB), 1u);
  37. EXPECT_EQ(GetPlaneNum(PixelFmt::ABGR), 1u);
  38. EXPECT_EQ(GetPlaneNum(PixelFmt::BGR24), 1u);
  39. EXPECT_EQ(GetPlaneNum(PixelFmt::RGB24), 1u);
  40. }
  41. TEST(InferServer, PlaneSize) {
  42. VideoFrame frame;
  43. frame.width = 224;
  44. frame.height = 224;
  45. size_t base_size = frame.width * frame.height;
  46. frame.format = PixelFmt::NV12;
  47. EXPECT_EQ(frame.GetPlaneSize(0), base_size);
  48. EXPECT_EQ(frame.GetPlaneSize(1), base_size / 2);
  49. EXPECT_EQ(frame.GetPlaneSize(2), 0u);
  50. EXPECT_EQ(frame.GetTotalSize(), base_size * 3 / 2);
  51. frame.format = PixelFmt::NV21;
  52. EXPECT_EQ(frame.GetPlaneSize(0), base_size);
  53. EXPECT_EQ(frame.GetPlaneSize(1), base_size / 2);
  54. EXPECT_EQ(frame.GetPlaneSize(2), 0u);
  55. EXPECT_EQ(frame.GetTotalSize(), base_size * 3 / 2);
  56. frame.format = PixelFmt::I420;
  57. EXPECT_EQ(frame.GetPlaneSize(0), base_size);
  58. EXPECT_EQ(frame.GetPlaneSize(1), base_size / 4);
  59. EXPECT_EQ(frame.GetPlaneSize(2), base_size / 4);
  60. EXPECT_EQ(frame.GetPlaneSize(3), 0u);
  61. EXPECT_EQ(frame.GetTotalSize(), base_size * 3 / 2);
  62. frame.format = PixelFmt::RGBA;
  63. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 4);
  64. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  65. EXPECT_EQ(frame.GetTotalSize(), base_size * 4);
  66. frame.format = PixelFmt::ARGB;
  67. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 4);
  68. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  69. EXPECT_EQ(frame.GetTotalSize(), base_size * 4);
  70. frame.format = PixelFmt::BGRA;
  71. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 4);
  72. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  73. EXPECT_EQ(frame.GetTotalSize(), base_size * 4);
  74. frame.format = PixelFmt::ABGR;
  75. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 4);
  76. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  77. EXPECT_EQ(frame.GetTotalSize(), base_size * 4);
  78. frame.format = PixelFmt::RGB24;
  79. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 3);
  80. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  81. EXPECT_EQ(frame.GetTotalSize(), base_size * 3);
  82. frame.format = PixelFmt::BGR24;
  83. EXPECT_EQ(frame.GetPlaneSize(0), base_size * 3);
  84. EXPECT_EQ(frame.GetPlaneSize(1), 0u);
  85. EXPECT_EQ(frame.GetTotalSize(), base_size * 3);
  86. }
  87. } // namespace infer_server
  88. #endif // CNIS_WITH_CONTRIB
  89. #ifndef CNIS_VERSION_MAJOR
  90. #error CNIS_VERSION_MAJOR is not defined
  91. #endif
  92. #ifndef CNIS_VERSION_MINOR
  93. #error CNIS_VERSION_MINOR is not defined
  94. #endif
  95. #ifndef CNIS_VERSION_PATCH
  96. #error CNIS_VERSION_PATCH is not defined
  97. #endif
  98. #ifndef CNIS_GET_VERSION
  99. #error CNIS_GET_VERSION is not defined
  100. #endif
  101. #ifndef CNIS_VERSION
  102. #error CNIS_VERSION is not defined
  103. #endif
  104. namespace infer_server {
  105. TEST(InferServer, DataTypeSize) {
  106. EXPECT_EQ(GetTypeSize(DataType::UINT8), 1u);
  107. EXPECT_EQ(GetTypeSize(DataType::FLOAT16), 2u);
  108. EXPECT_EQ(GetTypeSize(DataType::FLOAT32), 4u);
  109. EXPECT_EQ(GetTypeSize(DataType::INT32), 4u);
  110. EXPECT_EQ(GetTypeSize(DataType::INT16), 2u);
  111. }
  112. TEST(InferServer, Version) {
  113. ASSERT_EQ(CNIS_VERSION, CNIS_GET_VERSION(CNIS_VERSION_MAJOR, CNIS_VERSION_MINOR, CNIS_VERSION_PATCH));
  114. ASSERT_EQ(CNIS_VERSION, (CNIS_VERSION_MAJOR << 20) | (CNIS_VERSION_MINOR << 10) | CNIS_VERSION_PATCH);
  115. std::string version = std::to_string(CNIS_VERSION_MAJOR) + "." + std::to_string(CNIS_VERSION_MINOR) + "." +
  116. std::to_string(CNIS_VERSION_PATCH);
  117. EXPECT_EQ(version, infer_server::Version());
  118. }
  119. TEST(InferServer, PredictorBackend) {
  120. #ifdef CNIS_USE_MAGICMIND
  121. EXPECT_EQ(Predictor::Backend(), std::string("magicmind"));
  122. #else
  123. EXPECT_EQ(Predictor::Backend(), std::string("cnrt"));
  124. #endif
  125. }
  126. } // namespace infer_server