You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

any_test.cc 2.0 kB

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