xref: /aosp_15_r20/system/chre/util/tests/string_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker  *
4*84e33947SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker  *
8*84e33947SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker  *
10*84e33947SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker  * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker  */
16*84e33947SAndroid Build Coastguard Worker 
17*84e33947SAndroid Build Coastguard Worker #include "gtest/gtest.h"
18*84e33947SAndroid Build Coastguard Worker 
19*84e33947SAndroid Build Coastguard Worker #include "chre/util/nanoapp/string.h"
20*84e33947SAndroid Build Coastguard Worker 
21*84e33947SAndroid Build Coastguard Worker using ::chre::copyString;
22*84e33947SAndroid Build Coastguard Worker 
TEST(StringDeathTest,InvalidInput)23*84e33947SAndroid Build Coastguard Worker TEST(StringDeathTest, InvalidInput) {
24*84e33947SAndroid Build Coastguard Worker   char destination[100];
25*84e33947SAndroid Build Coastguard Worker   ASSERT_DEATH(copyString(nullptr, nullptr, 0), ".*");
26*84e33947SAndroid Build Coastguard Worker   ASSERT_DEATH(copyString(nullptr, destination, 1), ".*");
27*84e33947SAndroid Build Coastguard Worker   ASSERT_DEATH(copyString(destination, nullptr, 2), ".*");
28*84e33947SAndroid Build Coastguard Worker }
29*84e33947SAndroid Build Coastguard Worker 
TEST(String,ZeroCharsToCopy)30*84e33947SAndroid Build Coastguard Worker TEST(String, ZeroCharsToCopy) {
31*84e33947SAndroid Build Coastguard Worker   const char *source = "hello world";
32*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 100;
33*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
34*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
35*84e33947SAndroid Build Coastguard Worker 
36*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
37*84e33947SAndroid Build Coastguard Worker 
38*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, 0);
39*84e33947SAndroid Build Coastguard Worker   for (size_t i = 0; i < destinationLength; ++i) {
40*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], fillValue);
41*84e33947SAndroid Build Coastguard Worker   }
42*84e33947SAndroid Build Coastguard Worker }
43*84e33947SAndroid Build Coastguard Worker 
TEST(String,EmptyStringPadsWithZeroes)44*84e33947SAndroid Build Coastguard Worker TEST(String, EmptyStringPadsWithZeroes) {
45*84e33947SAndroid Build Coastguard Worker   const char *source = "";
46*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 100;
47*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
48*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
49*84e33947SAndroid Build Coastguard Worker 
50*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
51*84e33947SAndroid Build Coastguard Worker 
52*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, destinationLength);
53*84e33947SAndroid Build Coastguard Worker   for (size_t i = 0; i < destinationLength; ++i) {
54*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], 0);
55*84e33947SAndroid Build Coastguard Worker   }
56*84e33947SAndroid Build Coastguard Worker }
57*84e33947SAndroid Build Coastguard Worker 
TEST(String,NormalCopyOneChar)58*84e33947SAndroid Build Coastguard Worker TEST(String, NormalCopyOneChar) {
59*84e33947SAndroid Build Coastguard Worker   const char *source = "hello world";
60*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 100;
61*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
62*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
63*84e33947SAndroid Build Coastguard Worker 
64*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
65*84e33947SAndroid Build Coastguard Worker 
66*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, 2);  // one char + '\0'
67*84e33947SAndroid Build Coastguard Worker   ASSERT_EQ(destination[0], source[0]);
68*84e33947SAndroid Build Coastguard Worker   ASSERT_EQ(destination[1], 0);
69*84e33947SAndroid Build Coastguard Worker   for (size_t i = 2; i < destinationLength; ++i) {
70*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], fillValue);
71*84e33947SAndroid Build Coastguard Worker   }
72*84e33947SAndroid Build Coastguard Worker }
73*84e33947SAndroid Build Coastguard Worker 
TEST(String,NormalCopyAllChars)74*84e33947SAndroid Build Coastguard Worker TEST(String, NormalCopyAllChars) {
75*84e33947SAndroid Build Coastguard Worker   const char *source = "hello world";
76*84e33947SAndroid Build Coastguard Worker   constexpr size_t sourceLength = 11;
77*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 100;
78*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
79*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
80*84e33947SAndroid Build Coastguard Worker 
81*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
82*84e33947SAndroid Build Coastguard Worker 
83*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, sourceLength + 1);  // account for '\0'
84*84e33947SAndroid Build Coastguard Worker   size_t i = 0;
85*84e33947SAndroid Build Coastguard Worker   for (; i < sourceLength; ++i) {
86*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], source[i]);
87*84e33947SAndroid Build Coastguard Worker   }
88*84e33947SAndroid Build Coastguard Worker 
89*84e33947SAndroid Build Coastguard Worker   ASSERT_EQ(destination[i], 0);
90*84e33947SAndroid Build Coastguard Worker   ++i;
91*84e33947SAndroid Build Coastguard Worker 
92*84e33947SAndroid Build Coastguard Worker   for (; i < destinationLength; ++i) {
93*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], fillValue);
94*84e33947SAndroid Build Coastguard Worker   }
95*84e33947SAndroid Build Coastguard Worker }
96*84e33947SAndroid Build Coastguard Worker 
TEST(String,NormalCopyGreaterThanSourceLength)97*84e33947SAndroid Build Coastguard Worker TEST(String, NormalCopyGreaterThanSourceLength) {
98*84e33947SAndroid Build Coastguard Worker   const char *source = "hello world";
99*84e33947SAndroid Build Coastguard Worker   constexpr size_t sourceLength = 11;
100*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 100;
101*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
102*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
103*84e33947SAndroid Build Coastguard Worker 
104*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
105*84e33947SAndroid Build Coastguard Worker 
106*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, destinationLength);
107*84e33947SAndroid Build Coastguard Worker   size_t i = 0;
108*84e33947SAndroid Build Coastguard Worker   for (; i < sourceLength; ++i) {
109*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], source[i]);
110*84e33947SAndroid Build Coastguard Worker   }
111*84e33947SAndroid Build Coastguard Worker 
112*84e33947SAndroid Build Coastguard Worker   for (; i < destinationLength; ++i) {
113*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], 0);
114*84e33947SAndroid Build Coastguard Worker   }
115*84e33947SAndroid Build Coastguard Worker }
116*84e33947SAndroid Build Coastguard Worker 
TEST(String,NormalCopyLessThanSourceLength)117*84e33947SAndroid Build Coastguard Worker TEST(String, NormalCopyLessThanSourceLength) {
118*84e33947SAndroid Build Coastguard Worker   const char *source = "hello world";
119*84e33947SAndroid Build Coastguard Worker   constexpr size_t sourceLength = 11;
120*84e33947SAndroid Build Coastguard Worker   constexpr size_t destinationLength = 5;
121*84e33947SAndroid Build Coastguard Worker   char destination[destinationLength];
122*84e33947SAndroid Build Coastguard Worker   char fillValue = 123;
123*84e33947SAndroid Build Coastguard Worker 
124*84e33947SAndroid Build Coastguard Worker   memset(destination, fillValue, destinationLength);
125*84e33947SAndroid Build Coastguard Worker 
126*84e33947SAndroid Build Coastguard Worker   copyString(destination, source, destinationLength);
127*84e33947SAndroid Build Coastguard Worker   size_t i = 0;
128*84e33947SAndroid Build Coastguard Worker   for (; i < destinationLength - 1; ++i) {
129*84e33947SAndroid Build Coastguard Worker     ASSERT_EQ(destination[i], source[i]);
130*84e33947SAndroid Build Coastguard Worker   }
131*84e33947SAndroid Build Coastguard Worker   ASSERT_EQ(destination[i], 0);
132*84e33947SAndroid Build Coastguard Worker }
133