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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/allocator.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. using namespace mindspore::dataset;
  22. class MindDataTestMemoryPool : public UT::Common {
  23. public:
  24. std::shared_ptr<MemoryPool> mp_;
  25. MindDataTestMemoryPool() {}
  26. void SetUp() {
  27. Status rc = CircularPool::CreateCircularPool(&mp_, 1, 1, true);
  28. ASSERT_TRUE(rc.IsOk());
  29. }
  30. };
  31. TEST_F(MindDataTestMemoryPool, DumpPoolInfo) {
  32. MS_LOG(DEBUG) << *(std::dynamic_pointer_cast<CircularPool>(mp_)) << std::endl;
  33. }
  34. TEST_F(MindDataTestMemoryPool, TestOperator1) {
  35. Status rc;
  36. int *p = new (&rc, mp_) int;
  37. ASSERT_TRUE(rc.IsOk());
  38. *p = 2048;
  39. ::operator delete(p, mp_);
  40. }
  41. TEST_F(MindDataTestMemoryPool, TestOperator3) {
  42. Status rc;
  43. int *p = new (&rc, mp_) int[100];
  44. ASSERT_TRUE(rc.IsOk());
  45. for (int i = 0; i < 100; i++) {
  46. p[i] = i;
  47. }
  48. for (int i = 0; i < 100; i++) {
  49. ASSERT_EQ(p[i], i);
  50. }
  51. }
  52. TEST_F(MindDataTestMemoryPool, TestAllocator) {
  53. class A {
  54. public:
  55. explicit A(int x) : a(x) {}
  56. int val_a() const { return a; }
  57. private:
  58. int a;
  59. };
  60. Allocator<A> alloc(mp_);
  61. std::shared_ptr<A> obj_a = std::allocate_shared<A>(alloc, 3);
  62. int v = obj_a->val_a();
  63. ASSERT_EQ(v, 3);
  64. MS_LOG(DEBUG) << *(std::dynamic_pointer_cast<CircularPool>(mp_)) << std::endl;
  65. }
  66. TEST_F(MindDataTestMemoryPool, TestMemGuard) {
  67. MemGuard<uint8_t> mem;
  68. // Try some large value.
  69. int64_t sz = 5LL * 1024LL * 1024LL * 1024LL;
  70. Status rc = mem.allocate(sz);
  71. ASSERT_TRUE(rc.IsOk() || rc == StatusCode::kMDOutOfMemory);
  72. if (rc.IsOk()) {
  73. // Try write a character half way.
  74. auto *p = mem.GetMutablePointer();
  75. p[sz / 2] = 'a';
  76. }
  77. }