proxygen
ConstexprTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
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 
18 
20 
22 
23 TEST(ConstexprTest, constexpr_strlen_cstr) {
24  constexpr auto v = "hello";
25  constexpr auto a = folly::constexpr_strlen(v);
26  EXPECT_EQ(5, a);
27  EXPECT_TRUE((std::is_same<const size_t, decltype(a)>::value));
28 }
29 
30 // gcc-4.9 cannot compile the following constexpr code correctly
31 #if !(defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5)
32 TEST(ConstexprTest, constexpr_strlen_ints) {
33  constexpr int v[] = {5, 3, 4, 0, 7};
34  constexpr auto a = folly::constexpr_strlen(v);
35  EXPECT_EQ(3, a);
36  EXPECT_TRUE((std::is_same<const size_t, decltype(a)>::value));
37 }
38 
39 TEST(ConstexprTest, constexpr_strcmp_ints) {
40  constexpr int v[] = {5, 3, 4, 0, 7};
41  constexpr int v1[] = {6, 4};
42  static_assert(constexpr_strcmp(v1, v) > 0, "constexpr_strcmp is broken");
43  static_assert(constexpr_strcmp(v, v) == 0, "constexpr_strcmp is broken");
44 }
45 #endif
46 
47 static_assert(
48  constexpr_strcmp("abc", "abc") == 0,
49  "constexpr_strcmp is broken");
50 static_assert(constexpr_strcmp("", "") == 0, "constexpr_strcmp is broken");
51 static_assert(constexpr_strcmp("abc", "def") < 0, "constexpr_strcmp is broken");
52 static_assert(constexpr_strcmp("xyz", "abc") > 0, "constexpr_strcmp is broken");
53 static_assert(constexpr_strcmp("a", "abc") < 0, "constexpr_strcmp is broken");
54 static_assert(constexpr_strcmp("abc", "a") > 0, "constexpr_strcmp is broken");
TEST(ConstexprTest, constexpr_strlen_cstr)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr int constexpr_strcmp(const Char *s1, const Char *s2)
Definition: Constexpr.h:75
char a
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
constexpr size_t constexpr_strlen(const Char *s)
Definition: Constexpr.h:57