operation.cpp 1.22 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
12
    std::string name() const { return "simple"; }
    rtg::shape compute_shape(std::vector<rtg::shape>) const { RTG_THROW("not computable"); }
    rtg::argument compute(std::vector<rtg::argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
13
14
15
16
17
    friend std::ostream & operator<<(std::ostream & os, const simple_operation & op)
    {
        os << op.name();
        return os;
    }
Paul's avatar
Paul committed
18
19
20
21
22
};

void operation_copy_test()
{
    simple_operation s{};
Paul's avatar
Paul committed
23
    rtg::operation op1 = s;   // NOLINT
Paul's avatar
Paul committed
24
25
26
27
28
    rtg::operation op2 = op1; // NOLINT
    EXPECT(s.name() == op1.name());
    EXPECT(op2.name() == op1.name());
}

Paul's avatar
Paul committed
29
30
31
struct not_operation
{
};
Paul's avatar
Paul committed
32
33
34
35
36
37

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
38
    EXPECT(test::throws([&] { rtg::any_cast<not_operation&>(op1); }));
Paul's avatar
Paul committed
39
40
41
42
43
    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
44
45
46
47
int main()
{
    operation_copy_test();
    operation_any_cast();
Paul's avatar
Paul committed
48
}