variadic_tests.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  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. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_variadic_tests"
  18. #include <audio_utils/variadic_utils.h>
  19. #include <stdio.h>
  20. #include <gtest/gtest.h>
  21. // Our near expectation is 16x the bit that doesn't fit the mantissa.
  22. // this works so long as we add values close in exponent with each other
  23. // realizing that errors accumulate as the sqrt of N (random walk, lln, etc).
  24. #define TEST_EXPECT_NEAR(e, v) \
  25. EXPECT_NEAR((e), (v), abs((e) * std::numeric_limits<decltype(e)>::epsilon() * 8))
  26. #define PRINT_AND_EXPECT_EQ(expected, expr) { \
  27. auto value = (expr); \
  28. printf("(%s): %s\n", #expr, std::to_string(value).c_str()); \
  29. if ((expected) == (expected)) { EXPECT_EQ((expected), (value)); } \
  30. EXPECT_EQ((expected) != (expected), (value) != (value)); /* nan check */\
  31. }
  32. #define PRINT_AND_EXPECT_NEAR(expected, expr) { \
  33. auto ref = (expected); \
  34. auto value = (expr); \
  35. printf("(%s): %s\n", #expr, std::to_string(value).c_str()); \
  36. TEST_EXPECT_NEAR(ref, value); \
  37. }
  38. TEST(variadic_tests, printing)
  39. {
  40. // for operator overloading...
  41. using namespace android::audio_utils;
  42. // print simple, deep value
  43. std::cout << "std::make_tuple(1, 2, 3)= " << std::make_tuple(1, 2, 3) << "\n";
  44. std::cout << "std::make_pair(1, std::make_pair(0, 1))= "
  45. << std::make_pair(1, std::make_pair(0, 1)) << "\n";
  46. }
  47. TEST(variadic_tests, equivalence)
  48. {
  49. using android::audio_utils::equivalent;
  50. auto deep = std::make_pair(1., std::make_pair(2, 3));
  51. EXPECT_TRUE(equivalent(deep, deep));
  52. EXPECT_TRUE(equivalent(std::make_pair(1, 2), std::make_tuple(1, 2)));
  53. EXPECT_FALSE(equivalent(std::make_pair(1, 2), std::make_pair(0, 2)));
  54. EXPECT_FALSE(equivalent(std::make_pair(1, 2), 1));
  55. EXPECT_FALSE(equivalent(0, 2));
  56. EXPECT_TRUE(equivalent(1, 1.));
  57. }
  58. TEST(variadic_tests, template_checks)
  59. {
  60. EXPECT_FALSE(android::audio_utils::is_variadic<double>::value);
  61. using tuple_t = std::tuple<double, double>;
  62. EXPECT_TRUE(android::audio_utils::is_variadic<tuple_t>::value);
  63. EXPECT_TRUE(android::audio_utils::is_tuple<tuple_t>::value);
  64. EXPECT_FALSE(android::audio_utils::is_pair<tuple_t>::value);
  65. EXPECT_FALSE(android::audio_utils::is_array<tuple_t>::value);
  66. EXPECT_FALSE(std::is_array<tuple_t>::value);
  67. using pair_t = std::pair<double, double>;
  68. EXPECT_TRUE(android::audio_utils::is_variadic<pair_t>::value);
  69. EXPECT_FALSE(android::audio_utils::is_tuple<pair_t>::value);
  70. EXPECT_TRUE(android::audio_utils::is_pair<pair_t>::value);
  71. EXPECT_FALSE(android::audio_utils::is_array<pair_t>::value);
  72. EXPECT_FALSE(std::is_array<pair_t>::value);
  73. using array_t = std::array<double, 2>;
  74. EXPECT_TRUE(android::audio_utils::is_variadic<array_t>::value);
  75. EXPECT_FALSE(android::audio_utils::is_tuple<array_t>::value);
  76. EXPECT_FALSE(android::audio_utils::is_pair<array_t>::value);
  77. EXPECT_TRUE(android::audio_utils::is_array<array_t>::value);
  78. EXPECT_FALSE(std::is_array<array_t>::value);
  79. EXPECT_FALSE(android::audio_utils::is_iterator<char>::value);
  80. EXPECT_TRUE(android::audio_utils::is_iterator<char *>::value);
  81. EXPECT_TRUE(android::audio_utils::is_iterator<decltype(std::vector<int>{}.begin())>::value);
  82. }
  83. TEST(variadic_tests, basic_math)
  84. {
  85. // for operator overloading...
  86. using namespace android::audio_utils;
  87. using tuple_t = std::tuple<double, double>;
  88. tuple_t x{1, 2};
  89. tuple_t y{0, 3};
  90. double z = 3;
  91. std::cout << "x=" << x << " y=" << y << " x+y=" << (x + y) << "\n";
  92. std::cout << "x=" << x << " y=" << y << " x*y=" << (x * y) << "\n";
  93. std::cout << "x=" << x << " z=" << z << " x+z=" << (x + z) << "\n";
  94. std::cout << "x=" << x << " z=" << z << " x*z=" << (x * z) << "\n";
  95. std::cout << "x=" << x << " y=" << y << " innerProduct(x, y)=" << innerProduct(x, y) << "\n";
  96. std::cout << "x=" << x << " y=" << y << " outerProduct(x, y)=" << outerProduct(x, y) << "\n";
  97. std::cout << "x=" << x << " sqrt(x)=" << android::audio_utils::sqrt(x) << "\n";
  98. std::cout << "x=" << x << " y=" << y
  99. << " min(x, y)" << android::audio_utils::min(x, y) << "\n";
  100. // check opequals mode
  101. std::cout << "x=" << x;
  102. std::cout << " x+=2" << (x += 2) << "\n";
  103. std::cout << "x=" << x << " y=" << y;
  104. std::cout << " x*=y" << (x *= y) << "\n";
  105. using pair_t = std::pair<double, double>;
  106. pair_t px{1, 2};
  107. pair_t py{0, 3};
  108. std::cout << "px=" << px << " py=" << py << " px+py=" << (px + py) << "\n";
  109. std::cout << "px=" << px << " py=" << py << " px*py=" << (px * py) << "\n";
  110. std::cout << "px=" << px << " z=" << z << " px+z=" << (px + z) << "\n";
  111. std::cout << "px=" << px << " z=" << z << " px*z=" << (px * z) << "\n";
  112. std::cout << "px=" << px << " py=" << py << " innerProduct(px, py)="
  113. << innerProduct(px, py) << "\n";
  114. std::cout << "px=" << px << " py=" << py << " outerProduct(px, py)="
  115. << outerProduct(px, py) << "\n";
  116. using array_t = std::array<double, 2>;
  117. array_t ax{1, 2};
  118. array_t ay{0, 3};
  119. std::cout << "ax=" << ax << " ay=" << ay << " ax+ay=" << (ax + ay) << "\n";
  120. std::cout << "ax=" << ax << " ay=" << ay << " ax*ay=" << (ax * ay) << "\n";
  121. std::cout << "ax=" << ax << " z=" << z << " ax+z=" << (ax + z) << "\n";
  122. std::cout << "ax=" << ax << " z=" << z << " ax*z=" << (ax * z) << "\n";
  123. std::cout << "ax=" << px << " ay=" << ay << " innerProduct(ax, ay)="
  124. << innerProduct(ax, ay) << "\n";
  125. std::cout << "ax=" << px << " ay=" << ay << " outerProduct(ax, ay)="
  126. << outerProduct(ax, ay) << "\n";
  127. // deep math
  128. auto deep = std::make_pair(1., std::make_pair(2, 3));
  129. std::cout << "deep= " << deep << "\n";
  130. std::cout << "deep + deep= " << deep + deep << "\n";
  131. std::cout << "deep + 1= " << deep + 1 << "\n";
  132. }