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

Paul's avatar
Paul committed
2
#include <migraphx/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
    static auto reflect(T& x, F f)
    {
Paul's avatar
Paul committed
12
        return migraphx::pack(f(x.data, "data"));
Paul's avatar
Paul committed
13
    }
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
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
17
18
19
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
20
21
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::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
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
36
37
38
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
39
40
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::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
TEST_CASE(operation_copy_test)
Paul's avatar
Paul committed
47
48
{
    simple_operation s{};
Paul's avatar
Paul committed
49
50
    migraphx::operation op1 = s;   // NOLINT
    migraphx::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
    EXPECT(op2 == op1);
}

Paul's avatar
Paul committed
57
TEST_CASE(operation_equal_test)
Paul's avatar
Paul committed
58
59
{
    simple_operation s{};
Paul's avatar
Paul committed
60
    migraphx::operation op1 = s;
Paul's avatar
Paul committed
61
    s.data                 = 2;
Paul's avatar
Paul committed
62
63
    migraphx::operation op2 = op1; // NOLINT
    migraphx::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

Paul's avatar
Paul committed
75
TEST_CASE(operation_any_cast)
Paul's avatar
Paul committed
76
{
Paul's avatar
Paul committed
77
78
79
80
81
82
83
    migraphx::operation op1 = simple_operation{};
    EXPECT(migraphx::any_cast<simple_operation>(op1).data == 1);
    EXPECT(migraphx::any_cast<not_operation*>(&op1) == nullptr);
    EXPECT(test::throws([&] { migraphx::any_cast<not_operation&>(op1); }));
    migraphx::operation op2 = simple_operation{2};
    EXPECT(migraphx::any_cast<simple_operation>(op2).data == 2);
    EXPECT(migraphx::any_cast<not_operation*>(&op2) == nullptr);
Paul's avatar
Paul committed
84
85
}

Paul's avatar
Paul committed
86
TEST_CASE(operation_print)
Paul's avatar
Paul committed
87
{
Paul's avatar
Paul committed
88
    migraphx::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
}

Paul's avatar
Paul committed
95
TEST_CASE(operation_default_print)
Paul's avatar
Paul committed
96
{
Paul's avatar
Paul committed
97
    migraphx::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
int main(int argc, const char* argv[]) { test::run(argc, argv); }