Numeric.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/std/containers/vector.h>
  10. #include <AzCore/std/numeric.h>
  11. namespace UnitTest
  12. {
  13. class AccumulateFixture : public LeakDetectionFixture
  14. {
  15. };
  16. TEST_F(AccumulateFixture, AccumulateWithoutBinaryOperator)
  17. {
  18. using ::testing::Eq;
  19. AZStd::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20. const int total = AZStd::accumulate(AZStd::cbegin(numbers), AZStd::cend(numbers), 0);
  21. EXPECT_THAT(total, Eq(55));
  22. }
  23. TEST_F(AccumulateFixture, AccumulateWithBinaryOperator)
  24. {
  25. using ::testing::ElementsAre;
  26. using ::testing::Eq;
  27. const AZStd::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  28. const AZStd::vector<int> evenNumbers = AZStd::accumulate(
  29. AZStd::cbegin(numbers), AZStd::cend(numbers), AZStd::vector<int>{}, [](AZStd::vector<int> acc, const int number) {
  30. if (number % 2 == 0)
  31. {
  32. acc.push_back(number);
  33. }
  34. return acc;
  35. });
  36. EXPECT_THAT(evenNumbers.size(), Eq(5));
  37. EXPECT_THAT(evenNumbers, ElementsAre(2, 4, 6, 8, 10));
  38. }
  39. class InnerProductFixture : public LeakDetectionFixture
  40. {
  41. };
  42. TEST_F(InnerProductFixture, InnerProductWithoutBinaryOperator)
  43. {
  44. using ::testing::Eq;
  45. AZStd::vector<int> numbers1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  46. AZStd::vector<int> numbers2{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
  47. const int total = AZStd::inner_product(AZStd::cbegin(numbers1), AZStd::cend(numbers1), AZStd::cbegin(numbers2), 0);
  48. EXPECT_THAT(total, Eq(770));
  49. }
  50. TEST_F(InnerProductFixture, InnerProductWithBinaryOperator)
  51. {
  52. using ::testing::ElementsAre;
  53. using ::testing::Eq;
  54. const AZStd::vector<int> number_values{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  55. const AZStd::vector<AZStd::string> number_names{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
  56. struct NumberLabel
  57. {
  58. int m_value;
  59. AZStd::string m_name;
  60. };
  61. const AZStd::vector<NumberLabel> numberLabels = AZStd::inner_product(
  62. AZStd::cbegin(number_values), AZStd::cend(number_values), AZStd::cbegin(number_names), AZStd::vector<NumberLabel>{},
  63. [](AZStd::vector<NumberLabel> acc, const NumberLabel& numberLabel) {
  64. acc.push_back(numberLabel);
  65. return acc;
  66. },
  67. [](const int value, const AZStd::string& label) {
  68. return NumberLabel{value, label};
  69. });
  70. EXPECT_THAT(numberLabels.size(), Eq(10));
  71. for (size_t i = 0; i < numberLabels.size(); ++i)
  72. {
  73. EXPECT_THAT(numberLabels[i].m_value, Eq(number_values[i]));
  74. EXPECT_THAT(numberLabels[i].m_name, Eq(number_names[i]));
  75. }
  76. }
  77. } // namespace UnitTest