Path: blob/master/test/hotspot/gtest/utilities/test_stringUtils.cpp
41145 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/resourceArea.hpp"26#include "utilities/stringUtils.hpp"27#include "unittest.hpp"2829TEST(StringUtils, similarity) {30const char* str1 = "the quick brown fox jumps over the lazy dog";31const char* str2 = "the quick brown fox jumps over the lazy doh";32EXPECT_NEAR(0.95349, StringUtils::similarity(str1, strlen(str1), str2, strlen(str2)), 1e-5);33}3435static size_t count_char(const char* s, size_t len, char ch) {36size_t cnt = 0;3738for (size_t i = 0; i < len; ++i) {39if (s[i] == ch) {40cnt++;41}42}43return cnt;44}4546static size_t count_char(const stringStream& ss, char ch) {47return count_char(ss.base(), ss.size(), ch);48}4950static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n" \51"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" \52"Lacinia at quis risus sed vulputate odio ut enim blandit.\n" \53"Amet risus nullam eget felis eget.\n" \54"Viverra orci sagittis eu volutpat odio facilisis mauris sit.\n" \55"Erat velit scelerisque in dictum non.\n";565758TEST_VM(StringUtils, replace_no_expand) {59ResourceMark rm;60stringStream ss;6162ss.print_raw(lorem);63size_t newlines = count_char(ss, '\n');64char* s2 = ss.as_string(false);65int deleted = StringUtils::replace_no_expand(s2, "\n", "");66ASSERT_EQ(newlines, (size_t)deleted);6768newlines = count_char(s2, strlen(s2), '\n');69ASSERT_EQ(newlines, (size_t)0);7071deleted = StringUtils::replace_no_expand(s2, "\n", "");72ASSERT_EQ(deleted, 0);73}747576