mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830 Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase. Test Plan: CI Reviewed By: zertosh Differential Revision: D27979080 fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
84 lines
3.1 KiB
C++
84 lines
3.1 KiB
C++
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
#include <c10/util/accumulate.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <list>
|
|
#include <vector>
|
|
|
|
using namespace ::testing;
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(accumulate_test, vector_test) {
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
std::vector<int> ints = {1, 2, 3, 4, 5};
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints), 1 + 2 + 3 + 4 + 5);
|
|
EXPECT_EQ(c10::multiply_integers(ints), 1 * 2 * 3 * 4 * 5);
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints.begin(), ints.end()), 1 + 2 + 3 + 4 + 5);
|
|
EXPECT_EQ(
|
|
c10::multiply_integers(ints.begin(), ints.end()), 1 * 2 * 3 * 4 * 5);
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints.begin() + 1, ints.end() - 1), 2 + 3 + 4);
|
|
EXPECT_EQ(
|
|
c10::multiply_integers(ints.begin() + 1, ints.end() - 1), 2 * 3 * 4);
|
|
|
|
EXPECT_EQ(c10::numelements_from_dim(2, ints), 3 * 4 * 5);
|
|
EXPECT_EQ(c10::numelements_to_dim(3, ints), 1 * 2 * 3);
|
|
EXPECT_EQ(c10::numelements_between_dim(2, 4, ints), 3 * 4);
|
|
EXPECT_EQ(c10::numelements_between_dim(4, 2, ints), 3 * 4);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(accumulate_test, list_test) {
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
std::list<int> ints = {1, 2, 3, 4, 5};
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints), 1 + 2 + 3 + 4 + 5);
|
|
EXPECT_EQ(c10::multiply_integers(ints), 1 * 2 * 3 * 4 * 5);
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints.begin(), ints.end()), 1 + 2 + 3 + 4 + 5);
|
|
EXPECT_EQ(
|
|
c10::multiply_integers(ints.begin(), ints.end()), 1 * 2 * 3 * 4 * 5);
|
|
|
|
EXPECT_EQ(c10::numelements_from_dim(2, ints), 3 * 4 * 5);
|
|
EXPECT_EQ(c10::numelements_to_dim(3, ints), 1 * 2 * 3);
|
|
EXPECT_EQ(c10::numelements_between_dim(2, 4, ints), 3 * 4);
|
|
EXPECT_EQ(c10::numelements_between_dim(4, 2, ints), 3 * 4);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(accumulate_test, base_cases) {
|
|
std::vector<int> ints = {};
|
|
|
|
EXPECT_EQ(c10::sum_integers(ints), 0);
|
|
EXPECT_EQ(c10::multiply_integers(ints), 1);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(accumulate_test, errors) {
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
std::vector<int> ints = {1, 2, 3, 4, 5};
|
|
|
|
#ifndef NDEBUG
|
|
EXPECT_THROW(c10::numelements_from_dim(-1, ints), c10::Error);
|
|
#endif
|
|
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_to_dim(-1, ints), c10::Error);
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_between_dim(-1, 10, ints), c10::Error);
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_between_dim(10, -1, ints), c10::Error);
|
|
|
|
EXPECT_EQ(c10::numelements_from_dim(10, ints), 1);
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_to_dim(10, ints), c10::Error);
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_between_dim(10, 4, ints), c10::Error);
|
|
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
|
|
EXPECT_THROW(c10::numelements_between_dim(4, 10, ints), c10::Error);
|
|
}
|