Skip to content

Commit

Permalink
Merge branch 'hotfix/#829-Possible-bug-on-assign-of-etl-array' into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
John Wellbelove committed Jan 28, 2024
2 parents 9ed453e + ad33083 commit 3caffa1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
30 changes: 17 additions & 13 deletions include/etl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ namespace etl

static ETL_CONSTANT size_t SIZE = SIZE_;

typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
typedef const T* const_iterator;
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
typedef const T* const_iterator;
typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;

Expand Down Expand Up @@ -386,27 +386,31 @@ namespace etl
/// If the range is smaller than the array then the unused array elements are left unmodified.
///\param first The iterator to the first item in the range.
///\param last The iterator to one past the final item in the range.
///\return An iterator to the first unassigned array element, or end().
//*************************************************************************
template <typename TIterator>
void assign(TIterator first, const TIterator last)
iterator assign(TIterator first, const TIterator last)
{
etl::copy_s(first, last, begin(), end());
return etl::copy_s(first, last, begin(), end());
}

//*************************************************************************
/// Fills the array from the range.
/// If the range is smaller than the array then the unused array elements are initialised with the supplied value.
///\param first The iterator to the first item in the range.
///\param last The iterator to one past the final item in the range.
///\return An iterator to the first array element set to 'value', or end().
//*************************************************************************
template <typename TIterator>
void assign(TIterator first, const TIterator last, parameter_t value)
iterator assign(TIterator first, const TIterator last, parameter_t value)
{
// Copy from the range.
iterator p = etl::copy(first, last, begin());
iterator p = etl::copy_s(first, last, begin(), end());

// Initialise any that are left.
etl::fill(p, end(), value);

return p;
}

//*************************************************************************
Expand Down
17 changes: 12 additions & 5 deletions test/test_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,27 @@ namespace

Data data = { 0 };

Data::iterator result;

// Initial data.
data.assign(std::begin(initial), std::end(initial));
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
bool isEqual = std::equal(data.begin(), data.end(), std::begin(initial));
CHECK(isEqual);

// Assign smaller.
data.assign(std::begin(initial), std::end(initial));
data.assign(&source[0], &source[5]);
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
result = data.assign(&source[0], &source[5]);
CHECK(result == &data[5]);
isEqual = std::equal(data.begin(), data.end(), std::begin(check1));
CHECK(isEqual);

// Assign smaller + default.
data.assign(std::begin(initial), std::end(initial));
data.assign(&source[0], &source[5], 99);
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
result = data.assign(&source[0], &source[5], 99);
CHECK(result == &data[5]);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2));
CHECK(isEqual);
}
Expand Down

0 comments on commit 3caffa1

Please sign in to comment.