operation.cpp 2.27 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
    migraph::shape compute_shape(const std::vector<migraph::shape>&) const
Paul's avatar
Paul committed
12
13
14
15
    {
        MIGRAPH_THROW("not computable");
    }
    migraph::argument
Paul's avatar
Paul committed
16
    compute(migraph::context&, const migraph::shape&, const std::vector<migraph::argument>&) const
Paul's avatar
Paul committed
17
    {
Paul's avatar
Paul committed
18
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
19
    }
Paul's avatar
Paul committed
20
    friend std::ostream& operator<<(std::ostream& os, const simple_operation& op)
Paul's avatar
Paul committed
21
    {
Paul's avatar
Paul committed
22
        os << "[" << op.name() << "]";
Paul's avatar
Paul committed
23
24
        return os;
    }
Paul's avatar
Paul committed
25
26
};

Paul's avatar
Paul committed
27
28
29
struct simple_operation_no_print
{
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
30
    migraph::shape compute_shape(const std::vector<migraph::shape>&) const
Paul's avatar
Paul committed
31
32
33
34
    {
        MIGRAPH_THROW("not computable");
    }
    migraph::argument
Paul's avatar
Paul committed
35
    compute(migraph::context&, const migraph::shape&, const std::vector<migraph::argument>&) const
Paul's avatar
Paul committed
36
    {
Paul's avatar
Paul committed
37
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
38
    }
Paul's avatar
Paul committed
39
40
};

Paul's avatar
Paul committed
41
42
43
void operation_copy_test()
{
    simple_operation s{};
Paul's avatar
Paul committed
44
45
    migraph::operation op1 = s;   // NOLINT
    migraph::operation op2 = op1; // NOLINT
Paul's avatar
Paul committed
46
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
47
    EXPECT(s.name() == op1.name());
Paul's avatar
Paul committed
48
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
49
50
51
    EXPECT(op2.name() == op1.name());
}

Paul's avatar
Paul committed
52
53
54
struct not_operation
{
};
Paul's avatar
Paul committed
55
56
57

void operation_any_cast()
{
Paul's avatar
Paul committed
58
59
60
61
62
63
64
    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
65
66
}

Paul's avatar
Paul committed
67
68
void operation_print()
{
Paul's avatar
Paul committed
69
    migraph::operation op = simple_operation{};
Paul's avatar
Paul committed
70
71
72
73
74
75
76
77
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "[simple]");
}

void operation_default_print()
{
Paul's avatar
Paul committed
78
    migraph::operation op = simple_operation_no_print{};
Paul's avatar
Paul committed
79
80
81
82
83
84
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "simple");
}

Paul's avatar
Paul committed
85
86
87
88
int main()
{
    operation_copy_test();
    operation_any_cast();
Paul's avatar
Paul committed
89
90
    operation_print();
    operation_default_print();
Paul's avatar
Paul committed
91
}