Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Optional/BasicDouble.java
41149 views
1
/*
2
* Copyright (c) 2013, 2018, 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
/* @test
25
* @bug 8195649
26
* @summary Basic functional test of OptionalDouble
27
* @author Mike Duigou
28
* @build ObscureException
29
* @run testng BasicDouble
30
*/
31
32
import java.util.NoSuchElementException;
33
import java.util.OptionalDouble;
34
import java.util.concurrent.atomic.AtomicBoolean;
35
36
import static org.testng.Assert.*;
37
import org.testng.annotations.Test;
38
39
public class BasicDouble {
40
static final double DOUBLEVAL = Math.PI;
41
static final double UNEXPECTED = 6.62607004E-34;
42
43
/**
44
* Checks a block of assertions over an empty OptionalDouble.
45
*/
46
void checkEmpty(OptionalDouble empty) {
47
assertTrue(empty.equals(OptionalDouble.empty()));
48
assertTrue(OptionalDouble.empty().equals(empty));
49
assertFalse(empty.equals(OptionalDouble.of(UNEXPECTED)));
50
assertFalse(OptionalDouble.of(UNEXPECTED).equals(empty));
51
assertFalse(empty.equals("unexpected"));
52
53
assertFalse(empty.isPresent());
54
assertTrue(empty.isEmpty());
55
assertEquals(empty.hashCode(), 0);
56
assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
57
assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
58
59
assertThrows(NoSuchElementException.class, () -> empty.getAsDouble());
60
assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
61
assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));
62
63
var b = new AtomicBoolean();
64
empty.ifPresent(s -> b.set(true));
65
assertFalse(b.get());
66
67
var b1 = new AtomicBoolean(false);
68
var b2 = new AtomicBoolean(false);
69
empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
70
assertFalse(b1.get());
71
assertTrue(b2.get());
72
73
assertEquals(empty.toString(), "OptionalDouble.empty");
74
}
75
76
/**
77
* Checks a block of assertions over an OptionalDouble that is expected to
78
* have a particular value present.
79
*/
80
void checkPresent(OptionalDouble opt, double expected) {
81
assertFalse(opt.equals(OptionalDouble.empty()));
82
assertFalse(OptionalDouble.empty().equals(opt));
83
assertTrue(opt.equals(OptionalDouble.of(expected)));
84
assertTrue(OptionalDouble.of(expected).equals(opt));
85
assertFalse(opt.equals(OptionalDouble.of(UNEXPECTED)));
86
assertFalse(OptionalDouble.of(UNEXPECTED).equals(opt));
87
assertFalse(opt.equals("unexpected"));
88
89
assertTrue(opt.isPresent());
90
assertFalse(opt.isEmpty());
91
assertEquals(opt.hashCode(), Double.hashCode(expected));
92
assertEquals(opt.orElse(UNEXPECTED), expected);
93
assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
94
95
assertEquals(opt.getAsDouble(), expected);
96
assertEquals(opt.orElseThrow(), expected);
97
assertEquals(opt.orElseThrow(ObscureException::new), expected);
98
99
var b = new AtomicBoolean(false);
100
opt.ifPresent(s -> b.set(true));
101
assertTrue(b.get());
102
103
var b1 = new AtomicBoolean(false);
104
var b2 = new AtomicBoolean(false);
105
opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
106
assertTrue(b1.get());
107
assertFalse(b2.get());
108
109
assertEquals(opt.toString(), "OptionalDouble[" + expected + "]");
110
}
111
112
@Test(groups = "unit")
113
public void testEmpty() {
114
checkEmpty(OptionalDouble.empty());
115
}
116
117
@Test(groups = "unit")
118
public void testPresent() {
119
checkPresent(OptionalDouble.of(DOUBLEVAL), DOUBLEVAL);
120
}
121
122
@Test(groups = "unit")
123
public void testStreamEmpty() {
124
assertEquals(OptionalDouble.empty().stream().toArray(), new double[] { });
125
}
126
127
@Test(groups = "unit")
128
public void testStreamPresent() {
129
assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL });
130
}
131
}
132
133