Serenity Operating System
at hosted 187 lines 5.5 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 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 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/TestSuite.h> 28 29#include <AK/FlyString.h> 30#include <AK/String.h> 31#include <AK/StringBuilder.h> 32 33TEST_CASE(construct_empty) 34{ 35 EXPECT(String().is_null()); 36 EXPECT(String().is_empty()); 37 EXPECT(!String().characters()); 38 39 EXPECT(!String("").is_null()); 40 EXPECT(String("").is_empty()); 41 EXPECT(String("").characters() != nullptr); 42 43 EXPECT(String("").impl() == String::empty().impl()); 44} 45 46TEST_CASE(construct_contents) 47{ 48 String test_string = "ABCDEF"; 49 EXPECT(!test_string.is_empty()); 50 EXPECT(!test_string.is_null()); 51 EXPECT_EQ(test_string.length(), 6u); 52 EXPECT_EQ(test_string.length(), strlen(test_string.characters())); 53 EXPECT(test_string.characters() != nullptr); 54 EXPECT(!strcmp(test_string.characters(), "ABCDEF")); 55 56 EXPECT(test_string == "ABCDEF"); 57 EXPECT(test_string != "ABCDE"); 58 EXPECT(test_string != "ABCDEFG"); 59} 60 61TEST_CASE(compare) 62{ 63 String test_string = "ABCDEF"; 64 EXPECT("a" < String("b")); 65 EXPECT(!("a" > String("b"))); 66 EXPECT("b" > String("a")); 67 EXPECT(!("b" < String("b"))); 68 EXPECT("a" >= String("a")); 69 EXPECT(!("a" >= String("b"))); 70 EXPECT("a" <= String("a")); 71 EXPECT(!("b" <= String("a"))); 72} 73 74TEST_CASE(index_access) 75{ 76 String test_string = "ABCDEF"; 77 EXPECT_EQ(test_string[0], 'A'); 78 EXPECT_EQ(test_string[1], 'B'); 79} 80 81TEST_CASE(starts_with) 82{ 83 String test_string = "ABCDEF"; 84 EXPECT(test_string.starts_with("AB")); 85 EXPECT(test_string.starts_with('A')); 86 EXPECT(!test_string.starts_with('B')); 87 EXPECT(test_string.starts_with("ABCDEF")); 88 EXPECT(!test_string.starts_with("DEF")); 89} 90 91TEST_CASE(ends_with) 92{ 93 String test_string = "ABCDEF"; 94 EXPECT(test_string.ends_with("EF")); 95 EXPECT(test_string.ends_with('F')); 96 EXPECT(!test_string.ends_with('E')); 97 EXPECT(test_string.ends_with("ABCDEF")); 98 EXPECT(!test_string.ends_with("ABC")); 99} 100 101TEST_CASE(copy_string) 102{ 103 String test_string = "ABCDEF"; 104 auto test_string_copy = test_string; 105 EXPECT_EQ(test_string, test_string_copy); 106 EXPECT_EQ(test_string.characters(), test_string_copy.characters()); 107} 108 109TEST_CASE(move_string) 110{ 111 String test_string = "ABCDEF"; 112 auto test_string_copy = test_string; 113 auto test_string_move = move(test_string_copy); 114 EXPECT_EQ(test_string, test_string_move); 115 EXPECT(test_string_copy.is_null()); 116} 117 118TEST_CASE(repeated) 119{ 120 EXPECT_EQ(String::repeated('x', 0), ""); 121 EXPECT_EQ(String::repeated('x', 1), "x"); 122 EXPECT_EQ(String::repeated('x', 2), "xx"); 123} 124 125TEST_CASE(to_int) 126{ 127 bool ok; 128 EXPECT(String("123").to_int(ok) == 123 && ok); 129 EXPECT(String("-123").to_int(ok) == -123 && ok); 130} 131 132TEST_CASE(to_lowercase) 133{ 134 EXPECT(String("ABC").to_lowercase() == "abc"); 135} 136 137TEST_CASE(to_uppercase) 138{ 139 EXPECT(String("AbC").to_uppercase() == "ABC"); 140} 141 142TEST_CASE(flystring) 143{ 144 { 145 FlyString a("foo"); 146 FlyString b("foo"); 147 EXPECT_EQ(a.impl(), b.impl()); 148 } 149 150 { 151 String a = "foo"; 152 FlyString b = a; 153 StringBuilder builder; 154 builder.append('f'); 155 builder.append("oo"); 156 FlyString c = builder.to_string(); 157 EXPECT_EQ(a.impl(), b.impl()); 158 EXPECT_EQ(a.impl(), c.impl()); 159 } 160} 161 162TEST_CASE(replace) 163{ 164 String test_string = "Well, hello Friends!"; 165 u32 replacements = test_string.replace("Friends", "Testers"); 166 EXPECT(replacements == 1); 167 EXPECT(test_string == "Well, hello Testers!"); 168 169 replacements = test_string.replace("ell", "e're", true); 170 EXPECT(replacements == 2); 171 EXPECT(test_string == "We're, he'reo Testers!"); 172 173 replacements = test_string.replace("!", " :^)"); 174 EXPECT(replacements == 1); 175 EXPECT(test_string == "We're, he'reo Testers :^)"); 176 177 test_string = String("111._.111._.111"); 178 replacements = test_string.replace("111", "|||", true); 179 EXPECT(replacements == 3); 180 EXPECT(test_string == "|||._.|||._.|||"); 181 182 replacements = test_string.replace("|||", "111"); 183 EXPECT(replacements == 1); 184 EXPECT(test_string == "111._.|||._.|||"); 185} 186 187TEST_MAIN(String)