|
| 1 | +/*----------------------------------------------------------------------------------------------- |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2014-2024 Kim Kulling |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +this software and associated documentation files (the "Software"), to deal in |
| 8 | +the Software without restriction, including without limitation the rights to |
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +-----------------------------------------------------------------------------------------------*/ |
1 | 23 | #include <cppcore/Memory/TScratchAllocator.h>
|
2 | 24 |
|
3 | 25 | #include <gtest/gtest.h>
|
4 | 26 |
|
5 | 27 | using namespace cppcore;
|
6 | 28 |
|
7 |
| -class TScratchAllocatorTest : public testing::Test {}; |
| 29 | +class TScratchAllocatorTest : public testing::Test { |
| 30 | +public: |
| 31 | + static constexpr size_t BufferSize = 1024u; |
| 32 | +}; |
8 | 33 |
|
9 | 34 | TEST_F(TScratchAllocatorTest, CreateTest) {
|
10 | 35 | bool ok( true );
|
11 | 36 | try {
|
12 |
| - ScratchAllocator myAllocator(1024); |
| 37 | + ScratchAllocator myAllocator(BufferSize); |
13 | 38 | } catch ( ... ) {
|
14 | 39 | ok = false;
|
15 | 40 | }
|
16 | 41 | EXPECT_TRUE( ok );
|
17 | 42 | }
|
18 | 43 |
|
19 | 44 | TEST_F(TScratchAllocatorTest, AllocTest) {
|
20 |
| - ScratchAllocator myAllocator(1024); |
| 45 | + ScratchAllocator myAllocator(BufferSize); |
21 | 46 | char *ptr1 = myAllocator.alloc(512);
|
22 | 47 | EXPECT_NE(ptr1, nullptr);
|
23 |
| - EXPECT_EQ(myAllocator.capacity(), 1024); |
| 48 | + EXPECT_EQ(myAllocator.capacity(), BufferSize); |
24 | 49 | EXPECT_EQ(myAllocator.freeMem(), 512);
|
| 50 | + EXPECT_EQ(myAllocator.reservedMem(), 512); |
25 | 51 |
|
26 | 52 | char *ptr2 = myAllocator.alloc(600);
|
27 | 53 | EXPECT_EQ(ptr2, nullptr);
|
| 54 | + |
| 55 | + myAllocator.clear(); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(TScratchAllocatorTest, ClearTest) { |
| 59 | + ScratchAllocator myAllocator(BufferSize); |
| 60 | + EXPECT_EQ(myAllocator.capacity(), BufferSize); |
| 61 | + EXPECT_EQ(myAllocator.freeMem(), BufferSize); |
| 62 | + |
| 63 | + myAllocator.clear(); |
| 64 | + EXPECT_EQ(myAllocator.capacity(), 0u); |
| 65 | + EXPECT_EQ(myAllocator.freeMem(), 0u); |
28 | 66 | }
|
0 commit comments