operation.cpp 2.7 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
    template <class T, class F>
Paul's avatar
Paul committed
10
11
12
13
    static auto reflect(T& x, F f)
    {
        return migraph::pack(f(x.data, "data"));
    }
Paul's avatar
Paul committed
14
    int data = 1;
Paul's avatar
Paul committed
15
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
16
    migraph::shape compute_shape(const std::vector<migraph::shape>&) const
Paul's avatar
Paul committed
17
18
19
20
    {
        MIGRAPH_THROW("not computable");
    }
    migraph::argument
Paul's avatar
Paul committed
21
    compute(migraph::context&, const migraph::shape&, const std::vector<migraph::argument>&) const
Paul's avatar
Paul committed
22
    {
Paul's avatar
Paul committed
23
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
24
    }
Paul's avatar
Paul committed
25
    friend std::ostream& operator<<(std::ostream& os, const simple_operation& op)
Paul's avatar
Paul committed
26
    {
Paul's avatar
Paul committed
27
        os << op.name() << "[" << op.data << "]";
Paul's avatar
Paul committed
28
29
        return os;
    }
Paul's avatar
Paul committed
30
31
};

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

Paul's avatar
Paul committed
46
47
48
void operation_copy_test()
{
    simple_operation s{};
Paul's avatar
Paul committed
49
50
    migraph::operation op1 = s;   // NOLINT
    migraph::operation op2 = op1; // NOLINT
Paul's avatar
Paul committed
51
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
52
    EXPECT(s == op1);
Paul's avatar
Paul committed
53
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
54
55
56
57
58
59
60
    EXPECT(op2 == op1);
}

void operation_equal_test()
{
    simple_operation s{};
    migraph::operation op1 = s;
Paul's avatar
Paul committed
61
    s.data                 = 2;
Paul's avatar
Paul committed
62
    migraph::operation op2 = op1; // NOLINT
Paul's avatar
Paul committed
63
    migraph::operation op3 = s;   // NOLINT
Paul's avatar
Paul committed
64
65
66
67
68

    EXPECT(s != op1);
    EXPECT(op2 == op1);
    EXPECT(op3 != op2);
    EXPECT(op3 != op1);
Paul's avatar
Paul committed
69
70
}

Paul's avatar
Paul committed
71
72
73
struct not_operation
{
};
Paul's avatar
Paul committed
74
75
76

void operation_any_cast()
{
Paul's avatar
Paul committed
77
78
79
80
81
82
83
    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
84
85
}

Paul's avatar
Paul committed
86
87
void operation_print()
{
Paul's avatar
Paul committed
88
    migraph::operation op = simple_operation{};
Paul's avatar
Paul committed
89
90
91
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
Paul's avatar
Paul committed
92
    EXPECT(s == "simple[1]");
Paul's avatar
Paul committed
93
94
95
96
}

void operation_default_print()
{
Paul's avatar
Paul committed
97
    migraph::operation op = simple_operation_no_print{};
Paul's avatar
Paul committed
98
99
100
101
102
103
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "simple");
}

Paul's avatar
Paul committed
104
105
106
int main()
{
    operation_copy_test();
Paul's avatar
Paul committed
107
    operation_equal_test();
Paul's avatar
Paul committed
108
    operation_any_cast();
Paul's avatar
Paul committed
109
110
    operation_print();
    operation_default_print();
Paul's avatar
Paul committed
111
}