Added new unit tests

This commit is contained in:
jwellbelove 2014-12-08 20:20:06 +00:00
parent 4343e1ad91
commit 6b9ab4c0ab
9 changed files with 1273 additions and 0 deletions

112
test/test_checksum.cpp Normal file
View File

@ -0,0 +1,112 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include <iterator>
#include <string>
#include <vector>
#include <stdint.h>
#include "../checksum.h"
#include "../endian.h"
namespace
{
SUITE(test_checksum)
{
//*************************************************************************
TEST(test_checksum_constructor)
{
std::string data("123456789");
uint8_t sum = etl::checksum<uint8_t>(data.begin(), data.end());
CHECK_EQUAL(221, int(sum));
}
//*************************************************************************
TEST(test_checksum_add_values)
{
std::string data("123456789");
etl::checksum<uint8_t> checksum_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
checksum_calculator += data[i];
}
uint8_t sum = checksum_calculator;
CHECK_EQUAL(221, int(sum));
}
//*************************************************************************
TEST(test_checksum_add_range)
{
std::string data("123456789");
etl::checksum<uint8_t> checksum_calculator;
checksum_calculator.add(data.begin(), data.end());
uint8_t sum = checksum_calculator.value();
CHECK_EQUAL(221, int(sum));
}
//*************************************************************************
TEST(test_checksum_add_range_sum32)
{
std::string data("123456789");
etl::checksum<uint32_t> checksum_calculator;
checksum_calculator.add(data.begin(), data.end());
uint32_t sum = checksum_calculator.value();
CHECK_EQUAL(477, int(sum));
}
//*************************************************************************
TEST(test_checksum_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint32_t> data3 = { 0x01020304, 0x05060708 };
uint8_t sum1 = etl::checksum<uint8_t, etl::endian::little>(data1.begin(), data1.end());
uint8_t sum2 = etl::checksum<uint8_t, etl::endian::little>(data2.begin(), data2.end());
CHECK_EQUAL(int(sum1), int(sum2));
uint8_t sum3 = etl::checksum<uint8_t, etl::endian::big>(data3.begin(), data3.end());
CHECK_EQUAL(int(sum1), int(sum3));
}
};
}

233
test/test_compile.cpp Normal file
View File

