test_connector.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*************************************************************************
  2. * Copyright (C) [2019] 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 <ctime>
  22. #include <memory>
  23. #include <vector>
  24. #include "cnstream_frame.hpp"
  25. #include "connector.hpp"
  26. #include "conveyor.hpp"
  27. namespace cnstream {
  28. uint32_t seed = (uint32_t)time(0);
  29. TEST(CoreConnector, SetGetParams) {
  30. size_t conveyor_count = rand_r(&seed) % 100;
  31. size_t conveyor_capacity = rand_r(&seed) % 100;
  32. Connector connector(conveyor_count, conveyor_capacity);
  33. EXPECT_EQ(conveyor_count, connector.GetConveyorCount());
  34. EXPECT_EQ(conveyor_capacity, connector.GetConveyorCapacity());
  35. }
  36. // TEST(CoreConnectorDeathTest, GetConveyor) {
  37. // size_t conveyor_count = 10;
  38. // Connector connector(conveyor_count);
  39. // int idx = rand_r(&seed) % conveyor_count;
  40. // EXPECT_NE(nullptr, connector.GetConveyor(idx));
  41. // EXPECT_DEATH(connector.GetConveyor(conveyor_count + 1), "") << "conveyor vector out of range not reported";
  42. // EXPECT_DEATH(connector.GetConveyor(-1), "") << "conveyor vector out of range not reported";
  43. // }
  44. TEST(CoreConnector, PushPopDataBuffer) {
  45. size_t conveyor_count = 1;
  46. Connector connector(conveyor_count);
  47. CNFrameInfoPtr data = CNFrameInfo::Create("stream_id_0");
  48. connector.PushDataBufferToConveyor(0, data);
  49. CNFrameInfoPtr out_data = connector.PopDataBufferFromConveyor(0);
  50. EXPECT_EQ(data.get(), out_data.get());
  51. }
  52. TEST(CoreConnector, StartStop) {
  53. size_t conveyor_count = 10;
  54. Connector connector(conveyor_count);
  55. connector.Start();
  56. EXPECT_FALSE(connector.IsStopped());
  57. connector.Stop();
  58. EXPECT_TRUE(connector.IsStopped());
  59. }
  60. TEST(CoreConnector, GetConveyorSize) {
  61. size_t conveyor_count = 1;
  62. Connector connector(conveyor_count);
  63. CNFrameInfoPtr data = CNFrameInfo::Create("stream_id_0");
  64. connector.PushDataBufferToConveyor(0, data);
  65. EXPECT_EQ(connector.GetConveyorSize(0), conveyor_count);
  66. }
  67. TEST(CoreConnector, FullEmpty) {
  68. size_t conveyor_count = 1;
  69. size_t conveyor_capacity = 2;
  70. Connector connector(conveyor_count, conveyor_capacity);
  71. EXPECT_TRUE(connector.IsConveyorEmpty(0));
  72. EXPECT_FALSE(connector.IsConveyorFull(0));
  73. CNFrameInfoPtr data = CNFrameInfo::Create("stream_id_0");
  74. connector.PushDataBufferToConveyor(0, data);
  75. CNFrameInfoPtr data1 = CNFrameInfo::Create("stream_id_0");
  76. connector.PushDataBufferToConveyor(0, data1);
  77. EXPECT_FALSE(connector.IsConveyorEmpty(0));
  78. EXPECT_TRUE(connector.IsConveyorFull(0));
  79. }
  80. } // namespace cnstream