3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/buffer.h"
29 #include "talk/base/gunit.h"
33 static const char kTestData[] = {
34 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
37 TEST(BufferTest, TestConstructDefault) {
39 EXPECT_EQ(0U, buf.length());
40 EXPECT_EQ(0U, buf.capacity());
41 EXPECT_EQ(Buffer(), buf);
44 TEST(BufferTest, TestConstructEmptyWithCapacity) {
45 Buffer buf(NULL, 0, 256U);
46 EXPECT_EQ(0U, buf.length());
47 EXPECT_EQ(256U, buf.capacity());
48 EXPECT_EQ(Buffer(), buf);
51 TEST(BufferTest, TestConstructData) {
52 Buffer buf(kTestData, sizeof(kTestData));
53 EXPECT_EQ(sizeof(kTestData), buf.length());
54 EXPECT_EQ(sizeof(kTestData), buf.capacity());
55 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
56 EXPECT_EQ(Buffer(kTestData, sizeof(kTestData)), buf);
59 TEST(BufferTest, TestConstructDataWithCapacity) {
60 Buffer buf(kTestData, sizeof(kTestData), 256U);
61 EXPECT_EQ(sizeof(kTestData), buf.length());
62 EXPECT_EQ(256U, buf.capacity());
63 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
64 EXPECT_EQ(Buffer(kTestData, sizeof(kTestData)), buf);
67 TEST(BufferTest, TestConstructCopy) {
68 Buffer buf1(kTestData, sizeof(kTestData), 256), buf2(buf1);
69 EXPECT_EQ(sizeof(kTestData), buf2.length());
70 EXPECT_EQ(sizeof(kTestData), buf2.capacity()); // capacity isn't copied
71 EXPECT_EQ(0, memcmp(buf2.data(), kTestData, sizeof(kTestData)));
72 EXPECT_EQ(buf1, buf2);
75 TEST(BufferTest, TestAssign) {
76 Buffer buf1, buf2(kTestData, sizeof(kTestData), 256);
77 EXPECT_NE(buf1, buf2);
79 EXPECT_EQ(sizeof(kTestData), buf1.length());
80 EXPECT_EQ(sizeof(kTestData), buf1.capacity()); // capacity isn't copied
81 EXPECT_EQ(0, memcmp(buf1.data(), kTestData, sizeof(kTestData)));
82 EXPECT_EQ(buf1, buf2);
85 TEST(BufferTest, TestSetData) {
87 buf.SetData(kTestData, sizeof(kTestData));
88 EXPECT_EQ(sizeof(kTestData), buf.length());
89 EXPECT_EQ(sizeof(kTestData), buf.capacity());
90 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
93 TEST(BufferTest, TestAppendData) {
94 Buffer buf(kTestData, sizeof(kTestData));
95 buf.AppendData(kTestData, sizeof(kTestData));
96 EXPECT_EQ(2 * sizeof(kTestData), buf.length());
97 EXPECT_EQ(2 * sizeof(kTestData), buf.capacity());
98 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
99 EXPECT_EQ(0, memcmp(buf.data() + sizeof(kTestData),
100 kTestData, sizeof(kTestData)));
103 TEST(BufferTest, TestSetLengthSmaller) {
105 buf.SetData(kTestData, sizeof(kTestData));
106 buf.SetLength(sizeof(kTestData) / 2);
107 EXPECT_EQ(sizeof(kTestData) / 2, buf.length());
108 EXPECT_EQ(sizeof(kTestData), buf.capacity());
109 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData) / 2));
112 TEST(BufferTest, TestSetLengthLarger) {
114 buf.SetData(kTestData, sizeof(kTestData));
115 buf.SetLength(sizeof(kTestData) * 2);
116 EXPECT_EQ(sizeof(kTestData) * 2, buf.length());
117 EXPECT_EQ(sizeof(kTestData) * 2, buf.capacity());
118 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
121 TEST(BufferTest, TestSetCapacitySmaller) {
123 buf.SetData(kTestData, sizeof(kTestData));
124 buf.SetCapacity(sizeof(kTestData) / 2); // should be ignored
125 EXPECT_EQ(sizeof(kTestData), buf.length());
126 EXPECT_EQ(sizeof(kTestData), buf.capacity());
127 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
130 TEST(BufferTest, TestSetCapacityLarger) {
131 Buffer buf(kTestData, sizeof(kTestData));
132 buf.SetCapacity(sizeof(kTestData) * 2);
133 EXPECT_EQ(sizeof(kTestData), buf.length());
134 EXPECT_EQ(sizeof(kTestData) * 2, buf.capacity());
135 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
138 TEST(BufferTest, TestSetCapacityThenSetLength) {
139 Buffer buf(kTestData, sizeof(kTestData));
140 buf.SetCapacity(sizeof(kTestData) * 4);
141 memcpy(buf.data() + sizeof(kTestData), kTestData, sizeof(kTestData));
142 buf.SetLength(sizeof(kTestData) * 2);
143 EXPECT_EQ(sizeof(kTestData) * 2, buf.length());
144 EXPECT_EQ(sizeof(kTestData) * 4, buf.capacity());
145 EXPECT_EQ(0, memcmp(buf.data(), kTestData, sizeof(kTestData)));
146 EXPECT_EQ(0, memcmp(buf.data() + sizeof(kTestData),
147 kTestData, sizeof(kTestData)));
150 TEST(BufferTest, TestTransfer) {
151 Buffer buf1(kTestData, sizeof(kTestData), 256U), buf2;
152 buf1.TransferTo(&buf2);
153 EXPECT_EQ(0U, buf1.length());
154 EXPECT_EQ(0U, buf1.capacity());
155 EXPECT_EQ(sizeof(kTestData), buf2.length());
156 EXPECT_EQ(256U, buf2.capacity()); // capacity does transfer
157 EXPECT_EQ(0, memcmp(buf2.data(), kTestData, sizeof(kTestData)));
160 } // namespace talk_base