1 /*
2  * Copyright (c) 2009-2022, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/io/string.h"
29 
30 #include <string.h>
31 
32 #include "gtest/gtest.h"
33 #include "upb/upb.hpp"
34 
TEST(StringTest,Append)35 TEST(StringTest, Append) {
36   upb::Arena arena;
37 
38   upb_String foo;
39   EXPECT_TRUE(upb_String_Init(&foo, arena.ptr()));
40   EXPECT_EQ(upb_String_Size(&foo), 0);
41 
42   EXPECT_TRUE(upb_String_Assign(&foo, "foobar", 3));
43   EXPECT_EQ(upb_String_Size(&foo), 3);
44   EXPECT_EQ(strcmp(upb_String_Data(&foo), "foo"), 0);
45 
46   EXPECT_TRUE(upb_String_Append(&foo, "bar", 3));
47   EXPECT_EQ(upb_String_Size(&foo), 6);
48   EXPECT_EQ(strcmp(upb_String_Data(&foo), "foobar"), 0);
49 
50   EXPECT_TRUE(upb_String_Append(&foo, "baz", 3));
51   EXPECT_EQ(upb_String_Size(&foo), 9);
52   EXPECT_EQ(strcmp(upb_String_Data(&foo), "foobarbaz"), 0);
53 
54   EXPECT_TRUE(upb_String_Append(&foo, "bat", 3));
55   EXPECT_EQ(upb_String_Size(&foo), 12);
56   EXPECT_EQ(strcmp(upb_String_Data(&foo), "foobarbazbat"), 0);
57 
58   EXPECT_TRUE(upb_String_Append(&foo, "feefiefoefoo", 12));
59   EXPECT_EQ(upb_String_Size(&foo), 24);
60   EXPECT_EQ(strcmp(upb_String_Data(&foo), "foobarbazbatfeefiefoefoo"), 0);
61 
62   const char* password = "fiddlesnarf";
63   EXPECT_TRUE(upb_String_Assign(&foo, password, strlen(password)));
64   EXPECT_EQ(upb_String_Size(&foo), strlen(password));
65   EXPECT_EQ(strcmp(upb_String_Data(&foo), password), 0);
66 }
67 
TEST(StringTest,PushBack)68 TEST(StringTest, PushBack) {
69   upb::Arena arena;
70 
71   upb_String foo;
72   EXPECT_TRUE(upb_String_Init(&foo, arena.ptr()));
73   EXPECT_EQ(upb_String_Size(&foo), 0);
74 
75   const std::string big =
76       "asfashfxauwhfwu4fuwafxasnfwxnxwunxuwxufhwfaiwj4w9jvwxssldfjlasviorwnvwij"
77       "grsdjrfiasrjrasijgraisjvrvoiasjspjfsjgfasjgiasjidjsrvjsrjrasjfrijwjajsrF"
78       "JWJGF4WWJSAVSLJArSJGFrAISJGASrlafjgrivarijrraisrgjiawrijg3874f87f7hqfhpf"
79       "f8929hr32p8475902387459023475297328-22-3776-26";
80   EXPECT_TRUE(upb_String_Reserve(&foo, big.size() + 1));
81   EXPECT_TRUE(upb_String_Append(&foo, big.data(), big.size()));
82   EXPECT_EQ(upb_String_Size(&foo), big.size());
83   EXPECT_EQ(strcmp(upb_String_Data(&foo), big.data()), 0);
84 
85   upb_String bar;
86   EXPECT_TRUE(upb_String_Init(&bar, arena.ptr()));
87   EXPECT_EQ(upb_String_Size(&bar), 0);
88 
89   EXPECT_TRUE(upb_String_PushBack(&bar, 'x'));
90   EXPECT_TRUE(upb_String_PushBack(&bar, 'y'));
91   EXPECT_TRUE(upb_String_PushBack(&bar, 'z'));
92   EXPECT_TRUE(upb_String_PushBack(&bar, 'z'));
93   EXPECT_TRUE(upb_String_PushBack(&bar, 'y'));
94   EXPECT_EQ(upb_String_Size(&bar), 5);
95   EXPECT_EQ(strcmp(upb_String_Data(&bar), "xyzzy"), 0);
96 }
97 
TEST(StringTest,Erase)98 TEST(StringTest, Erase) {
99   upb::Arena arena;
100 
101   upb_String foo;
102   EXPECT_TRUE(upb_String_Init(&foo, arena.ptr()));
103 
104   const char* sent = "This is an example sentence.";
105   EXPECT_TRUE(upb_String_Assign(&foo, sent, strlen(sent)));
106   EXPECT_EQ(upb_String_Size(&foo), 28);
107 
108   upb_String_Erase(&foo, 10, 8);
109   EXPECT_EQ(upb_String_Size(&foo), 20);
110   EXPECT_EQ(strcmp(upb_String_Data(&foo), "This is an sentence."), 0);
111 
112   upb_String_Erase(&foo, 9, 1);
113   EXPECT_EQ(upb_String_Size(&foo), 19);
114   EXPECT_EQ(strcmp(upb_String_Data(&foo), "This is a sentence."), 0);
115 
116   upb_String_Erase(&foo, 5, 5);
117   EXPECT_EQ(upb_String_Size(&foo), 14);
118   EXPECT_EQ(strcmp(upb_String_Data(&foo), "This sentence."), 0);
119 
120   upb_String_Erase(&foo, 4, 99);
121   EXPECT_EQ(upb_String_Size(&foo), 4);
122   EXPECT_EQ(strcmp(upb_String_Data(&foo), "This"), 0);
123 
124   upb_String_Erase(&foo, 0, 4);
125   EXPECT_EQ(upb_String_Size(&foo), 0);
126   EXPECT_EQ(strcmp(upb_String_Data(&foo), ""), 0);
127 }
128