@ -0,0 +1,233 @@
#include "algorithm.h"
#include "alignment.h"
#include "array.h"
#include "bitset.h"
#include "container.h"
#include "crc8_ccitt.h"
#include "crc16.h"
#include "crc16_ccitt.h"
#include "crc16_kermit.h"
#include "crc32.h"
#include "crc64_ecma.h"
#include "cyclic_value.h"
#include "deque.h"
#if !defined(COMPILER_IAR)
#include "variant.h"
#endif
#if defined(COMPILER_KEIL)
#pragma diag_suppress 550
#pragma diag_suppress 177
#endif
//*****************************************************************************
// algorithm
//*****************************************************************************
void test_algorithm()
{
int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int data2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int data3[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
std::pair<int*, int*> result1;
std::pair<int, int> result2;
int x = 0;
int y = 1;
int* p;
bool b;
// minmax_element
result1 = etl::minmax_element(etl::begin(data), etl::end(data));
result1 = etl::minmax_element(etl::begin(data), etl::end(data), std::greater<int>());
// minmax
result2 = etl::minmax(x, y);
result2 = etl::minmax(x, y, std::greater<int>());
// is_sorted_until
p = etl::is_sorted_until(etl::begin(data), etl::end(data));
p = etl::is_sorted_until(etl::begin(data), etl::end(data), std::greater<int>());
// is_sorted
b = etl::is_sorted(etl::begin(data), etl::end(data));
b = etl::is_sorted(etl::begin(data), etl::end(data), std::greater<int>());
// copy_n
p = etl::copy_n(etl::begin(data), 5, etl::begin(data2));
// copy_if
p = etl::copy_if(etl::begin(data), etl::end(data), etl::begin(data2), std::bind2nd(std::greater<int>(), 4));
// find_if_not
p = etl::find_if_not(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// all_of
b = etl::all_of(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// any_of
b = etl::any_of(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// none_of
b = etl::none_of(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// is_permutation
b = etl::is_permutation(etl::begin(data), etl::end(data), etl::begin(data2));
b = etl::is_permutation(etl::begin(data), etl::end(data), etl::begin(data2), std::equal_to<int>());
b = etl::is_permutation(etl::begin(data), etl::end(data), etl::begin(data2), etl::end(data2));
b = etl::is_permutation(etl::begin(data), etl::end(data), etl::begin(data2), etl::end(data2), std::equal_to<int>());
// is_partitioned
b = etl::is_partitioned(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// partition_point
p = etl::partition_point(etl::begin(data), etl::end(data), std::bind2nd(std::greater<int>(), 4));
// partition_copy
result1 = etl::partition_copy(etl::begin(data), etl::end(data), etl::begin(data2), etl::begin(data3), std::bind2nd(std::greater<int>(), 4));
}
//*****************************************************************************
// alignment
//*****************************************************************************
#if !defined(COMPILER_IAR)
etl::align_at<char, 16> data5;
void test_alignment()
{
etl::align_at<char, 1> data1;
etl::align_at<char, 2> data2;
etl::align_at<char, 4> data3;
etl::align_at<char, 8> data4;
etl::align_as<char, char> data5;
etl::align_as<char, short> data6;
etl::align_as<char, int> data7;
etl::align_as<char, double> data8;
}
#endif
//*****************************************************************************
// array
//*****************************************************************************
void test_array()
{
etl::array<int, 10> a;
int i = a[4];
int s = a.size();
a.fill(45);
}
//*****************************************************************************
// bitset
//*****************************************************************************
void test_bitset()
{
etl::bitset<7> b7; // uint8_t
etl::bitset<8> b8; // uint8_t
etl::bitset<9> b9; // uint16_t
etl::bitset<15> b15; // uint16_t
etl::bitset<16> b16; // uint16_t
etl::bitset<17> b17; // uint32_t
etl::bitset<31> b31; // uint32_t
etl::bitset<32> b32; // uint32_t
etl::bitset<33> b33; // uint64_t
etl::bitset<63> b63; // uint64_t
etl::bitset<64> b64; // uint64_t
etl::bitset<65> b65; // 2 * uint64_t
b65.set();
b65.set(4, true);
b65.reset();
b65.reset(37);
b65 = ~b65;
bool b = b65[4];
b = b65[64];
b65.flip();
b65.flip(5);
etl::bitset<7>::iterator b1 = b7.begin();
etl::bitset<7>::iterator e1 = b7.end();
etl::bitset<7>::const_iterator b2 = b7.cbegin();
etl::bitset<7>::const_iterator e2 = b7.cend();
++b1;
--e1;
b2 += 2;
e2 -= 2;
*b1 = true;
const etl::bitset<7>::iterator b3 = b7.begin();
bool t = *b3;
}
//*****************************************************************************
// crc
//*****************************************************************************
void test_crc()
{
int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
uint8_t crc1 = etl::crc8_ccitt<>(etl::begin(data), etl::end(data));
uint8_t crc2 = etl::crc8_ccitt<etl::endian::big>(etl::begin(data), etl::end(data));
uint16_t crc3 = etl::crc16<>(etl::begin(data), etl::end(data));
uint16_t crc4 = etl::crc16<etl::endian::big>(etl::begin(data), etl::end(data));
uint16_t crc5 = etl::crc16_ccitt<>(etl::begin(data), etl::end(data));
uint16_t crc6 = etl::crc16_ccitt<etl::endian::big>(etl::begin(data), etl::end(data));
uint16_t crc7 = etl::crc16_kermit<>(etl::begin(data), etl::end(data));
uint16_t crc8 = etl::crc16_kermit<etl::endian::big>(etl::begin(data), etl::end(data));
uint32_t crc9 = etl::crc32<>(etl::begin(data), etl::end(data));
uint32_t crc10 = etl::crc32<etl::endian::big>(etl::begin(data), etl::end(data));
uint64_t crc11 = etl::crc64_ecma<>(etl::begin(data), etl::end(data));
uint64_t crc12 = etl::crc64_ecma<etl::endian::big>(etl::begin(data), etl::end(data));
}
//*****************************************************************************
// deque
//*****************************************************************************
void test_cyclic_value()
{
etl::cyclic_value<int, 1, 10> cv1;
etl::cyclic_value<int> cv2;
cv2.set(3, 8);
cv1.advance(3);
cv1.to_first();
cv1.to_last();
--cv1;
++cv1;
int f = cv1.first();
int l = cv1.last();
int v = cv1;
cv1 = v;
cv1 = cv2;
bool b;
b = cv1 == cv2;
b = cv1 != cv2;
}
//*****************************************************************************
// cyclic_value
//*****************************************************************************
void test_deque()
{
}
//*****************************************************************************
// main
//*****************************************************************************
int main()
{
}

44
test/test_endian.cpp Normal file
View File

@ -0,0 +1,44 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include <string>
#include "../endian.h"
namespace
{
SUITE(test_endian)
{
//*************************************************************************
TEST(test_endianness)
{
// Intel platform is little endian.
CHECK(etl::endianness()() == etl::endian::little);
CHECK(etl::endianness()() != etl::endian::big);
}
};
}

107
test/test_error_handler.cpp Normal file
View File

@ -0,0 +1,107 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include "../error_handler.h"
#include "../exception.h"
bool error_received;
//*****************************************************************************
// An exception.
//*****************************************************************************
class test_exception : public etl::exception
{
public:
test_exception()
: exception("test_exception")
{
error_received = false;
}
};
//*****************************************************************************
// A free error handler function.
//*****************************************************************************
void receive_error(const etl::exception&)
{
error_received = true;
}
//*****************************************************************************
class test_class
{
public:
//***************************************************************************
// A member error handler function.
//***************************************************************************
void receive_error(const etl::exception&)
{
error_received = true;
}
};
namespace
{
SUITE(test_error_handler)
{
//*************************************************************************
TEST(test_free_handler_function)
{
// Create the function callback object.
etl::error_handler::free_function error_callback(receive_error);
// Tell the error handler about it.
etl::error_handler::set_callback(error_callback);
// Log an error.
etl::error_handler::error(test_exception());
CHECK(error_received);
}
//*************************************************************************
TEST(test_member_handler_function)
{
// Create the class that contains the handler.
test_class test;
// Create the function callback object.
etl::error_handler::member_function<test_class> error_callback(test, &test_class::receive_error);
// Tell the error handler about it.
etl::error_handler::set_callback(error_callback);
// Log an error.
etl::error_handler::error(test_exception());
CHECK(error_received);
}
};
}

266
test/test_fnv_1.cpp Normal file
View File

@ -0,0 +1,266 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include <iterator>
#include <string>
#include <vector>
#include <stdint.h>
#include "../fnv_1.h"
#include "../endian.h"
namespace
{
SUITE(test_fnv1)
{
//*************************************************************************
TEST(test_fnv_1_32_constructor)
{
std::string data("123456789");
uint32_t hash = etl::fnv_1_32<>(data.begin(), data.end());
CHECK_EQUAL(0x24148816, hash);
}
//*************************************************************************
TEST(test_fnv_1_32_add_values)
{
std::string data("123456789");
etl::fnv_1_32<> fnv_1_32_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
fnv_1_32_calculator += data[i];
}
uint32_t hash = fnv_1_32_calculator;
CHECK_EQUAL(0x24148816, hash);
}
//*************************************************************************
TEST(test_fnv_1_32_add_range)
{
std::string data("123456789");
etl::fnv_1_32<> fnv_1_32_calculator;
fnv_1_32_calculator.add(data.begin(), data.end());
uint32_t hash = fnv_1_32_calculator.value();
CHECK_EQUAL(0x24148816, hash);
}
//*************************************************************************
TEST(test_fnv_1_32_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint32_t> data3 = { 0x01020304, 0x05060708 };
uint32_t hash1 = etl::fnv_1_32<etl::endian::little>(data1.begin(), data1.end());
uint32_t hash2 = etl::fnv_1_32<etl::endian::little>(data2.begin(), data2.end());
CHECK_EQUAL(hash1, hash2);
uint32_t hash3 = etl::fnv_1_32<etl::endian::big>(data3.begin(), data3.end());
CHECK_EQUAL(hash1, hash3);
}
//*************************************************************************
TEST(test_fnv_1a_32_constructor)
{
std::string data("123456789");
uint32_t hash = etl::fnv_1a_32<>(data.begin(), data.end());
CHECK_EQUAL(0xBB86B11C, hash);
}
//*************************************************************************
TEST(test_fnv_1a_32_add_values)
{
std::string data("123456789");
etl::fnv_1a_32<> fnv_1a_32_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
fnv_1a_32_calculator += data[i];
}
uint32_t hash = fnv_1a_32_calculator;
CHECK_EQUAL(0xBB86B11C, hash);
}
//*************************************************************************
TEST(test_fnv_1a_32_add_range)
{
std::string data("123456789");
etl::fnv_1a_32<> fnv_1a_32_calculator;
fnv_1a_32_calculator.add(data.begin(), data.end());
uint32_t hash = fnv_1a_32_calculator.value();
CHECK_EQUAL(0xBB86B11C, hash);
}
//*************************************************************************
TEST(test_fnv_1a_32_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint32_t> data3 = { 0x01020304, 0x05060708 };
uint32_t hash1 = etl::fnv_1a_32<etl::endian::little>(data1.begin(), data1.end());
uint32_t hash2 = etl::fnv_1a_32<etl::endian::little>(data2.begin(), data2.end());
CHECK_EQUAL(hash1, hash2);
uint32_t hash3 = etl::fnv_1a_32<etl::endian::big>(data3.begin(), data3.end());
CHECK_EQUAL(hash1, hash3);
}
//*************************************************************************
TEST(test_fnv_1_64_constructor)
{
std::string data("123456789");
uint64_t hash = etl::fnv_1_64<>(data.begin(), data.end());
CHECK_EQUAL(0xA72FFC362BF916D6, hash);
}
//*************************************************************************
TEST(test_fnv_1_64_add_values)
{
std::string data("123456789");
etl::fnv_1_64<> fnv_1_64_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
fnv_1_64_calculator += data[i];
}
uint64_t hash = fnv_1_64_calculator;
CHECK_EQUAL(0xA72FFC362BF916D6, hash);
}
//*************************************************************************
TEST(test_fnv_1_64_add_range)
{
std::string data("123456789");
etl::fnv_1_64<> fnv_1_64_calculator;
fnv_1_64_calculator.add(data.begin(), data.end());
uint64_t hash = fnv_1_64_calculator.value();
CHECK_EQUAL(0xA72FFC362BF916D6, hash);
}
//*************************************************************************
TEST(test_fnv_1_64_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint32_t> data3 = { 0x01020304, 0x05060708 };
uint64_t hash1 = etl::fnv_1_64<etl::endian::little>(data1.begin(), data1.end());
uint64_t hash2 = etl::fnv_1_64<etl::endian::little>(data2.begin(), data2.end());
CHECK_EQUAL(hash1, hash2);
uint64_t hash3 = etl::fnv_1_64<etl::endian::big>(data3.begin(), data3.end());
CHECK_EQUAL(hash1, hash3);
}
//*************************************************************************
TEST(test_fnv_1a_64_constructor)
{
std::string data("123456789");
uint64_t hash = etl::fnv_1a_64<>(data.begin(), data.end());
CHECK_EQUAL(0x06D5573923C6CDFC, hash);
}
//*************************************************************************
TEST(test_fnv_1a_64_add_values)
{
std::string data("123456789");
etl::fnv_1a_64<> fnv_1a_64_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
fnv_1a_64_calculator += data[i];
}
uint64_t hash = fnv_1a_64_calculator;
CHECK_EQUAL(0x06D5573923C6CDFC, hash);
}
//*************************************************************************
TEST(test_fnv_1a_64_add_range)
{
std::string data("123456789");
etl::fnv_1a_64<> fnv_1a_64_calculator;
fnv_1a_64_calculator.add(data.begin(), data.end());
uint64_t hash = fnv_1a_64_calculator.value();
CHECK_EQUAL(0x06D5573923C6CDFC, hash);
}
//*************************************************************************
TEST(test_fnv_1a_64_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint32_t> data3 = { 0x01020304, 0x05060708 };
uint64_t hash1 = etl::fnv_1a_64<etl::endian::little>(data1.begin(), data1.end());
uint64_t hash2 = etl::fnv_1a_64<etl::endian::little>(data2.begin(), data2.end());
CHECK_EQUAL(hash1, hash2);
uint64_t hash3 = etl::fnv_1a_64<etl::endian::big>(data3.begin(), data3.end());
CHECK_EQUAL(hash1, hash3);
}
};
}

97
test/test_functional.cpp Normal file
View File

@ -0,0 +1,97 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include "../functional.h"
#include <list>
#include <vector>
#include <numeric>
namespace
{
SUITE(test_functional)
{
//*************************************************************************
TEST(test_reference_wrapper)
{
int a = 0;
etl::reference_wrapper<int> ra(a);
ra = 1;
CHECK_EQUAL(1, a);
CHECK_EQUAL(1, ra);
a = 2;
CHECK_EQUAL(2, a);
CHECK_EQUAL(2, ra);
}
//*************************************************************************
TEST(test_reference_wrapper_container)
{
std::list<int> test = { 0, 1, 2, 3, 4 };
std::list<int> compare = { 5, 6, 7, 8, 9 };
std::vector<etl::reference_wrapper<int>> test_ref(test.begin(), test.end());
std::iota(test_ref.begin(), test_ref.end(), 5);
std::list<int>::const_iterator itest = test.begin();
std::list<int>::const_iterator icompare = compare.begin();
std::vector<etl::reference_wrapper<int>>::const_iterator ivector = test_ref.begin();
while (icompare != compare.end())
{
CHECK_EQUAL(*icompare, *itest);
++itest;
++icompare;
}
}
//*************************************************************************
TEST(test_ref)
{
int a = 0;
etl::reference_wrapper<int> ra = etl::ref(a);
ra = 1;
CHECK_EQUAL(1, a);
CHECK_EQUAL(1, ra);
}
//*************************************************************************
TEST(test_cref)
{
int a = 0;
etl::reference_wrapper<const int> ra = etl::cref(a);
a = 1;
CHECK_EQUAL(1, a);
CHECK_EQUAL(1, ra);
}
};
}

164
test/test_hash.cpp Normal file
View File

@ -0,0 +1,164 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include <iterator>
#include <string>
#include <vector>
#include <stdint.h>
#include "../hash.h"
namespace
{
SUITE(test_hash)
{
//*************************************************************************
TEST(test_hash_bool)
{
size_t hash = etl::hash<bool>()(false);
CHECK_EQUAL(0, hash);
hash = etl::hash<bool>()(true);
CHECK_EQUAL(1, hash);
}
//*************************************************************************
TEST(test_hash_char)
{
size_t hash = etl::hash<char>()(char(0x5A));
CHECK_EQUAL(0x5A, hash);
}
//*************************************************************************
TEST(test_hash_signed_char)
{
size_t hash = etl::hash<signed char>()(signed char(0x5A));
CHECK_EQUAL(0x5A, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_char)
{
size_t hash = etl::hash<unsigned char>()(unsigned char(0x5A));
CHECK_EQUAL(0x5A, hash);
}
//*************************************************************************
TEST(test_hash_short)
{
size_t hash = etl::hash<short>()(short(0x5AA5));
CHECK_EQUAL(0x5AA5, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_short)
{
size_t hash = etl::hash<unsigned short>()(unsigned short(0x5AA5));
CHECK_EQUAL(0x5AA5, hash);
}
//*************************************************************************
TEST(test_hash_int)
{
size_t hash = etl::hash<int>()(int(0x5AA555AA));
CHECK_EQUAL(0x5AA555AA, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_int)
{
size_t hash = etl::hash<unsigned int>()(unsigned int(0x5AA555AA));
CHECK_EQUAL(0x5AA555AA, hash);
}
//*************************************************************************
TEST(test_hash_long)
{
size_t hash = etl::hash<long>()(long(0x5AA555AA));
CHECK_EQUAL(0x5AA555AA, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_long)
{
size_t hash = etl::hash<unsigned long>()(unsigned long(0x5AA555AA));
CHECK_EQUAL(0x5AA555AA, hash);
}
//*************************************************************************
TEST(test_hash_long_long)
{
size_t hash = etl::hash<long long>()(long long(0x5AA555AA3CC333CC));
CHECK_EQUAL(0xEC6A8D69, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_long_long)
{
size_t hash = etl::hash<unsigned long long>()(unsigned long long(0x5AA555AA3CC333CC));
CHECK_EQUAL(0xEC6A8D69, hash);
}
//*************************************************************************
TEST(test_hash_float)
{
size_t hash = etl::hash<float>()(float(1.2345));
CHECK_EQUAL(0x3F9E0419, hash);
}
//*************************************************************************
TEST(test_hash_double)
{
size_t hash = etl::hash<double>()(double(1.2345));
CHECK_EQUAL(0x86FBF224, hash);
}
//*************************************************************************
TEST(test_hash_pointer)
{
int i;
size_t hash = etl::hash<int*>()(&i);
CHECK_EQUAL(size_t(&i), hash);
}
};
}

View File

@ -0,0 +1,69 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include "../instance_count.h"
#include <list>
#include <vector>
#include <numeric>
namespace
{
SUITE(test_instance_count)
{
//*************************************************************************
TEST(test_count)
{
struct Test1 : public etl::instance_count<Test1>
{};
struct Test2 : public etl::instance_count<Test2>
{};
CHECK_EQUAL(0, Test1::get_instance_count());
CHECK_EQUAL(0, Test2::get_instance_count());
//Test1 test1a;
//CHECK_EQUAL(1, Test1::get_instance_count());
//CHECK_EQUAL(0, Test2::get_instance_count());
//Test1 test1b;
//Test2 test2a;
//CHECK_EQUAL(2, Test1::get_instance_count());
//CHECK_EQUAL(1, Test2::get_instance_count());
//Test2* ptest2b = new Test2;
//CHECK_EQUAL(2, Test1::get_instance_count());
//CHECK_EQUAL(2, Test2::get_instance_count());
//delete ptest2b;
//CHECK_EQUAL(2, Test1::get_instance_count());
//CHECK_EQUAL(1, Test2::get_instance_count());
}
};
}

181
test/test_pool.cpp Normal file
View File

@ -0,0 +1,181 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
Copyright(c) 2014 jwellbelove
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 <UnitTest++/UnitTest++.h>
#include "../pool.h"
struct Test_Data
{
int i;
char c;
};
namespace
{
SUITE(test_pool)
{
//*************************************************************************
TEST(test_allocate)
{
etl::pool<Test_Data, 4> pool;
Test_Data* p1;
Test_Data* p2;
Test_Data* p3;
Test_Data* p4;
CHECK_NO_THROW(p1 = pool.allocate());
CHECK_NO_THROW(p2 = pool.allocate());
CHECK_NO_THROW(p3 = pool.allocate());
CHECK_NO_THROW(p4 = pool.allocate());
CHECK(p1 != p2);
CHECK(p1 != p3);
CHECK(p1 != p4);
CHECK(p2 != p3);
CHECK(p2 != p4);
CHECK(p3 != p4);
CHECK_THROW(pool.allocate(), etl::pool_no_allocation);
}
//*************************************************************************
TEST(test_release)
{
etl::pool<Test_Data, 4> pool;
Test_Data* p1 = pool.allocate();
Test_Data* p2 = pool.allocate();
Test_Data* p3 = pool.allocate();
Test_Data* p4 = pool.allocate();
CHECK_NO_THROW(pool.release(p2));
CHECK_NO_THROW(pool.release(*p3));
CHECK_NO_THROW(pool.release(p1));
CHECK_NO_THROW(pool.release(*p4));
CHECK_EQUAL(4, pool.available());
Test_Data not_in_pool;
CHECK_THROW(pool.release(not_in_pool), etl::pool_object_not_in_pool);
}
//*************************************************************************
TEST(test_allocate_release)
{
etl::pool<Test_Data, 4> pool;
Test_Data* p1 = pool.allocate();
Test_Data* p2 = pool.allocate();
Test_Data* p3 = pool.allocate();
Test_Data* p4 = pool.allocate();
// Allocated p1, p2, p3, p4
CHECK_EQUAL(0, pool.available());
CHECK_NO_THROW(pool.release(p2));
CHECK_NO_THROW(pool.release(p3));
// Allocated p1, p4
CHECK_EQUAL(2, pool.available());
Test_Data* p5 = pool.allocate();
Test_Data* p6 = pool.allocate();
// Allocated p1, p4, p5, p6
CHECK_EQUAL(0, pool.available());
CHECK(p5 != p1);
CHECK(p5 != p4);
CHECK(p6 != p1);
CHECK(p6 != p4);
CHECK_NO_THROW(pool.release(p5));
// Allocated p1, p4, p6
CHECK_EQUAL(1, pool.available());
Test_Data* p7 = pool.allocate();
// Allocated p1, p4, p6, p7
CHECK(p7 != p1);
CHECK(p7 != p4);
CHECK(p7 != p6);
CHECK(pool.empty());
}
//*************************************************************************
TEST(test_available)
{
etl::pool<Test_Data, 4> pool;
CHECK_EQUAL(4, pool.available());
Test_Data* p;
p = pool.allocate();
CHECK_EQUAL(3, pool.available());
p = pool.allocate();
CHECK_EQUAL(2, pool.available());
p = pool.allocate();
CHECK_EQUAL(1, pool.available());
p = pool.allocate();
CHECK_EQUAL(0, pool.available());
}
//*************************************************************************
TEST(test_empty)
{
etl::pool<Test_Data, 4> pool;
CHECK_EQUAL(4, pool.available());
Test_Data* p;
p = pool.allocate();
CHECK(!pool.empty());
p = pool.allocate();
CHECK(!pool.empty());
p = pool.allocate();
CHECK(!pool.empty());
p = pool.allocate();
CHECK(pool.empty());
}
};
}