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/66746 Modified loops in files under fbsource/fbcode/caffe2/ from the format `for(TYPE var=x0;var<x_max;x++)` to the format `for(const auto var: irange(xmax))` This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand. Test Plan: Sandcastle Reviewed By: malfet Differential Revision: D31705361 fbshipit-source-id: 33fd22eb03086d114e2c98e56703e8ec84460268
143 lines
3.1 KiB
C++
143 lines
3.1 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <c10/util/Bitset.h>
|
|
#include <c10/util/irange.h>
|
|
|
|
using c10::utils::bitset;
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenGettingBit_thenIsZero) {
|
|
bitset b;
|
|
for (size_t i = 0; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenUnsettingBit_thenIsZero) {
|
|
bitset b;
|
|
b.unset(4);
|
|
for (size_t i = 0; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenSettingAndUnsettingBit_thenIsZero) {
|
|
bitset b;
|
|
b.set(4);
|
|
b.unset(4);
|
|
for (size_t i = 0; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenSettingBit_thenIsSet) {
|
|
bitset b;
|
|
b.set(6);
|
|
EXPECT_TRUE(b.get(6));
|
|
}
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenSettingBit_thenOthersStayUnset) {
|
|
bitset b;
|
|
b.set(6);
|
|
for (const auto i : c10::irange(6)) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
for (size_t i = 7; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
TEST(BitsetTest, givenNonemptyBitset_whenSettingBit_thenIsSet) {
|
|
bitset b;
|
|
b.set(6);
|
|
b.set(30);
|
|
EXPECT_TRUE(b.get(30));
|
|
}
|
|
|
|
TEST(BitsetTest, givenNonemptyBitset_whenSettingBit_thenOthersStayAtOldValue) {
|
|
bitset b;
|
|
b.set(6);
|
|
b.set(30);
|
|
for (const auto i : c10::irange(6)) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
for (const auto i : c10::irange(7, 30)) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
for (size_t i = 31; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
TEST(BitsetTest, givenNonemptyBitset_whenUnsettingBit_thenIsUnset) {
|
|
bitset b;
|
|
b.set(6);
|
|
b.set(30);
|
|
b.unset(6);
|
|
EXPECT_FALSE(b.get(6));
|
|
}
|
|
|
|
TEST(
|
|
BitsetTest,
|
|
givenNonemptyBitset_whenUnsettingBit_thenOthersStayAtOldValue) {
|
|
bitset b;
|
|
b.set(6);
|
|
b.set(30);
|
|
b.unset(6);
|
|
for (const auto i : c10::irange(30)) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
EXPECT_TRUE(b.get(30));
|
|
for (size_t i = 31; i < bitset::NUM_BITS(); ++i) {
|
|
EXPECT_FALSE(b.get(i));
|
|
}
|
|
}
|
|
|
|
struct IndexCallbackMock final {
|
|
std::vector<size_t> called_for_indices;
|
|
|
|
void operator()(size_t index) {
|
|
called_for_indices.push_back(index);
|
|
}
|
|
|
|
void expect_was_called_for_indices(std::vector<size_t> expected_indices) {
|
|
EXPECT_EQ(expected_indices.size(), called_for_indices.size());
|
|
for (const auto i : c10::irange(expected_indices.size())) {
|
|
EXPECT_EQ(expected_indices[i], called_for_indices[i]);
|
|
}
|
|
}
|
|
};
|
|
|
|
TEST(BitsetTest, givenEmptyBitset_whenCallingForEachBit_thenDoesntCall) {
|
|
IndexCallbackMock callback;
|
|
bitset b;
|
|
b.for_each_set_bit(callback);
|
|
callback.expect_was_called_for_indices({});
|
|
}
|
|
|
|
TEST(
|
|
BitsetTest,
|
|
givenBitsetWithOneBitSet_whenCallingForEachBit_thenCallsForEachBit) {
|
|
IndexCallbackMock callback;
|
|
bitset b;
|
|
b.set(5);
|
|
b.for_each_set_bit(callback);
|
|
callback.expect_was_called_for_indices({5});
|
|
}
|
|
|
|
TEST(
|
|
BitsetTest,
|
|
givenBitsetWithMultipleBitsSet_whenCallingForEachBit_thenCallsForEachBit) {
|
|
IndexCallbackMock callback;
|
|
bitset b;
|
|
b.set(5);
|
|
b.set(2);
|
|
b.set(25);
|
|
b.set(32);
|
|
b.set(50);
|
|
b.set(0);
|
|
b.unset(25);
|
|
b.set(10);
|
|
b.for_each_set_bit(callback);
|
|
callback.expect_was_called_for_indices({0, 2, 5, 10, 32, 50});
|
|
}
|