Path: blob/main/libshaderc_util/src/string_piece_test.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/string_piece.h"1516#include <gtest/gtest.h>17#include <sstream>18#include <unordered_map>1920#include "death_test.h"2122namespace {2324using shaderc_util::string_piece;2526TEST(string_piece, creation) {27std::string my_string("std::string");28const char* my_c_string = "c::string";2930string_piece my_string_piece(my_string);31string_piece my_c_string_piece(my_c_string);32string_piece my_partial_c_string_piece(my_c_string, my_c_string + 3);33string_piece my_string_piece_string_piece(my_string_piece);3435EXPECT_EQ("std::string", my_string_piece);36EXPECT_EQ("c::string", my_c_string_piece);37EXPECT_EQ("c::", my_partial_c_string_piece);38EXPECT_EQ("std::string", my_string_piece_string_piece);39}4041TEST(string_piece, creation_with_empty_data) {42string_piece my_string_piece(nullptr, nullptr);43EXPECT_EQ("", my_string_piece);44}4546TEST(string_piece, creation_with_nullptr) {47string_piece my_string_piece(nullptr);48EXPECT_EQ("", my_string_piece);49}5051TEST(string_pieceDeathTest, creation_causing_assert) {52EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("my_cstring", nullptr), ".*");53EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, "my_cstring"), ".*");54}5556TEST(string_pieceDeathTest, front) {57EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr).front(), "Assertion");58EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, nullptr).front(),59"Assertion");60EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("").front(), "Assertion");61string_piece s("nonempty");62s.clear();63EXPECT_DEBUG_DEATH_IF_SUPPORTED(s.front(), "Assertion");64}6566TEST(string_pieceDeathTest, back) {67EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr).back(), "Assertion");68EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, nullptr).back(),69"Assertion");70EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("").back(), "Assertion");71string_piece s("nonempty");72s.clear();73EXPECT_DEBUG_DEATH_IF_SUPPORTED(s.back(), "Assertion");74}7576TEST(string_piece, substr) {77string_piece my_string("my really long string");78EXPECT_EQ("my really long string", my_string.substr(0, string_piece::npos));79EXPECT_EQ("my really long string", my_string.substr(0));80EXPECT_EQ("really long string", my_string.substr(3, string_piece::npos));81EXPECT_EQ("really long string", my_string.substr(3));82EXPECT_EQ("really", my_string.substr(3, 6));83}8485TEST(string_piece, length) {86EXPECT_EQ(0u, string_piece().size());87EXPECT_TRUE(string_piece().empty());88EXPECT_EQ(10u, string_piece("0123456789").size());8990std::string my_string("std::string");91EXPECT_EQ(my_string.size(), string_piece(my_string).size());92}9394TEST(string_piece, clear) {95string_piece my_string("my really long string");96EXPECT_EQ("my really long string", my_string);9798string_piece other_string(my_string);99EXPECT_EQ("my really long string", other_string);100101my_string.clear();102EXPECT_EQ("", my_string);103EXPECT_EQ("my really long string", other_string);104}105106TEST(string_piece, str) {107std::string test_string;108{109std::string temporary_string("my really long string");110string_piece my_stringpiece(temporary_string);111string_piece my_substring = my_stringpiece.substr(3, 6);112113EXPECT_EQ("really", my_substring);114test_string = my_substring.str();115}116EXPECT_EQ("really", test_string);117}118119template <char C>120bool find_char(char c) {121return c == C;122}123124TEST(string_piece, find_first_not_matching) {125string_piece my_string("aaaaaaa b");126EXPECT_EQ(7u, my_string.find_first_not_matching(find_char<'a'>));127EXPECT_EQ(0u, my_string.find_first_not_matching(find_char<'b'>));128EXPECT_EQ(0u, string_piece(" ").find_first_not_matching(::isdigit));129size_t npos = string_piece::npos;130EXPECT_EQ(npos, string_piece("").find_first_not_matching(::isdigit));131EXPECT_EQ(npos, string_piece("123").find_first_not_matching(::isdigit));132EXPECT_EQ(3u, string_piece("123 ").find_first_not_matching(::isdigit));133}134135TEST(string_piece, find_first_not_of) {136size_t npos = string_piece::npos;137string_piece my_string("aaaaaaa b");138EXPECT_EQ(7u, my_string.find_first_not_of("a"));139EXPECT_EQ(0u, my_string.find_first_not_of("b"));140EXPECT_EQ(7u, my_string.find_first_not_of('a'));141EXPECT_EQ(0u, my_string.find_first_not_of('b'));142EXPECT_EQ(0u, string_piece(" ").find_first_not_of("0123456789"));143144EXPECT_EQ(7u, my_string.find_first_not_of("a", 2));145EXPECT_EQ(2u, my_string.find_first_not_of("b", 2));146EXPECT_EQ(7u, my_string.find_first_not_of('a', 2));147EXPECT_EQ(4u, my_string.find_first_not_of('b', 4));148EXPECT_EQ(0u, string_piece(" ").find_first_not_of("0123456789"));149EXPECT_EQ(npos, string_piece(" ").find_first_not_of("0123456789", 5));150151EXPECT_EQ(npos, string_piece("").find_first_not_of("012345689"));152EXPECT_EQ(npos, string_piece("").find_first_not_of("012345689", 1));153EXPECT_EQ(npos, string_piece("123").find_first_not_of("0123456789"));154EXPECT_EQ(npos, string_piece("123").find_first_not_of("0123456789", 1));155EXPECT_EQ(3u, string_piece("123 ").find_first_not_of("0123456789", 2));156EXPECT_EQ(npos, string_piece("123 ").find_first_not_of("0123456789", 4));157EXPECT_EQ(npos, string_piece("").find_first_not_of("1"));158EXPECT_EQ(npos, string_piece("111").find_first_not_of('1'));159}160161TEST(string_piece, find_first_of_char) {162const size_t npos = string_piece::npos;163string_piece my_string("my really long string");164EXPECT_EQ(0u, my_string.find_first_of('m'));165EXPECT_EQ(3u, my_string.find_first_of('r'));166EXPECT_EQ(npos, my_string.find_first_of('z'));167168size_t pos = my_string.find_first_of('l');169EXPECT_EQ(6u, pos);170// If pos points to a 'l' then we should just find that one171EXPECT_EQ(6u, my_string.find_first_of('l', pos));172EXPECT_EQ(7u, my_string.find_first_of('l', pos + 1));173EXPECT_EQ(10u, my_string.find_first_of('l', pos + 2));174EXPECT_EQ(npos, my_string.find_first_of('l', pos + 5));175EXPECT_EQ(npos, my_string.find_first_of('z', 0));176EXPECT_EQ(npos, my_string.find_first_of('z', npos));177178my_string.clear();179EXPECT_EQ(npos, my_string.find_first_of('a'));180EXPECT_EQ(npos, my_string.find_first_of('a', 0));181}182183TEST(string_piece, find_first_of) {184string_piece my_string("aaaaaa b");185EXPECT_EQ(0u, my_string.find_first_of("a"));186EXPECT_EQ(7u, my_string.find_first_of("b"));187EXPECT_EQ(6u, my_string.find_first_of(" "));188size_t npos = string_piece::npos;189EXPECT_EQ(npos, my_string.find_first_of("xh"));190EXPECT_EQ(6u, my_string.find_first_of(" x"));191EXPECT_EQ(6u, my_string.find_first_of(" b"));192EXPECT_EQ(0u, my_string.find_first_of("ab"));193194EXPECT_EQ(6u, my_string.find_first_of(" x", 2));195EXPECT_EQ(6u, my_string.find_first_of(" b", 2));196EXPECT_EQ(2u, my_string.find_first_of("ab", 2));197EXPECT_EQ(npos, my_string.find_first_of("ab", 10));198199EXPECT_EQ(npos, my_string.find_first_of("c"));200EXPECT_EQ(npos, my_string.find_first_of("c", 1));201EXPECT_EQ(npos, string_piece(" ").find_first_of("a"));202EXPECT_EQ(npos, string_piece(" ").find_first_of("a", 10));203EXPECT_EQ(npos, string_piece("aa").find_first_of(""));204EXPECT_EQ(npos, string_piece("aa").find_first_of("", 1));205EXPECT_EQ(npos, string_piece("").find_first_of(""));206EXPECT_EQ(npos, string_piece("").find_first_of("", 1));207EXPECT_EQ(npos, string_piece("").find_first_of("a"));208EXPECT_EQ(npos, string_piece("").find_first_of("ae"));209EXPECT_EQ(npos, string_piece("").find_first_of("ae", 32));210}211212TEST(string_piece, find_last_of) {213string_piece my_string("aaaaaa b");214EXPECT_EQ(5u, my_string.find_last_of('a'));215EXPECT_EQ(7u, my_string.find_last_of('b'));216EXPECT_EQ(6u, my_string.find_last_of(' '));217EXPECT_EQ(5u, my_string.find_last_of("a"));218EXPECT_EQ(7u, my_string.find_last_of("b"));219EXPECT_EQ(6u, my_string.find_last_of(" "));220size_t npos = string_piece::npos;221EXPECT_EQ(npos, my_string.find_last_of("xh"));222EXPECT_EQ(6u, my_string.find_last_of(" x"));223EXPECT_EQ(7u, my_string.find_last_of(" b"));224EXPECT_EQ(7u, my_string.find_last_of("ab"));225226EXPECT_EQ(4u, my_string.find_last_of('a', 4));227EXPECT_EQ(5u, my_string.find_last_of('a', 6));228EXPECT_EQ(0u, string_piece("abbbaa").find_last_of('a', 3));229EXPECT_EQ(4u, string_piece("abbbaa").find_last_of('a', 4));230EXPECT_EQ(5u, string_piece("abbbaa").find_last_of('a', 5));231EXPECT_EQ(5u, string_piece("abbbaa").find_last_of('a', 6));232EXPECT_EQ(npos, string_piece("abbbaa").find_last_of('c', 2));233234EXPECT_EQ(npos, my_string.find_last_of("c"));235EXPECT_EQ(npos, string_piece(" ").find_last_of("a"));236EXPECT_EQ(npos, string_piece("aa").find_last_of(""));237EXPECT_EQ(npos, string_piece("").find_last_of(""));238EXPECT_EQ(npos, string_piece("").find_last_of("a"));239EXPECT_EQ(npos, my_string.find_last_of('c'));240EXPECT_EQ(npos, string_piece(" ").find_last_of('a'));241EXPECT_EQ(npos, string_piece("").find_last_of('a'));242EXPECT_EQ(npos, string_piece("").find_last_of("ae"));243}244245TEST(string_piece, begin_end) {246const char* my_string = "my really long string";247string_piece p(my_string);248size_t pos = 0;249for (auto it = p.begin(); it != p.end(); ++it) {250EXPECT_EQ(my_string[pos++], *it);251}252pos = 0;253for (auto c : p) {254EXPECT_EQ(my_string[pos++], c);255}256}257258TEST(string_piece, front_back) {259// EXPECT_TRUE() is used here because gtest will think we are comparing260// between pointer and integer here if EXPECT_EQ() is used.261const string_piece one_char("a");262EXPECT_TRUE(one_char.front() == 'a');263EXPECT_TRUE(one_char.back() == 'a');264265const string_piece two_chars("bc");266EXPECT_TRUE(two_chars.front() == 'b');267EXPECT_TRUE(two_chars.back() == 'c');268269const string_piece multi_chars("w vm g gg t\t");270EXPECT_TRUE(multi_chars.front() == 'w');271EXPECT_TRUE(multi_chars.back() == '\t');272}273274TEST(string_piece, starts_with) {275EXPECT_TRUE(string_piece("my string").starts_with("my"));276EXPECT_TRUE(string_piece("my string").starts_with("my s"));277EXPECT_TRUE(string_piece("my string").starts_with("m"));278EXPECT_TRUE(string_piece("my string").starts_with(""));279EXPECT_TRUE(string_piece("my string").starts_with("my string"));280EXPECT_TRUE(string_piece("").starts_with(""));281282EXPECT_FALSE(string_piece("").starts_with("a"));283EXPECT_FALSE(string_piece("my string").starts_with(" "));284EXPECT_FALSE(string_piece("my string").starts_with("my stq"));285EXPECT_FALSE(string_piece("my string").starts_with("a"));286EXPECT_FALSE(string_piece("my string").starts_with("my strings"));287}288289TEST(string_piece, find) {290const size_t npos = string_piece::npos;291string_piece my_string("gooogle gooogle");292293EXPECT_EQ(0u, my_string.find(""));294295EXPECT_EQ(0u, my_string.find("g"));296EXPECT_EQ(4u, my_string.find("g", 1));297298EXPECT_EQ(0u, my_string.find("go"));299EXPECT_EQ(8u, my_string.find("go", 1));300301EXPECT_EQ(1u, my_string.find("oo"));302EXPECT_EQ(1u, my_string.find("oo", 1));303EXPECT_EQ(2u, my_string.find("oo", 2));304EXPECT_EQ(9u, my_string.find("oo", 3));305306EXPECT_EQ(4u, my_string.find("gle"));307EXPECT_EQ(12u, my_string.find("gle", 5));308309EXPECT_EQ(npos, my_string.find("0"));310EXPECT_EQ(npos, my_string.find("does-not-exist"));311EXPECT_EQ(npos, my_string.find("longer than gooogle gooogle"));312313EXPECT_EQ(npos, my_string.find("", npos));314EXPECT_EQ(npos, my_string.find("gle", npos));315}316317TEST(string_piece, get_fields) {318string_piece input;319std::vector<string_piece> expected_lines;320EXPECT_EQ(expected_lines, input.get_fields('\n'));321EXPECT_EQ(expected_lines, input.get_fields('\n', true));322323input = "first line";324expected_lines = {"first line"};325EXPECT_EQ(expected_lines, input.get_fields('\n'));326EXPECT_EQ(expected_lines, input.get_fields('\n', true));327328input = "first line\n";329expected_lines = {"first line"};330EXPECT_EQ(expected_lines, input.get_fields('\n'));331expected_lines = {"first line\n"};332EXPECT_EQ(expected_lines, input.get_fields('\n', true));333334input = "\nfirst line";335expected_lines = {"", "first line"};336EXPECT_EQ(expected_lines, input.get_fields('\n'));337expected_lines = {"\n", "first line"};338EXPECT_EQ(expected_lines, input.get_fields('\n', true));339340input = "first line\nsecond line\nthird line\n";341expected_lines = {"first line", "second line", "third line"};342EXPECT_EQ(expected_lines, input.get_fields('\n'));343expected_lines = {"first line\n", "second line\n", "third line\n"};344EXPECT_EQ(expected_lines, input.get_fields('\n', true));345346input = "first line\n\nsecond line\n\nthird line\n\n";347expected_lines = {"first line", "", "second line", "", "third line", ""};348EXPECT_EQ(expected_lines, input.get_fields('\n'));349expected_lines = {"first line\n", "\n", "second line\n",350"\n", "third line\n", "\n"};351EXPECT_EQ(expected_lines, input.get_fields('\n', true));352}353354TEST(string_piece, operator_stream_out) {355std::stringstream stream;356string_piece my_string("my really long string");357stream << my_string;358EXPECT_EQ("my really long string", stream.str());359stream.str("");360stream << my_string.substr(3, 6);361EXPECT_EQ("really", stream.str());362stream.str("");363stream << string_piece();364EXPECT_EQ("", stream.str());365}366367TEST(string_piece, lrstrip) {368string_piece nothing_to_remove("abcdefg");369EXPECT_EQ("abcdefg", nothing_to_remove.lstrip("hijklmn"));370EXPECT_EQ("abcdefg", nothing_to_remove.rstrip("hijklmn"));371EXPECT_EQ("abcdefg", nothing_to_remove.strip("hijklmn"));372373string_piece empty_string("");374EXPECT_EQ(0u, empty_string.lstrip("google").size());375EXPECT_EQ(0u, empty_string.rstrip("google").size());376EXPECT_EQ(0u, empty_string.strip("google").size());377378string_piece remove_nothing("asdfghjkl");379EXPECT_EQ("asdfghjkl", remove_nothing.lstrip(""));380EXPECT_EQ("asdfghjkl", remove_nothing.rstrip(""));381EXPECT_EQ("asdfghjkl", remove_nothing.strip(""));382383string_piece strip_numbers("0123g4o5o6g7l8e9");384EXPECT_EQ("g4o5o6g7l8e9", strip_numbers.lstrip("0123456789"));385EXPECT_EQ("0123g4o5o6g7l8e", strip_numbers.rstrip("0123456789"));386EXPECT_EQ("g4o5o6g7l8e", strip_numbers.strip("0123456789"));387}388389TEST(string_piece, strip_whitespace) {390string_piece lots_of_space(" b i n g o ");391EXPECT_EQ("b i n g o", lots_of_space.strip_whitespace());392393string_piece whitespaces("\v\t\f\n\rleft\r\t\f\n\vright\f\n\t\v\r");394EXPECT_EQ("left\r\t\f\n\vright", whitespaces.strip_whitespace());395396string_piece remove_all(" \t ");397EXPECT_EQ(0u, remove_all.strip_whitespace().size());398}399400TEST(string_piece, not_equal) {401EXPECT_FALSE(string_piece() != string_piece());402EXPECT_FALSE(string_piece("") != string_piece());403EXPECT_TRUE(string_piece() != string_piece(" "));404EXPECT_FALSE(string_piece("abc") != string_piece("abc"));405EXPECT_TRUE(string_piece("abc") != string_piece("abc "));406EXPECT_TRUE(string_piece("abc") != string_piece("abd"));407408EXPECT_FALSE("" != string_piece());409EXPECT_FALSE("" != string_piece(""));410EXPECT_TRUE(" " != string_piece(""));411EXPECT_FALSE("abc" != string_piece("abc"));412EXPECT_TRUE(" abc" != string_piece("abc"));413EXPECT_TRUE("abd" != string_piece("abc"));414}415416TEST(string_piece, data) {417EXPECT_EQ(nullptr, string_piece().data());418const char* empty = "";419EXPECT_EQ(empty, string_piece(empty).data());420const char* space = " ";421EXPECT_EQ(space, string_piece(space).data());422const char* a = "a";423EXPECT_EQ(a, string_piece(a).data());424const char* abc = "abc";425EXPECT_EQ(abc, string_piece(abc).data());426EXPECT_EQ(abc + 1, string_piece(abc).substr(1).data());427EXPECT_EQ(abc + 3, string_piece(abc).substr(3).data());428}429430TEST(string_piece, unordered_map) {431std::unordered_map<string_piece, int> dict;432dict["abc"] = 123;433EXPECT_EQ(123, dict["abc"]);434}435436} // anonymous namespace437438439