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.

memory_pool_test.cc 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright 2019 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 "dataset/util/memory_pool.h"
  17. #include "dataset/util/circular_pool.h"
  18. #include "dataset/util/system_pool.h"
  19. #include "dataset/util/allocator.h"
  20. #include "common/common.h"
  21. #include "gtest/gtest.h"
  22. using namespace mindspore::dataset;
  23. class MindDataTestMemoryPool : public UT::Common {
  24. public:
  25. std::shared_ptr<MemoryPool> mp_;
  26. MindDataTestMemoryPool() {}
  27. void SetUp() {
  28. Status rc = CircularPool::CreateCircularPool(&mp_, 1, 1, true);
  29. ASSERT_TRUE(rc.IsOk());
  30. }
  31. };
  32. TEST_F(MindDataTestMemoryPool, DumpPoolInfo) {
  33. MS_LOG(DEBUG) << *(std::dynamic_pointer_cast<CircularPool>(mp_)) << std::endl;
  34. }
  35. TEST_F(MindDataTestMemoryPool, TestOperator1) {
  36. Status rc;
  37. int *p = new(&rc, mp_) int;
  38. ASSERT_TRUE(rc.IsOk());
  39. *p = 2048;
  40. ::operator delete(p, mp_);
  41. }
  42. TEST_F(MindDataTestMemoryPool, TestOperator3) {
  43. Status rc;
  44. int *p = new (&rc, mp_) int[100];
  45. ASSERT_TRUE(rc.IsOk());
  46. for (int i = 0; i < 100; i++) {
  47. p[i] = i;
  48. }
  49. for (int i = 0; i < 100; i++) {
  50. ASSERT_EQ(p[i], i);
  51. }
  52. }
  53. TEST_F(MindDataTestMemoryPool, TestAllocator) {
  54. class A {
  55. public:
  56. explicit A (int x) : a(x) {}
  57. int val_a() const {
  58. return a;
  59. }
  60. private:
  61. int a;
  62. };
  63. Allocator<A> alloc(mp_);
  64. std::shared_ptr<A> obj_a = std::allocate_shared<A>(alloc, 3);
  65. int v = obj_a->val_a();
  66. ASSERT_EQ(v, 3);
  67. MS_LOG(DEBUG) << *(std::dynamic_pointer_cast<CircularPool>(mp_)) << std::endl;
  68. }