test_fr_controller.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <chrono>
  22. #include <memory>
  23. #include <string>
  24. #include <thread>
  25. #include "cnrt.h"
  26. #include "cnstream_source.hpp"
  27. #include "data_handler_file.hpp"
  28. #include "test_base.hpp"
  29. namespace cnstream {
  30. TEST(SourceFrController, SetAndGetFrameRate) {
  31. uint32_t seed = (uint32_t)time(0);
  32. FrController fr_controller(0);
  33. EXPECT_EQ(fr_controller.GetFrameRate(), (uint32_t)0);
  34. uint32_t loop_num = 10;
  35. while (loop_num--) {
  36. uint32_t frame_rate = rand_r(&seed) % 100;
  37. fr_controller.SetFrameRate(frame_rate);
  38. EXPECT_EQ(fr_controller.GetFrameRate(), frame_rate);
  39. }
  40. }
  41. TEST(SourceFrController, Control) {
  42. uint32_t frame_rate = 0;
  43. FrController fr_controller(frame_rate);
  44. EXPECT_EQ(fr_controller.GetFrameRate(), frame_rate);
  45. // return directly
  46. EXPECT_NO_THROW(fr_controller.Control());
  47. // reset frame rate to 10
  48. frame_rate = 10;
  49. fr_controller.SetFrameRate(frame_rate);
  50. auto start = std::chrono::steady_clock::now();
  51. fr_controller.Start();
  52. std::chrono::duration<double, std::milli> diff;
  53. uint32_t loop_num = 10;
  54. while (loop_num--) {
  55. fr_controller.Control();
  56. auto end = std::chrono::steady_clock::now();
  57. diff = end - start;
  58. EXPECT_TRUE(diff.count() >= static_cast<double>(1000) / static_cast<double>(frame_rate));
  59. start = end;
  60. }
  61. // reset frame rate to 30
  62. frame_rate = 30;
  63. fr_controller.SetFrameRate(frame_rate);
  64. loop_num = 20;
  65. while (loop_num--) {
  66. fr_controller.Control();
  67. auto end = std::chrono::steady_clock::now();
  68. diff = end - start;
  69. EXPECT_TRUE(diff.count() >= static_cast<double>(1000) / static_cast<double>(frame_rate));
  70. start = end;
  71. }
  72. }
  73. } // namespace cnstream