Skip to content

Commit

Permalink
test: add inplace add, sub, smul, sdiv (#164)
Browse files Browse the repository at this point in the history
- For brevity, consistency and locality, each inplace test is appended to the end of each corresponding non-inplace method. Otherwise we have duplicate code computing the expected value.
- Replaced make_ns_tensor with make_ones_tensor after discussing with Sam, as the former is technically testing a more nuanced case that we will pick up in other tests.
  • Loading branch information
alexallmont committed Dec 13, 2024
1 parent 08a834d commit 1aeae2f
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions algebra/src/test_dense_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,44 @@ TEST_F(DenseTensorFixture, test_borrow_mut)

TEST_F(DenseTensorFixture, test_add)
{
FreeTensor lhs = make_ns_tensor('x', 2);
FreeTensor rhs = make_ns_tensor('y', 3);
FreeTensor lhs = make_ones_tensor('x');
FreeTensor rhs = make_ones_tensor('y');
FreeTensor result = lhs.add(rhs);

FreeTensor expected = make_tensor([](size_t i) {
auto lhs_coeff = rational_poly_scalar(indeterminate_type('x', i), 2);
auto rhs_coeff = rational_poly_scalar(indeterminate_type('y', i), 3);
return lhs_coeff + rhs_coeff;
auto lhs_coeff = rational_poly_scalar(indeterminate_type('x', i), 1);
auto rhs_coeff = rational_poly_scalar(indeterminate_type('y', i), 1);
auto coeff = lhs_coeff + rhs_coeff;
return coeff;
});

ASSERT_EQ(result, expected);

// Test again with inplace version
result = lhs;
result.add_inplace(rhs);
ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_sub)
{
FreeTensor lhs = make_ns_tensor('x', 3);
FreeTensor rhs = make_ns_tensor('y', 5);
FreeTensor lhs = make_ones_tensor('x');
FreeTensor rhs = make_ones_tensor('y');
FreeTensor result = lhs.sub(rhs);

FreeTensor expected = make_tensor([](size_t i) {
auto lhs_coeff = rational_poly_scalar(indeterminate_type('x', i), 3);
auto rhs_coeff = rational_poly_scalar(indeterminate_type('y', i), 5);
return lhs_coeff - rhs_coeff;
auto lhs_coeff = rational_poly_scalar(indeterminate_type('x', i), 1);
auto rhs_coeff = rational_poly_scalar(indeterminate_type('y', i), 1);
auto coeff = lhs_coeff - rhs_coeff;
return coeff;
});

ASSERT_EQ(result, expected);

// Test again with inplace version
result = lhs;
result.sub_inplace(rhs);
ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_mul)
Expand Down Expand Up @@ -201,49 +213,54 @@ TEST_F(DenseTensorFixture, test_uminus)

FreeTensor expected = make_tensor([](size_t i) {
auto key = indeterminate_type('x', i);
auto coeff = rational_poly_scalar(key, 1);
return -coeff;
auto coeff = -rational_poly_scalar(key, 1);
return coeff;
});

ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_smul)
{
FreeTensor lhs = make_ns_tensor('x', 5);
FreeTensor lhs = make_ones_tensor('x');
Scalar rhs(7);
FreeTensor result = lhs.smul(rhs);

FreeTensor expected = make_tensor([](size_t i) {
auto key = indeterminate_type('x', i);
auto coeff = rational_poly_scalar(key, 5);
return coeff * 7;
auto coeff = rational_poly_scalar(key, 7);
return coeff;
});

ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_sdiv)
{
// Test again with inplace version
result = lhs;
result.smul_inplace(rhs);
ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_add_inplace)
TEST_F(DenseTensorFixture, test_sdiv)
{
}
FreeTensor lhs = make_ones_tensor('x');
Scalar rhs(11);
FreeTensor result = lhs.sdiv(rhs);

TEST_F(DenseTensorFixture, test_sub_inplace)
{
}
FreeTensor expected = make_tensor([](size_t i) {
auto key = indeterminate_type('x', i);
auto coeff = rational_poly_scalar(key, 1) / 11;
return coeff;
});

TEST_F(DenseTensorFixture, test_mul_inplace)
{
}
ASSERT_EQ(result, expected);

TEST_F(DenseTensorFixture, test_smul_inplace)
{
// Test again with inplace version
result = lhs;
result.sdiv_inplace(rhs);
ASSERT_EQ(result, expected);
}

TEST_F(DenseTensorFixture, test_sdiv_inplace)
TEST_F(DenseTensorFixture, test_mul_inplace)
{
}

Expand Down

0 comments on commit 1aeae2f

Please sign in to comment.