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
17#include <folly/portability/Constexpr.h>
18
19#include <folly/portability/GTest.h>
20
21using folly::constexpr_strcmp;
22
23TEST(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)
32TEST(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
39TEST(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
47static_assert(
48 constexpr_strcmp("abc", "abc") == 0,
49 "constexpr_strcmp is broken");
50static_assert(constexpr_strcmp("", "") == 0, "constexpr_strcmp is broken");
51static_assert(constexpr_strcmp("abc", "def") < 0, "constexpr_strcmp is broken");
52static_assert(constexpr_strcmp("xyz", "abc") > 0, "constexpr_strcmp is broken");
53static_assert(constexpr_strcmp("a", "abc") < 0, "constexpr_strcmp is broken");
54static_assert(constexpr_strcmp("abc", "a") > 0, "constexpr_strcmp is broken");
55