Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/CharSequence/Emptiness.java
41152 views
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
import java.nio.ByteBuffer;
25
import java.nio.CharBuffer;
26
27
import org.testng.Assert;
28
import org.testng.annotations.Test;
29
30
/**
31
* @test
32
* @bug 8215401
33
* @summary Test to verify the isEmpty method is behaviorally consistent
34
* with length
35
* @run testng Emptiness
36
*/
37
public class Emptiness {
38
39
@Test
40
public void isEmpty() {
41
checkEmpty(new StringBuilder());
42
checkEmpty(new StringBuffer());
43
checkEmpty("");
44
checkEmpty(new CharSequence() {
45
@Override public int length() { return 0; }
46
@Override public char charAt(int index) { return 'f'; }
47
@Override public CharSequence subSequence(int start, int end) {
48
throw new UnsupportedOperationException();
49
}
50
});
51
checkEmpty(CharBuffer.wrap(new char[0]));
52
checkEmpty(CharBuffer.wrap(""));
53
54
// A CharBuffer being filled is empty when there's no room remaining
55
// - or there's nothing in the buffer after a flip
56
checkEmpty(ByteBuffer.allocate(0).asCharBuffer());
57
checkEmpty(ByteBuffer.allocate(2).asCharBuffer().append('f'));
58
checkEmpty(ByteBuffer.allocate(2).asCharBuffer().flip());
59
60
checkEmpty(ByteBuffer.allocateDirect(0).asCharBuffer());
61
checkEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().append('f'));
62
checkEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().flip());
63
}
64
65
@Test
66
public void isNotEmpty() {
67
checkNotEmpty(new StringBuilder().append("foo"));
68
checkNotEmpty(new StringBuffer().append("bar"));
69
checkNotEmpty("baz");
70
checkNotEmpty(new CharSequence() {
71
@Override public int length() { return 1; }
72
@Override public char charAt(int index) { return 'f'; }
73
@Override public CharSequence subSequence(int start, int end) {
74
throw new UnsupportedOperationException();
75
}
76
});
77
checkNotEmpty(CharBuffer.wrap(new char[] { 'f' }));
78
checkNotEmpty(CharBuffer.wrap("foo"));
79
80
// A CharBuffer being filled is non-empty when there's room remaining
81
// - or when there's something in the buffer after a flip
82
checkNotEmpty(ByteBuffer.allocate(2).asCharBuffer());
83
checkNotEmpty(ByteBuffer.allocate(4).asCharBuffer().append('f'));
84
checkNotEmpty(ByteBuffer.allocate(2).asCharBuffer().append('f').flip());
85
86
checkNotEmpty(ByteBuffer.allocateDirect(2).asCharBuffer());
87
checkNotEmpty(ByteBuffer.allocateDirect(4).asCharBuffer().append('f'));
88
checkNotEmpty(ByteBuffer.allocateDirect(2).asCharBuffer().append('f').flip());
89
}
90
91
public void checkEmpty(CharSequence cs) {
92
Assert.assertTrue(cs.isEmpty());
93
Assert.assertTrue(consistentWithLength(cs));
94
}
95
96
public void checkNotEmpty(CharSequence cs) {
97
Assert.assertTrue(!cs.isEmpty());
98
Assert.assertTrue(consistentWithLength(cs));
99
}
100
101
public boolean consistentWithLength(CharSequence cs) {
102
return cs.isEmpty() == (cs.length() == 0);
103
}
104
}
105
106