utility_test.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <initializer_list>
  5. #include <string>
  6. #include <tuple>
  7. #include <type_traits>
  8. #include <vector>
  9. #include "opentelemetry/nostd/utility.h"
  10. namespace nostd = opentelemetry::nostd;
  11. template <class T>
  12. auto IsDataCallable(const T &t) -> decltype(nostd::data(t), std::true_type{});
  13. std::false_type IsDataCallable(...);
  14. template <class T>
  15. auto IsSizeCallable(const T &t) -> decltype(nostd::size(t), std::true_type{});
  16. std::false_type IsSizeCallable(...);
  17. TEST(UtilityTest, Data)
  18. {
  19. std::vector<int> v = {1, 2, 3};
  20. int array[3] = {1, 2, 3};
  21. std::initializer_list<int> list{1, 2, 3};
  22. int x = 0;
  23. std::ignore = x;
  24. EXPECT_EQ(nostd::data(v), v.data());
  25. EXPECT_EQ(nostd::data(array), array);
  26. EXPECT_EQ(nostd::data(list), list.begin());
  27. EXPECT_FALSE(decltype(IsDataCallable(x)){});
  28. }
  29. TEST(UtilityTest, Size)
  30. {
  31. std::vector<int> v = {1, 2, 3};
  32. int array[3] = {1, 2, 3};
  33. int x = 0;
  34. std::ignore = x;
  35. EXPECT_EQ(nostd::size(v), v.size());
  36. EXPECT_EQ(nostd::size(array), 3);
  37. EXPECT_FALSE(decltype(IsSizeCallable(x)){});
  38. }
  39. TEST(UtilityTest, MakeIndexSequence)
  40. {
  41. EXPECT_TRUE((std::is_same<nostd::make_index_sequence<0>, nostd::index_sequence<>>::value));
  42. EXPECT_TRUE((std::is_same<nostd::make_index_sequence<1>, nostd::index_sequence<0>>::value));
  43. EXPECT_TRUE((std::is_same<nostd::make_index_sequence<2>, nostd::index_sequence<0, 1>>::value));
  44. }