ソースを参照

!Compile: Test files for meta:: and ostream_dev

doc
Christos Houtouridis 5年前
コミット
02e3e8e19a
2個のファイルの変更431行の追加0行の削除
  1. +174
    -0
      test/tests/Tmeta.cpp
  2. +257
    -0
      test/tests/test_ostream_dev.cpp

+ 174
- 0
test/tests/Tmeta.cpp ファイルの表示

@@ -0,0 +1,174 @@
/*!
* \file Tmeta.cpp
*
* Copyright (C) 2018 Christos Choutouridis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <utl/meta/meta.h>
#include <gtest/gtest.h>
#include <type_traits>
namespace test_meta {
using namespace utl;
using namespace meta;
/*
* Test integral constant
*/
// Test type_of fixture
template<class T> struct TestTypeOf {
using type = T;
};
TEST(Tmeta_integral, Integreal_type_) {
EXPECT_EQ(true, (std::is_same<int, type_<TestTypeOf<int>>>::value));
}
TEST(Tmeta_integral, IntegrealConstant) {
EXPECT_EQ(true, (std::is_same<int, integral_constant<int, 42>::value_type>::value));
EXPECT_EQ(true, (std::is_same<int, integral_constant<int, 42>::type::value_type>::value));
EXPECT_EQ(42, (integral_constant<int, 0>::value_type(42)));
EXPECT_EQ(42, (integral_constant<int, 42>()));
}
TEST(Tmeta_integral, BasicTypes) {
EXPECT_EQ(true, (std::is_same<bool, bool_<false>::value_type>::value));
EXPECT_EQ(true, bool_<true>::value);
EXPECT_EQ(true, (std::is_same<bool, false_::value_type>::value));
EXPECT_EQ(false, false_::value);
EXPECT_EQ(true, (std::is_same<bool, true_::value_type>::value));
EXPECT_EQ(true, true_::value);
EXPECT_EQ(true, (std::is_same<int, int_<0>::value_type>::value));
EXPECT_EQ(42, int_<42>::value);
EXPECT_EQ(true, (std::is_same<index_t, index_t_<0>::value_type>::value));
EXPECT_EQ(42U, index_t_<42U>::value);
EXPECT_EQ(true, (std::is_same<size_t, size_t_<0>::value_type>::value));
EXPECT_EQ(42U, size_t_<42U>::value);
EXPECT_EQ(sizeof(int), sizeof_<int>::value);
EXPECT_EQ(alignof(int), alignof_<int>::value);
EXPECT_EQ(static_cast<index_t>(-1), Npos::value);
}
/*
* Test integral constant arithmetic operations
*/
TEST(Tmeta_integral, ArithmeticOperations) {
EXPECT_EQ (int_<42>(), inc<int_<41>>());
EXPECT_EQ (int_<42>(), dec<int_<43>>());
EXPECT_EQ (int_<42>(), (add<int_<23>, add<int_<17>, int_<2>>>()));
EXPECT_EQ (int_<42>(), (sub<int_<108>, int_<66>>()));
EXPECT_EQ (int_<42>(), (mult<int_<7>, mult<int_<3>, int_<2>>>()));
EXPECT_EQ (int_<42>(), (divide<int_<210>, int_<5>>()));
EXPECT_EQ (int_<42>(), negate<int_<-42>>());
EXPECT_EQ (int_< 1>(), (modulo<int_<43>, int_<42>>()));
}
/*
* Test logical
*/
TEST(Tmeta_logical, ComparisonOperations) {
EXPECT_EQ (true, (std::is_same<bool_<true>, not_c<false>>::value));
EXPECT_EQ (true, (comp_eq<int_<7>, int_<7>>()));
EXPECT_EQ (true, (comp_ne<int_<42>, int_<7>>()));
EXPECT_EQ (true, (comp_lt<int_<42>, int_<43>>()));
EXPECT_EQ (true, (comp_gt<int_<43>, int_<42>>()));
EXPECT_EQ (true, (comp_le<int_<42>, int_<42>>()));
EXPECT_EQ (true, (comp_ge<int_<42>, int_<42>>()));
}
TEST(Tmeta_logical, BitOperations) {
EXPECT_EQ (0x00, (bitand_<int_<0x55>, int_<0xAA>>()));
EXPECT_EQ (0xFF, (bitor_<int_<0x55>, int_<0xAA>>()));
EXPECT_EQ (0xFA, (bitxor_<int_<0x55>, int_<0xAF>>()));
EXPECT_EQ (0x00, (bitnot_<int_<-1>>()));
//XXX: add shifts
}
TEST(Tmeta_logical, TypeOperations) {
struct Foo {};
struct Bar {};
EXPECT_EQ (true, (std::is_same<bool_<true>, not_<bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<int_<42>, if_c<true, int_<42>, bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<int_<42>, if_<bool_<true>, int_<42>, bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<bool_<true>, or_<bool_<true>, bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<bool_<false>, or_<bool_<false>, bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<bool_<false>, and_<bool_<true>, bool_<false>>>()));
EXPECT_EQ (true, (std::is_same<bool_<true>, and_<bool_<true>, bool_<true>>>()));
EXPECT_EQ (true, (same_<Foo, Foo>()));
EXPECT_EQ (false, (same_<Foo, Bar>()));
EXPECT_EQ (true, (not_same_<Foo, Bar>()));
}
/*
* Test void
*/
TEST(Tmeta_void, VoidType) {
struct Foo {};
struct Bar {};
EXPECT_EQ(true, (std::is_same<void, void_t<int, long, Foo, Bar>>()));
}
/*
* Test invoke
*/
template<class T1, class T2> struct MetaFunction { using type = int; };
template<typename... Args> struct MetaClass {using apply = int; };
TEST(Tmeta_invoke, Invoke) {
//EXPECT_EQ (true, (is_evaluable<MetaFunction, int .long>()));
}
/*
* Test typelist
*/
template<class T1, class T2> struct F {};
TEST(Tmeta_typelist, List_fold) {
struct X1 {};
struct X2 {};
struct X3 {};
struct X4 {};
using Q = quote<F>;
EXPECT_EQ(true, (std::is_same<fold<typelist<>, void, Q>, void>()));
EXPECT_EQ(true, (std::is_same<fold<typelist<X1>, void, Q>, F<void, X1>>()));
EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2>, void, Q>, F<F<void, X1>, X2>>()));
EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2, X3>, void, Q>, F<F<F<void, X1>, X2>, X3>>()));
EXPECT_EQ(true, (std::is_same<fold<typelist<X1, X2, X3, X4>, void, Q>, F<F<F<F<void, X1>, X2>, X3>, X4>>()));
//EXPECT_EQ(true, (std::is_same<rev_fold<typelist<>, void, Q>, void>()));
EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1>, void, Q>, F<X1, void>>()));
EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2>, void, Q>, F<X1, F<X2, void>>>()));
EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2, X3>, void, Q>, F<X1, F<X2, F<X3, void>>>>()));
EXPECT_EQ(true, (std::is_same<rev_fold<typelist<X1, X2, X3, X4>, void, Q>, F<X1, F<X2, F<X3, F<X4, void>>>>>()));
}
TEST(Tmeta_typelist, ListOperations) {
using l1 = typelist<int, short, long>;
using l2 = typelist<>;
using l3 = typelist<float, double, long double>;
using l4 = typelist<void*, char*>;
using conc = typelist<int, short, long, float, double, long double, void*, char*>;
using rep = typelist<int, int, int>;
EXPECT_EQ(3, size<l1>());
EXPECT_EQ(true, empty<l2>());
EXPECT_EQ(true, (std::is_same<conc, concat<l1, l2, l3, l4>>()));
EXPECT_EQ(true, (std::is_same<rep, repeat_n<int_<3>, int>>()));
EXPECT_EQ(true, (std::is_same<int, front<l1>>()));
EXPECT_EQ(true, (std::is_same<long, back<l1>>()));
EXPECT_EQ(true, (std::is_same<double, at<l3, int_<1>>>()));
EXPECT_EQ(true, (std::is_same<typelist<short, long>, pop_front<l1>>()));
}
}

