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 17 #include <string> 18 #include "minddata/dataset/util/allocator.h" 19 #include "minddata/dataset/util/arena.h" 20 #include "minddata/dataset/util/system_pool.h" 21 #include "common/common.h" 22 #include "utils/log_adapter.h" 23 24 using namespace mindspore::dataset; 25 26 class MindDataTestArena : public UT::Common { 27 public: 28 MindDataTestArena() {} 29 }; 30 31 32 TEST_F(MindDataTestArena, Test1) { 33 std::shared_ptr<Arena> mp; 34 Status rc = Arena::CreateArena(&mp); 35 ASSERT_TRUE(rc.IsOk()); 36 std::vector<void *> v; 37 38 srand(time(NULL)); 39 for (int i = 0; i < 1000; i++) { 40 uint64_t sz = rand() % 1048576; 41 void *ptr = nullptr; 42 ASSERT_TRUE(mp->Allocate(sz, &ptr)); 43 v.push_back(ptr); 44 } 45 for (int i = 0; i < 1000; i++) { 46 mp->Deallocate(v.at(i)); 47 } 48 MS_LOG(DEBUG) << *mp; 49 } 50 51 TEST_F(MindDataTestArena, Test2) { 52 std::shared_ptr<Arena> arena; 53 Status rc = Arena::CreateArena(&arena); 54 std::shared_ptr<MemoryPool> mp = std::static_pointer_cast<MemoryPool>(arena); 55 auto alloc = Allocator<int>(mp); 56 ASSERT_TRUE(rc.IsOk()); 57 std::vector<int, Allocator<int>> v(alloc); 58 v.reserve(1000); 59 for (auto i = 0; i < 1000; ++i) { 60 v.push_back(i); 61 } 62 // Test copy 63 std::vector<int, Allocator<int>> w(v, SystemPool::GetAllocator<int>()); 64 auto val = w.at(10); 65 EXPECT_EQ(val, 10); 66 // Test move 67 std::vector<int, Allocator<int>> s(std::move(v), SystemPool::GetAllocator<int>()); 68 val = s.at(100); 69 EXPECT_EQ(val, 100); 70 EXPECT_EQ(v.size(), 0); 71 } 72