Path: blob/master/test/jdk/java/foreign/TestStringEncoding.java
66643 views
/*1* Copyright (c) 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*/2324import jdk.incubator.foreign.CLinker;25import jdk.incubator.foreign.MemorySegment;2627import jdk.incubator.foreign.ResourceScope;28import org.testng.annotations.*;29import static org.testng.Assert.*;3031/*32* @test33* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"34* @run testng TestStringEncoding35*/3637public class TestStringEncoding {3839@Test(dataProvider = "strings")40public void testStrings(String testString, int expectedByteLength) {41try (ResourceScope scope = ResourceScope.newConfinedScope()) {42MemorySegment text = CLinker.toCString(testString, scope);4344assertEquals(text.byteSize(), expectedByteLength);4546String roundTrip = CLinker.toJavaString(text);47assertEquals(roundTrip, testString);48}49}5051@DataProvider52public static Object[][] strings() {53return new Object[][] {54{ "testing", 8 },55{ "", 1 },56{ "X", 2 },57{ "12345", 6 },58{ "yen \u00A5", 7 }, // in UTF-8 2 bytes: 0xC2 0xA559{ "snowman \u26C4", 12 }, // in UTF-8 three bytes: 0xE2 0x9B 0x8460{ "rainbow \uD83C\uDF08", 13 } // in UTF-8 four bytes: 0xF0 0x9F 0x8C 0x8861};62}63}646566