operation.cpp 2.1 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraph/operation.hpp>
Paul's avatar
Paul committed
3
4
5
6
7
8
#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
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
11
12
    migraph::shape compute_shape(std::vector<migraph::shape>) const { MIGRAPH_THROW("not computable"); }
    migraph::argument compute(migraph::context&, migraph::shape, std::vector<migraph::argument>) const
Paul's avatar
Paul committed
13
    {
Paul's avatar
Paul committed
14
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
15
    }
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
struct simple_operation_no_print
{
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
26
27
    migraph::shape compute_shape(std::vector<migraph::shape>) const { MIGRAPH_THROW("not computable"); }
    migraph::argument compute(migraph::context&, migraph::shape, std::vector<migraph::argument>) const
Paul's avatar
Paul committed
28
    {
Paul's avatar
Paul committed
29
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
30
    }
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
37
    migraph::operation op1 = s;   // NOLINT
    migraph::operation op2 = op1; // NOLINT
Paul's avatar
Paul committed
38
39
40
41
    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

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

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

void operation_default_print()
{
Paul's avatar
Paul committed
68
    migraph::operation op = simple_operation_no_print{};
Paul's avatar
Paul committed
69
70
71
72
73
74
    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
}