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.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "minddata/dataset/util/memory_pool.h"
  17. #include "minddata/dataset/util/circular_pool.h"
  18. #include "minddata/dataset/util/system_pool.h"
  19. #include "minddata/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 { return a; }
  58. private:
  59. int a;
  60. };
  61. Allocator<A> alloc(mp_);
  62. std::shared_ptr<A> obj_a = std::allocate_shared<A>(alloc, 3);
  63. int v = obj_a->val_a();
  64. ASSERT_EQ(v, 3);
  65. MS_LOG(DEBUG) << *(std::dynamic_pointer_cast<CircularPool>(mp_)) << std::endl;
  66. }
  67. TEST_F(MindDataTestMemoryPool, TestMemGuard) {
  68. MemGuard<uint8_t> mem;
  69. // Try some large value.
  70. int64_t sz = 5LL * 1024LL * 1024LL * 1024LL;
  71. Status rc = mem.allocate(sz);
  72. ASSERT_TRUE(rc.IsOk() || rc.IsOutofMemory());
  73. if (rc.IsOk()) {
  74. // Try write a character half way.
  75. auto *p = mem.GetMutablePointer();
  76. p[sz / 2] = 'a';
  77. }
  78. }