QtBase  v6.3.1
tst_qbytearrayview.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QByteArrayView>
30 
31 #include <QTest>
32 
33 // for negative testing (can't convert from)
34 #include <deque>
35 #include <list>
36 
37 template <typename T>
38 constexpr bool CanConvert = std::is_convertible_v<T, QByteArrayView>;
39 
40 static_assert(!CanConvert<QString>);
41 static_assert(!CanConvert<QStringView>);
42 static_assert(!CanConvert<const char16_t*>);
43 
44 static_assert(!CanConvert<char>);
45 static_assert(CanConvert<char[1]>);
46 static_assert(CanConvert<const char[1]>);
47 static_assert(CanConvert<char*>);
48 static_assert(CanConvert<const char*>);
49 
50 static_assert(!CanConvert<uchar>);
51 static_assert(!CanConvert<uchar[1]>);
52 static_assert(!CanConvert<const uchar[1]>);
53 static_assert(CanConvert<uchar*>);
54 static_assert(CanConvert<const uchar*>);
55 
56 static_assert(!CanConvert<signed char>);
57 static_assert(!CanConvert<signed char[1]>);
58 static_assert(!CanConvert<const signed char[1]>);
59 static_assert(CanConvert<signed char*>);
60 static_assert(CanConvert<const signed char*>);
61 
62 static_assert(!CanConvert<std::byte>);
63 static_assert(!CanConvert<std::byte[1]>);
64 static_assert(!CanConvert<const std::byte[1]>);
65 static_assert(CanConvert<std::byte*>);
66 static_assert(CanConvert<const std::byte*>);
67 
68 static_assert(CanConvert< QByteArray >);
69 static_assert(CanConvert<const QByteArray >);
70 static_assert(CanConvert< QByteArray&>);
71 static_assert(CanConvert<const QByteArray&>);
72 
73 static_assert(CanConvert< std::string >);
74 static_assert(CanConvert<const std::string >);
75 static_assert(CanConvert< std::string&>);
76 static_assert(CanConvert<const std::string&>);
77 
78 static_assert(CanConvert< std::string_view >);
80 static_assert(CanConvert< std::string_view&>);
82 
83 static_assert(CanConvert< QVector<char> >);
84 static_assert(CanConvert<const QVector<char> >);
85 static_assert(CanConvert< QVector<char>&>);
86 static_assert(CanConvert<const QVector<char>&>);
87 
88 static_assert(CanConvert< QVarLengthArray<char> >);
89 static_assert(CanConvert<const QVarLengthArray<char> >);
90 static_assert(CanConvert< QVarLengthArray<char>&>);
91 static_assert(CanConvert<const QVarLengthArray<char>&>);
92 
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>&>);
97 
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>&>);
102 
103 static_assert(!CanConvert<std::deque<char>>);
104 static_assert(!CanConvert<std::list<char>>);
105 
107 {
108  Q_OBJECT
109 private slots:
110  // Note: much of the shared API is tested in ../qbytearrayapisymmetry/
111  void constExpr() const;
112  void basics() const;
113  void literals() const;
114  void fromArray() const;
115  void literalsWithInternalNulls() const;
116  void at() const;
117 
118  void fromQByteArray() const;
119 
120  void fromCharStar() const
121  {
122  fromEmptyLiteral<char>();
123  conversionTests("Hello, World!");
124  }
125 
126  void fromUCharStar() const
127  {
128  fromEmptyLiteral<uchar>();
129 
130  const uchar data[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 };
131  conversionTests(data);
132  }
133 
134  void fromSignedCharStar() const
135  {
136  fromEmptyLiteral<signed char>();
137 
138  const signed char data[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 };
139  conversionTests(data);
140  }
141 
142  void fromStdByteArray() const
143  {
144  fromEmptyLiteral<std::byte>();
145 
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);
149  }
150 
151  void fromCharRange() const
152  {
153  const char data[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
154  fromRange(std::begin(data), std::end(data));
155  }
156 
157  void fromUCharRange() const
158  {
159  const uchar data[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 };
160  fromRange(std::begin(data), std::end(data));
161  }
162 
163  void fromSignedCharRange() const
164  {
165  const signed char data[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 };
166  fromRange(std::begin(data), std::end(data));
167  }
168 
169  void fromStdByteRange() const
170  {
171  const std::byte data[] = {std::byte{'H'}, std::byte{'e'}, std::byte{'l'}, std::byte{'l'},
172  std::byte{'o'}, std::byte{0}};
173  fromRange(std::begin(data), std::end(data));
174  }
175 
176  void fromCharContainers() const
177  {
178  fromContainers<char>();
179  }
180 
181  void fromUCharContainers() const
182  {
183  fromContainers<uchar>();
184  }
185 
186  void fromSignedCharContainers() const
187  {
188  fromContainers<signed char>();
189  }
190 
191  void fromStdByteContainers() const
192  {
193  fromContainers<std::byte>();
194  }
195 
196  void comparison() const;
197  void compare() const;
198 
199 private:
200  template <typename Data>
201  void conversionTests(Data arg) const;
202  template <typename Char>
203  void fromEmptyLiteral() const;
204  template <typename Char>
205  void fromRange(const Char *first, const Char *last) const;
206  template <typename Char, typename Container>
207  void fromContainer() const;
208  template <typename Char>
209  void fromContainers() const;
210 };
211 
212 void tst_QByteArrayView::constExpr() const
213 {
214  // compile-time checks
215  {
216  constexpr QByteArrayView bv;
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);
222 
223  constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
224  static_assert(bv2.isNull());
225  static_assert(bv2.empty());
226  }
227  {
228  constexpr QByteArrayView bv = "";
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);
234 
235  constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
236  static_assert(!bv2.isNull());
237  static_assert(bv2.empty());
238  }
239  {
240  static_assert(QByteArrayView("Hello").size() == 5);
241  constexpr QByteArrayView bv = "Hello";
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');
255 
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' );
262 
263  // This is just to test that rend()/crend() are constexpr.
264  static_assert(bv.rbegin() != bv.rend());
265  static_assert(bv.crbegin() != bv.crend());
266 
267  constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
268  static_assert(!bv2.isNull());
269  static_assert(!bv2.empty());
270  static_assert(bv2.size() == 5);
271  }
272 #if !defined(Q_CC_GNU) || defined(Q_CC_CLANG) || defined(Q_CC_INTEL)
273  // Below checks are disabled because of a compilation issue with GCC and
274  // -fsanitize=undefined. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71962.
275  // Note: Q_CC_GNU is also defined for Clang and ICC, so we need to check those too.
276  {
277  static constexpr char hello[] = "Hello";
278  constexpr QByteArrayView bv(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');
292  }
293  {
294  static constexpr char hello[] = { 'H', 'e', 'l', 'l', 'o' };
295  constexpr QByteArrayView bv(hello, std::size(hello));
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');
309  }
310 #endif
311  {
312  constexpr char *null = nullptr;
313  constexpr QByteArrayView bv(null);
314  static_assert(bv.isNull());
315  static_assert(bv.isEmpty());
316  static_assert(bv.size() == 0);
317  }
318 }
319 
320 void tst_QByteArrayView::basics() const
321 {
322  QByteArrayView bv1;
323 
324  // a default-constructed QByteArrayView is null:
325  QVERIFY(bv1.isNull());
326  // which implies it's empty();
327  QVERIFY(bv1.isEmpty());
328 
329  QByteArrayView bv2;
330 
331  QVERIFY(bv2 == bv1);
332  QVERIFY(!(bv2 != bv1));
333 }
334 
335 // Note: initially the size would be deduced from the array literal,
336 // but it caused source compatibility issues so this is currently not the case.
337 void tst_QByteArrayView::literals() const
338 {
339  const char hello[] = "Hello\0This shouldn't be found";
340 
342  QCOMPARE(QByteArrayView(hello + 0).size(), 5); // forces decay to pointer
343  QByteArrayView bv = hello;
344  QCOMPARE(bv.size(), 5);
345  QVERIFY(!bv.empty());
346  QVERIFY(!bv.isEmpty());
347  QVERIFY(!bv.isNull());
348  QCOMPARE(*bv.data(), 'H');
349  QCOMPARE(bv[0], 'H');
350  QCOMPARE(bv.at(0), 'H');
351  QCOMPARE(bv.front(), 'H');
352  QCOMPARE(bv.first(), 'H');
353  QCOMPARE(bv[4], 'o');
354  QCOMPARE(bv.at(4), 'o');
355  QCOMPARE(bv.back(), 'o');
356  QCOMPARE(bv.last(), 'o');
357 
358  QByteArrayView bv2(bv.data(), bv.data() + bv.size());
359  QVERIFY(!bv2.isNull());
360  QVERIFY(!bv2.empty());
361  QCOMPARE(bv2.size(), 5);
362 
363  const char abc[] = "abc";
364  bv = abc;
365  QCOMPARE(bv.size(), 3);
366 
367  const char def[3] = {'d', 'e', 'f'};
368  bv = def;
369  QCOMPARE(bv.size(), 3);
370 }
371 
372 void tst_QByteArrayView::fromArray() const
373 {
374  static constexpr char hello[] = "Hello\0abc\0\0.";
375 
377  QCOMPARE(bv.size(), 13);
378  QVERIFY(!bv.empty());
379  QVERIFY(!bv.isEmpty());
380  QVERIFY(!bv.isNull());
381  QCOMPARE(*bv.data(), 'H');
382  QCOMPARE(bv[0], 'H');
383  QCOMPARE(bv.at(0), 'H');
384  QCOMPARE(bv.front(), 'H');
385  QCOMPARE(bv.first(), 'H');
386  QCOMPARE(bv[4], 'o');
387  QCOMPARE(bv.at(4), 'o');
388  QCOMPARE(bv[5], '\0');
389  QCOMPARE(bv.at(5), '\0');
390  QCOMPARE(*(bv.data() + bv.size() - 2), '.');
391  QCOMPARE(bv.back(), '\0');
392  QCOMPARE(bv.last(), '\0');
393 
394  const std::byte bytes[] = {std::byte(0x0), std::byte(0x1), std::byte(0x2)};
396  QCOMPARE(bbv.data(), reinterpret_cast<const char *>(bytes + 0));
397  QCOMPARE(bbv.size(), 3);
398  QCOMPARE(bbv.first(), 0x0);
399  QCOMPARE(bbv.last(), 0x2);
400 }
401 
402 void tst_QByteArrayView::literalsWithInternalNulls() const
403 {
404  const char withnull[] = "a\0zzz";
405 
406  // these are different results
407  QCOMPARE(size_t(QByteArrayView::fromArray(withnull).size()), std::size(withnull));
408  QCOMPARE(QByteArrayView(withnull + 0).size(), 1);
409 
410  QByteArrayView nulled = QByteArrayView::fromArray(withnull);
411  QCOMPARE(nulled.last(), '\0');
412  nulled.chop(1); // cut off trailing \0
413  QCOMPARE(nulled[1], '\0');
414  QCOMPARE(nulled.indexOf('\0'), 1);
415  QCOMPARE(nulled.indexOf('z'), 2);
416  QCOMPARE(nulled.lastIndexOf('z'), 4);
417  QCOMPARE(nulled.lastIndexOf('a'), 0);
418  QVERIFY(nulled.startsWith("a\0z"));
419  QVERIFY(nulled.startsWith("a\0y"));
420  QVERIFY(!nulled.startsWith(QByteArrayView("a\0y", 3)));
421  QVERIFY(nulled.endsWith("zz"));
422  QVERIFY(nulled.contains("z"));
423  QVERIFY(nulled.contains(QByteArrayView("\0z", 2)));
424  QVERIFY(!nulled.contains(QByteArrayView("\0y", 2)));
425  QCOMPARE(nulled.first(5), QByteArrayView(withnull, 5));
426  QCOMPARE(nulled.last(5), QByteArrayView(withnull, 5));
427  QCOMPARE(nulled.sliced(0), QByteArrayView(withnull, 5));
428  QCOMPARE(nulled.sliced(2, 2), "zz");
429  QCOMPARE(nulled.chopped(2), QByteArrayView("a\0z", 3));
430  QVERIFY(nulled.chopped(2) != QByteArrayView("a\0y", 3));
431  QCOMPARE(nulled.count('z'), 3);
432 
433  const char nullfirst[] = "\0buzz";
434  QByteArrayView fromnull = QByteArrayView::fromArray(nullfirst);
435  QVERIFY(!fromnull.isEmpty());
436 
437  const char nullNotEnd[] = { 'b', 'o', 'w', '\0', 'a', 'f', 't', 'z' };
438  QByteArrayView midNull = QByteArrayView::fromArray(nullNotEnd);
439  QCOMPARE(midNull.back(), 'z');
440 }
441 
442 void tst_QByteArrayView::at() const
443 {
444  QByteArray hello("Hello");
445  QByteArrayView bv(hello);
446  QCOMPARE(bv.at(0), 'H'); QCOMPARE(bv[0], 'H');
447  QCOMPARE(bv.at(1), 'e'); QCOMPARE(bv[1], 'e');
448  QCOMPARE(bv.at(2), 'l'); QCOMPARE(bv[2], 'l');
449  QCOMPARE(bv.at(3), 'l'); QCOMPARE(bv[3], 'l');
450  QCOMPARE(bv.at(4), 'o'); QCOMPARE(bv[4], 'o');
451 }
452 
453 void tst_QByteArrayView::fromQByteArray() const
454 {
455  QByteArray null;
456  QByteArray empty = "";
457 
458  QVERIFY(QByteArrayView(null).isNull());
459  QVERIFY(!qToByteArrayViewIgnoringNull(null).isNull());
460 
461  QVERIFY(QByteArrayView(null).isEmpty());
462  QVERIFY(qToByteArrayViewIgnoringNull(null).isEmpty());
463 
464  QVERIFY(QByteArrayView(empty).isEmpty());
465  QVERIFY(qToByteArrayViewIgnoringNull(empty).isEmpty());
466 
467  QVERIFY(!QByteArrayView(empty).isNull());
468  QVERIFY(!qToByteArrayViewIgnoringNull(empty).isNull());
469 
470  conversionTests(QByteArray("Hello World!"));
471 }
472 
473 namespace help {
474 template <typename T>
475 size_t size(const T &t) { return size_t(t.size()); }
476 template <typename T>
477 size_t size(const T *t) { return std::char_traits<T>::length(t); }
478 
479 template <typename T>
480 decltype(auto) cbegin(const T &t) { return t.begin(); }
481 template <typename T>
482 const T * cbegin(const T *t) { return t; }
483 
484 template <typename T>
485 decltype(auto) cend(const T &t) { return t.end(); }
486 template <typename T>
487 const T * cend(const T *t) { return t + size(t); }
488 
489 template <typename T>
490 decltype(auto) crbegin(const T &t) { return t.rbegin(); }
491 template <typename T>
492 std::reverse_iterator<const T*> crbegin(const T *t) { return std::reverse_iterator<const T*>(cend(t)); }
493 
494 template <typename T>
495 decltype(auto) crend(const T &t) { return t.rend(); }
496 template <typename T>
497 std::reverse_iterator<const T*> crend(const T *t) { return std::reverse_iterator<const T*>(cbegin(t)); }
498 
499 } // namespace help
500 
501 template <typename Data>
502 void tst_QByteArrayView::conversionTests(Data data) const
503 {
504  // copy-construct:
505  {
506  QByteArrayView bv = data;
507 
509 
510  const auto compare = [](auto v1, auto v2) {
511  if constexpr (std::is_same_v<decltype(v1), std::byte>)
512  return std::to_integer<char>(v1) == v2;
513  else
514  return v1 == v2;
515  };
516  QVERIFY(std::equal(help::cbegin(data), help::cend(data),
517  QT_MAKE_CHECKED_ARRAY_ITERATOR(bv.cbegin(), bv.size()), compare));
518  QVERIFY(std::equal(help::cbegin(data), help::cend(data),
519  QT_MAKE_CHECKED_ARRAY_ITERATOR(bv.begin(), bv.size()), compare));
520  QVERIFY(std::equal(help::crbegin(data), help::crend(data), bv.crbegin(), compare));
521  QVERIFY(std::equal(help::crbegin(data), help::crend(data), bv.rbegin(), compare));
522  QCOMPARE(bv, data);
523  }
524 
525  QByteArrayView bv;
526 
527  // copy-assign:
528  {
529  bv = data;
530 
532 
533  // check relational operators:
534 
535  QCOMPARE(bv, data);
536  QCOMPARE(data, bv);
537 
538  QVERIFY(!(bv != data));
539  QVERIFY(!(data != bv));
540 
541  QVERIFY(!(bv < data));
542  QVERIFY(bv <= data);
543  QVERIFY(!(bv > data));
544  QVERIFY(bv >= data);
545 
546  QVERIFY(!(data < bv));
547  QVERIFY(data <= bv);
548  QVERIFY(!(data > bv));
549  QVERIFY(data >= bv);
550  }
551 
552  // copy-construct from rvalue (QByteArrayView never assumes ownership):
553  {
554  QByteArrayView bv2 = std::move(data);
555  QCOMPARE(bv2, bv);
556  QCOMPARE(bv2, data);
557  }
558 
559  // copy-assign from rvalue (QByteArrayView never assumes ownership):
560  {
561  QByteArrayView bv2;
562  bv2 = std::move(data);
563  QCOMPARE(bv2, bv);
564  QCOMPARE(bv2, data);
565  }
566 }
567 
568 template <typename Char>
569 void tst_QByteArrayView::fromEmptyLiteral() const
570 {
571  const Char *null = nullptr;
572  const Char empty[] = { Char{0} };
573 
574  QCOMPARE(QByteArrayView(null).size(), 0);
575  QCOMPARE(QByteArrayView(null).data(), nullptr);
577  QCOMPARE(static_cast<const void*>(QByteArrayView::fromArray(empty).data()),
578  static_cast<const void*>(empty));
579 
580  QVERIFY(QByteArrayView(null).isNull());
581  QVERIFY(QByteArrayView(null).isEmpty());
582  QVERIFY(!QByteArrayView::fromArray(empty).isEmpty());
583  QVERIFY(!QByteArrayView::fromArray(empty).isNull());
584 }
585 
586 template <typename Char>
587 void tst_QByteArrayView::fromRange(const Char *first, const Char *last) const
588 {
589  const Char *null = nullptr;
590  QCOMPARE(QByteArrayView(null, null).size(), 0);
591  QCOMPARE(QByteArrayView(null, null).data(), nullptr);
593  QCOMPARE(static_cast<const void*>(QByteArrayView(first, first).data()),
594  static_cast<const void*>(first));
595 
596  const auto bv = QByteArrayView(first, last);
597  QCOMPARE(bv.size(), last - first);
598  QCOMPARE(static_cast<const void*>(bv.data()),
599  static_cast<const void*>(first));
600 
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));
605 
606  // can't call conversionTests() here, as it requires a single object
607 }
608 
609 template <typename Char, typename Container>
610 void tst_QByteArrayView::fromContainer() const
611 {
612  const QByteArray s = "Hello World!";
613 
614  Container c;
615  // unspecified whether empty containers make null QByteArrayView
616  QVERIFY(QByteArrayView(c).isEmpty());
617 
618  QCOMPARE(sizeof(Char), sizeof(char));
619 
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));
623 }
624 
625 template <typename Char>
626 void tst_QByteArrayView::fromContainers() const
627 {
628  fromContainer<Char, QVector<Char>>();
629  fromContainer<Char, QVarLengthArray<Char>>();
630  fromContainer<Char, std::vector<Char>>();
631  fromContainer<Char, std::basic_string<Char>>();
632 }
633 
634 void tst_QByteArrayView::comparison() const
635 {
636  const QByteArrayView aa = "aa";
637  const QByteArrayView bb = "bb";
638 
639  QVERIFY(aa == aa);
640  QVERIFY(aa != bb);
641  QVERIFY(aa < bb);
642  QVERIFY(bb > aa);
643 }
644 
645 void tst_QByteArrayView::compare() const
646 {
647  QByteArrayView alpha = "original";
648 
649  QVERIFY(alpha.compare("original", Qt::CaseSensitive) == 0);
650  QVERIFY(alpha.compare("Original", Qt::CaseSensitive) > 0);
651  QVERIFY(alpha.compare("Original", Qt::CaseInsensitive) == 0);
652  QByteArrayView beta = "unoriginal";
653  QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
654  beta = "Unoriginal";
655  QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
656  QVERIFY(alpha.compare(beta, Qt::CaseSensitive) > 0);
657 }
658 
660 #include "tst_qbytearrayview.moc"
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
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
Definition: qlist.h:108
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
QCOMPARE(spy.count(), 1)
else
Definition: ftgrays.c:1658
auto it unsigned count const
Definition: hb-iter.hh:848
@ CaseInsensitive
Definition: qnamespace.h:1283
@ CaseSensitive
Definition: qnamespace.h:1284
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)
size_t size(const T &t)
std::reverse_iterator< const T * > crbegin(const T *t)
size_t size(const T *t)
Definition: qfloat16.h:381
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
Definition: qglobal.h:332
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLint GLfloat GLfloat GLfloat v2
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint end
GLint GLfloat GLfloat v1
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
const GLubyte * c
Definition: qopenglext.h:12701
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat GLfloat alpha
Definition: qopenglext.h:418
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
SSL_CTX int(*) void arg)
#define v1
QT_BEGIN_NAMESPACE typedef char Char
#define QTEST_APPLESS_MAIN(TestObject)
Definition: qtest.h:632
#define QVERIFY(statement)
Definition: qtestcase.h:64
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
HelloWorldTask * hello
Definition: main.cpp:38
std::is_convertible< T, QStringView > CanConvert