xref: /aosp_15_r20/external/fbjni/test/jni/simple_fixed_string_tests.cpp (revision 65c59e023c5336bbd4a23be7af78407e3d80e7e7)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <fbjni/detail/SimpleFixedString.h>
20 
21 using facebook::jni::detail::makeSimpleFixedString;
22 using facebook::jni::detail::SimpleFixedString;
23 
24 // Prevent unused function warnings portably.
25 #if (                                                 \
26     !defined(__has_feature) ||                        \
27     !__has_feature(cxx_constexpr_string_builtins)) && \
28     (!defined(__GNUC__) || defined(__clang__))
constexpr_strcmp_internal(const char * s1,const char * s2)29 constexpr int constexpr_strcmp_internal(const char* s1, const char* s2) {
30   return (*s1 == '\0' || *s1 != *s2)
31       ? (static_cast<int>(*s1 - *s2))
32       : constexpr_strcmp_internal(s1 + 1, s2 + 1);
33 }
34 #endif
35 
constexpr_strcmp(const char * s1,const char * s2)36 constexpr int constexpr_strcmp(const char* s1, const char* s2) {
37 #if defined(__has_feature) && __has_feature(cxx_constexpr_string_builtins)
38   // clang provides a constexpr builtin
39   return __builtin_strcmp(s1, s2);
40 #elif defined(__GNUC__) && !defined(__clang__)
41   // strcmp() happens to already be constexpr under gcc
42   return std::strcmp(s1, s2);
43 #else
44   return constexpr_strcmp_internal(s1, s2);
45 #endif
46 }
47 
TEST(SimpleFixedString_test,Tests)48 TEST(SimpleFixedString_test, Tests) {
49   static constexpr SimpleFixedString<0> empty = "";
50   static_assert(empty.size() == 0, "empty not empty!");
51 
52   static constexpr auto str = makeSimpleFixedString("hello");
53   static_assert(str.size() == 5, "wrong length for hello!");
54   static_assert(
55       constexpr_strcmp(str.c_str(), "hello") == 0, "bad fixedstring contents!");
56 
57   static constexpr auto substr = str.substr(1, str.size() - 2);
58   static_assert(substr.size() == 3, "substr has wrong size");
59   static_assert(
60       constexpr_strcmp(substr.c_str(), "ell") == 0, "substr is broken!");
61 
62   static constexpr auto concatLeft = "Why " + str;
63   static_assert(concatLeft.size() == 9, "wrong length for Why hello");
64   static_assert(
65       constexpr_strcmp(concatLeft.c_str(), "Why hello") == 0,
66       "left concat is broken!");
67 
68   static constexpr auto concatRight = str + " there";
69   static_assert(concatRight.size() == 11, "wrong length for hello there");
70   static_assert(
71       constexpr_strcmp(concatRight.c_str(), "hello there") == 0,
72       "right concat is broken!");
73 
74   static constexpr auto bigConcat = makeSimpleFixedString("Let's ") +
75       makeSimpleFixedString("make ") + makeSimpleFixedString("a ") +
76       makeSimpleFixedString("long ") + makeSimpleFixedString("string!");
77   static_assert(bigConcat.size() == 25, "big concat has wrong size");
78   static_assert(
79       constexpr_strcmp(bigConcat.c_str(), "Let's make a long string!") == 0,
80       "bigConcat is broken!");
81 }
82 
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84   testing::InitGoogleTest(&argc, argv);
85   return RUN_ALL_TESTS();
86 }
87