Path: blob/master/test/hotspot/gtest/utilities/test_utf8.cpp
41144 views
/*1* Copyright (c) 2016, 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*/2223#include "precompiled.hpp"24#include "utilities/utf8.hpp"25#include "unittest.hpp"2627static void stamp(char* p, size_t len) {28if (len > 0) {29::memset(p, 'A', len);30}31}3233static bool test_stamp(const char* p, size_t len) {34for (const char* q = p; q < p + len; q++) {35if (*q != 'A') {36return false;37}38}39return true;40}4142TEST_VM(utf8, jchar_length) {43char res[60];44jchar str[20];4546for (int i = 0; i < 20; i++) {47str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-848}49str[19] = (jchar) '\0';5051// The resulting string in UTF-8 is 3*19 bytes long, but should be truncated52stamp(res, sizeof(res));53UNICODE::as_utf8(str, 19, res, 10);54ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";55ASSERT_TRUE(test_stamp(res + 10, sizeof(res) - 10));5657stamp(res, sizeof(res));58UNICODE::as_utf8(str, 19, res, 18);59ASSERT_EQ(strlen(res), (size_t) 15) << "string should be truncated here";60ASSERT_TRUE(test_stamp(res + 18, sizeof(res) - 18));6162stamp(res, sizeof(res));63UNICODE::as_utf8(str, 19, res, 20);64ASSERT_EQ(strlen(res), (size_t) 18) << "string should be truncated here";65ASSERT_TRUE(test_stamp(res + 20, sizeof(res) - 20));6667// Test with an "unbounded" buffer68UNICODE::as_utf8(str, 19, res, INT_MAX);69ASSERT_EQ(strlen(res), (size_t) 3 * 19) << "string should end here";7071// Test that we do not overflow the output buffer72for (int i = 1; i < 5; i ++) {73stamp(res, sizeof(res));74UNICODE::as_utf8(str, 19, res, i);75EXPECT_TRUE(test_stamp(res + i, sizeof(res) - i));76}7778}7980TEST_VM(utf8, jbyte_length) {81char res[60];82jbyte str[20];8384for (int i = 0; i < 19; i++) {85str[i] = 0x42;86}87str[19] = '\0';8889stamp(res, sizeof(res));90UNICODE::as_utf8(str, 19, res, 10);91ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";92ASSERT_TRUE(test_stamp(res + 10, sizeof(res) - 10));9394UNICODE::as_utf8(str, 19, res, INT_MAX);95ASSERT_EQ(strlen(res), (size_t) 19) << "string should end here";9697// Test that we do not overflow the output buffer98for (int i = 1; i < 5; i ++) {99stamp(res, sizeof(res));100UNICODE::as_utf8(str, 19, res, i);101EXPECT_TRUE(test_stamp(res + i, sizeof(res) - i));102}103104}105106107