GitHub Viewer
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor-python/pyarray.hpp"
#include "xtensor/xarray.hpp"
#include "test_common.hpp"
namespace xt
{
using container_type = std::vector;
TEST(pyarray, initializer_constructor)
{
pyarray t
{{{ 0, 1, 2},
{ 3, 4, 5},
{ 6, 7, 8}},
{{ 9, 10, 11},
{12, 13, 14},
{15, 16, 17}}};
EXPECT_EQ(t.dimension(), 3);
EXPECT_EQ(t(0, 0, 1), 1);
EXPECT_EQ(t.shape()[0], 2);
}
TEST(pyarray, shaped_constructor)
{
{
SCOPED_TRACE("row_major constructor");
row_major_result rm;
pyarray ra(rm.m_shape);
compare_shape(ra, rm);
EXPECT_EQ(layout_type::row_major, ra.layout());
}
{
SCOPED_TRACE("column_major constructor");
column_major_result cm;
pyarray ca(cm.m_shape, layout_type::column_major);
compare_shape(ca, cm);
EXPECT_EQ(layout_type::column_major, ca.layout());
}
}
TEST(pyarray, strided_constructor)
{
central_major_result cmr;
pyarray cma(cmr.m_shape, cmr.m_strides);
compare_shape(cma, cmr);
}
TEST(pyarray, valued_constructor)
{
{
SCOPED_TRACE("row_major valued constructor");
row_major_result rm;
int value = 2;
pyarray ra(rm.m_shape, value);
compare_shape(ra, rm);
std::vector vec(ra.size(), value);
EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), ra.data().cbegin()));
}
{
SCOPED_TRACE("column_major valued constructor");
column_major_result cm;
int value = 2;
pyarray ca(cm.m_shape, value, layout_type::column_major);
compare_shape(ca, cm);
std::vector vec(ca.size(), value);
EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), ca.data().cbegin()));
}
}
TEST(pyarray, strided_valued_constructor)
{
central_major_result cmr;
int value = 2;
pyarray cma(cmr.m_shape, cmr.m_strides, value);
compare_shape(cma, cmr);
std::vector vec(cma.size(), value);
EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), cma.data().cbegin()));
}
TEST(pyarray, copy_semantic)
{
central_major_result res;
int value = 2;
pyarray a(res.m_shape, res.m_strides, value);
{
SCOPED_TRACE("copy constructor");
pyarray b(a);
compare_shape(a, b);
EXPECT_EQ(a.data(), b.data());
a.data()[0] += 1;
EXPECT_NE(a.data()[0], b.data()[0]);
}
{
SCOPED_TRACE("assignment operator");
row_major_result r;
pyarray c(r.m_shape, 0);
EXPECT_NE(a.data(), c.data());
c = a;
compare_shape(a, c);
EXPECT_EQ(a.data(), c.data());
a.data()[0] += 1;
EXPECT_NE(a.data()[0], c.data()[0]);
}
}
TEST(pyarray, move_semantic)
{
central_major_result res;
int value = 2;
pyarray a(res.m_shape, res.m_strides, value);
{
SCOPED_TRACE("move constructor");
pyarray tmp(a);
pyarray b(std::move(tmp));
compare_shape(a, b);
EXPECT_EQ(a.data(), b.data());
}
{
SCOPED_TRACE("move assignment");
row_major_result r;
pyarray c(r.m_shape, 0);
EXPECT_NE(a.data(), c.data());
pyarray tmp(a);
c = std::move(tmp);
compare_shape(a, c);
EXPECT_EQ(a.data(), c.data());
}
}
TEST(pyarray, extended_constructor)
{
xt::xarray a1 = { { 1, 2 },{ 3, 4 } };
xt::xarray a2 = { { 1, 2 },{ 3, 4 } };
pyarray c = a1 + a2;
EXPECT_EQ(c(0, 0), a1(0, 0) + a2(0, 0));
EXPECT_EQ(c(0, 1), a1(0, 1) + a2(0, 1));
EXPECT_EQ(c(1, 0), a1(1, 0) + a2(1, 0));
EXPECT_EQ(c(1, 1), a1(1, 1) + a2(1, 1));
}
TEST(pyarray, reshape)
{
pyarray a;
test_reshape(a);
pyarray b = { {1, 2}, {3, 4} };
a.reshape(b.shape());
EXPECT_EQ(a.shape(), b.shape());
}
TEST(pyarray, transpose)
{
pyarray a;
test_transpose(a);
}
TEST(pyarray, access)
{
pyarray a;
test_access(a);
}
TEST(pyarray, indexed_access)
{
pyarray a;
test_indexed_access(a);
}
TEST(pyarray, broadcast_shape)
{
pyarray a;
test_broadcast(a);
test_broadcast2(a);
}
TEST(pyarray, iterator)
{
pyarray a;
pyarray b;
test_iterator(a, b);
}
TEST(pyarray, initializer_list)
{
pyarray a0(1);
pyarray a1({1, 2});
pyarray a2({{1, 2}, {2, 4}, {5, 6}});
EXPECT_EQ(1, a0());
EXPECT_EQ(2, a1(1));
EXPECT_EQ(4, a2(1, 1));
}
TEST(pyarray, zerod)
{
pyarray a;
EXPECT_EQ(0, a());
}
}