test_env.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef _MSC_VER
  22. #include "windows.h"
  23. #else
  24. #include "sys/sysinfo.h"
  25. #endif
  26. #include <cstdlib>
  27. #include <string>
  28. #include "util/env.h"
  29. namespace infer_server {
  30. TEST(InferServerUtil, CpuCoreNumber) {
  31. int core_number = GetCpuCoreNumber();
  32. EXPECT_EQ(get_nprocs(), core_number);
  33. EXPECT_EQ(sysconf(_SC_NPROCESSORS_ONLN), core_number);
  34. }
  35. TEST(InferServerUtil, GetBoolFromEnv) {
  36. const std::string env_str = "CNIS_TEST_ENV_BOOL";
  37. // clear env
  38. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  39. // get default value if env is not set
  40. EXPECT_FALSE(GetBoolFromEnv(env_str));
  41. EXPECT_FALSE(GetBoolFromEnv(env_str, false));
  42. EXPECT_TRUE(GetBoolFromEnv(env_str, true));
  43. // only false and 0 means false
  44. ASSERT_EQ(setenv(env_str.c_str(), "false", 1), 0);
  45. EXPECT_FALSE(GetBoolFromEnv(env_str, true));
  46. ASSERT_EQ(setenv(env_str.c_str(), "0", 1), 0);
  47. EXPECT_FALSE(GetBoolFromEnv(env_str, true));
  48. // otherwise means true
  49. ASSERT_EQ(setenv(env_str.c_str(), "true", 1), 0);
  50. EXPECT_TRUE(GetBoolFromEnv(env_str, false));
  51. ASSERT_EQ(setenv(env_str.c_str(), "True", 1), 0);
  52. EXPECT_TRUE(GetBoolFromEnv(env_str, false));
  53. ASSERT_EQ(setenv(env_str.c_str(), "1", 1), 0);
  54. EXPECT_TRUE(GetBoolFromEnv(env_str, false));
  55. ASSERT_EQ(setenv(env_str.c_str(), "ON", 1), 0);
  56. EXPECT_TRUE(GetBoolFromEnv(env_str, false));
  57. ASSERT_EQ(setenv(env_str.c_str(), "asguwieb", 1), 0);
  58. EXPECT_TRUE(GetBoolFromEnv(env_str, false));
  59. // clear env
  60. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  61. }
  62. TEST(InferServerUtil, GetIntFromEnv) {
  63. const std::string env_str = "CNIS_TEST_ENV_INT";
  64. // clear env
  65. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  66. // get default value if env is not set
  67. EXPECT_EQ(GetIntFromEnv(env_str), 0);
  68. EXPECT_EQ(GetIntFromEnv(env_str, 1), 1);
  69. EXPECT_EQ(GetIntFromEnv(env_str, -2), -2);
  70. EXPECT_EQ(GetIntFromEnv(env_str, 4), 4);
  71. ASSERT_EQ(setenv(env_str.c_str(), "2", 1), 0);
  72. EXPECT_EQ(GetIntFromEnv(env_str), 2);
  73. ASSERT_EQ(setenv(env_str.c_str(), "-17", 1), 0);
  74. EXPECT_EQ(GetIntFromEnv(env_str), -17);
  75. ASSERT_EQ(setenv(env_str.c_str(), "124", 1), 0);
  76. EXPECT_EQ(GetIntFromEnv(env_str), 124);
  77. ASSERT_EQ(setenv(env_str.c_str(), "vbuiwe", 1), 0);
  78. EXPECT_THROW(GetIntFromEnv(env_str), std::invalid_argument);
  79. ASSERT_EQ(setenv(env_str.c_str(), "213546189236846283746182768323", 1), 0);
  80. EXPECT_THROW(GetIntFromEnv(env_str), std::out_of_range);
  81. // clear env
  82. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  83. }
  84. TEST(InferServerUtil, GetUlongFromEnv) {
  85. const std::string env_str = "CNIS_TEST_ENV_ULONG";
  86. // clear env
  87. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  88. // get default value if env is not set
  89. EXPECT_EQ(GetUlongFromEnv(env_str), 0u);
  90. EXPECT_EQ(GetUlongFromEnv(env_str, 1), 1u);
  91. EXPECT_EQ(GetUlongFromEnv(env_str, 2), 2u);
  92. EXPECT_EQ(GetUlongFromEnv(env_str, 4), 4u);
  93. ASSERT_EQ(setenv(env_str.c_str(), "2", 1), 0);
  94. EXPECT_EQ(GetUlongFromEnv(env_str), 2u);
  95. ASSERT_EQ(setenv(env_str.c_str(), "1732", 1), 0);
  96. EXPECT_EQ(GetUlongFromEnv(env_str), 1732u);
  97. ASSERT_EQ(setenv(env_str.c_str(), "124", 1), 0);
  98. EXPECT_EQ(GetUlongFromEnv(env_str), 124u);
  99. ASSERT_EQ(setenv(env_str.c_str(), "gwgsdawe", 1), 0);
  100. EXPECT_THROW(GetUlongFromEnv(env_str), std::invalid_argument);
  101. ASSERT_EQ(setenv(env_str.c_str(), "214235234612353546189324234236846283746182768323", 1), 0);
  102. EXPECT_THROW(GetUlongFromEnv(env_str), std::out_of_range);
  103. // clear env
  104. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  105. }
  106. TEST(InferServerUtil, GetStringFromEnv) {
  107. const std::string env_str = "CNIS_TEST_ENV_STRING";
  108. // clear env
  109. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  110. std::string str_value = "some str";
  111. // get default value if env is not set
  112. EXPECT_EQ(GetStringFromEnv(env_str), "");
  113. EXPECT_EQ(GetStringFromEnv(env_str, str_value), str_value);
  114. str_value = "gbawrebawe";
  115. ASSERT_EQ(setenv(env_str.c_str(), str_value.c_str(), 1), 0);
  116. EXPECT_EQ(GetStringFromEnv(env_str), str_value);
  117. str_value = "aweuggaibefiu";
  118. ASSERT_EQ(setenv(env_str.c_str(), str_value.c_str(), 1), 0);
  119. EXPECT_EQ(GetStringFromEnv(env_str), str_value);
  120. str_value = "aeui12529hgdkd29";
  121. ASSERT_EQ(setenv(env_str.c_str(), str_value.c_str(), 1), 0);
  122. EXPECT_EQ(GetStringFromEnv(env_str), str_value);
  123. // clear env
  124. ASSERT_EQ(unsetenv(env_str.c_str()), 0);
  125. }
  126. } // namespace infer_server