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 <sstream>
18 #include <memory>
19 #include <algorithm>
20 #include <unordered_map>
21
22 #include "common/common_test.h"
23 #include "frontend/operator/ops.h"
24 #include "utils/any.h"
25 #include "utils/misc.h"
26
27 using std::cout;
28 using std::endl;
29 using std::string;
30
31 namespace mindspore {
32
33 class TestAny : public UT::Common {
34 public:
TestAny()35 TestAny() {}
36 };
37
38 using Primitive = Primitive;
39 using PrimitivePtr = PrimitivePtr;
40
f(const Any & a)41 Any f(const Any &a) {
42 Any value = a;
43 return value;
44 }
45
TEST_F(TestAny,test_common)46 TEST_F(TestAny, test_common) {
47 Any value(std::make_shared<Primitive>("add"));
48 if (value.type() == typeid(PrimitivePtr)) {
49 PrimitivePtr a = value.cast<PrimitivePtr>();
50 }
51 if (value.is<PrimitivePtr>()) {
52 PrimitivePtr a = value.cast<PrimitivePtr>();
53 }
54
55 Any value1 = f(std::make_shared<Primitive>("add"));
56
57 Any a = 1;
58 Any b = string("hello, world");
59 Any c;
60
61 ASSERT_FALSE(a.empty());
62 ASSERT_FALSE(b.empty());
63 ASSERT_TRUE(c.empty());
64
65 ASSERT_TRUE(a.is<int>());
66 ASSERT_FALSE(a.is<std::string>());
67 ASSERT_EQ(1, a.cast<int>());
68 c = a;
69
70 ASSERT_TRUE(c.is<int>());
71 ASSERT_FALSE(c.is<std::string>());
72 ASSERT_EQ(1, c.cast<int>());
73 }
74
TEST_F(TestAny,test_unordered_map)75 TEST_F(TestAny, test_unordered_map) {
76 std::unordered_map<Any, int, AnyHash> ids;
77 Any a = 1;
78 ids[a] = 2;
79 ASSERT_EQ(2, ids[a]);
80 }
81
TEST_F(TestAny,test_unordered_map1)82 TEST_F(TestAny, test_unordered_map1) {
83 std::unordered_map<int, Any> ids;
84 ids[1] = 1;
85 ASSERT_EQ(1, ids[1].cast<int>());
86 }
87
88 } // namespace mindspore
89