+ 257
- 0
test/tests/test_ostream_dev.cpp ファイルの表示

@@ -0,0 +1,257 @@
/*!
* \file test_ostream_dev.cpp
*
* Copyright (C) 2018 Christos Choutouridis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <utl/dev/ostream_dev.h>
#include <array>
namespace test_ostream_dev {
using namespace utl;
// Device base data types
// MUST be DefaultConstructible, Copyable
using test_data_t = uint8_t;
// Test data
static const size_t SIZE = 10;
test_data_t Idata = 42;
test_data_t& IdataR = Idata;
test_data_t&& IdataRR = 0xAA;
test_data_t Ibuffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// ostream device implementer (Mocks)
class Ostream_dev_impl : public ostream_dev<Ostream_dev_impl, test_data_t> {
friend ostream_dev<Ostream_dev_impl, test_data_t>;
public:
// virtual device
static constexpr size_t N =SIZE;
std::array<test_data_t, N> v {}; // more than one so we can remember N-1 previous values
size_t c {0};
protected:
// ostream_dev requirements
size_t put_ (const test_data_t& data) {
v[c++] = data;
if (c >= N)
c = 0;
return 1;
}
size_t put_ (const test_data_t* data, size_t n) {
for (size_t i=0 ; i<n && i<N; ++i) {
v[i] = data[i];
}
return n;
}
public:
test_data_t& getLastV () { return (c) ? v[c-1] : v[N-1]; }
};
// virtual ostream device
class Ostream_vdev_impl : public ostream_dev<virtual_tag, test_data_t> {
public:
// virtual device
static constexpr size_t N =SIZE;
std::array<test_data_t, N> v {};
size_t c {0};
protected:
// ostream_dev requirements
size_t put_ (const test_data_t& data) {
v[c++] = data;
if (c >= N)
c = 0;
return 1;
}
size_t put_ (const test_data_t* data, size_t n) {
for (size_t i=0 ; i<n && i<N; ++i) {
v[i] = data[i];
}
return n;
}
public:
test_data_t& getLastV () { return (c) ? v[c-1] : v[N-1]; }
};
// fixtures
class Tostream_Idev : public ::testing::Test {
protected:
// zero initialized device
Ostream_dev_impl osIdev {};
};
class Tostream_Vdev : public ::testing::Test {
protected:
// zero initialized devices
std::array<Ostream_vdev_impl, 5> osVdev {};
ostream_dev<virtual_tag, test_data_t> *basePointer = nullptr;
};
//ToDo: Must add Concept test for ostream_dev
// TEST_F(Tostream_Idev, TestConcept) {
// EXPECT_EQ(true, Ostream_dev<osIdev>);
// }
TEST_F(Tostream_Idev, Construction) {
EXPECT_LT(0UL, sizeof(osIdev));
}
TEST_F (Tostream_Idev, Api) {
EXPECT_EQ(1UL, osIdev.put(Idata)); // single write from var
EXPECT_EQ(Idata, osIdev.getLastV ());
EXPECT_EQ(1UL, osIdev.put(IdataR));// single write from lvalue ref
EXPECT_EQ(IdataR, osIdev.getLastV ());
EXPECT_EQ(1UL, osIdev.put(IdataRR));// single write from rvalue ref
EXPECT_EQ(IdataRR, osIdev.getLastV ());
EXPECT_EQ(1UL, osIdev.put(42));// single write from rvalue
EXPECT_EQ(42U, osIdev.getLastV ());
EXPECT_EQ(7U, osIdev.put(Ibuffer, 7)); // batch write some data
for (size_t i =0 ; i<7 ; ++i) {
EXPECT_EQ(Ibuffer[i], osIdev.v[i]);
}
EXPECT_EQ(0, osIdev.v[7]);
EXPECT_EQ(SIZE, osIdev.put(Ibuffer, SIZE)); // batch write all data
for (size_t i =0 ; i<SIZE ; ++i) {
EXPECT_EQ(Ibuffer[i], osIdev.v[i]);
}
}
TEST_F (Tostream_Idev, streamOperator) {
struct Blop {
test_data_t x, y, z;
};
osIdev << Idata; // single write from var
EXPECT_EQ(Idata, osIdev.getLastV ());
osIdev << IdataR;// single write from lvalue ref
EXPECT_EQ(IdataR, osIdev.getLastV ());
osIdev << IdataRR;// single write from rvalue ref
EXPECT_EQ(IdataRR, osIdev.getLastV ());
osIdev << (test_data_t)42; // single write from rvalue
EXPECT_EQ(42U, osIdev.getLastV ());
// stream a blop of data (should use put(T*, N) version)
Blop blop {1, 1, 42};
osIdev << blop;
EXPECT_EQ(1U, osIdev.v[0]);
EXPECT_EQ(1U, osIdev.v[1]);
EXPECT_EQ(42U, osIdev.v[2]);
const Blop cblop {2, 2, 42};
osIdev << cblop;
EXPECT_EQ(2U, osIdev.v[0]);
EXPECT_EQ(2U, osIdev.v[1]);
EXPECT_EQ(42U, osIdev.v[2]);
}
TEST_F (Tostream_Idev, Iterator1) {
// default constructible
Ostream_dev_impl::iterator def_it;
EXPECT_EQ (0, *(int32_t*)&def_it); // not dereferencable
// output iterator requirements
// https://en.cppreference.com/w/cpp/named_req/OutputIterator
auto it = osIdev.begin();
auto it2(it);
EXPECT_EQ (*(int32_t*)&it, *(int32_t*)&it2);
it2 = it;
EXPECT_EQ (*(int32_t*)&it, *(int32_t*)&it2);
def_it = it;
EXPECT_EQ (*(int32_t*)&it, *(int32_t*)&def_it);
++it, it++;
*it = Idata;
EXPECT_EQ (Idata, osIdev.getLastV ());
*it = IdataR;
EXPECT_EQ (IdataR, osIdev.getLastV ());
*it = IdataRR;
EXPECT_EQ (IdataRR, osIdev.getLastV ());
*it++ = Idata;
EXPECT_EQ (Idata, osIdev.getLastV ());
}
TEST_F (Tostream_Idev, Iterator2) {
auto it = osIdev.begin();
std::fill_n(it, SIZE, Idata);
for (size_t i=0 ;i<SIZE ; ++i) {
EXPECT_EQ (Idata, osIdev.v[i]);
}
}
TEST_F (Tostream_Vdev, virtualApi) {
// loop to virtual devices and use them via base pointer
for (auto& dev : osVdev) {
basePointer = &dev;
basePointer->put(Idata);
EXPECT_EQ(Idata, dev.v[0]);
EXPECT_EQ(SIZE, basePointer->put(Ibuffer, SIZE)); // batch write all data
for (size_t i =0 ; i<SIZE ; ++i) {
EXPECT_EQ(Ibuffer[i], dev.v[i]);
}
}
}
TEST_F (Tostream_Vdev, virtualStream) {
struct Blop {
test_data_t x, y, z;
};
Blop blop {1, 1, 42};
const Blop cblop {2, 2, 42};
// loop to virtual devices and use them via base pointer
for (auto& dev : osVdev) {
basePointer = &dev;
*basePointer << IdataR;
EXPECT_EQ(IdataR, dev.v[0]);
*basePointer << blop;
EXPECT_EQ(1U, dev.v[0]);
EXPECT_EQ(1U, dev.v[1]);
EXPECT_EQ(42U, dev.v[2]);
*basePointer << cblop;
EXPECT_EQ(2U, dev.v[0]);
EXPECT_EQ(2U, dev.v[1]);
EXPECT_EQ(42U, dev.v[2]);
}
}
TEST_F (Tostream_Vdev, virtualIterator) {
// loop to virtual devices and use them via base pointer
for (auto& dev : osVdev) {
basePointer = &dev;
auto it = *basePointer->begin();
std::fill_n(it, SIZE, Idata);
for (size_t i=0 ;i<SIZE ; ++i) {
EXPECT_EQ (Idata, dev.v[i]);
}
}
}
}

読み込み中…
キャンセル
保存