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