- add sources.
[platform/framework/web/crosswalk.git] / src / net / disk_cache / v3 / block_bitmaps_unittest.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/addr.h"
6 #include "net/disk_cache/block_files.h"
7 #include "net/disk_cache/disk_format_base.h"
8 #include "net/disk_cache/v3/block_bitmaps.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 // Tests that we add and remove blocks correctly.
12 TEST(DiskCacheBlockBitmaps, V3AllocationMap) {
13   disk_cache::BlockBitmaps block_bitmaps;
14   disk_cache::BlockFilesBitmaps bitmaps;
15
16   const int kNumHeaders = 10;
17   disk_cache::BlockFileHeader headers[kNumHeaders];
18   for (int i = 0; i < kNumHeaders; i++) {
19     memset(&headers[i], 0, sizeof(headers[i]));
20     headers[i].magic = disk_cache::kBlockMagic;
21     headers[i].version = disk_cache::kBlockCurrentVersion;
22     headers[i].this_file = static_cast<int16>(i);
23     headers[i].empty[3] = 200;
24     headers[i].max_entries = 800;
25     bitmaps.push_back(disk_cache::BlockHeader(&headers[i]));
26   }
27
28   block_bitmaps.Init(bitmaps);
29
30   // Create a bunch of entries.
31   const int kSize = 100;
32   disk_cache::Addr address[kSize];
33   for (int i = 0; i < kSize; i++) {
34     SCOPED_TRACE(i);
35     int block_size = i % 4 + 1;
36     ASSERT_TRUE(block_bitmaps.CreateBlock(disk_cache::BLOCK_1K, block_size,
37                                           &address[i]));
38     EXPECT_EQ(disk_cache::BLOCK_1K, address[i].file_type());
39     EXPECT_EQ(block_size, address[i].num_blocks());
40     int start = address[i].start_block();
41
42     // Verify that the allocated entry doesn't cross a 4 block boundary.
43     EXPECT_EQ(start / 4, (start + block_size - 1) / 4);
44   }
45
46   for (int i = 0; i < kSize; i++) {
47     SCOPED_TRACE(i);
48     EXPECT_TRUE(block_bitmaps.IsValid(address[i]));
49   }
50
51   // The first part of the allocation map should be completely filled. We used
52   // 10 bits per each of four entries, so 250 bits total. All entries should go
53   // to the third file.
54   uint8* buffer = reinterpret_cast<uint8*>(&headers[2].allocation_map);
55   for (int i = 0; i < 29; i++) {
56     SCOPED_TRACE(i);
57     EXPECT_EQ(0xff, buffer[i]);
58   }
59
60   for (int i = 0; i < kSize; i++) {
61     SCOPED_TRACE(i);
62     block_bitmaps.DeleteBlock(address[i]);
63   }
64
65   // The allocation map should be empty.
66   for (int i =0; i < 50; i++) {
67     SCOPED_TRACE(i);
68     EXPECT_EQ(0, buffer[i]);
69   }
70 }