1 /*
2 * Copyright (C) 2023 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 <string.h>
20
TEST(String,Strcmp)21 TEST(String, Strcmp) {
22 char s0[] = "aaaaa";
23 char s1[] = "aaaaa";
24 char s2[] = "aaaab";
25
26 EXPECT_EQ(strcmp(s0, s1), 0);
27 EXPECT_LT(strcmp(s1, s2), 0);
28 EXPECT_GT(strcmp(s2, s1), 0);
29 }
30
TEST(String,Strdup)31 TEST(String, Strdup) {
32 char s[] = "test string";
33 char* s_dup = strdup(s);
34 EXPECT_NE(s_dup, s);
35 EXPECT_EQ(strcmp(s, s_dup), 0);
36 free(s_dup);
37 }
38
TEST(String,Strsep)39 TEST(String, Strsep) {
40 char* null_string = nullptr;
41 char* s1 = strsep(&null_string, " ");
42 EXPECT_EQ(s1, nullptr);
43
44 const char test_string[] = "Lorem ipsum \ndolor sit\tamet";
45 const char* tokens[] = {"Lorem", "ipsum", "", "dolor", "sit", "amet"};
46 {
47 char* test = strdup(test_string);
48 char* s2 = strsep(&test, "Z");
49 EXPECT_STREQ(s2, test_string);
50 free(test);
51 }
52 {
53 char* test = strdup(test_string);
54 for (size_t i = 0; i < sizeof(tokens) / sizeof(tokens[0]); ++i) {
55 char* s2 = strsep(&test, " \n\t");
56 EXPECT_STREQ(s2, tokens[i]);
57 }
58 free(test);
59 }
60 }
61