| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/spdy_priority_tree.h" | 5 #include "net/spdy/spdy_priority_tree.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 9 |
| 11 namespace net { | 10 namespace net { |
| 12 | 11 |
| 13 namespace test { | 12 namespace test { |
| 14 | 13 |
| 15 template <typename NodeId> | 14 template <typename NodeId> |
| 16 class SpdyPriorityTreePeer { | 15 class SpdyPriorityTreePeer { |
| 17 public: | 16 public: |
| 18 explicit SpdyPriorityTreePeer(SpdyPriorityTree<NodeId>* tree) : tree_(tree) {} | 17 explicit SpdyPriorityTreePeer(SpdyPriorityTree<NodeId>* tree) : tree_(tree) {} |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 SpdyPriorityTree<uint32> tree; | 28 SpdyPriorityTree<uint32> tree; |
| 30 EXPECT_EQ(1, tree.num_nodes()); | 29 EXPECT_EQ(1, tree.num_nodes()); |
| 31 EXPECT_TRUE(tree.NodeExists(0)); | 30 EXPECT_TRUE(tree.NodeExists(0)); |
| 32 EXPECT_FALSE(tree.NodeExists(1)); | 31 EXPECT_FALSE(tree.NodeExists(1)); |
| 33 | 32 |
| 34 EXPECT_TRUE(tree.AddNode(1, 0, 100, false)); | 33 EXPECT_TRUE(tree.AddNode(1, 0, 100, false)); |
| 35 EXPECT_EQ(2, tree.num_nodes()); | 34 EXPECT_EQ(2, tree.num_nodes()); |
| 36 ASSERT_TRUE(tree.NodeExists(1)); | 35 ASSERT_TRUE(tree.NodeExists(1)); |
| 37 EXPECT_EQ(100, tree.GetWeight(1)); | 36 EXPECT_EQ(100, tree.GetWeight(1)); |
| 38 EXPECT_FALSE(tree.NodeExists(5)); | 37 EXPECT_FALSE(tree.NodeExists(5)); |
| 39 EXPECT_THAT(tree.GetChildren(0), | |
| 40 ::testing::Pointee(::testing::ElementsAre(1))); | |
| 41 | 38 |
| 42 EXPECT_TRUE(tree.AddNode(5, 0, 50, false)); | 39 EXPECT_TRUE(tree.AddNode(5, 0, 50, false)); |
| 43 // Should not be able to add a node with an id that already exists. | 40 // Should not be able to add a node with an id that already exists. |
| 44 EXPECT_FALSE(tree.AddNode(5, 1, 50, false)); | 41 EXPECT_FALSE(tree.AddNode(5, 1, 50, false)); |
| 45 EXPECT_EQ(3, tree.num_nodes()); | 42 EXPECT_EQ(3, tree.num_nodes()); |
| 46 EXPECT_TRUE(tree.NodeExists(1)); | 43 EXPECT_TRUE(tree.NodeExists(1)); |
| 47 ASSERT_TRUE(tree.NodeExists(5)); | 44 ASSERT_TRUE(tree.NodeExists(5)); |
| 48 EXPECT_EQ(50, tree.GetWeight(5)); | 45 EXPECT_EQ(50, tree.GetWeight(5)); |
| 49 EXPECT_FALSE(tree.NodeExists(13)); | 46 EXPECT_FALSE(tree.NodeExists(13)); |
| 50 | 47 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 67 | 64 |
| 68 // The parent node 19 doesn't exist, so this should fail: | 65 // The parent node 19 doesn't exist, so this should fail: |
| 69 EXPECT_FALSE(tree.AddNode(7, 19, 70, false)); | 66 EXPECT_FALSE(tree.AddNode(7, 19, 70, false)); |
| 70 // This should succeed, creating node 7: | 67 // This should succeed, creating node 7: |
| 71 EXPECT_TRUE(tree.AddNode(7, 13, 70, false)); | 68 EXPECT_TRUE(tree.AddNode(7, 13, 70, false)); |
| 72 // Now node 7 already exists, so this should fail: | 69 // Now node 7 already exists, so this should fail: |
| 73 EXPECT_FALSE(tree.AddNode(7, 1, 70, false)); | 70 EXPECT_FALSE(tree.AddNode(7, 1, 70, false)); |
| 74 // Try adding a second child to node 13: | 71 // Try adding a second child to node 13: |
| 75 EXPECT_TRUE(tree.AddNode(17, 13, 170, false)); | 72 EXPECT_TRUE(tree.AddNode(17, 13, 170, false)); |
| 76 | 73 |
| 77 // TODO(birenroy): Add a separate test that verifies weight invariants when | |
| 78 // SetWeight is called. | |
| 79 EXPECT_TRUE(tree.SetWeight(17, 150)); | |
| 80 EXPECT_EQ(150, tree.GetWeight(17)); | |
| 81 | |
| 82 ASSERT_TRUE(tree.ValidateInvariantsForTests()); | 74 ASSERT_TRUE(tree.ValidateInvariantsForTests()); |
| 83 } | 75 } |
| 84 | 76 |
| 85 TEST(SpdyPriorityTreeTest, SetParent) { | 77 TEST(SpdyPriorityTreeTest, SetParent) { |
| 86 SpdyPriorityTree<uint32> tree; | 78 SpdyPriorityTree<uint32> tree; |
| 87 tree.AddNode(1, 0, 100, false); | 79 tree.AddNode(1, 0, 100, false); |
| 88 tree.AddNode(2, 1, 20, false); | 80 tree.AddNode(2, 1, 20, false); |
| 89 tree.AddNode(3, 2, 30, false); | 81 tree.AddNode(3, 2, 30, false); |
| 90 tree.AddNode(5, 3, 50, false); | 82 tree.AddNode(5, 3, 50, false); |
| 91 tree.AddNode(7, 5, 70, false); | 83 tree.AddNode(7, 5, 70, false); |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 EXPECT_EQ(3, tree.GetWeight(4)); | 390 EXPECT_EQ(3, tree.GetWeight(4)); |
| 399 EXPECT_EQ(3, tree.GetWeight(5)); | 391 EXPECT_EQ(3, tree.GetWeight(5)); |
| 400 // 2.5 rounded up = 3. | 392 // 2.5 rounded up = 3. |
| 401 EXPECT_EQ(3, tree.GetWeight(6)); | 393 EXPECT_EQ(3, tree.GetWeight(6)); |
| 402 EXPECT_EQ(3, tree.GetWeight(7)); | 394 EXPECT_EQ(3, tree.GetWeight(7)); |
| 403 // 0 is not a valid weight, so round up to 1. | 395 // 0 is not a valid weight, so round up to 1. |
| 404 EXPECT_EQ(1, tree.GetWeight(8)); | 396 EXPECT_EQ(1, tree.GetWeight(8)); |
| 405 } | 397 } |
| 406 } // namespace test | 398 } // namespace test |
| 407 } // namespace gfe_spdy | 399 } // namespace gfe_spdy |
| OLD | NEW |