1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package test.java.lang.CharSequence;
25 
26 import java.nio.ByteBuffer;
27 import java.nio.CharBuffer;
28 
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31 
32 /**
33  * @test
34  * @bug 8215401
35  * @summary Test to verify the isEmpty method is behaviorally consistent
36  *          with length
37  * @run testng Emptiness
38  */
39 public class Emptiness {
40 
41     @Test
isEmpty()42     public void isEmpty() {
43         checkEmpty(new StringBuilder());
44         checkEmpty(new StringBuffer());
45         checkEmpty("");
46         checkEmpty(new CharSequence() {
47             @Override public int length() { return 0; }
48             @Override public char charAt(int index) { return 'f'; }
49             @Override public CharSequence subSequence(int start, int end) {
50                 throw new UnsupportedOperationException();
51             }
52         });
53         checkEmpty(CharBuffer.wrap(new char[0]));
54         checkEmpty(CharBuffer.wrap(""));
55 
56         // A CharBuffer being filled is empty when there's no room remaining
57         // - or there's nothing in the buffer after a flip
58         checkEmpty(ByteBuffer.allocate(0).asCharBuffer());
59         checkEmpty(ByteBuffer.allocate(2).asCharBuffer().append('f'));
60         // Android-changed: Cast to CharBuffer explicitly due to the variant return type of flip().
61         checkEmpty((CharBuffer) ByteBuffer.allocate(2).asCharBuffer().flip());
62 
63         checkEmpty(ByteBuffer.allocateDirect(0).asCharBuffer());
64         checkEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().append('f'));
65         // Android-changed: Cast to CharBuffer explicitly due to the variant return type of flip().
66         checkEmpty((CharBuffer) ByteBuffer.allocateDirect(2).asCharBuffer().flip());
67     }
68 
69     @Test
isNotEmpty()70     public void isNotEmpty() {
71         checkNotEmpty(new StringBuilder().append("foo"));
72         checkNotEmpty(new StringBuffer().append("bar"));
73         checkNotEmpty("baz");
74         checkNotEmpty(new CharSequence() {
75             @Override public int length() { return 1; }
76             @Override public char charAt(int index) { return 'f'; }
77             @Override public CharSequence subSequence(int start, int end) {
78                 throw new UnsupportedOperationException();
79             }
80         });
81         checkNotEmpty(CharBuffer.wrap(new char[] { 'f' }));
82         checkNotEmpty(CharBuffer.wrap("foo"));
83 
84         // A CharBuffer being filled is non-empty when there's room remaining
85         // - or when there's something in the buffer after a flip
86         checkNotEmpty(ByteBuffer.allocate(2).asCharBuffer());
87         checkNotEmpty(ByteBuffer.allocate(4).asCharBuffer().append('f'));
88         // Android-changed: Cast to CharBuffer explicitly due to the variant return type of flip().
89         checkNotEmpty((CharBuffer) ByteBuffer.allocate(2).asCharBuffer().append('f').flip());
90 
91         checkNotEmpty(ByteBuffer.allocateDirect(2).asCharBuffer());
92         checkNotEmpty(ByteBuffer.allocateDirect(4).asCharBuffer().append('f'));
93         // Android-changed: Cast to CharBuffer explicitly due to the variant return type of flip().
94         checkNotEmpty((CharBuffer) ByteBuffer.allocateDirect(2).asCharBuffer().append('f').flip());
95     }
96 
checkEmpty(CharSequence cs)97     public void checkEmpty(CharSequence cs) {
98         Assert.assertTrue(cs.isEmpty());
99         Assert.assertTrue(consistentWithLength(cs));
100     }
101 
checkNotEmpty(CharSequence cs)102     public void checkNotEmpty(CharSequence cs) {
103         Assert.assertTrue(!cs.isEmpty());
104         Assert.assertTrue(consistentWithLength(cs));
105     }
106 
consistentWithLength(CharSequence cs)107     public boolean consistentWithLength(CharSequence cs) {
108         return cs.isEmpty() == (cs.length() == 0);
109     }
110 }
111