etl/test/test_memory.cpp
John Wellbelove 5fb3e4c2e6 Work-In-Progress for full implementation of etl::expected
fix set of ETL_NO_STL flag (#628)

Co-authored-by: Sergey Skorokhod <s.skorokhod@1440.space>

Removed duplicate include

unique_ptr updates - Work in progress

Updated versions & memory.h

Fix duplicate function

Fixed incorrect 'valid' flag in assignment operator for arithmetic specialisation

Updated version and release notes

Fix bug #636 in optional emplace for pod types (#638)

Updated version info

Updated generator test script

Only build tests if top level project (#639)

Removed trailing spaces

Updated version info

Incorrect C++03 enable_if syntax

Updated version info

Don't use `push_macro` and `pull_macro` with Tasking compiler (#643)

* Autodetect Tasking compiler

#642

* Don't use `push_macro` and `pop_macro` for Tasking compiler

#642

Co-authored-by: Todd Snider <tsnider@jlg.com>

#643 Don't use push_macro and pull_macro with Tasking compiler

Updated etl::delgate to handle const functors correctly

Updated version info

Fixed functor delegate enable_if

Updated release notes

Avoid 'missing return statement at end of non-void function' in `etl::visit<>()`. (#645)

* Avoid 'missing return statement at end of non-void function' in `etl::visit<>()`.

For some definitions of `ETL_ASSERT()` there may be no return statement in case of an invalid type.
This results in undefined behavior.

Warning[Pe940]: missing return statement at end of non-void function "etl::visit<TReturn,TVisitor,TVariant>(TVisitor &, TVariant const &) include\etl\private\variant_legacy.h 976

* Use more self-explaining code.

Substitute ET_ASSERT() and return by dedicated macro.
This moves the responsibility of how to handle errors to the dedicated place.

improve is_constructible, is_copy_constructible, is_move_constructible for type traits with default definitions (#648)

Removed unused ETL_USE_MEM_BUILTINS option

Updated version info

Updated release notes

Added etl::result<TValue, void> specialisation

Reverted code for etl::result<void, TError> specialisation

Added etl::result<TValue, void> specialisation

Reverted code for etl::result<void, TError> specialisation

Fixed perfect forwarding for make_xxx helper functions

Don't warn on tag missing when subproject (#653) (#655)

Different solution than proposed in the issue, since that proposed
solution would given unexpected results when an intermediate
(untagged) commit is checked out.

This change simply skips warning about a missing git version when this
is a subproject, and uses the original version calculation logic.

I've also renamed `determine_version` to `determine_version_with_file`.
I'd originally done this in an intermediate version of this PR, but I
think that keeping the renaming is clearer code.

Removed superfluous semicolons

Updated version and release notes

Removed testing for 18.04

Added testing for 22.04

Updated Github Actions for Clang

Updated version and release notes

clang updates for Github Actions

Added missing notes

emplace member functions return reference to emplaced value (#659)

emplace_front, emplace_back updates

Updated version and release info

Improved emplace testing

Changed unit test macro CHECK_FALSE_EQUAL to CHECK_NOT_EQUAL

Improved emplace testing

Changed unit test macro CHECK_FALSE_EQUAL to CHECK_NOT_EQUAL

Improved emplace testing

Work-In-Progress

Work-In-Progress

Added indexed emplace

More typedefs for etl::result

Work in progress

Work in progress

Work in progress

Changed default constructor

Added function comments
2023-01-21 10:55:15 +00:00

1349 lines
41 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "unit_test_framework.h"
#include "etl/memory.h"
#include "etl/debug_count.h"
#include "data.h"
#include <string>
#include <array>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <stdint.h>
#include <vector>
#include <memory>
namespace
{
typedef std::string non_trivial_t;
typedef uint32_t trivial_t;
typedef TestDataM<int> moveable_t;
const size_t SIZE = 10UL;
std::array<non_trivial_t, SIZE> test_data_non_trivial =
{
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten"
};
std::array<trivial_t, SIZE> test_data_trivial =
{
0x11223344UL, 0x22334455UL, 0x33445566UL, 0x44556677UL, 0x55667788UL,
0x66778899UL, 0x778899AAUL, 0x8899AABBUL, 0x99AABBCCUL, 0xAABBCCDDUL
};
non_trivial_t test_item_non_trivial("eleven");
non_trivial_t test_item_non_trivial_null("");
trivial_t test_item_trivial(0xBBCCDDEEUL);
char buffer_non_trivial[sizeof(non_trivial_t) * SIZE];
char buffer_trivial[sizeof(trivial_t) * SIZE];
char buffer_moveable[sizeof(moveable_t) * SIZE];
non_trivial_t* output_non_trivial = reinterpret_cast<non_trivial_t*>(buffer_non_trivial);
trivial_t* output_trivial = reinterpret_cast<trivial_t*>(buffer_trivial);
moveable_t* output_moveable = reinterpret_cast<moveable_t*>(buffer_moveable);
struct overloaded
{
overloaded* operator&()
{
return nullptr;
}
};
//***********************************
template <typename T>
struct NoDelete
{
NoDelete()
{
}
void operator()(T*) const
{
}
};
//***********************************
template <typename T>
struct NoDelete<T[]>
{
NoDelete()
{
}
template <class U>
void operator()(U* /*p*/) const
{
}
};
}
namespace
{
SUITE(test_memory)
{
//*************************************************************************
TEST(test_addressof)
{
int i;
CHECK(&i == etl::addressof(i));
overloaded ol;
CHECK(&ol != etl::addressof(ol));
CHECK(reinterpret_cast<overloaded*>(&reinterpret_cast<char&>(ol)) == etl::addressof(ol));
}
//*************************************************************************
TEST(test_create_destroy_trivial)
{
char n[sizeof(trivial_t)];
trivial_t* pn = reinterpret_cast<trivial_t*>(n);
// Non count.
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_default_at(pn);
CHECK_EQUAL(0xFFFFFFFFUL, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(0xFFFFFFFFUL, *pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_value_at(pn);
CHECK_EQUAL(0x00000000UL, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(0x00000000UL, *pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_copy_at(pn, test_item_trivial);
CHECK_EQUAL(test_item_trivial, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(test_item_trivial, *pn);
// Count.
size_t count = 0UL;
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_default_at(pn, count);
CHECK_EQUAL(0xFFFFFFFFUL, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(0xFFFFFFFFUL, *pn);
CHECK_EQUAL(0U, count);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_value_at(pn, count);
CHECK_EQUAL(0x00000000UL, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(0x00000000UL, *pn);
CHECK_EQUAL(0U, count);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_copy_at(pn, test_item_trivial, count);
CHECK_EQUAL(test_item_trivial, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(test_item_trivial, *pn);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_create_destroy_non_trivial)
{
char n[sizeof(non_trivial_t)];
non_trivial_t* pn = reinterpret_cast<non_trivial_t*>(n);
// Non count.
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_default_at(pn);
CHECK_EQUAL(test_item_non_trivial_null, *pn);
etl::destroy_at(pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_value_at(pn);
CHECK_EQUAL(test_item_non_trivial_null, *pn);
etl::destroy_at(pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_copy_at(pn, test_item_non_trivial);
CHECK_EQUAL(test_item_non_trivial, *pn);
etl::destroy_at(pn);
// Count.
size_t count = 0UL;
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_default_at(pn, count);
CHECK_EQUAL(test_item_non_trivial_null, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(0U, count);
count = 0;
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_value_at(pn, count);
CHECK_EQUAL(test_item_non_trivial_null, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(0U, count);
count = 0;
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::create_copy_at(pn, test_item_non_trivial, count);
CHECK_EQUAL(test_item_non_trivial, *pn);
CHECK_EQUAL(1U, count);
etl::destroy_at(pn, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_construct_destroy_trivial)
{
char n[sizeof(trivial_t)];
trivial_t* pn = reinterpret_cast<trivial_t*>(n);
// Non count.
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::construct_at(pn);
CHECK_EQUAL(0UL, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(0UL, *pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::construct_at(pn);
CHECK_EQUAL(0x00000000UL, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(0x00000000UL, *pn);
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::construct_at(pn, test_item_trivial);
CHECK_EQUAL(test_item_trivial, *pn);
etl::destroy_at(pn);
CHECK_EQUAL(test_item_trivial, *pn);
}
//*************************************************************************
TEST(test_uninitialized_fill_n_trivial)
{
// Also tests uninitialized_fill.
// Non count.
trivial_t* p = reinterpret_cast<trivial_t*>(buffer_trivial);
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0);
etl::uninitialized_fill_n(p, SIZE, test_item_trivial);
trivial_t* result;
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == test_item_trivial; });
CHECK(result == output_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0);
etl::uninitialized_fill_n(p, SIZE, test_item_trivial, count);
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == test_item_trivial; });
CHECK(result == output_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_fill_n_non_trivial)
{
// Also tests uninitialized_fill.
// Non count.
non_trivial_t* p = reinterpret_cast<non_trivial_t*>(buffer_non_trivial);
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
etl::uninitialized_fill_n(p, SIZE, test_item_non_trivial);
non_trivial_t* result;
result = std::find_if_not(output_non_trivial, output_non_trivial + SIZE, [](const non_trivial_t& i) { return i == test_item_non_trivial; });
CHECK(result == output_non_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
etl::uninitialized_fill_n(p, SIZE, test_item_non_trivial, count);
result = std::find_if_not(output_non_trivial,
output_non_trivial + SIZE,
[](non_trivial_t i) { return i == test_item_non_trivial; });
CHECK(result == output_non_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_copy_n_trivial)
{
// Also tests uninitialized_copy.
bool is_equal;
// Non count.
trivial_t* p = reinterpret_cast<trivial_t*>(buffer_trivial);
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0);
etl::uninitialized_copy_n(test_data_trivial.begin(), SIZE, p);
is_equal = std::equal(output_trivial, output_trivial + SIZE, test_data_trivial.begin());
CHECK(is_equal);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0);
etl::uninitialized_copy_n(test_data_trivial.begin(), SIZE, p, count);
is_equal = std::equal(output_trivial, output_trivial + SIZE, test_data_trivial.begin());
CHECK(is_equal);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_copy_n_non_trivial)
{
// Also tests uninitialized_copy.
bool is_equal;
// Non count.
non_trivial_t* p = reinterpret_cast<non_trivial_t*>(buffer_non_trivial);
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
etl::uninitialized_copy_n(test_data_non_trivial.begin(), SIZE, p);
is_equal = std::equal(output_non_trivial, output_non_trivial + SIZE, test_data_non_trivial.begin());
CHECK(is_equal);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
etl::uninitialized_copy_n(test_data_non_trivial.begin(), SIZE, p, count);
is_equal = std::equal(output_non_trivial, output_non_trivial + SIZE, test_data_non_trivial.begin());
CHECK(is_equal);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_move)
{
bool is_equal;
// Non count.
moveable_t* p = reinterpret_cast<moveable_t*>(buffer_moveable);
std::fill(std::begin(buffer_moveable), std::end(buffer_moveable), 0);
{
std::array<moveable_t, SIZE> test_data_moveable =
{
moveable_t(0), moveable_t(1), moveable_t(2), moveable_t(3), moveable_t(4),
moveable_t(5), moveable_t(6), moveable_t(7), moveable_t(8), moveable_t(9)
};
etl::uninitialized_move(test_data_moveable.begin(), test_data_moveable.end(), p);
}
is_equal = (output_moveable[0] == moveable_t(0)) &&
(output_moveable[1] == moveable_t(1)) &&
(output_moveable[2] == moveable_t(2)) &&
(output_moveable[3] == moveable_t(3)) &&
(output_moveable[4] == moveable_t(4)) &&
(output_moveable[5] == moveable_t(5)) &&
(output_moveable[6] == moveable_t(6)) &&
(output_moveable[7] == moveable_t(7)) &&
(output_moveable[8] == moveable_t(8)) &&
(output_moveable[9] == moveable_t(9));
CHECK(is_equal);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
{
std::array<moveable_t, SIZE> test_data_moveable =
{
moveable_t(0), moveable_t(1), moveable_t(2), moveable_t(3), moveable_t(4),
moveable_t(5), moveable_t(6), moveable_t(7), moveable_t(8), moveable_t(9)
};
etl::uninitialized_move(test_data_moveable.begin(), test_data_moveable.end(), p, count);
}
is_equal = (output_moveable[0] == moveable_t(0)) &&
(output_moveable[1] == moveable_t(1)) &&
(output_moveable[2] == moveable_t(2)) &&
(output_moveable[3] == moveable_t(3)) &&
(output_moveable[4] == moveable_t(4)) &&
(output_moveable[5] == moveable_t(5)) &&
(output_moveable[6] == moveable_t(6)) &&
(output_moveable[7] == moveable_t(7)) &&
(output_moveable[8] == moveable_t(8)) &&
(output_moveable[9] == moveable_t(9));
CHECK(is_equal);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_move_n)
{
bool is_equal;
// Non count.
moveable_t* p = reinterpret_cast<moveable_t*>(buffer_moveable);
std::fill(std::begin(buffer_moveable), std::end(buffer_moveable), 0);
{
std::array<moveable_t, SIZE> test_data_moveable =
{
moveable_t(0), moveable_t(1), moveable_t(2), moveable_t(3), moveable_t(4),
moveable_t(5), moveable_t(6), moveable_t(7), moveable_t(8), moveable_t(9)
};
etl::uninitialized_move_n(test_data_moveable.begin(), SIZE, p);
}
is_equal = (output_moveable[0] == moveable_t(0)) &&
(output_moveable[1] == moveable_t(1)) &&
(output_moveable[2] == moveable_t(2)) &&
(output_moveable[3] == moveable_t(3)) &&
(output_moveable[4] == moveable_t(4)) &&
(output_moveable[5] == moveable_t(5)) &&
(output_moveable[6] == moveable_t(6)) &&
(output_moveable[7] == moveable_t(7)) &&
(output_moveable[8] == moveable_t(8)) &&
(output_moveable[9] == moveable_t(9));
CHECK(is_equal);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0);
{
std::array<moveable_t, SIZE> test_data_moveable =
{
moveable_t(0), moveable_t(1), moveable_t(2), moveable_t(3), moveable_t(4),
moveable_t(5), moveable_t(6), moveable_t(7), moveable_t(8), moveable_t(9)
};
etl::uninitialized_move_n(test_data_moveable.begin(), SIZE, p, count);
}
is_equal = (output_moveable[0] == moveable_t(0)) &&
(output_moveable[1] == moveable_t(1)) &&
(output_moveable[2] == moveable_t(2)) &&
(output_moveable[3] == moveable_t(3)) &&
(output_moveable[4] == moveable_t(4)) &&
(output_moveable[5] == moveable_t(5)) &&
(output_moveable[6] == moveable_t(6)) &&
(output_moveable[7] == moveable_t(7)) &&
(output_moveable[8] == moveable_t(8)) &&
(output_moveable[9] == moveable_t(9));
CHECK(is_equal);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_default_construct_n_trivial)
{
// Also tests uninitialized_default_construct.
// Non count.
trivial_t* p = reinterpret_cast<trivial_t*>(buffer_trivial);
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0xFFU);
etl::uninitialized_default_construct_n(p, SIZE);
trivial_t* result;
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == 0xFFFFFFFFUL; });
CHECK(result == output_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0xFFU);
etl::uninitialized_default_construct_n(p, SIZE, count);
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == 0xFFFFFFFFUL; });
CHECK(result == output_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_default_construct_n_non_trivial)
{
// Also tests uninitialized_default_construct.
// Non count.
non_trivial_t* p = reinterpret_cast<non_trivial_t*>(buffer_non_trivial);
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0xFFU);
etl::uninitialized_default_construct_n(p, SIZE);
non_trivial_t* result;
result = std::find_if_not(output_non_trivial, output_non_trivial + SIZE, [](non_trivial_t i) { return i == test_item_non_trivial_null; });
CHECK(result == output_non_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0xFFU);
etl::uninitialized_default_construct_n(p, SIZE, count);
result = std::find_if_not(output_non_trivial, output_non_trivial + SIZE, [](non_trivial_t i) { return i == test_item_non_trivial_null; });
CHECK(result == output_non_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_value_construct_n_trivial)
{
// Also tests uninitialized_default_construct.
// Non count.
trivial_t* p = reinterpret_cast<trivial_t*>(buffer_trivial);
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0xFFU);
etl::uninitialized_value_construct_n(p, SIZE);
trivial_t* result;
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == trivial_t(); });
CHECK(result == output_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_trivial), std::end(buffer_trivial), 0xFFU);
etl::uninitialized_value_construct_n(p, SIZE, count);
result = std::find_if_not(output_trivial, output_trivial + SIZE, [](trivial_t i) { return i == trivial_t(); });
CHECK(result == output_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_uninitialized_value_construct_n_non_trivial)
{
// Also tests uninitialized_default_construct.
// Non count.
non_trivial_t* p = reinterpret_cast<non_trivial_t*>(buffer_non_trivial);
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0xFFU);
etl::uninitialized_value_construct_n(p, SIZE);
non_trivial_t* result;
result = std::find_if_not(output_non_trivial, output_non_trivial + SIZE, [](non_trivial_t i) { return i == non_trivial_t(); });
CHECK(result == output_non_trivial + SIZE);
etl::destroy(p, p + SIZE);
// Count.
size_t count = 0UL;
std::fill(std::begin(buffer_non_trivial), std::end(buffer_non_trivial), 0xFFU);
etl::uninitialized_value_construct_n(p, SIZE, count);
result = std::find_if_not(output_non_trivial, output_non_trivial + SIZE, [](non_trivial_t i) { return i == non_trivial_t(); });
CHECK(result == output_non_trivial + SIZE);
CHECK_EQUAL(SIZE, count);
etl::destroy(p, p + SIZE, count);
CHECK_EQUAL(0U, count);
}
//*************************************************************************
TEST(test_create_copy)
{
struct Test : etl::create_copy<Test>
{
std::string text;
};
char buffer[sizeof(Test)];
Test test1;
test1.text = "12345678";
test1.create_copy_at(buffer);
test1.text = "87654321";
Test& test2 = *reinterpret_cast<Test*>(buffer);
CHECK_EQUAL(std::string("87654321"), test1.text);
CHECK_EQUAL(std::string("12345678"), test2.text);
int count = 0;
test1.create_copy_at(buffer, count);
CHECK_EQUAL(1, count);
}
//*************************************************************************
TEST(test_create_make_copy)
{
struct Test : etl::create_copy<Test>
{
std::string text;
};
char buffer[sizeof(Test)];
Test test1;
test1.text = "12345678";
Test& test2 = test1.make_copy_at(buffer);
test1.text = "87654321";
CHECK_EQUAL(std::string("87654321"), test1.text);
CHECK_EQUAL(std::string("12345678"), test2.text);
int count = 0;
test1.make_copy_at(buffer, count);
CHECK_EQUAL(1, count);
}
//*************************************************************************
TEST(test_make_trivial)
{
char n[sizeof(trivial_t)];
trivial_t* pn = reinterpret_cast<trivial_t*>(n);
// Non count.
std::fill(std::begin(n), std::end(n), 0xFFU);
CHECK_EQUAL(0x00000000UL, etl::make_default_at(pn));
std::fill(std::begin(n), std::end(n), 0x00U);
CHECK_EQUAL(0xFFFFFFFFUL, etl::make_value_at(pn, 0xFFFFFFFFU));
std::fill(std::begin(n), std::end(n), 0xFFU);
etl::make_copy_at(pn, test_item_trivial);
CHECK_EQUAL(test_item_trivial, etl::make_copy_at(pn, test_item_trivial));
// Count.
size_t count = 0UL;
std::fill(std::begin(n), std::end(n), 0xFFU);
CHECK_EQUAL(0x00000000UL, etl::make_default_at(pn, count));
CHECK_EQUAL(1U, count);
std::fill(std::begin(n), std::end(n), 0x00U);
CHECK_EQUAL(0xFFFFFFFFUL, etl::make_value_at(pn, 0xFFFFFFFFUL, count));
CHECK_EQUAL(2U, count);
std::fill(std::begin(n), std::end(n), 0xFFU);
CHECK_EQUAL(test_item_trivial, etl::make_copy_at(pn, test_item_trivial, count));
CHECK_EQUAL(3U, count);
}
//*************************************************************************
TEST(test_memory_clear)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data = { 0xFFFFFFFFUL, char(0xFFU) };
etl::memory_clear(data);
CHECK_EQUAL(0x00000000UL, data.d1);
CHECK_EQUAL(0x00, data.d2);
}
//*************************************************************************
TEST(test_memory_clear_range_pointer_n)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data[3] = { { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) } };
etl::memory_clear_range(data, 3);
CHECK_EQUAL(0x00000000UL, data[0].d1);
CHECK_EQUAL(0x00U, data[0].d2);
CHECK_EQUAL(0x00000000UL, data[1].d1);
CHECK_EQUAL(0x00U, data[1].d2);
CHECK_EQUAL(0x00000000UL, data[2].d1);
CHECK_EQUAL(0x00U, data[2].d2);
}
//*************************************************************************
TEST(test_memory_clear_range_pointer_pointer)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data[3] = { { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) } };
etl::memory_clear_range(std::begin(data), std::end(data));
CHECK_EQUAL(0x00000000UL, data[0].d1);
CHECK_EQUAL(0x00U, data[0].d2);
CHECK_EQUAL(0x00000000UL, data[1].d1);
CHECK_EQUAL(0x00U, data[1].d2);
CHECK_EQUAL(0x00000000UL, data[2].d1);
CHECK_EQUAL(0x00U, data[2].d2);
}
//*************************************************************************
TEST(test_memory_set)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data = { 0xFFFFFFFFUL, char(0xFFU) };
etl::memory_set(data, 0x5A);
CHECK_EQUAL(0x5A5A5A5AUL, data.d1);
CHECK_EQUAL(0x5AU, data.d2);
}
//*************************************************************************
TEST(test_memory_set_range_pointer_n)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data[3] = { { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) } };
etl::memory_set_range(data, 3, 0x5A);
CHECK_EQUAL(0x5A5A5A5AUL, data[0].d1);
CHECK_EQUAL(0x5AU, data[0].d2);
CHECK_EQUAL(0x5A5A5A5AUL, data[1].d1);
CHECK_EQUAL(0x5AU, data[1].d2);
CHECK_EQUAL(0x5A5A5A5AUL, data[2].d1);
CHECK_EQUAL(0x5AU, data[2].d2);
}
//*************************************************************************
TEST(test_memory_set_range_pointer_pointer)
{
struct Data
{
uint32_t d1;
char d2;
};
Data data[3] = { { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) }, { 0xFFFFFFFFUL, char(0xFFU) } };
etl::memory_set_range(std::begin(data), std::end(data), 0x5A);
CHECK_EQUAL(0x5A5A5A5AUL, data[0].d1);
CHECK_EQUAL(0x5AU, data[0].d2);
CHECK_EQUAL(0x5A5A5A5AUL, data[1].d1);
CHECK_EQUAL(0x5AU, data[1].d2);
CHECK_EQUAL(0x5A5A5A5AUL, data[2].d1);
CHECK_EQUAL(0x5AU, data[2].d2);
}
//*************************************************************************
TEST(test_unique_ptr_default_construction)
{
etl::unique_ptr<int> up;
CHECK(up.get() == nullptr);
CHECK(!bool(up));
}
//*************************************************************************
TEST(test_unique_ptr_from_pointer_construction)
{
etl::unique_ptr<int> up(new int(1));
CHECK(up.get() != nullptr);
CHECK(bool(up));
CHECK_EQUAL(1, *up);
}
//*************************************************************************
TEST(test_unique_ptr_move_construction)
{
etl::unique_ptr<int> up1(new int(1));
etl::unique_ptr<int> up2(std::move(up1));
CHECK(up1.get() == nullptr);
CHECK(!bool(up1));
CHECK(up2.get() != nullptr);
CHECK(bool(up2));
CHECK_EQUAL(1, *up2);
}
//*************************************************************************
TEST(test_unique_ptr_release)
{
auto buffer = new int;
etl::unique_ptr<int> up(buffer);
CHECK(up.release() != nullptr);
CHECK(!bool(up));
delete buffer;
}
//*************************************************************************
TEST(test_unique_ptr_reset)
{
etl::unique_ptr<int> up(new int(1));
int* p = new int(2);
CHECK_EQUAL(1, *up);
up.reset(p);
CHECK_EQUAL(2, *up);
}
//*************************************************************************
TEST(test_unique_ptr_swap)
{
etl::unique_ptr<int> up1(new int(1));
etl::unique_ptr<int> up2(new int(2));
up1.swap(up2);
CHECK_EQUAL(2, *up1);
CHECK_EQUAL(1, *up2);
}
//*************************************************************************
TEST(test_unique_ptr_from_nullptr_assignment)
{
etl::unique_ptr<int> up(new int);
up = nullptr;
CHECK(up.get() == nullptr);
CHECK(!bool(up));
}
//*************************************************************************
TEST(test_unique_ptr_move_assignment)
{
etl::unique_ptr<int> up1(new int(1));
etl::unique_ptr<int> up2(new int(2));
up1 = std::move(up2);
CHECK(!bool(up2));
CHECK_EQUAL(2, *up1);
}
//*************************************************************************
TEST(test_unique_ptr_comparison_tests)
{
int* p1 = (int*)1U;
int* p2 = (int*)2U;
etl::unique_ptr<int, NoDelete<int>> up1(p1);
etl::unique_ptr<int, NoDelete<int>> up2(p1);
etl::unique_ptr<int, NoDelete<int>> up3(p2);
CHECK(up1 == up2);
CHECK(!(up1 == up3));
CHECK(!(up1 < up2));
CHECK(up1 < up3);
CHECK(!(up3 <= up1));
CHECK(up1 <= up2);
CHECK(up1 <= up3);
CHECK(!(up1 > up2));
CHECK(up3 > up1);
CHECK(!(up1 >= up3));
CHECK(up2 >= up1);
CHECK(up3 >= up1);
}
//*************************************************************************
TEST(test_unique_ptr_from_array_pointer_construction)
{
etl::unique_ptr<int[]> up(new int[4]);
std::iota(&up[0], &up[4], 0);
CHECK(up.get() != nullptr);
CHECK(bool(up));
CHECK_EQUAL(0, up[0]);
CHECK_EQUAL(1, up[1]);
CHECK_EQUAL(2, up[2]);
CHECK_EQUAL(3, up[3]);
}
//*************************************************************************
TEST(test_unique_ptr_move_array_construction)
{
etl::unique_ptr<int[]> up1(new int[4]);
std::iota(&up1[0], &up1[4], 0);
etl::unique_ptr<int[]> up2(std::move(up1));
CHECK(up1.get() == nullptr);
CHECK(!bool(up1));
CHECK(up2.get() != nullptr);
CHECK(bool(up2));
CHECK_EQUAL(0, up2[0]);
CHECK_EQUAL(1, up2[1]);
CHECK_EQUAL(2, up2[2]);
CHECK_EQUAL(3, up2[3]);
}
//*************************************************************************
TEST(test_unique_ptr_array_release)
{
auto buffer = new int[4];
etl::unique_ptr<int[]> up(buffer);
std::iota(&up[0], &up[4], 0);
CHECK(up.release() != nullptr);
CHECK(!bool(up));
delete[] buffer;
}
//*************************************************************************
TEST(test_unique_ptr_array_reset)
{
etl::unique_ptr<int[]> up(new int[4]);
std::iota(&up[0], &up[4], 0);
int* p = new int[4];
std::iota(p, p + 4, 4);
CHECK_EQUAL(0, up[0]);
CHECK_EQUAL(1, up[1]);
CHECK_EQUAL(2, up[2]);
CHECK_EQUAL(3, up[3]);
up.reset(p);
CHECK_EQUAL(4, up[0]);
CHECK_EQUAL(5, up[1]);
CHECK_EQUAL(6, up[2]);
CHECK_EQUAL(7, up[3]);
}
//*************************************************************************
TEST(test_unique_ptr_array_swap)
{
etl::unique_ptr<int[]> up1(new int[4]);
std::iota(&up1[0], &up1[4], 0);
etl::unique_ptr<int[]> up2(new int[4]);
std::iota(&up2[0], &up2[4], 4);
up1.swap(up2);
CHECK_EQUAL(4, up1[0]);
CHECK_EQUAL(5, up1[1]);
CHECK_EQUAL(6, up1[2]);
CHECK_EQUAL(7, up1[3]);
CHECK_EQUAL(0, up2[0]);
CHECK_EQUAL(1, up2[1]);
CHECK_EQUAL(2, up2[2]);
CHECK_EQUAL(3, up2[3]);
}
//*************************************************************************
TEST(test_unique_ptr_array_from_nullptr_assignment)
{
etl::unique_ptr<int[]> up(new int[4]);
up = nullptr;
CHECK(up.get() == nullptr);
CHECK(!bool(up));
}
//*************************************************************************
TEST(test_unique_ptr_array_move_assignment)
{
etl::unique_ptr<int[]> up1(new int[4]);
std::iota(&up1[0], &up1[4], 0);
etl::unique_ptr<int[]> up2(new int[4]);
std::iota(&up2[0], &up2[4], 4);
up1 = std::move(up2);
CHECK(!bool(up2));
CHECK_EQUAL(4, up1[0]);
CHECK_EQUAL(5, up1[1]);
CHECK_EQUAL(6, up1[2]);
CHECK_EQUAL(7, up1[3]);
}
//*************************************************************************
TEST(test_unique_ptr_custom_deleter)
{
//*******************************
struct Object
{
Object()
: count(1)
{
}
void Delete()
{
count = 0;
}
int count;
};
//*******************************
struct Deleter
{
void operator()(Object* p)
{
p->Delete();
}
};
Deleter deleter;
Object object;
CHECK_EQUAL(1, object.count);
{
etl::unique_ptr<Object, Deleter> up(&object, deleter);
}
CHECK_EQUAL(0, object.count);
}
//*************************************************************************
TEST(test_uninitialized_buffer)
{
typedef etl::uninitialized_buffer<sizeof(uint32_t), 4, etl::alignment_of_v<uint32_t>> storage32_t;
size_t alignment = etl::alignment_of_v<storage32_t>;
size_t expected = std::alignment_of_v<uint32_t>;
CHECK_EQUAL(expected, alignment);
}
//*************************************************************************
TEST(test_uninitialized_buffer_of)
{
typedef etl::uninitialized_buffer_of<uint32_t, 4> storage32_t;
static storage32_t buffer;
uint32_t* i = buffer;
const uint32_t* ci = buffer;
CHECK(i == ci);
buffer[0] = 0U;
buffer[1] = 1U;
buffer[2] = 2U;
buffer[3] = 3U;
CHECK_EQUAL(0U, buffer[0]);
CHECK_EQUAL(1U, buffer[1]);
CHECK_EQUAL(2U, buffer[2]);
CHECK_EQUAL(3U, buffer[3]);
const storage32_t& refbuffer = buffer;
CHECK_EQUAL(0U, refbuffer[0]);
CHECK_EQUAL(1U, refbuffer[1]);
CHECK_EQUAL(2U, refbuffer[2]);
CHECK_EQUAL(3U, refbuffer[3]);
size_t alignment = etl::alignment_of_v<storage32_t>;
size_t expected = std::alignment_of_v<uint32_t>;
CHECK_EQUAL(expected, alignment);
}
//*************************************************************************
TEST(test_mem_copy_pointer_pointer_pointer)
{
uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
etl::mem_copy(src, src + 8, dst);
CHECK(std::equal(src, src + 8, dst));
}
//*************************************************************************
TEST(test_mem_copy_pointer_length_pointer)
{
uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
etl::mem_copy(src, 8, dst);
CHECK(std::equal(src, src + 8, dst));
}
//*************************************************************************
TEST(test_mem_move_pointer_pointer_pointer)
{
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
etl::mem_move(data, data + 8, data + 4);
CHECK(std::equal(expected, expected + 8, data + 4));
}
//*************************************************************************
TEST(test_mem_move_pointer_length_pointer)
{
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
etl::mem_move(data, 8, data + 4);
CHECK(std::equal(expected, expected + 8, data + 4));
}
//*************************************************************************
TEST(test_mem_compare_pointer_pointer_pointer)
{
uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
CHECK(etl::mem_compare(data, data + 8, same) == 0);
CHECK(etl::mem_compare(data, data + 8, grtr) > 0);
CHECK(etl::mem_compare(data, data + 8, less) < 0);
}
//*************************************************************************
TEST(test_mem_compare_pointer_length_pointer)
{
uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
CHECK(etl::mem_compare(data, 8, same) == 0);
CHECK(etl::mem_compare(data, 8, grtr) > 0);
CHECK(etl::mem_compare(data, 8, less) < 0);
}
//*************************************************************************
TEST(test_mem_set_pointer_pointer)
{
uint32_t data[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
uint32_t expected[8] = { 0, 0x5A5A5A5A, 0x5A5A5A5A, 0x5A5A5A5A, 0x5A5A5A5A, 0, 0, 0 };
etl::mem_set(data + 1, data + 5, 0x5A);
CHECK(std::equal(expected, expected + 8, data));
}
//*************************************************************************
TEST(test_mem_set_pointer_length)
{
uint32_t data[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
uint32_t expected[8] = { 0, 0x5A5A5A5A, 0x5A5A5A5A, 0x5A5A5A5A, 0x5A5A5A5A, 0, 0, 0 };
etl::mem_set(data + 1, 4, 0x5A);
CHECK(std::equal(expected, expected + 8, data));
}
//*************************************************************************
TEST(test_mem_char_pointer_pointer)
{
uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67294501, 0x45016723, 0x01324576, 0x76453201 };
char *p1 = etl::mem_char(data, data + 8, 0x29);
char* p2 = etl::mem_char(data, data + 8, 0x99);
CHECK_EQUAL(uint32_t(0x29), uint32_t(*p1));
CHECK((reinterpret_cast<char*>(data) + 18) == p1);
CHECK((reinterpret_cast<char*>(data) + 32) == p2);
}
//*************************************************************************
TEST(test_mem_char_pointer_pointer_const)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67294501, 0x45016723, 0x01324576, 0x76453201 };
const char* p1 = etl::mem_char(data, data + 8, 0x29);
const char* p2 = etl::mem_char(data, data + 8, 0x99);
CHECK_EQUAL(uint32_t(0x29), uint32_t(*p1));
CHECK((reinterpret_cast<const char*>(data) + 18) == p1);
CHECK((reinterpret_cast<const char*>(data) + 32) == p2);
}
//*************************************************************************
TEST(test_mem_char_pointer_length)
{
uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67294501, 0x45016723, 0x01324576, 0x76453201 };
char* p1 = etl::mem_char(data, 8, 0x29);
char* p2 = etl::mem_char(data, 8, 0x99);
CHECK_EQUAL(uint32_t(0x29), uint32_t(*p1));
CHECK((reinterpret_cast<char*>(data) + 18) == p1);
CHECK((reinterpret_cast<char*>(data) + 32) == p2);
}
//*************************************************************************
TEST(test_mem_char_pointer_length_const)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67294501, 0x45016723, 0x01324576, 0x76453201 };
const char* p1 = etl::mem_char(data, 8, 0x29);
const char* p2 = etl::mem_char(data, 8, 0x99);
CHECK_EQUAL(uint32_t(0x29), uint32_t(*p1));
CHECK((reinterpret_cast<const char*>(data) + 18) == p1);
CHECK((reinterpret_cast<const char*>(data) + 32) == p2);
}
//*************************************************************************
class Base
{
public:
virtual ~Base() {};
virtual void function() = 0;
};
static bool function_was_called = false;
class Derived : public Base
{
public:
Derived()
{
function_was_called = false;
}
void function()
{
function_was_called = true;
}
};
void call(etl::unique_ptr<Base> ptr)
{
ptr->function();
}
TEST(test_derived_type)
{
CHECK(!function_was_called);
etl::unique_ptr<Derived> ptr(new Derived());
CHECK(ptr.get() != ETL_NULLPTR);
call(etl::move(ptr));
CHECK(function_was_called);
CHECK(ptr.get() == ETL_NULLPTR);
}
};
}