1 #include "gtest/gtest.h"
6 #include "nsStringStream.h"
8 // Here we test the reading a pre-allocated size
9 TEST(TestReadStreamToString
, SyncStreamPreAllocatedSize
)
12 buffer
.AssignLiteral("Hello world!");
14 nsCOMPtr
<nsIInputStream
> stream
;
15 ASSERT_EQ(NS_OK
, NS_NewCStringInputStream(getter_AddRefs(stream
), buffer
));
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
)
35 buffer
.AssignLiteral("Hello world!");
37 nsCOMPtr
<nsIInputStream
> stream
;
38 ASSERT_EQ(NS_OK
, NS_NewCStringInputStream(getter_AddRefs(stream
), buffer
));
43 ASSERT_EQ(NS_OK
, NS_ReadInputStreamToString(stream
, result
, buffer
.Length(),
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
)
53 buffer
.AssignLiteral("Hello world!");
55 nsCOMPtr
<nsIInputStream
> stream
;
56 ASSERT_EQ(NS_OK
, NS_NewCStringInputStream(getter_AddRefs(stream
), buffer
));
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
)
70 buffer
.AssignLiteral("Hello world!");
72 nsCOMPtr
<nsIInputStream
> stream
;
73 ASSERT_EQ(NS_OK
, NS_NewCStringInputStream(getter_AddRefs(stream
), buffer
));
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
)
89 buffer
.AssignLiteral("Hello world!");
91 nsCOMPtr
<nsIInputStream
> stream
;
92 ASSERT_EQ(NS_OK
, NS_NewCStringInputStream(getter_AddRefs(stream
), buffer
));
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
)
107 buffer
.AssignLiteral("Hello world!");
109 nsCOMPtr
<nsIInputStream
> stream
= new testing::AsyncStringStream(buffer
);
112 nsAutoCString result
;
114 ASSERT_EQ(NS_OK
, NS_ReadInputStreamToString(stream
, result
, buffer
.Length(),
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
)
124 buffer
.AssignLiteral("Hello world!");
126 nsCOMPtr
<nsIInputStream
> stream
= new testing::AsyncStringStream(buffer
);
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
)
140 buffer
.AssignLiteral("Hello world!");
142 nsCOMPtr
<nsIInputStream
> stream
= new testing::AsyncStringStream(buffer
);
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
)
158 buffer
.AssignLiteral("Hello world!");
160 nsCOMPtr
<nsIInputStream
> stream
= new testing::AsyncStringStream(buffer
);
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
)
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
);
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
));