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