blob: daa61b3194130fd1fe8997e255d0f0f51d95788f [file] [log] [blame]
danakjc077a30e2024-03-22 19:25:361// Copyright 2024 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/strings/cstring_view.h"
9
Hans Wennborgc176cf782024-04-18 20:07:3210#include <vector>
11
danakjc077a30e2024-03-22 19:25:3612namespace base {
13namespace {
14
15void WontCompileTypeMismatch() {
16 cstring_view(u"123"); // expected-error {{no matching conversion}}
17 cstring_view(U"123"); // expected-error {{no matching conversion}}
18 u16cstring_view("123"); // expected-error {{no matching conversion}}
19 u16cstring_view(U"123"); // expected-error {{no matching conversion}}
20 u32cstring_view("123"); // expected-error {{no matching conversion}}
21 u32cstring_view(u"123"); // expected-error {{no matching conversion}}
22
23#if BUILDFLAG(IS_WIN)
24 cstring_view(L""); // expected-error {{no matching conversion}}
25 u16cstring_view(L""); // expected-error {{no matching conversion}}
26 u32cstring_view(L""); // expected-error {{no matching conversion}}
27 wcstring_view(""); // expected-error {{no matching conversion}}
28 wcstring_view(u""); // expected-error {{no matching conversion}}
29 wcstring_view(U""); // expected-error {{no matching conversion}}
30#endif
31}
32
33void WontCompileNoNulInArray() {
34 const char abc_good[] = {'a', 'b', 'c', '\0'};
35 auto v1 = cstring_view(abc_good); // No error, NUL exists.
36
37#if defined(__clang__)
38 const char abc_bad[] = {'a', 'b', 'c'};
39 const char after = 'd';
40 auto v2 = cstring_view(abc_bad); // expected-error {{no matching conversion}}
41#endif
42}
43
danakjcd2391a2024-10-11 13:56:1744void WontCompileNullptr() {
45 auto v = cstring_view(nullptr); // expected-error {{no matching conversion}}
danakjc077a30e2024-03-22 19:25:3646}
47
48void WontCompileCompareTypeMismatch() {
49 // TODO(crbug.com/330213589): This should be testable with a static_assert on
50 // a concept.
51 (void)(cstring_view() == u16cstring_view()); // expected-error {{invalid operands to binary expression}}
52 (void)(cstring_view() <=> u16cstring_view()); // expected-error {{invalid operands to binary expression}}
53}
54
danakjed4930f2024-03-25 21:21:2955void WontCompileSwapTypeMismatch() {
56 auto a = cstring_view("8");
57 auto b = u16cstring_view(u"16");
58 a.swap(b); // expected-error {{cannot bind to a value of unrelated type}}
59}
60
61void WontCompileStartsEndWithMismatch() {
62 u16cstring_view(u"abc").starts_with("ab"); // expected-error {{no matching member function}}
63 u16cstring_view(u"abc").ends_with("ab"); // expected-error {{no matching member function}}
64}
65
danakjc077a30e2024-03-22 19:25:3666void WontCompileDanglingInput() {
67 // TODO: construct from string.
68 // auto v1 = cstring_view(std::string("abc"));
danakjc077a30e2024-03-22 19:25:3669}
70
71} // namespace
72} // namespace base