test_priority.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <algorithm>
  22. #include <limits>
  23. #include <random>
  24. #include "core/priority.h"
  25. using infer_server::Priority;
  26. TEST(InferServerCore, Priority) {
  27. std::random_device rd;
  28. std::mt19937 gen(rd());
  29. std::uniform_int_distribution<int> base_dis(-100, 100);
  30. std::uniform_int_distribution<int> offset_dis(0, 9);
  31. std::uniform_int_distribution<int64_t> bias_dis(std::numeric_limits<int64_t>::min() / 1e7,
  32. std::numeric_limits<int64_t>::max() / 1e7);
  33. for (int i = 0; i < 1000; ++i) {
  34. int base = base_dis(gen);
  35. int64_t bias = bias_dis(gen);
  36. int offset = offset_dis(gen);
  37. Priority p(base);
  38. int64_t major = Priority::ShiftMajor(Priority::BaseToMajor(base));
  39. ASSERT_EQ(Priority::BaseToMajor(base), 10 * std::min(std::max(base, 0), 9));
  40. ASSERT_EQ(Priority::ShiftMajor(offset * 10), static_cast<int64_t>(offset * 10) << 56);
  41. ASSERT_EQ(p.Get(0), major);
  42. ASSERT_EQ(p.Get(bias), major + bias);
  43. ASSERT_EQ(Priority::Offset(major, offset), major + Priority::ShiftMajor(offset));
  44. ASSERT_EQ(Priority::Next(major), Priority::Offset(major, 1));
  45. ASSERT_EQ(p, Priority(base));
  46. if (base > 0 && base < 10) {
  47. ASSERT_GT(p, Priority(base - 1));
  48. } else {
  49. ASSERT_EQ(p, Priority(base - 1));
  50. }
  51. if (base > -1 && base < 9) {
  52. ASSERT_LT(p, Priority(base + 1));
  53. } else {
  54. ASSERT_EQ(p, Priority(base + 1));
  55. }
  56. }
  57. }
  58. TEST(InferServerCore, ConstexprPriority) {
  59. constexpr int base = 6;
  60. constexpr int offset = 2;
  61. constexpr int64_t bias = -23521;
  62. constexpr int64_t major = Priority::ShiftMajor(Priority::BaseToMajor(base));
  63. constexpr Priority c_p(base);
  64. ASSERT_EQ(c_p.Get(0), major);
  65. ASSERT_EQ(c_p.Get(bias), major + bias);
  66. ASSERT_EQ(Priority::Offset(major, offset), major + Priority::ShiftMajor(offset));
  67. ASSERT_EQ(Priority::Next(major), Priority::Offset(major, 1));
  68. ASSERT_EQ(c_p, Priority(base));
  69. if (base > 0 && base < 10) {
  70. ASSERT_GT(c_p, Priority(base - 1));
  71. } else {
  72. ASSERT_EQ(c_p, Priority(base - 1));
  73. }
  74. if (base > -1 && base < 9) {
  75. ASSERT_LT(c_p, Priority(base + 1));
  76. } else {
  77. ASSERT_EQ(c_p, Priority(base + 1));
  78. }
  79. }