xref: /aosp_15_r20/system/chre/util/tests/string_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
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 "chre/util/nanoapp/string.h"
20 
21 using ::chre::copyString;
22 
TEST(StringDeathTest,InvalidInput)23 TEST(StringDeathTest, InvalidInput) {
24   char destination[100];
25   ASSERT_DEATH(copyString(nullptr, nullptr, 0), ".*");
26   ASSERT_DEATH(copyString(nullptr, destination, 1), ".*");
27   ASSERT_DEATH(copyString(destination, nullptr, 2), ".*");
28 }
29 
TEST(String,ZeroCharsToCopy)30 TEST(String, ZeroCharsToCopy) {
31   const char *source = "hello world";
32   constexpr size_t destinationLength = 100;
33   char destination[destinationLength];
34   char fillValue = 123;
35 
36   memset(destination, fillValue, destinationLength);
37 
38   copyString(destination, source, 0);
39   for (size_t i = 0; i < destinationLength; ++i) {
40     ASSERT_EQ(destination[i], fillValue);
41   }
42 }
43 
TEST(String,EmptyStringPadsWithZeroes)44 TEST(String, EmptyStringPadsWithZeroes) {
45   const char *source = "";
46   constexpr size_t destinationLength = 100;
47   char destination[destinationLength];
48   char fillValue = 123;
49 
50   memset(destination, fillValue, destinationLength);
51 
52   copyString(destination, source, destinationLength);
53   for (size_t i = 0; i < destinationLength; ++i) {
54     ASSERT_EQ(destination[i], 0);
55   }
56 }
57 
TEST(String,NormalCopyOneChar)58 TEST(String, NormalCopyOneChar) {
59   const char *source = "hello world";
60   constexpr size_t destinationLength = 100;
61   char destination[destinationLength];
62   char fillValue = 123;
63 
64   memset(destination, fillValue, destinationLength);
65 
66   copyString(destination, source, 2);  // one char + '\0'
67   ASSERT_EQ(destination[0], source[0]);
68   ASSERT_EQ(destination[1], 0);
69   for (size_t i = 2; i < destinationLength; ++i) {
70     ASSERT_EQ(destination[i], fillValue);
71   }
72 }
73 
TEST(String,NormalCopyAllChars)74 TEST(String, NormalCopyAllChars) {
75   const char *source = "hello world";
76   constexpr size_t sourceLength = 11;
77   constexpr size_t destinationLength = 100;
78   char destination[destinationLength];
79   char fillValue = 123;
80 
81   memset(destination, fillValue, destinationLength);
82 
83   copyString(destination, source, sourceLength + 1);  // account for '\0'
84   size_t i = 0;
85   for (; i < sourceLength; ++i) {
86     ASSERT_EQ(destination[i], source[i]);
87   }
88 
89   ASSERT_EQ(destination[i], 0);
90   ++i;
91 
92   for (; i < destinationLength; ++i) {
93     ASSERT_EQ(destination[i], fillValue);
94   }
95 }
96 
TEST(String,NormalCopyGreaterThanSourceLength)97 TEST(String, NormalCopyGreaterThanSourceLength) {
98   const char *source = "hello world";
99   constexpr size_t sourceLength = 11;
100   constexpr size_t destinationLength = 100;
101   char destination[destinationLength];
102   char fillValue = 123;
103 
104   memset(destination, fillValue, destinationLength);
105 
106   copyString(destination, source, destinationLength);
107   size_t i = 0;
108   for (; i < sourceLength; ++i) {
109     ASSERT_EQ(destination[i], source[i]);
110   }
111 
112   for (; i < destinationLength; ++i) {
113     ASSERT_EQ(destination[i], 0);
114   }
115 }
116 
TEST(String,NormalCopyLessThanSourceLength)117 TEST(String, NormalCopyLessThanSourceLength) {
118   const char *source = "hello world";
119   constexpr size_t sourceLength = 11;
120   constexpr size_t destinationLength = 5;
121   char destination[destinationLength];
122   char fillValue = 123;
123 
124   memset(destination, fillValue, destinationLength);
125 
126   copyString(destination, source, destinationLength);
127   size_t i = 0;
128   for (; i < destinationLength - 1; ++i) {
129     ASSERT_EQ(destination[i], source[i]);
130   }
131   ASSERT_EQ(destination[i], 0);
132 }
133