1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <iostream>
17 #include <memory>
18
19 #include "common/common_test.h"
20
21 #include "ir/anf.h"
22 #include "ir/func_graph.h"
23 #include "frontend/operator/ops.h"
24 #include "base/core_ops.h"
25
26 namespace mindspore {
27
28 using Named = Named;
29
30 class TestAnf : public UT::Common {
31 public:
TestAnf()32 TestAnf() {}
33 };
34
TEST_F(TestAnf,test_ValueNode)35 TEST_F(TestAnf, test_ValueNode) {
36 auto prim = std::make_shared<Primitive>(prim::kScalarAdd);
37 ValueNodePtr c = NewValueNode(prim);
38 ASSERT_EQ(c->isa<ValueNode>(), true);
39 ASSERT_EQ(IsValueNode<Primitive>(c), true);
40 ASSERT_EQ(IsValueNode<FuncGraph>(c), false);
41
42 FuncGraphPtr fg = std::make_shared<FuncGraph>();
43 ValueNode c1(fg);
44 ASSERT_EQ(c1.value()->isa<FuncGraph>(), true);
45 }
46
TEST_F(TestAnf,test_Parameter)47 TEST_F(TestAnf, test_Parameter) {
48 FuncGraphPtr fg = std::make_shared<FuncGraph>();
49 Parameter a(fg);
50 assert(a.isa<Parameter>());
51 }
52
TEST_F(TestAnf,test_CNode)53 TEST_F(TestAnf, test_CNode) {
54 auto primitive = prim::kPrimScalarAdd;
55
56 FuncGraphPtr fg = std::make_shared<FuncGraph>();
57 std::string s = fg->ToString();
58
59 Parameter param(fg);
60 std::vector<AnfNodePtr> params;
61 CNode app_1(params, fg);
62 params.push_back(NewValueNode(primitive));
63 params.push_back(AnfNodePtr(new Parameter(param)));
64 CNode app(params, fg);
65 assert(app.isa<CNode>());
66 assert(app.IsApply(primitive));
67 }
68
TEST_F(TestAnf,is_exception)69 TEST_F(TestAnf, is_exception) {
70 FuncGraphPtr fg = std::make_shared<FuncGraph>();
71 Parameter a(fg);
72 assert(!a.isa<CNode>());
73 assert(!a.isa<ValueNode>());
74 }
75
76 } // namespace mindspore
77