operation.cpp 1.96 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8

#include <rtg/operation.hpp>
#include <sstream>
#include <string>
#include "test.hpp"

struct simple_operation
{
Paul's avatar
Paul committed
9
    int data = 1;
Paul's avatar
Paul committed
10
11
    std::string name() const { return "simple"; }
    rtg::shape compute_shape(std::vector<rtg::shape>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
12
13
14
15
    rtg::argument compute(rtg::shape, std::vector<rtg::argument>) const
    {
        RTG_THROW("not computable");
    }
Paul's avatar
Paul committed
16
    friend std::ostream& operator<<(std::ostream& os, const simple_operation& op)
Paul's avatar
Paul committed
17
    {
Paul's avatar
Paul committed
18
        os << "[" << op.name() << "]";
Paul's avatar
Paul committed
19
20
        return os;
    }
Paul's avatar
Paul committed
21
22
};

Paul's avatar
Paul committed
23
24
25
26
struct simple_operation_no_print
{
    std::string name() const { return "simple"; }
    rtg::shape compute_shape(std::vector<rtg::shape>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
27
28
29
30
    rtg::argument compute(rtg::shape, std::vector<rtg::argument>) const
    {
        RTG_THROW("not computable");
    }
Paul's avatar
Paul committed
31
32
};

Paul's avatar
Paul committed
33
34
35
void operation_copy_test()
{
    simple_operation s{};
Paul's avatar
Paul committed
36
    rtg::operation op1 = s;   // NOLINT
Paul's avatar
Paul committed
37
38
39
40
41
    rtg::operation op2 = op1; // NOLINT
    EXPECT(s.name() == op1.name());
    EXPECT(op2.name() == op1.name());
}

Paul's avatar
Paul committed
42
43
44
struct not_operation
{
};
Paul's avatar
Paul committed
45
46
47
48
49
50

void operation_any_cast()
{
    rtg::operation op1 = simple_operation{};
    EXPECT(rtg::any_cast<simple_operation>(op1).data == 1);
    EXPECT(rtg::any_cast<not_operation*>(&op1) == nullptr);
Paul's avatar
Paul committed
51
    EXPECT(test::throws([&] { rtg::any_cast<not_operation&>(op1); }));
Paul's avatar
Paul committed
52
53
54
55
56
    rtg::operation op2 = simple_operation{2};
    EXPECT(rtg::any_cast<simple_operation>(op2).data == 2);
    EXPECT(rtg::any_cast<not_operation*>(&op2) == nullptr);
}

Paul's avatar
Paul committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
void operation_print()
{
    rtg::operation op = simple_operation{};
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "[simple]");
}

void operation_default_print()
{
    rtg::operation op = simple_operation_no_print{};
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "simple");
}

Paul's avatar
Paul committed
75
76
77
78
int main()
{
    operation_copy_test();
    operation_any_cast();
Paul's avatar
Paul committed
79
80
    operation_print();
    operation_default_print();
Paul's avatar
Paul committed
81
}