1 /*
2  * Copyright (C) 2014 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/swap_space.h"
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <cstdio>
24 
25 #include "gtest/gtest.h"
26 
27 #include "base/os.h"
28 #include "base/unix_file/fd_file.h"
29 #include "common_runtime_test.h"
30 
31 namespace art {
32 
33 class SwapSpaceTest : public CommonRuntimeTest {
34 };
35 
SwapTest(bool use_file)36 static void SwapTest(bool use_file) {
37   ScratchFile scratch;
38   int fd = scratch.GetFd();
39   unlink(scratch.GetFilename().c_str());
40 
41   SwapSpace pool(fd, 1 * MB);
42   SwapAllocator<void> alloc(use_file ? &pool : nullptr);
43 
44   SwapVector<int32_t> v(alloc);
45   v.reserve(1000000);
46   for (int32_t i = 0; i < 1000000; ++i) {
47     v.push_back(i);
48     EXPECT_EQ(i, v[i]);
49   }
50 
51   SwapVector<int32_t> v2(alloc);
52   v2.reserve(1000000);
53   for (int32_t i = 0; i < 1000000; ++i) {
54     v2.push_back(i);
55     EXPECT_EQ(i, v2[i]);
56   }
57 
58   SwapVector<int32_t> v3(alloc);
59   v3.reserve(500000);
60   for (int32_t i = 0; i < 1000000; ++i) {
61     v3.push_back(i);
62     EXPECT_EQ(i, v2[i]);
63   }
64 
65   // Verify contents.
66   for (int32_t i = 0; i < 1000000; ++i) {
67     EXPECT_EQ(i, v[i]);
68     EXPECT_EQ(i, v2[i]);
69     EXPECT_EQ(i, v3[i]);
70   }
71 
72   scratch.Close();
73 }
74 
TEST_F(SwapSpaceTest,Memory)75 TEST_F(SwapSpaceTest, Memory) {
76   SwapTest(false);
77 }
78 
TEST_F(SwapSpaceTest,Swap)79 TEST_F(SwapSpaceTest, Swap) {
80   SwapTest(true);
81 }
82 
83 }  // namespace art
84