"vscode:/vscode.git/clone" did not exist on "e87381446d2e11e290481686e7aebd99e6a51b55"
literal_test.cpp 3.11 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraphx/literal.hpp>
3
#include <migraphx/serialize.hpp>
Paul's avatar
Paul committed
4
5
#include <sstream>
#include <string>
Paul's avatar
Paul committed
6
7
#include "test.hpp"

Paul's avatar
Paul committed
8
TEST_CASE(literal_test)
Paul's avatar
Paul committed
9
{
Paul's avatar
Paul committed
10
11
12
13
    EXPECT(migraphx::literal{1} == migraphx::literal{1});
    EXPECT(migraphx::literal{1} != migraphx::literal{2});
    EXPECT(migraphx::literal{} == migraphx::literal{});
    EXPECT(migraphx::literal{} != migraphx::literal{2});
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
16
    migraphx::literal l1{1};
    migraphx::literal l2 = l1; // NOLINT
Paul's avatar
Paul committed
17
18
19
20
21
    EXPECT(l1 == l2);
    EXPECT(l1.at<int>(0) == 1);
    EXPECT(!l1.empty());
    EXPECT(!l2.empty());

Paul's avatar
Paul committed
22
23
    migraphx::literal l3{};
    migraphx::literal l4{};
Paul's avatar
Paul committed
24
25
    EXPECT(l3 == l4);
    EXPECT(l3.empty());
Paul's avatar
Paul committed
26
    EXPECT(l4.empty());
Paul's avatar
Paul committed
27
28
}

Paul's avatar
Paul committed
29
TEST_CASE(literal_os1)
Paul's avatar
Paul committed
30
{
Paul's avatar
Paul committed
31
    migraphx::literal l{1};
Paul's avatar
Paul committed
32
33
34
35
36
    std::stringstream ss;
    ss << l;
    EXPECT(ss.str() == "1");
}

Paul's avatar
Paul committed
37
TEST_CASE(literal_os2)
Paul's avatar
Paul committed
38
{
Paul's avatar
Paul committed
39
    migraphx::literal l{};
Paul's avatar
Paul committed
40
41
    std::stringstream ss;
    ss << l;
Paul's avatar
Paul committed
42
    EXPECT(ss.str().empty());
Paul's avatar
Paul committed
43
44
}

Paul's avatar
Paul committed
45
TEST_CASE(literal_os3)
Paul's avatar
Paul committed
46
{
Paul's avatar
Paul committed
47
48
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal l{s, {1, 2, 3}};
Paul's avatar
Paul committed
49
50
51
52
53
    std::stringstream ss;
    ss << l;
    EXPECT(ss.str() == "1, 2, 3");
}

Paul's avatar
Paul committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
TEST_CASE(literal_visit_at)
{
    migraphx::literal x{1};
    bool visited = false;
    x.visit_at([&](int i) {
        visited = true;
        EXPECT(i == 1);
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit)
{
    migraphx::literal x{1};
    migraphx::literal y{1};
    bool visited = false;
    x.visit([&](auto i) {
Paul's avatar
Paul committed
71
72
73
74
        y.visit([&](auto j) {
            visited = true;
            EXPECT(i == j);
        });
Paul's avatar
Paul committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_all)
{
    migraphx::literal x{1};
    migraphx::literal y{1};
    bool visited = false;
    migraphx::visit_all(x, y)([&](auto i, auto j) {
        visited = true;
        EXPECT(i == j);
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_mismatch_shape)
{
    migraphx::literal x{1};
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal y{s, {1, 2, 3}};
    bool visited = false;
    x.visit([&](auto i) {
Paul's avatar
Paul committed
98
99
100
101
        y.visit([&](auto j) {
            visited = true;
            EXPECT(i != j);
        });
Paul's avatar
Paul committed
102
103
104
105
106
107
108
109
110
111
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_all_mismatch_type)
{
    migraphx::shape s1{migraphx::shape::int32_type, {1}};
    migraphx::literal x{s1, {1}};
    migraphx::shape s2{migraphx::shape::int8_type, {1}};
    migraphx::literal y{s2, {1}};
Paul's avatar
Paul committed
112
113
    EXPECT(
        test::throws<migraphx::exception>([&] { migraphx::visit_all(x, y)([&](auto, auto) {}); }));
Paul's avatar
Paul committed
114
115
116
117
118
}

TEST_CASE(literal_visit_empty)
{
    migraphx::literal x{};
Paul's avatar
Paul committed
119
120
    EXPECT(test::throws([&] { x.visit([](auto) {}); }));
    EXPECT(test::throws([&] { x.visit_at([](auto) {}); }));
Paul's avatar
Paul committed
121
122
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
TEST_CASE(value_literal)
{
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal l1{s, {1, 2, 3}};
    auto v1 = migraphx::to_value(l1);
    migraphx::literal l2{1};
    auto v2 = migraphx::to_value(l2);
    EXPECT(v1 != v2);

    auto l3 = migraphx::from_value<migraphx::literal>(v1);
    EXPECT(l3 == l1);
    auto l4 = migraphx::from_value<migraphx::literal>(v2);
    EXPECT(l4 == l2);
}

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