xref: /aosp_15_r20/external/libtextclassifier/native/utils/base/arena_leakage_unittest.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 "utils/base/arena.h"
18 #include "gtest/gtest.h"
19 
20 namespace libtextclassifier3 {
21 
TEST(Arena,Leakage)22 TEST(Arena, Leakage) {
23   UnsafeArena arena(32);
24   // Grab just 10 bytes.
25   EXPECT_EQ(arena.bytes_until_next_allocation(), 32);
26   const char* block = arena.Alloc(10);
27   EXPECT_NE(block, nullptr);
28   EXPECT_EQ(arena.bytes_until_next_allocation(), 22);
29   // Grab the rest.
30   const char* expected_next_block = block + 10;
31   const char* next_block = arena.Alloc(22);
32   // If the below test fails, a new block has been allocated for "next_block".
33   // This means that the last 22 bytes of the previous block have been lost.
34   EXPECT_EQ(next_block, expected_next_block);
35   EXPECT_EQ(arena.bytes_until_next_allocation(), 0);
36   // Try allocating a 0 bytes block. Arena should remain unchanged.
37   const char* null_block = arena.Alloc(0);
38   EXPECT_EQ(null_block, nullptr);
39   EXPECT_EQ(arena.bytes_until_next_allocation(), 0);
40 }
41 
42 }  //  namespace libtextclassifier3
43