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