operation.cpp 2.78 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
22
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
23
    {
Paul's avatar
Paul committed
24
        MIGRAPH_THROW("not computable");
Paul's avatar
Paul committed
25
    }
Paul's avatar
Paul committed
26
    friend std::ostream& operator<<(std::ostream& os, const simple_operation& op)
Paul's avatar
Paul committed
27
    {
Paul's avatar
Paul committed
28
        os << op.name() << "[" << op.data << "]";
Paul's avatar
Paul committed
29
30
        return os;
    }
Paul's avatar
Paul committed
31
32
};

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

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

Paul's avatar
Paul committed
59
TEST_CASE(operation_equal_test)
Paul's avatar
Paul committed
60
61
{
    simple_operation s{};
Paul's avatar
Paul committed
62
    migraphx::operation op1 = s;
Paul's avatar
Paul committed
63
    s.data                  = 2;
Paul's avatar
Paul committed
64
65
    migraphx::operation op2 = op1; // NOLINT
    migraphx::operation op3 = s;   // NOLINT
Paul's avatar
Paul committed
66
67
68
69
70

    EXPECT(s != op1);
    EXPECT(op2 == op1);
    EXPECT(op3 != op2);
    EXPECT(op3 != op1);
Paul's avatar
Paul committed
71
72
}

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

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

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

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

Paul's avatar
Paul committed
106
int main(int argc, const char* argv[]) { test::run(argc, argv); }