29 #include <QByteArrayView>
38 constexpr
bool CanConvert = std::is_convertible_v<T, QByteArrayView>;
58 static_assert(!
CanConvert<
const signed char[1]>);
64 static_assert(!
CanConvert<
const std::byte[1]>);
93 static_assert(
CanConvert< std::vector<char> >);
94 static_assert(
CanConvert<
const std::vector<char> >);
95 static_assert(
CanConvert< std::vector<char>&>);
96 static_assert(
CanConvert<
const std::vector<char>&>);
98 static_assert(
CanConvert< std::array<char, 1> >);
99 static_assert(
CanConvert<
const std::array<char, 1> >);
100 static_assert(
CanConvert< std::array<char, 1>&>);
101 static_assert(
CanConvert<
const std::array<char, 1>&>);
111 void constExpr()
const;
113 void literals()
const;
114 void fromArray()
const;
115 void literalsWithInternalNulls()
const;
118 void fromQByteArray()
const;
120 void fromCharStar()
const
122 fromEmptyLiteral<char>();
123 conversionTests(
"Hello, World!");
126 void fromUCharStar()
const
128 fromEmptyLiteral<uchar>();
130 const uchar data[] = {
'H',
'e',
'l',
'l',
'o',
',',
' ',
'W',
'o',
'r',
'l',
'd',
'!', 0 };
131 conversionTests(
data);
134 void fromSignedCharStar()
const
136 fromEmptyLiteral<signed char>();
138 const signed char data[] = {
'H',
'e',
'l',
'l',
'o',
',',
' ',
'W',
'o',
'r',
'l',
'd',
'!', 0 };
139 conversionTests(
data);
142 void fromStdByteArray()
const
144 fromEmptyLiteral<std::byte>();
146 const std::byte
data[] = {std::byte{
'H'}, std::byte{
'e'}, std::byte{
'l'}, std::byte{
'l'},
147 std::byte{
'o'}, std::byte{0}};
148 conversionTests(
data);
151 void fromCharRange()
const
153 const char data[] = {
'H',
'e',
'l',
'l',
'o',
',',
' ',
'W',
'o',
'r',
'l',
'd',
'!' };
157 void fromUCharRange()
const
159 const uchar data[] = {
'H',
'e',
'l',
'l',
'o',
',',
' ',
'W',
'o',
'r',
'l',
'd',
'!', 0 };
163 void fromSignedCharRange()
const
165 const signed char data[] = {
'H',
'e',
'l',
'l',
'o',
',',
' ',
'W',
'o',
'r',
'l',
'd',
'!', 0 };
169 void fromStdByteRange()
const
171 const std::byte
data[] = {std::byte{
'H'}, std::byte{
'e'}, std::byte{
'l'}, std::byte{
'l'},
172 std::byte{
'o'}, std::byte{0}};
176 void fromCharContainers()
const
178 fromContainers<char>();
181 void fromUCharContainers()
const
183 fromContainers<uchar>();
186 void fromSignedCharContainers()
const
188 fromContainers<signed char>();
191 void fromStdByteContainers()
const
193 fromContainers<std::byte>();
196 void comparison()
const;
197 void compare()
const;
200 template <
typename Data>
201 void conversionTests(
Data arg)
const;
202 template <
typename Char>
203 void fromEmptyLiteral()
const;
204 template <
typename Char>
206 template <
typename Char,
typename Container>
207 void fromContainer()
const;
208 template <
typename Char>
209 void fromContainers()
const;
212 void tst_QByteArrayView::constExpr()
const
217 static_assert(bv.size() == 0);
218 static_assert(bv.isNull());
219 static_assert(bv.empty());
220 static_assert(bv.isEmpty());
221 static_assert(bv.data() ==
nullptr);
224 static_assert(bv2.isNull());
225 static_assert(bv2.empty());
229 static_assert(bv.size() == 0);
230 static_assert(!bv.isNull());
231 static_assert(bv.empty());
232 static_assert(bv.isEmpty());
233 static_assert(bv.data() !=
nullptr);
236 static_assert(!bv2.isNull());
237 static_assert(bv2.empty());
242 static_assert(bv.size() == 5);
243 static_assert(!bv.empty());
244 static_assert(!bv.isEmpty());
245 static_assert(!bv.isNull());
246 static_assert(*bv.data() ==
'H');
247 static_assert(bv[0] ==
'H');
248 static_assert(bv.at(0) ==
'H');
249 static_assert(bv.front() ==
'H');
250 static_assert(bv.first() ==
'H');
251 static_assert(bv[4] ==
'o');
252 static_assert(bv.at(4) ==
'o');
253 static_assert(bv.back() ==
'o');
254 static_assert(bv.last() ==
'o');
256 static_assert(*bv.begin() ==
'H' );
257 static_assert(*(bv.end() - 1) ==
'o' );
258 static_assert(*bv.cbegin() ==
'H' );
259 static_assert(*(bv.cend() - 1) ==
'o' );
260 static_assert(*bv.rbegin() ==
'o' );
261 static_assert(*bv.crbegin() ==
'o' );
264 static_assert(bv.rbegin() != bv.rend());
265 static_assert(bv.crbegin() != bv.crend());
268 static_assert(!bv2.isNull());
269 static_assert(!bv2.empty());
270 static_assert(bv2.size() == 5);
272 #if !defined(Q_CC_GNU) || defined(Q_CC_CLANG) || defined(Q_CC_INTEL)
277 static constexpr
char hello[] =
"Hello";
279 static_assert(bv.size() == 5);
280 static_assert(!bv.empty());
281 static_assert(!bv.isEmpty());
282 static_assert(!bv.isNull());
283 static_assert(*bv.data() ==
'H');
284 static_assert(bv[0] ==
'H');
285 static_assert(bv.at(0) ==
'H');
286 static_assert(bv.front() ==
'H');
287 static_assert(bv.first() ==
'H');
288 static_assert(bv[4] ==
'o');
289 static_assert(bv.at(4) ==
'o');
290 static_assert(bv.back() ==
'o');
291 static_assert(bv.last() ==
'o');
294 static constexpr
char hello[] = {
'H',
'e',
'l',
'l',
'o' };
296 static_assert(bv.size() == 5);
297 static_assert(!bv.empty());
298 static_assert(!bv.isEmpty());
299 static_assert(!bv.isNull());
300 static_assert(*bv.data() ==
'H');
301 static_assert(bv[0] ==
'H');
302 static_assert(bv.at(0) ==
'H');
303 static_assert(bv.front() ==
'H');
304 static_assert(bv.first() ==
'H');
305 static_assert(bv[4] ==
'o');
306 static_assert(bv.at(4) ==
'o');
307 static_assert(bv.back() ==
'o');
308 static_assert(bv.last() ==
'o');
312 constexpr
char *
null =
nullptr;
314 static_assert(bv.isNull());
315 static_assert(bv.isEmpty());
316 static_assert(bv.size() == 0);
320 void tst_QByteArrayView::basics()
const
337 void tst_QByteArrayView::literals()
const
339 const char hello[] =
"Hello\0This shouldn't be found";
363 const char abc[] =
"abc";
367 const char def[3] = {
'd',
'e',
'f'};
372 void tst_QByteArrayView::fromArray()
const
374 static constexpr
char hello[] =
"Hello\0abc\0\0.";
390 QCOMPARE(*(bv.data() + bv.size() - 2),
'.');
394 const std::byte bytes[] = {std::byte(0x0), std::byte(0x1), std::byte(0x2)};
396 QCOMPARE(bbv.
data(),
reinterpret_cast<const char *
>(bytes + 0));
402 void tst_QByteArrayView::literalsWithInternalNulls()
const
404 const char withnull[] =
"a\0zzz";
433 const char nullfirst[] =
"\0buzz";
437 const char nullNotEnd[] = {
'b',
'o',
'w',
'\0',
'a',
'f',
't',
'z' };
442 void tst_QByteArrayView::at()
const
453 void tst_QByteArrayView::fromQByteArray()
const
474 template <
typename T>
475 size_t size(
const T &
t) {
return size_t(
t.size()); }
476 template <
typename T>
479 template <
typename T>
481 template <
typename T>
484 template <
typename T>
486 template <
typename T>
489 template <
typename T>
491 template <
typename T>
492 std::reverse_iterator<const T*>
crbegin(
const T *
t) {
return std::reverse_iterator<const T*>(
cend(
t)); }
494 template <
typename T>
496 template <
typename T>
497 std::reverse_iterator<const T*>
crend(
const T *
t) {
return std::reverse_iterator<const T*>(
cbegin(
t)); }
501 template <
typename Data>
502 void tst_QByteArrayView::conversionTests(
Data data)
const
510 const auto compare = [](
auto v1,
auto v2) {
511 if constexpr (std::is_same_v<decltype(
v1), std::byte>)
562 bv2 = std::move(
data);
568 template <
typename Char>
569 void tst_QByteArrayView::fromEmptyLiteral()
const
571 const Char *
null =
nullptr;
578 static_cast<const void*
>(empty));
586 template <
typename Char>
587 void tst_QByteArrayView::fromRange(
const Char *
first,
const Char *last)
const
589 const Char *
null =
nullptr;
594 static_cast<const void*
>(
first));
598 QCOMPARE(
static_cast<const void*
>(bv.data()),
599 static_cast<const void*
>(
first));
601 QCOMPARE(
static_cast<const void*
>(bv.last(0).data()),
602 static_cast<const void*
>(last));
603 QCOMPARE(
static_cast<const void*
>(bv.sliced(bv.size()).data()),
604 static_cast<const void*
>(last));
609 template <
typename Char,
typename Container>
610 void tst_QByteArrayView::fromContainer()
const
620 const auto *
data =
reinterpret_cast<const Char *
>(
s.data());
621 std::copy(
data,
data +
s.size(), std::back_inserter(
c));
622 conversionTests(std::move(
c));
625 template <
typename Char>
626 void tst_QByteArrayView::fromContainers()
const
628 fromContainer<Char, QVector<Char>>();
629 fromContainer<Char, QVarLengthArray<Char>>();
630 fromContainer<Char, std::vector<Char>>();
631 fromContainer<Char, std::basic_string<Char>>();
634 void tst_QByteArrayView::comparison()
const
645 void tst_QByteArrayView::compare()
const
660 #include "tst_qbytearrayview.moc"
The QByteArray class provides an array of bytes.
constexpr char front() const
constexpr bool isNull() const noexcept
constexpr char back() const
bool contains(QByteArrayView a) const noexcept
bool startsWith(QByteArrayView other) const noexcept
constexpr char at(qsizetype n) const
constexpr QByteArrayView last(qsizetype n) const
bool endsWith(QByteArrayView other) const noexcept
constexpr QByteArrayView sliced(qsizetype pos) const
constexpr QByteArrayView first(qsizetype n) const
constexpr bool isEmpty() const noexcept
qsizetype count(QByteArrayView a) const noexcept
constexpr qsizetype size() const noexcept
constexpr bool empty() const noexcept
qsizetype lastIndexOf(QByteArrayView a) const noexcept
constexpr QByteArrayView chopped(qsizetype len) const
constexpr void chop(qsizetype n)
constexpr const_pointer data() const noexcept
qsizetype indexOf(QByteArrayView a, qsizetype from=0) const noexcept
constexpr static QByteArrayView fromArray(const Byte(&data)[Size]) noexcept
The QObject class is the base class of all Qt objects.
auto it unsigned count const
decltype(auto) crbegin(const T &t)
decltype(auto) cend(const T &t)
decltype(auto) crend(const T &t)
std::reverse_iterator< const T * > crend(const T *t)
const T * cbegin(const T *t)
const T * cend(const T *t)
decltype(auto) cbegin(const T &t)
std::reverse_iterator< const T * > crbegin(const T *t)
QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
#define QT_MAKE_CHECKED_ARRAY_ITERATOR(x, N)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
GLenum GLuint GLenum GLsizei length
GLint GLfloat GLfloat GLfloat v2
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat GLfloat alpha
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
QT_BEGIN_NAMESPACE typedef char Char
#define QTEST_APPLESS_MAIN(TestObject)
#define QVERIFY(statement)
std::is_convertible< T, QStringView > CanConvert