operation.cpp 1.09 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
13
14
15
16
17
    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"); }
};

void operation_copy_test()
{
    simple_operation s{};
Paul's avatar
Paul committed
18
    rtg::operation op1 = s;   // NOLINT
Paul's avatar
Paul committed
19
20
21
22
23
    rtg::operation op2 = op1; // NOLINT
    EXPECT(s.name() == op1.name());
    EXPECT(op2.name() == op1.name());
}

Paul's avatar
Paul committed
24
25
26
struct not_operation
{
};
Paul's avatar
Paul committed
27
28
29
30
31
32

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
33
    EXPECT(test::throws([&] { rtg::any_cast<not_operation&>(op1); }));
Paul's avatar
Paul committed
34
35
36
37
38
    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
39
40
41
42
int main()
{
    operation_copy_test();
    operation_any_cast();
Paul's avatar
Paul committed
43
}