danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 1 | // 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 Wennborg | c176cf78 | 2024-04-18 20:07:32 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 12 | namespace base { |
| 13 | namespace { |
| 14 | |
| 15 | void 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 | |
| 33 | void 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 | |
danakj | cd2391a | 2024-10-11 13:56:17 | [diff] [blame] | 44 | void WontCompileNullptr() { |
| 45 | auto v = cstring_view(nullptr); // expected-error {{no matching conversion}} |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void 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 | |
danakj | ed4930f | 2024-03-25 21:21:29 | [diff] [blame] | 55 | void 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 | |
| 61 | void 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 | |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 66 | void WontCompileDanglingInput() { |
| 67 | // TODO: construct from string. |
| 68 | // auto v1 = cstring_view(std::string("abc")); |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace |
| 72 | } // namespace base |