Path: blob/master/test/jdk/java/lang/CharSequence/Emptiness.java
41152 views
/*1* Copyright (c) 2020, 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*/2223import java.nio.ByteBuffer;24import java.nio.CharBuffer;2526import org.testng.Assert;27import org.testng.annotations.Test;2829/**30* @test31* @bug 821540132* @summary Test to verify the isEmpty method is behaviorally consistent33* with length34* @run testng Emptiness35*/36public class Emptiness {3738@Test39public void isEmpty() {40checkEmpty(new StringBuilder());41checkEmpty(new StringBuffer());42checkEmpty("");43checkEmpty(new CharSequence() {44@Override public int length() { return 0; }45@Override public char charAt(int index) { return 'f'; }46@Override public CharSequence subSequence(int start, int end) {47throw new UnsupportedOperationException();48}49});50checkEmpty(CharBuffer.wrap(new char[0]));51checkEmpty(CharBuffer.wrap(""));5253// A CharBuffer being filled is empty when there's no room remaining54// - or there's nothing in the buffer after a flip55checkEmpty(ByteBuffer.allocate(0).asCharBuffer());56checkEmpty(ByteBuffer.allocate(2).asCharBuffer().append('f'));57checkEmpty(ByteBuffer.allocate(2).asCharBuffer().flip());5859checkEmpty(ByteBuffer.allocateDirect(0).asCharBuffer());60checkEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().append('f'));61checkEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().flip());62}6364@Test65public void isNotEmpty() {66checkNotEmpty(new StringBuilder().append("foo"));67checkNotEmpty(new StringBuffer().append("bar"));68checkNotEmpty("baz");69checkNotEmpty(new CharSequence() {70@Override public int length() { return 1; }71@Override public char charAt(int index) { return 'f'; }72@Override public CharSequence subSequence(int start, int end) {73throw new UnsupportedOperationException();74}75});76checkNotEmpty(CharBuffer.wrap(new char[] { 'f' }));77checkNotEmpty(CharBuffer.wrap("foo"));7879// A CharBuffer being filled is non-empty when there's room remaining80// - or when there's something in the buffer after a flip81checkNotEmpty(ByteBuffer.allocate(2).asCharBuffer());82checkNotEmpty(ByteBuffer.allocate(4).asCharBuffer().append('f'));83checkNotEmpty(ByteBuffer.allocate(2).asCharBuffer().append('f').flip());8485checkNotEmpty(ByteBuffer.allocateDirect(2).asCharBuffer());86checkNotEmpty(ByteBuffer.allocateDirect(4).asCharBuffer().append('f'));87checkNotEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().append('f').flip());88}8990public void checkEmpty(CharSequence cs) {91Assert.assertTrue(cs.isEmpty());92Assert.assertTrue(consistentWithLength(cs));93}9495public void checkNotEmpty(CharSequence cs) {96Assert.assertTrue(!cs.isEmpty());97Assert.assertTrue(consistentWithLength(cs));98}99100public boolean consistentWithLength(CharSequence cs) {101return cs.isEmpty() == (cs.length() == 0);102}103}104105106