".github/git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "094bd2711920344ef30328a4f58ebfd27a3f97bf"
serialize_test.cpp 3.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
#include <migraphx/serialize.hpp>
#include <migraphx/functional.hpp>
#include <test.hpp>

28
29
#include <numeric>

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
struct empty_type
{
};
struct reflectable_type
{
    enum simple_enum
    {
        simple1,
        simple2,
        simple3
    };
    enum class class_enum
    {
        class1,
        class2,
        class3
    };
    std::vector<std::size_t> ints = {};
    std::string name              = "";
    float fvalue                  = 0.0;
    empty_type et{};
    simple_enum se = simple1;
    class_enum ce  = class_enum::class1;

    struct nested_type
    {
        int value;
        template <class Self, class F>
        static auto reflect(Self& self, F f)
        {
            return migraphx::pack(f(self.value, "value"));
        }
    };
    std::vector<nested_type> nested_types = {};

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return migraphx::pack(f(self.ints, "ints"),
                              f(self.name, "name"),
                              f(self.fvalue, "fvalue"),
                              f(self.et, "et"),
                              f(self.se, "se"),
                              f(self.ce, "ce"),
                              f(self.nested_types, "nested_types"));
    }
};

TEST_CASE(serialize_reflectable_type)
{
    reflectable_type t1{{1, 2},
                        "hello",
                        1.0,
                        {},
                        reflectable_type::simple1,
                        reflectable_type::class_enum::class2,
                        {{1}, {2}}};
    migraphx::value v1  = migraphx::to_value(t1);
    reflectable_type t2 = migraphx::from_value<reflectable_type>(v1);
    migraphx::value v2  = migraphx::to_value(t2);
    migraphx::value v3  = migraphx::to_value(reflectable_type{});

    EXPECT(v1 == v2);
    EXPECT(v1 != v3);
    EXPECT(v2 != v3);
}

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
TEST_CASE(serialize_empty_array)
{
    std::vector<std::size_t> ints = {};
    migraphx::value v             = migraphx::to_value(ints);
    EXPECT(v.is_array());
    EXPECT(v.empty());
    v.push_back(1);
    EXPECT(v.size() == 1);
    EXPECT(v.front().to<int>() == 1);
}

struct empty_struct
{
    template <class Self, class F>
    static auto reflect(Self&, F)
    {
        return migraphx::pack();
    }
};

TEST_CASE(serialize_empty_struct)
{
    empty_struct es{};
    migraphx::value v = migraphx::to_value(es);
    EXPECT(v.is_object());
    EXPECT(v.empty());
    v["a"] = 1;
    EXPECT(v.size() == 1);
    EXPECT(v.at("a").to<int>() == 1);
}

128
129
130
131
132
133
134
135
136
137
138
TEST_CASE(from_value_binary)
{
    std::vector<std::uint8_t> data(10);
    std::iota(data.begin(), data.end(), 0);

    migraphx::value v = migraphx::value::binary{data};

    auto out = migraphx::from_value<migraphx::value::binary>(v);
    EXPECT(out == data);
}

139
int main(int argc, const char* argv[]) { test::run(argc, argv); }