1 /*
2  * Copyright (C) 2013 The Android Open Source Project
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 <errno.h>
20 #include <locale.h>
21 #include <strings.h>
22 
23 #if defined(NOFORTIFY)
24 #define STRINGS_TEST strings_nofortify
25 #else
26 #define STRINGS_TEST strings
27 #endif
28 
TEST(STRINGS_TEST,ffs)29 TEST(STRINGS_TEST, ffs) {
30   ASSERT_EQ( 0, ffs(0x00000000));
31   ASSERT_EQ( 1, ffs(0x00000001));
32   ASSERT_EQ( 6, ffs(0x00000020));
33   ASSERT_EQ(11, ffs(0x00000400));
34   ASSERT_EQ(16, ffs(0x00008000));
35   ASSERT_EQ(17, ffs(0x00010000));
36   ASSERT_EQ(22, ffs(0x00200000));
37   ASSERT_EQ(27, ffs(0x04000000));
38   ASSERT_EQ(32, ffs(0x80000000));
39 }
40 
TEST(STRINGS_TEST,ffsl)41 TEST(STRINGS_TEST, ffsl) {
42   ASSERT_EQ( 0, ffsl(0x00000000L));
43   ASSERT_EQ( 1, ffsl(0x00000001L));
44   ASSERT_EQ( 6, ffsl(0x00000020L));
45   ASSERT_EQ(11, ffsl(0x00000400L));
46   ASSERT_EQ(16, ffsl(0x00008000L));
47   ASSERT_EQ(17, ffsl(0x00010000L));
48   ASSERT_EQ(22, ffsl(0x00200000L));
49   ASSERT_EQ(27, ffsl(0x04000000L));
50   ASSERT_EQ(32, ffsl(0x80000000L));
51 #if defined(__LP64__)
52   ASSERT_EQ(33, ffsl(0x0000000100000000L));
53   ASSERT_EQ(38, ffsl(0x0000002000000000L));
54   ASSERT_EQ(43, ffsl(0x0000040000000000L));
55   ASSERT_EQ(48, ffsl(0x0000800000000000L));
56   ASSERT_EQ(49, ffsl(0x0001000000000000L));
57   ASSERT_EQ(54, ffsl(0x0020000000000000L));
58   ASSERT_EQ(59, ffsl(0x0400000000000000L));
59   ASSERT_EQ(64, ffsl(0x8000000000000000L));
60 #endif
61 }
62 
TEST(STRINGS_TEST,ffsll)63 TEST(STRINGS_TEST, ffsll) {
64   ASSERT_EQ( 0, ffsll(0x0000000000000000LL));
65   ASSERT_EQ( 1, ffsll(0x0000000000000001LL));
66   ASSERT_EQ( 6, ffsll(0x0000000000000020LL));
67   ASSERT_EQ(11, ffsll(0x0000000000000400LL));
68   ASSERT_EQ(16, ffsll(0x0000000000008000LL));
69   ASSERT_EQ(17, ffsll(0x0000000000010000LL));
70   ASSERT_EQ(22, ffsll(0x0000000000200000LL));
71   ASSERT_EQ(27, ffsll(0x0000000004000000LL));
72   ASSERT_EQ(32, ffsll(0x0000000080000000LL));
73   ASSERT_EQ(33, ffsll(0x0000000100000000LL));
74   ASSERT_EQ(38, ffsll(0x0000002000000000LL));
75   ASSERT_EQ(43, ffsll(0x0000040000000000LL));
76   ASSERT_EQ(48, ffsll(0x0000800000000000LL));
77   ASSERT_EQ(49, ffsll(0x0001000000000000LL));
78   ASSERT_EQ(54, ffsll(0x0020000000000000LL));
79   ASSERT_EQ(59, ffsll(0x0400000000000000LL));
80   ASSERT_EQ(64, ffsll(0x8000000000000000LL));
81 }
82 
TEST(STRINGS_TEST,strcasecmp)83 TEST(STRINGS_TEST, strcasecmp) {
84   ASSERT_EQ(0, strcasecmp("hello", "HELLO"));
85   ASSERT_LT(strcasecmp("hello1", "hello2"), 0);
86   ASSERT_GT(strcasecmp("hello2", "hello1"), 0);
87 }
88 
TEST(STRINGS_TEST,strcasecmp_l)89 TEST(STRINGS_TEST, strcasecmp_l) {
90   locale_t l = newlocale(LC_ALL, "C", nullptr);
91   ASSERT_EQ(0, strcasecmp_l("hello", "HELLO", l));
92   ASSERT_LT(strcasecmp_l("hello1", "hello2", l), 0);
93   ASSERT_GT(strcasecmp_l("hello2", "hello1", l), 0);
94   freelocale(l);
95 }
96 
TEST(STRINGS_TEST,strncasecmp)97 TEST(STRINGS_TEST, strncasecmp) {
98   ASSERT_EQ(0, strncasecmp("hello", "HELLO", 3));
99   ASSERT_EQ(0, strncasecmp("abcXX", "ABCYY", 3));
100   ASSERT_LT(strncasecmp("hello1", "hello2", 6), 0);
101   ASSERT_GT(strncasecmp("hello2", "hello1", 6), 0);
102 }
103 
TEST(STRINGS_TEST,strncasecmp_l)104 TEST(STRINGS_TEST, strncasecmp_l) {
105   locale_t l = newlocale(LC_ALL, "C", nullptr);
106   ASSERT_EQ(0, strncasecmp_l("hello", "HELLO", 3, l));
107   ASSERT_EQ(0, strncasecmp_l("abcXX", "ABCYY", 3, l));
108   ASSERT_LT(strncasecmp_l("hello1", "hello2", 6, l), 0);
109   ASSERT_GT(strncasecmp_l("hello2", "hello1", 6, l), 0);
110   freelocale(l);
111 }
112