xref: /aosp_15_r20/external/cronet/base/containers/buffer_iterator_nocompile.nc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2019 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include "base/containers/buffer_iterator.h"
9
10#include <stdint.h>
11
12#include <string>
13
14namespace base {
15
16class Complex {
17 public:
18  Complex() : string_("Moo") {}
19
20 private:
21  std::string string_;
22};
23
24void CreateTypeUint16() {
25  constexpr size_t size = 64;
26  uint16_t data[size];
27  BufferIterator<uint16_t> iterator(data, size);  // expected-error@*:* {{Underlying buffer type must be char-type.}}
28}
29
30void ComplexMutableObject() {
31  constexpr size_t size = 64;
32  uint8_t data[size];
33  BufferIterator<uint8_t> iterator(data, size);
34  Complex* c = iterator.MutableObject<Complex>();  // expected-error {{no matching member function for call to 'MutableObject'}}
35}
36
37void ComplexObject() {
38  constexpr size_t size = 64;
39  uint8_t data[size];
40  BufferIterator<uint8_t> iterator(data, size);
41  const Complex* c = iterator.Object<Complex>();  // expected-error {{no matching member function for call to 'Object'}}
42}
43
44void ComplexMutableSpan() {
45  constexpr size_t size = 64;
46  uint8_t data[size];
47  BufferIterator<uint8_t> iterator(data, size);
48  base::span<Complex> s = iterator.MutableSpan<Complex>(3);  // expected-error {{no matching member function for call to 'MutableSpan'}}
49}
50
51void ComplexSpan() {
52  constexpr size_t size = 64;
53  uint8_t data[size];
54  BufferIterator<uint8_t> iterator(data, size);
55  base::span<const Complex> s = iterator.Span<Complex>();  // expected-error {{no matching member function for call to 'Span'}}
56}
57
58void OverflowCompileTime() {
59  char buffer[64];
60
61  BufferIterator<char> iterator(
62      // SAFETY: This intentionally makes an incorrectly-sized span. The span
63      // pointer, stored in the BufferIterator is never moved past the start in
64      // this test, as that would cause Undefined Behaviour.
65      UNSAFE_BUFFERS(span(buffer, std::numeric_limits<size_t>::max())));
66
67  constexpr size_t kInvalidU64Size =
68      (std::numeric_limits<size_t>::max() / sizeof(uint64_t)) + 1u;
69
70  iterator.Span<uint64_t, kInvalidU64Size>(); // expected-error {{no matching member function}}
71  iterator.Span<uint64_t, std::numeric_limits<size_t>::max()>();  // expected-error {{no matching member function}}
72}
73
74}  // namespace base
75