Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / frame_id_wrap_helper_test.cc
blob499d7de795d4a788cc1fa0de6a6b62868a984624
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 <gtest/gtest.h>
6 #include "media/cast/cast_defines.h"
7 #include "media/cast/net/cast_transport_defines.h"
9 namespace media {
10 namespace cast {
12 class FrameIdWrapHelperTest : public ::testing::Test {
13 protected:
14 FrameIdWrapHelperTest() {}
15 ~FrameIdWrapHelperTest() override {}
17 void RunOneTest(uint32 starting_point, int iterations) {
18 const int window_size = 127;
19 uint32 window_base = starting_point;
20 frame_id_wrap_helper_.largest_frame_id_seen_ = starting_point;
21 for (int i = 0; i < iterations; i++) {
22 uint32 largest_frame_id_seen =
23 frame_id_wrap_helper_.largest_frame_id_seen_;
24 int offset = rand() % window_size;
25 uint32 frame_id = window_base + offset;
26 uint32 mapped_frame_id =
27 frame_id_wrap_helper_.MapTo32bitsFrameId(frame_id & 0xff);
28 EXPECT_EQ(frame_id, mapped_frame_id)
29 << " Largest ID seen: " << largest_frame_id_seen
30 << " Window base: " << window_base
31 << " Offset: " << offset;
32 window_base = frame_id;
36 FrameIdWrapHelper frame_id_wrap_helper_;
38 DISALLOW_COPY_AND_ASSIGN(FrameIdWrapHelperTest);
41 TEST_F(FrameIdWrapHelperTest, FirstFrame) {
42 EXPECT_EQ(kStartFrameId, frame_id_wrap_helper_.MapTo32bitsFrameId(255u));
45 TEST_F(FrameIdWrapHelperTest, Rollover) {
46 uint32 new_frame_id = 0u;
47 for (int i = 0; i <= 256; ++i) {
48 new_frame_id =
49 frame_id_wrap_helper_.MapTo32bitsFrameId(static_cast<uint8>(i));
51 EXPECT_EQ(256u, new_frame_id);
54 TEST_F(FrameIdWrapHelperTest, OutOfOrder) {
55 uint32 new_frame_id = 0u;
56 for (int i = 0; i < 255; ++i) {
57 new_frame_id =
58 frame_id_wrap_helper_.MapTo32bitsFrameId(static_cast<uint8>(i));
60 EXPECT_EQ(254u, new_frame_id);
61 new_frame_id = frame_id_wrap_helper_.MapTo32bitsFrameId(0u);
62 EXPECT_EQ(256u, new_frame_id);
63 new_frame_id = frame_id_wrap_helper_.MapTo32bitsFrameId(255u);
64 EXPECT_EQ(255u, new_frame_id);
65 new_frame_id = frame_id_wrap_helper_.MapTo32bitsFrameId(1u);
66 EXPECT_EQ(257u, new_frame_id);
69 TEST_F(FrameIdWrapHelperTest, Windowed) {
70 srand(0);
71 for (int i = 0; i < 50000 && !HasFailure(); i++) {
72 RunOneTest(i * 4711, 20);
73 // Test wrap-around scenarios.
74 RunOneTest(0x7fffff00ul, 20);
75 RunOneTest(0xffffff00ul, 20);
79 } // namespace cast
80 } // namespace media