Add col and row funcs an tests

This commit is contained in:
2026-02-17 22:27:25 +03:00
parent 7195734765
commit 97c14870c0
2 changed files with 489 additions and 4 deletions

View File

@@ -993,6 +993,243 @@ TEST(Vector2DEdgeTest, single_element_vector) {
EXPECT_EQ(single.element(0, 0), 42);
}
// ==================== PROXY ADDITIONAL OPERATIONS TESTS ====================
TEST_F(Vector2DTest, row_proxy_forEach_modifies_elements) {
auto row = vec[5];
row.forEach([](int & e) { e += 100; });
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
EXPECT_EQ(vec.element(5, c), 5 * COLS_COUNT_INIT + c + 100);
}
}
TEST_F(Vector2DTest, row_proxy_forEach_const_iterates) {
const auto & constVec = vec;
auto row = constVec[5];
size_t count = 0;
row.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, COLS_COUNT_INIT);
}
TEST_F(Vector2DTest, row_proxy_fill_sets_all_elements) {
auto row = vec[12];
row.fill(999);
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
EXPECT_EQ(vec.element(12, c), 999);
}
}
TEST_F(Vector2DTest, row_proxy_contains_finds_element) {
auto row = vec[8];
EXPECT_TRUE(row.contains(vec.element(8, 10)));
EXPECT_FALSE(row.contains(-999));
}
TEST_F(Vector2DTest, row_proxy_entries_counts_occurrences) {
auto row = vec[15];
// Add a duplicate
int val = vec.element(15, 5);
vec.element(15, 20) = val;
EXPECT_EQ(row.entries(val), 2);
EXPECT_EQ(row.entries(-999), 0);
}
TEST_F(Vector2DTest, row_proxy_entries_with_predicate_counts_matches) {
auto row = vec[20];
auto isEven = [](const int & e) { return e % 2 == 0; };
int expected = 0;
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
if (row[c] % 2 == 0) ++expected;
}
EXPECT_EQ(row.entries(isEven), expected);
}
TEST_F(Vector2DTest, row_proxy_any_returns_true_if_any_match) {
auto row = vec[25];
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(row.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(row.any(isPositive));
}
TEST_F(Vector2DTest, row_proxy_every_returns_true_if_all_match) {
auto row = vec[30];
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(row.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(row.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, col_proxy_forEach_modifies_elements) {
auto col = vec.col(7);
col.forEach([](int & e) { e += 50; });
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
EXPECT_EQ(vec.element(r, 7), r * COLS_COUNT_INIT + 7 + 50);
}
}
TEST_F(Vector2DTest, col_proxy_forEach_const_iterates) {
const auto & constVec = vec;
auto col = constVec.col(9);
size_t count = 0;
col.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, ROWS_COUNT_INIT);
}
TEST_F(Vector2DTest, col_proxy_fill_sets_all_elements) {
auto col = vec.col(11);
col.fill(777);
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
EXPECT_EQ(vec.element(r, 11), 777);
}
}
TEST_F(Vector2DTest, col_proxy_contains_finds_element) {
auto col = vec.col(13);
EXPECT_TRUE(col.contains(vec.element(5, 13)));
EXPECT_FALSE(col.contains(-999));
}
TEST_F(Vector2DTest, col_proxy_entries_counts_occurrences) {
auto col = vec.col(17);
int val = vec.element(3, 17);
vec.element(22, 17) = val; // duplicate
EXPECT_EQ(col.entries(val), 2);
EXPECT_EQ(col.entries(-999), 0);
}
TEST_F(Vector2DTest, col_proxy_entries_with_predicate_counts_matches) {
auto col = vec.col(19);
auto isOdd = [](const int & e) { return e % 2 != 0; };
int expected = 0;
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
if (col[r] % 2 != 0) ++expected;
}
EXPECT_EQ(col.entries(isOdd), expected);
}
TEST_F(Vector2DTest, col_proxy_any_returns_true_if_any_match) {
auto col = vec.col(21);
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(col.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(col.any(isPositive));
}
TEST_F(Vector2DTest, col_proxy_every_returns_true_if_all_match) {
auto col = vec.col(23);
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(col.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(col.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, rowconst_proxy_forEach_iterates) {
const auto & constVec = vec;
auto row = constVec[5];
size_t count = 0;
row.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, COLS_COUNT_INIT);
}
TEST_F(Vector2DTest, rowconst_proxy_contains_finds_element) {
const auto & constVec = vec;
auto row = constVec[6];
EXPECT_TRUE(row.contains(vec.element(6, 10)));
EXPECT_FALSE(row.contains(-999));
}
TEST_F(Vector2DTest, rowconst_proxy_entries_counts_occurrences) {
const auto & constVec = vec;
auto row = constVec[7];
int val = vec.element(7, 5);
// We can't modify through const proxy, but duplicates already exist from previous tests
EXPECT_GE(row.entries(val), 1); // at least one
}
TEST_F(Vector2DTest, rowconst_proxy_entries_with_predicate_counts_matches) {
const auto & constVec = vec;
auto row = constVec[9];
auto isEven = [](const int & e) { return e % 2 == 0; };
int expected = 0;
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
if (vec.element(9, c) % 2 == 0) ++expected;
}
EXPECT_EQ(row.entries(isEven), expected);
}
TEST_F(Vector2DTest, rowconst_proxy_any_returns_true_if_any_match) {
const auto & constVec = vec;
auto row = constVec[10];
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(row.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(row.any(isPositive));
}
TEST_F(Vector2DTest, rowconst_proxy_every_returns_true_if_all_match) {
const auto & constVec = vec;
auto row = constVec[11];
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(row.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(row.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, colconst_proxy_forEach_iterates) {
const auto & constVec = vec;
auto col = constVec.col(25);
size_t count = 0;
col.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, ROWS_COUNT_INIT);
}
TEST_F(Vector2DTest, colconst_proxy_contains_finds_element) {
const auto & constVec = vec;
auto col = constVec.col(27);
EXPECT_TRUE(col.contains(vec.element(4, 27)));
EXPECT_FALSE(col.contains(-999));
}
TEST_F(Vector2DTest, colconst_proxy_entries_counts_occurrences) {
const auto & constVec = vec;
auto col = constVec.col(29);
int val = vec.element(8, 29);
EXPECT_GE(col.entries(val), 1);
}
TEST_F(Vector2DTest, colconst_proxy_entries_with_predicate_counts_matches) {
const auto & constVec = vec;
auto col = constVec.col(31);
auto isOdd = [](const int & e) { return e % 2 != 0; };
int expected = 0;
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
if (vec.element(r, 31) % 2 != 0) ++expected;
}
EXPECT_EQ(col.entries(isOdd), expected);
}
TEST_F(Vector2DTest, colconst_proxy_any_returns_true_if_any_match) {
const auto & constVec = vec;
auto col = constVec.col(33);
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(col.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(col.any(isPositive));
}
TEST_F(Vector2DTest, colconst_proxy_every_returns_true_if_all_match) {
const auto & constVec = vec;
auto col = constVec.col(0);
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(col.every(isLessThanMax));
auto isNotEven = [](const int & e) { return e % 2 != 0; };
col.forEach([](const int & v) { piCout << v; });
EXPECT_FALSE(col.every(isNotEven));
}
// ==================== OUTPUT TESTS ====================
TEST_F(Vector2DTest, picout_operator_works) {
// Just test that it compiles and doesn't crash