File: test_sfinae.cpp

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (142 lines) | stat: -rw-r--r-- 5,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht          *
 * Copyright (c) QuantStack                                                 *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#include <complex>
#include <limits>

#include "xtensor/xarray.hpp"
#include "xtensor/xtensor.hpp"

#include "test_common_macros.hpp"
// #include "xtensor/xfixed.hpp"
#include "xtensor/xview.hpp"

namespace xt
{
    template <class E, std::enable_if_t<!xt::has_rank_t<E, 2>::value, int> = 0>
    inline size_t sfinae_rank_basic_func(E&&)
    {
        return 0;
    }

    template <class E, std::enable_if_t<xt::has_rank_t<E, 2>::value, int> = 0>
    inline size_t sfinae_rank_basic_func(E&&)
    {
        return 2;
    }

    TEST(sfinae, rank_basic)
    {
        xt::xarray<size_t> a = {{9, 9, 9}, {9, 9, 9}};
        xt::xtensor<size_t, 1> b = {9, 9};
        xt::xtensor<size_t, 2> c = {{9, 9}, {9, 9}};
        // xt::xtensor_fixed<size_t, xt::xshape<2, 2>> d = {{9, 9}, {9, 9}};
        auto v = xt::view(c, 0, xt::all());

        EXPECT_TRUE(sfinae_rank_basic_func(a) == 0ul);
        EXPECT_TRUE(sfinae_rank_basic_func(b) == 0ul);
        EXPECT_TRUE(sfinae_rank_basic_func(c) == 2ul);
        // EXPECT_TRUE(sfinae_rank_basic_func(d) == 2ul);
        EXPECT_TRUE(sfinae_rank_basic_func(v) == 0ul);
        EXPECT_TRUE(sfinae_rank_basic_func(2ul * a) == 0ul);
        EXPECT_TRUE(sfinae_rank_basic_func(2ul * b) == 0ul);
        EXPECT_TRUE(sfinae_rank_basic_func(2ul * c) == 0ul);
    }

    template <class E, std::enable_if_t<xt::has_rank_t<E, SIZE_MAX>::value, int> = 0>
    inline size_t sfinae_rank_func(E&&)
    {
        return 0;
    }

    template <class E, std::enable_if_t<xt::has_rank_t<E, 1>::value, int> = 0>
    inline size_t sfinae_rank_func(E&&)
    {
        return 1;
    }

    template <class E, std::enable_if_t<xt::has_rank_t<E, 2>::value, int> = 0>
    inline size_t sfinae_rank_func(E&&)
    {
        return 2;
    }

    TEST(sfinae, rank)
    {
        xt::xarray<size_t> a = {{9, 9, 9}, {9, 9, 9}};
        xt::xtensor<size_t, 1> b = {9, 9};
        xt::xtensor<size_t, 2> c = {{9, 9}, {9, 9}};
        // xt::xtensor_fixed<size_t, xt::xshape<2, 2>> d = {{9, 9}, {9, 9}};
        auto v = xt::view(c, 0, xt::all());

        EXPECT_TRUE(sfinae_rank_func(a) == 0ul);
        EXPECT_TRUE(sfinae_rank_func(b) == 1ul);
        EXPECT_TRUE(sfinae_rank_func(c) == 2ul);
        // EXPECT_TRUE(sfinae_rank_func(d) == 2ul);
        EXPECT_TRUE(sfinae_rank_func(v) == 0ul);
        EXPECT_TRUE(sfinae_rank_func(2ul * a) == 0ul);
        EXPECT_TRUE(sfinae_rank_func(2ul * b) == 0ul);
        EXPECT_TRUE(sfinae_rank_func(2ul * c) == 0ul);
    }

    template <class E, std::enable_if_t<!xt::has_fixed_rank_t<E>::value, int> = 0>
    inline bool sfinae_fixed_func(E&&)
    {
        return false;
    }

    template <class E, std::enable_if_t<xt::has_fixed_rank_t<E>::value, int> = 0>
    inline bool sfinae_fixed_func(E&&)
    {
        return true;
    }

    TEST(sfinae, fixed_rank)
    {
        xt::xarray<size_t> a = {{9, 9, 9}, {9, 9, 9}};
        xt::xtensor<size_t, 1> b = {9, 9};
        xt::xtensor<size_t, 2> c = {{9, 9}, {9, 9}};
        // xt::xtensor_fixed<size_t, xt::xshape<2, 2>> d = {{9, 9}, {9, 9}};
        auto v = xt::view(c, 0, xt::all());

        EXPECT_TRUE(sfinae_fixed_func(a) == false);
        EXPECT_TRUE(sfinae_fixed_func(b) == true);
        EXPECT_TRUE(sfinae_fixed_func(c) == true);
        // EXPECT_TRUE(sfinae_fixed_func(d) == 2ul);
        EXPECT_TRUE(sfinae_fixed_func(v) == false);
        EXPECT_TRUE(sfinae_fixed_func(2ul * a) == false);
        EXPECT_TRUE(sfinae_fixed_func(2ul * b) == false);
        EXPECT_TRUE(sfinae_fixed_func(2ul * c) == false);
    }

    template <class T>
    struct sfinae_get_rank
    {
        static const size_t rank = xt::get_rank<T>::value;

        static size_t value()
        {
            return rank;
        }
    };

    TEST(sfinae, get_rank)
    {
        xt::xtensor<double, 1> A = xt::zeros<double>({2});
        xt::xtensor<double, 2> B = xt::zeros<double>({2, 2});
        xt::xarray<double> C = xt::zeros<double>({2, 2});

        EXPECT_TRUE(sfinae_get_rank<decltype(A)>::value() == 1ul);
        EXPECT_TRUE(sfinae_get_rank<decltype(B)>::value() == 2ul);
        EXPECT_TRUE(sfinae_get_rank<decltype(C)>::value() == SIZE_MAX);
        EXPECT_TRUE(sfinae_get_rank<decltype(2.0 * A)>::value() == SIZE_MAX);
        EXPECT_TRUE(sfinae_get_rank<decltype(2.0 * B)>::value() == SIZE_MAX);
        EXPECT_TRUE(sfinae_get_rank<decltype(2.0 * C)>::value() == SIZE_MAX);
    }
}