Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / gtest / TestReadStreamToString.cpp
blobf5fa0a4979450c8b3f72de2d591e6b9e92a4b308
1 #include "gtest/gtest.h"
3 #include "Helpers.h"
4 #include "nsCOMPtr.h"
5 #include "nsNetUtil.h"
6 #include "nsStringStream.h"
8 // Here we test the reading a pre-allocated size
9 TEST(TestReadStreamToString, SyncStreamPreAllocatedSize)
11 nsCString buffer;
12 buffer.AssignLiteral("Hello world!");
14 nsCOMPtr<nsIInputStream> stream;
15 ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
17 uint64_t written;
18 nsAutoCString result;
19 result.SetLength(5);
21 void* ptr = result.BeginWriting();
23 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
24 ASSERT_EQ((uint64_t)5, written);
25 ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
27 // The pointer should be equal: no relocation.
28 ASSERT_EQ(ptr, result.BeginWriting());
31 // Here we test the reading the full size of a sync stream
32 TEST(TestReadStreamToString, SyncStreamFullSize)
34 nsCString buffer;
35 buffer.AssignLiteral("Hello world!");
37 nsCOMPtr<nsIInputStream> stream;
38 ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
40 uint64_t written;
41 nsAutoCString result;
43 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
44 &written));
45 ASSERT_EQ(buffer.Length(), written);
46 ASSERT_TRUE(buffer.Equals(result));
49 // Here we test the reading less than the full size of a sync stream
50 TEST(TestReadStreamToString, SyncStreamLessThan)
52 nsCString buffer;
53 buffer.AssignLiteral("Hello world!");
55 nsCOMPtr<nsIInputStream> stream;
56 ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
58 uint64_t written;
59 nsAutoCString result;
61 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
62 ASSERT_EQ((uint64_t)5, written);
63 ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
66 // Here we test the reading more than the full size of a sync stream
67 TEST(TestReadStreamToString, SyncStreamMoreThan)
69 nsCString buffer;
70 buffer.AssignLiteral("Hello world!");
72 nsCOMPtr<nsIInputStream> stream;
73 ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
75 uint64_t written;
76 nsAutoCString result;
78 // Reading more than the buffer size.
79 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
80 buffer.Length() + 5, &written));
81 ASSERT_EQ(buffer.Length(), written);
82 ASSERT_TRUE(buffer.Equals(result));
85 // Here we test the reading a sync stream without passing the size
86 TEST(TestReadStreamToString, SyncStreamUnknownSize)
88 nsCString buffer;
89 buffer.AssignLiteral("Hello world!");
91 nsCOMPtr<nsIInputStream> stream;
92 ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
94 uint64_t written;
95 nsAutoCString result;
97 // Reading all without passing the size
98 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
99 ASSERT_EQ(buffer.Length(), written);
100 ASSERT_TRUE(buffer.Equals(result));
103 // Here we test the reading the full size of an async stream
104 TEST(TestReadStreamToString, AsyncStreamFullSize)
106 nsCString buffer;
107 buffer.AssignLiteral("Hello world!");
109 nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
111 uint64_t written;
112 nsAutoCString result;
114 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
115 &written));
116 ASSERT_EQ(buffer.Length(), written);
117 ASSERT_TRUE(buffer.Equals(result));
120 // Here we test the reading less than the full size of an async stream
121 TEST(TestReadStreamToString, AsyncStreamLessThan)
123 nsCString buffer;
124 buffer.AssignLiteral("Hello world!");
126 nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
128 uint64_t written;
129 nsAutoCString result;
131 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
132 ASSERT_EQ((uint64_t)5, written);
133 ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
136 // Here we test the reading more than the full size of an async stream
137 TEST(TestReadStreamToString, AsyncStreamMoreThan)
139 nsCString buffer;
140 buffer.AssignLiteral("Hello world!");
142 nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
144 uint64_t written;
145 nsAutoCString result;
147 // Reading more than the buffer size.
148 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
149 buffer.Length() + 5, &written));
150 ASSERT_EQ(buffer.Length(), written);
151 ASSERT_TRUE(buffer.Equals(result));
154 // Here we test the reading an async stream without passing the size
155 TEST(TestReadStreamToString, AsyncStreamUnknownSize)
157 nsCString buffer;
158 buffer.AssignLiteral("Hello world!");
160 nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
162 uint64_t written;
163 nsAutoCString result;
165 // Reading all without passing the size
166 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
167 ASSERT_EQ(buffer.Length(), written);
168 ASSERT_TRUE(buffer.Equals(result));
171 // Here we test the reading an async big stream without passing the size
172 TEST(TestReadStreamToString, AsyncStreamUnknownBigSize)
174 nsCString buffer;
176 buffer.SetLength(4096 * 2);
177 for (uint32_t i = 0; i < 4096 * 2; ++i) {
178 buffer.BeginWriting()[i] = i % 10;
181 nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
183 uint64_t written;
184 nsAutoCString result;
186 // Reading all without passing the size
187 ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
188 ASSERT_EQ(buffer.Length(), written);
189 ASSERT_TRUE(buffer.Equals(result));