Skip to content

Commit

Permalink
Fix issues pointed by reviews.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcojean committed Mar 25, 2020
1 parent 765e3a1 commit e4c8fe7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 13 additions & 2 deletions core/test/base/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ TYPED_TEST(Array, CanBeResized)
}


TYPED_TEST(Array, VewCanBeResetNotResized)
TYPED_TEST(Array, ViewCanBeResetNotResized)
{
TypeParam data[] = {1, 2, 3};
auto view = gko::Array<TypeParam>::view(this->exec, 3, data);
view.resize_and_reset(1);

EXPECT_EQ(view.get_const_data(), nullptr);
ASSERT_EQ(view.get_num_elems(), 1);
ASSERT_EQ(view.get_num_elems(), 0);
}


Expand Down Expand Up @@ -342,6 +342,7 @@ TYPED_TEST(Array, CopyArrayToArray)
EXPECT_EQ(array.get_data()[2], TypeParam{2});
EXPECT_EQ(array.get_data()[3], TypeParam{1});
EXPECT_EQ(array.get_num_elems(), 4);
EXPECT_NE(array.get_data(), array2.get_data());
ASSERT_EQ(array2.get_num_elems(), 4);
}

Expand Down Expand Up @@ -435,6 +436,11 @@ TYPED_TEST(Array, MoveViewToView)
EXPECT_EQ(view.get_data()[2], TypeParam{2});
EXPECT_EQ(view.get_num_elems(), 3);
EXPECT_EQ(view2.get_data(), nullptr);
EXPECT_NE(data, nullptr);
EXPECT_EQ(data[0], TypeParam{1});
EXPECT_EQ(data[1], TypeParam{2});
EXPECT_EQ(data[2], TypeParam{3});
EXPECT_EQ(data[3], TypeParam{4});
ASSERT_EQ(view2.get_num_elems(), 0);
}

Expand Down Expand Up @@ -468,13 +474,18 @@ TYPED_TEST(Array, MoveArrayToView)
auto view = gko::Array<TypeParam>::view(this->exec, 3, data);
gko::Array<TypeParam> array_size2(this->exec, {5, 4});
gko::Array<TypeParam> array_size4(this->exec, {5, 4, 2, 1});
auto size2_ptr = array_size2.get_data();
auto size4_ptr = array_size4.get_data();

view = std::move(array_size2);

EXPECT_EQ(view.get_data()[0], TypeParam{5});
EXPECT_EQ(view.get_data()[1], TypeParam{4});
EXPECT_EQ(view.get_num_elems(), 2);
EXPECT_NE(view.get_data(), data);
EXPECT_EQ(view.get_data(), size2_ptr);
EXPECT_NO_THROW(view = array_size4);
EXPECT_EQ(view.get_data(), size4_ptr);
EXPECT_EQ(array_size2.get_data(), nullptr);
ASSERT_EQ(array_size2.get_num_elems(), 0);
}
Expand Down
7 changes: 4 additions & 3 deletions include/ginkgo/core/base/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ class Array {
* Depending on whether `a` and `b` are array or view, this happens:
* + `a` and `b` are view, `b` becomes the only valid view of `a`
* + `a` and `b` are array, `b` becomes the only valid array of `a`
* + `a` is `a` view and `b` is an array, `b` frees its data and becomes the
* + `a` is a view and `b` is an array, `b` frees its data and becomes the
* only valid view of `a`
* + `a` is an array and `b` is `a` view, `b` becomes the only valid array
* + `a` is an array and `b` is a view, `b` becomes the only valid array
* of `a`
*
* This does not invoke the constructors of the elements, instead they are
Expand Down Expand Up @@ -400,13 +400,14 @@ class Array {
"gko::Executor (nullptr)");
}

num_elems_ = num_elems;
// For views, we should never allocate any data, all we can do is unset
// the pointer.
if (num_elems > 0 &&
data_.get_deleter().target_type() != typeid(view_deleter)) {
num_elems_ = num_elems;
data_.reset(exec_->alloc<value_type>(num_elems));
} else {
num_elems_ = 0;
data_.reset(nullptr);
}
}
Expand Down

0 comments on commit e4c8fe7

Please sign in to comment.