Using pre-existing constants instead of hard-coding
[chromium-blink-merge.git] / net / spdy / hpack_entry_test.cc
blob919a316a9b32dd293708be1f9a8764e4bc3d3345
1 // Copyright 2014 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/spdy/hpack_entry.h"
7 #include <string>
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace net {
13 namespace {
15 using std::string;
17 class HpackEntryTest : public ::testing::Test {
18 protected:
19 HpackEntryTest()
20 : name_("header-name"),
21 value_("header value"),
22 total_insertions_(0),
23 table_size_(0) {}
25 // These builders maintain the same external table invariants that a "real"
26 // table (ie HpackHeaderTable) would.
27 HpackEntry StaticEntry() {
28 return HpackEntry(name_, value_, true, total_insertions_++);
30 HpackEntry DynamicEntry() {
31 ++table_size_;
32 size_t index = total_insertions_++;
33 return HpackEntry(name_, value_, false, index);
35 void DropEntry() { --table_size_; }
37 size_t IndexOf(const HpackEntry& entry) const {
38 if (entry.IsStatic()) {
39 return 1 + entry.InsertionIndex() + table_size_;
40 } else {
41 return total_insertions_ - entry.InsertionIndex();
45 size_t Size() {
46 return name_.size() + value_.size() + HpackEntry::kSizeOverhead;
49 string name_, value_;
51 private:
52 // Referenced by HpackEntry instances.
53 size_t total_insertions_;
54 size_t table_size_;
57 TEST_F(HpackEntryTest, StaticConstructor) {
58 HpackEntry entry(StaticEntry());
60 EXPECT_EQ(name_, entry.name());
61 EXPECT_EQ(value_, entry.value());
62 EXPECT_TRUE(entry.IsStatic());
63 EXPECT_EQ(1u, IndexOf(entry));
64 EXPECT_EQ(0u, entry.state());
65 EXPECT_EQ(Size(), entry.Size());
68 TEST_F(HpackEntryTest, DynamicConstructor) {
69 HpackEntry entry(DynamicEntry());
71 EXPECT_EQ(name_, entry.name());
72 EXPECT_EQ(value_, entry.value());
73 EXPECT_FALSE(entry.IsStatic());
74 EXPECT_EQ(1u, IndexOf(entry));
75 EXPECT_EQ(0u, entry.state());
76 EXPECT_EQ(Size(), entry.Size());
79 TEST_F(HpackEntryTest, LookupConstructor) {
80 HpackEntry entry(name_, value_);
82 EXPECT_EQ(name_, entry.name());
83 EXPECT_EQ(value_, entry.value());
84 EXPECT_FALSE(entry.IsStatic());
85 EXPECT_EQ(0u, IndexOf(entry));
86 EXPECT_EQ(0u, entry.state());
87 EXPECT_EQ(Size(), entry.Size());
90 TEST_F(HpackEntryTest, DefaultConstructor) {
91 HpackEntry entry;
93 EXPECT_TRUE(entry.name().empty());
94 EXPECT_TRUE(entry.value().empty());
95 EXPECT_EQ(0u, entry.state());
96 EXPECT_EQ(HpackEntry::kSizeOverhead, entry.Size());
99 TEST_F(HpackEntryTest, IndexUpdate) {
100 HpackEntry static1(StaticEntry());
101 HpackEntry static2(StaticEntry());
103 EXPECT_EQ(1u, IndexOf(static1));
104 EXPECT_EQ(2u, IndexOf(static2));
106 HpackEntry dynamic1(DynamicEntry());
107 HpackEntry dynamic2(DynamicEntry());
109 EXPECT_EQ(1u, IndexOf(dynamic2));
110 EXPECT_EQ(2u, IndexOf(dynamic1));
111 EXPECT_EQ(3u, IndexOf(static1));
112 EXPECT_EQ(4u, IndexOf(static2));
114 DropEntry(); // Drops |dynamic1|.
116 EXPECT_EQ(1u, IndexOf(dynamic2));
117 EXPECT_EQ(2u, IndexOf(static1));
118 EXPECT_EQ(3u, IndexOf(static2));
120 HpackEntry dynamic3(DynamicEntry());
122 EXPECT_EQ(1u, IndexOf(dynamic3));
123 EXPECT_EQ(2u, IndexOf(dynamic2));
124 EXPECT_EQ(3u, IndexOf(static1));
125 EXPECT_EQ(4u, IndexOf(static2));
128 } // namespace
130 } // namespace net