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.
5 #include "net/quic/quic_bandwidth.h"
6 #include "testing/gtest/include/gtest/gtest.h"
11 class QuicBandwidthTest
: public ::testing::Test
{
14 TEST_F(QuicBandwidthTest
, FromTo
) {
15 EXPECT_EQ(QuicBandwidth::FromKBitsPerSecond(1),
16 QuicBandwidth::FromBitsPerSecond(1000));
17 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1),
18 QuicBandwidth::FromBytesPerSecond(1000));
19 EXPECT_EQ(QuicBandwidth::FromBitsPerSecond(8000),
20 QuicBandwidth::FromBytesPerSecond(1000));
21 EXPECT_EQ(QuicBandwidth::FromKBitsPerSecond(8),
22 QuicBandwidth::FromKBytesPerSecond(1));
24 EXPECT_EQ(0, QuicBandwidth::Zero().ToBitsPerSecond());
25 EXPECT_EQ(0, QuicBandwidth::Zero().ToKBitsPerSecond());
26 EXPECT_EQ(0, QuicBandwidth::Zero().ToBytesPerSecond());
27 EXPECT_EQ(0, QuicBandwidth::Zero().ToKBytesPerSecond());
29 EXPECT_EQ(1, QuicBandwidth::FromBitsPerSecond(1000).ToKBitsPerSecond());
30 EXPECT_EQ(1000, QuicBandwidth::FromKBitsPerSecond(1).ToBitsPerSecond());
31 EXPECT_EQ(1, QuicBandwidth::FromBytesPerSecond(1000).ToKBytesPerSecond());
32 EXPECT_EQ(1000, QuicBandwidth::FromKBytesPerSecond(1).ToBytesPerSecond());
35 TEST_F(QuicBandwidthTest
, Add
) {
36 QuicBandwidth bandwidht_1
= QuicBandwidth::FromKBitsPerSecond(1);
37 QuicBandwidth bandwidht_2
= QuicBandwidth::FromKBytesPerSecond(1);
39 EXPECT_EQ(9000, bandwidht_1
.Add(bandwidht_2
).ToBitsPerSecond());
40 EXPECT_EQ(9000, bandwidht_2
.Add(bandwidht_1
).ToBitsPerSecond());
43 TEST_F(QuicBandwidthTest
, Subtract
) {
44 QuicBandwidth bandwidht_1
= QuicBandwidth::FromKBitsPerSecond(1);
45 QuicBandwidth bandwidht_2
= QuicBandwidth::FromKBytesPerSecond(1);
47 EXPECT_EQ(7000, bandwidht_2
.Subtract(bandwidht_1
).ToBitsPerSecond());
50 TEST_F(QuicBandwidthTest
, TimeDelta
) {
51 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1000),
52 QuicBandwidth::FromBytesAndTimeDelta(
53 1000, QuicTime::Delta::FromMilliseconds(1)));
55 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(10),
56 QuicBandwidth::FromBytesAndTimeDelta(
57 1000, QuicTime::Delta::FromMilliseconds(100)));
60 TEST_F(QuicBandwidthTest
, Scale
) {
61 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(500),
62 QuicBandwidth::FromKBytesPerSecond(1000).Scale(0.5f
));
63 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(750),
64 QuicBandwidth::FromKBytesPerSecond(1000).Scale(0.75f
));
65 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1250),
66 QuicBandwidth::FromKBytesPerSecond(1000).Scale(1.25f
));
70 TEST_F(QuicBandwidthTest
, BytesPerPeriod
) {
71 EXPECT_EQ(2000u, QuicBandwidth::FromKBytesPerSecond(2000).ToBytesPerPeriod(
72 QuicTime::Delta::FromMilliseconds(1)));
73 EXPECT_EQ(2u, QuicBandwidth::FromKBytesPerSecond(2000).ToKBytesPerPeriod(
74 QuicTime::Delta::FromMilliseconds(1)));
75 EXPECT_EQ(200000u, QuicBandwidth::FromKBytesPerSecond(2000).ToBytesPerPeriod(
76 QuicTime::Delta::FromMilliseconds(100)));
77 EXPECT_EQ(200u, QuicBandwidth::FromKBytesPerSecond(2000).ToKBytesPerPeriod(
78 QuicTime::Delta::FromMilliseconds(100)));
81 TEST_F(QuicBandwidthTest
, TransferTime
) {
82 EXPECT_EQ(QuicTime::Delta::FromSeconds(1),
83 QuicBandwidth::FromKBytesPerSecond(1).TransferTime(1000));
84 EXPECT_EQ(QuicTime::Delta::Zero(), QuicBandwidth::Zero().TransferTime(1000));
87 TEST_F(QuicBandwidthTest
, RelOps
) {
88 const QuicBandwidth b1
= QuicBandwidth::FromKBitsPerSecond(1);
89 const QuicBandwidth b2
= QuicBandwidth::FromKBytesPerSecond(2);