File: test_xexpression_holder.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 (70 lines) | stat: -rw-r--r-- 2,315 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
/***************************************************************************
 * 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 "xtensor/xarray.hpp"
#include "xtensor/xexpression_holder.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"

#include "test_common_macros.hpp"

namespace xt
{
    TEST(xexpression_holder, ctor)
    {
        xarray<double> a = {{1, 2, 3, 4}, {5, 6, 7, 8}};
        xarray<double> b = {{3, 2, 1}, {5, 6, 7}};
        xarray<double> c = {{56, 5, 2}, {4, 2, 6}};

        xexpression_holder holder_a = xexpression_holder(a);
        xexpression_holder holder_b(b);
        xexpression_holder holder_c(std::move(xexpression_holder(c)));
    }

    TEST(xexpression_holder, assign)
    {
        xarray<double> a = {{1, 2, 3, 4}, {5, 6, 7, 8}};
        xarray<double> b = {{3, 2, 1}, {5, 6, 7}};
        xarray<double> c = {{56, 5, 2}, {4, 2, 6}};

        xexpression_holder holder_a = xexpression_holder(a);
        xexpression_holder holder_b(b);

        holder_a = holder_b;
        holder_b = xexpression_holder(c);
    }

    TEST(xexpression_holder, to_json)
    {
        xarray<double> a = {{1, 2, 3, 4}, {5, 6, 7, 8}};
        xexpression_holder holder_a = xexpression_holder(a);

        nlohmann::json json_out;
        to_json(json_out, holder_a);

        ASSERT_EQ(json_out[0][0], 1);
        ASSERT_EQ(json_out[0][1], 2);
        ASSERT_EQ(json_out[0][2], 3);
        ASSERT_EQ(json_out[1][1], 6);
    }

    TEST(xexpression_holder, from_json)
    {
        xarray<double> a = {{1, 2, 3, 4}, {5, 6, 7, 8}};
        xarray<double> b = {{5, 6, 7, 8}, {1, 2, 3, 4}};

        xexpression_holder holder_a = xexpression_holder(a);

        nlohmann::json json_b;
        to_json(json_b, b);
        from_json(json_b, holder_a);

        ASSERT_EQ(a, b);
    }
}