Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / stats_unittest.cc
blobfe47bdd4cf634861feb5c74e1e5fdf371840d13f
1 // Copyright 2015 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.
5 #include "net/disk_cache/blockfile/stats.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 TEST(DiskCacheStatsTest, Init) {
11 disk_cache::Stats stats;
12 EXPECT_TRUE(stats.Init(nullptr, 0, disk_cache::Addr()));
13 EXPECT_EQ(0, stats.GetCounter(disk_cache::Stats::TRIM_ENTRY));
16 TEST(DiskCacheStatsTest, InitWithEmptyBuffer) {
17 disk_cache::Stats stats;
18 int required_len = stats.StorageSize();
19 scoped_ptr<char[]> storage(new char[required_len]);
20 memset(storage.get(), 0, required_len);
22 ASSERT_TRUE(stats.Init(storage.get(), required_len, disk_cache::Addr()));
23 EXPECT_EQ(0, stats.GetCounter(disk_cache::Stats::TRIM_ENTRY));
26 TEST(DiskCacheStatsTest, FailsInit) {
27 disk_cache::Stats stats;
28 int required_len = stats.StorageSize();
29 scoped_ptr<char[]> storage(new char[required_len]);
30 memset(storage.get(), 0, required_len);
32 // Try a small buffer.
33 EXPECT_LT(200, required_len);
34 disk_cache::Addr addr;
35 EXPECT_FALSE(stats.Init(storage.get(), 200, addr));
37 // Try a buffer with garbage.
38 memset(storage.get(), 'a', required_len);
39 EXPECT_FALSE(stats.Init(storage.get(), required_len, addr));
42 TEST(DiskCacheStatsTest, SaveRestore) {
43 scoped_ptr<disk_cache::Stats> stats(new disk_cache::Stats);
45 disk_cache::Addr addr(5);
46 ASSERT_TRUE(stats->Init(nullptr, 0, addr));
47 stats->SetCounter(disk_cache::Stats::CREATE_ERROR, 11);
48 stats->SetCounter(disk_cache::Stats::DOOM_ENTRY, 13);
49 stats->OnEvent(disk_cache::Stats::MIN_COUNTER);
50 stats->OnEvent(disk_cache::Stats::TRIM_ENTRY);
51 stats->OnEvent(disk_cache::Stats::DOOM_RECENT);
53 int required_len = stats->StorageSize();
54 scoped_ptr<char[]> storage(new char[required_len]);
55 disk_cache::Addr out_addr;
56 int real_len = stats->SerializeStats(storage.get(), required_len, &out_addr);
57 EXPECT_GE(required_len, real_len);
58 EXPECT_EQ(out_addr, addr);
60 stats.reset(new disk_cache::Stats);
61 ASSERT_TRUE(stats->Init(storage.get(), real_len, addr));
62 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::MIN_COUNTER));
63 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::TRIM_ENTRY));
64 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::DOOM_RECENT));
65 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::OPEN_HIT));
66 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::READ_DATA));
67 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::LAST_REPORT_TIMER));
68 EXPECT_EQ(11, stats->GetCounter(disk_cache::Stats::CREATE_ERROR));
69 EXPECT_EQ(13, stats->GetCounter(disk_cache::Stats::DOOM_ENTRY));
71 // Now pass the whole buffer. It shoulod not matter that there is unused
72 // space at the end.
73 stats.reset(new disk_cache::Stats);
74 ASSERT_TRUE(stats->Init(storage.get(), required_len, addr));
75 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::MIN_COUNTER));
76 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::TRIM_ENTRY));
77 EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::DOOM_RECENT));
78 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::OPEN_HIT));
79 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::READ_DATA));
80 EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::LAST_REPORT_TIMER));
81 EXPECT_EQ(11, stats->GetCounter(disk_cache::Stats::CREATE_ERROR));
82 EXPECT_EQ(13, stats->GetCounter(disk_cache::Stats::DOOM_ENTRY));