-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CTS tests for java.util.function changes
Bug: 26814204 (cherry-picked from commit 7cf6b7d) Change-Id: I0b3157581f2b540292d6ddfd669cfb966b5279df
- Loading branch information
Showing
19 changed files
with
1,299 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
luni/src/test/java/libcore/java/util/function/BiConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package libcore.java.util.function; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
public class BiConsumerTest extends TestCase { | ||
|
||
public void testAndThen() throws Exception { | ||
BiConsumer<String, StringBuilder> one = (s, t) -> t.append("one").append(s); | ||
BiConsumer<String, StringBuilder> two = (s, t) -> t.append("two").append(s); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
one.andThen(two).accept("z", sb); | ||
assertEquals("oneztwoz", sb.toString()); | ||
|
||
sb.setLength(0); | ||
two.andThen(one).accept("z", sb); | ||
assertEquals("twozonez", sb.toString()); | ||
} | ||
|
||
public void testAndThen_null() throws Exception { | ||
BiConsumer<String, StringBuilder> one = (s, t) -> t.append("one").append(s); | ||
try { | ||
one.andThen(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
luni/src/test/java/libcore/java/util/function/BiFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package libcore.java.util.function; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
public class BiFunctionTest extends TestCase { | ||
|
||
public void testAndThen() throws Exception { | ||
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y; | ||
Function<Integer, String> toString = i -> Integer.toString(i); | ||
assertEquals("4", add.andThen(toString).apply(2, 2)); | ||
} | ||
|
||
public void testAndThen_nullFunction() throws Exception { | ||
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y; | ||
try { | ||
add.andThen(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
|
||
public void testAndThen_nullResult() throws Exception { | ||
BiFunction<Integer, Integer, Integer> toNull = (x, y) -> null; | ||
Function<Integer, String> assertNull = i -> { assertNull(i); return "ok"; }; | ||
assertEquals("ok", toNull.andThen(assertNull).apply(2, 2)); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
luni/src/test/java/libcore/java/util/function/BiPredicateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package libcore.java.util.function; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.BiPredicate; | ||
|
||
public class BiPredicateTest extends TestCase { | ||
|
||
public void testAnd() throws Exception { | ||
Object arg1 = "one"; | ||
Object arg2 = "two"; | ||
AtomicBoolean alwaysTrueInvoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysTrue2Invoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysFalseInvoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysFalse2Invoked = new AtomicBoolean(false); | ||
AtomicBoolean[] invocationState = { | ||
alwaysTrueInvoked, alwaysTrue2Invoked, alwaysFalseInvoked, alwaysFalse2Invoked }; | ||
|
||
BiPredicate<Object, Object> alwaysTrue = | ||
(x, y) -> { alwaysTrueInvoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return true; }; | ||
BiPredicate<Object, Object> alwaysTrue2 = | ||
(x, y) -> { alwaysTrue2Invoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return true; }; | ||
BiPredicate<Object, Object> alwaysFalse = | ||
(x, y) -> { alwaysFalseInvoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return false; }; | ||
BiPredicate<Object, Object> alwaysFalse2 = | ||
(x, y) -> { alwaysFalse2Invoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return false; }; | ||
|
||
// true && true | ||
resetToFalse(invocationState); | ||
assertTrue(alwaysTrue.and(alwaysTrue2).test(arg1, arg2)); | ||
assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get()); | ||
|
||
// true && false | ||
resetToFalse(invocationState); | ||
assertFalse(alwaysTrue.and(alwaysFalse).test(arg1, arg2)); | ||
assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get()); | ||
|
||
// false && false | ||
resetToFalse(invocationState); | ||
assertFalse(alwaysFalse.and(alwaysFalse2).test(arg1, arg2)); | ||
assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get()); | ||
|
||
// false && true | ||
resetToFalse(invocationState); | ||
assertFalse(alwaysFalse.and(alwaysTrue).test(arg1, arg2)); | ||
assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get()); | ||
} | ||
|
||
public void testAnd_null() throws Exception { | ||
BiPredicate<Object, Object> alwaysTrue = (x, y) -> true; | ||
try { | ||
alwaysTrue.and(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
|
||
public void testNegate() throws Exception { | ||
Object arg1 = "one"; | ||
Object arg2 = "two"; | ||
BiPredicate<Object, Object> alwaysTrue = | ||
(x, y) -> { assertSame(arg1, x); assertSame(arg2, y); return true; }; | ||
assertFalse(alwaysTrue.negate().test(arg1, arg2)); | ||
|
||
BiPredicate<Object, Object> alwaysFalse = | ||
(x, y) -> { assertSame(arg1, x); assertSame(arg2, y); return false; }; | ||
assertTrue(alwaysFalse.negate().test(arg1, arg2)); | ||
} | ||
|
||
public void testOr() throws Exception { | ||
Object arg1 = "one"; | ||
Object arg2 = "two"; | ||
AtomicBoolean alwaysTrueInvoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysTrue2Invoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysFalseInvoked = new AtomicBoolean(false); | ||
AtomicBoolean alwaysFalse2Invoked = new AtomicBoolean(false); | ||
AtomicBoolean[] invocationState = { | ||
alwaysTrueInvoked, alwaysTrue2Invoked, alwaysFalseInvoked, alwaysFalse2Invoked }; | ||
|
||
BiPredicate<Object, Object> alwaysTrue = | ||
(x, y) -> { alwaysTrueInvoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return true; }; | ||
BiPredicate<Object, Object> alwaysTrue2 = | ||
(x, y) -> { alwaysTrue2Invoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return true; }; | ||
BiPredicate<Object, Object> alwaysFalse = | ||
(x, y) -> { alwaysFalseInvoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return false; }; | ||
BiPredicate<Object, Object> alwaysFalse2 = | ||
(x, y) -> { alwaysFalse2Invoked.set(true); assertSame(arg1, x); assertSame(arg2, y); return false; }; | ||
|
||
// true || true | ||
resetToFalse(invocationState); | ||
assertTrue(alwaysTrue.or(alwaysTrue2).test(arg1, arg2)); | ||
assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get()); | ||
|
||
// true || false | ||
resetToFalse(invocationState); | ||
assertTrue(alwaysTrue.or(alwaysFalse).test(arg1, arg2)); | ||
assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get()); | ||
|
||
// false || false | ||
resetToFalse(invocationState); | ||
assertFalse(alwaysFalse.or(alwaysFalse2).test(arg1, arg2)); | ||
assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get()); | ||
|
||
// false || true | ||
resetToFalse(invocationState); | ||
assertTrue(alwaysFalse.or(alwaysTrue).test(arg1, arg2)); | ||
assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get()); | ||
} | ||
|
||
public void testOr_null() throws Exception { | ||
BiPredicate<Object, Object> alwaysTrue = (x, y) -> true; | ||
try { | ||
alwaysTrue.or(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
|
||
private static void resetToFalse(AtomicBoolean... toResets) { | ||
for (AtomicBoolean toReset : toResets) { | ||
toReset.set(false); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
luni/src/test/java/libcore/java/util/function/BinaryOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package libcore.java.util.function; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.Comparator; | ||
import java.util.function.BinaryOperator; | ||
|
||
public class BinaryOperatorTest extends TestCase { | ||
|
||
public void testMinBy() throws Exception { | ||
Comparator<String> stringComparator = String::compareTo; | ||
assertEquals("a", BinaryOperator.minBy(stringComparator).apply("a", "b")); | ||
} | ||
|
||
public void testMinBy_null() throws Exception { | ||
try { | ||
BinaryOperator.minBy(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
|
||
public void testMaxBy() throws Exception { | ||
Comparator<String> stringComparator = String::compareTo; | ||
assertEquals("b", BinaryOperator.maxBy(stringComparator).apply("a", "b")); | ||
} | ||
|
||
public void testMaxBy_null() throws Exception { | ||
try { | ||
BinaryOperator.maxBy(null); | ||
fail(); | ||
} catch (NullPointerException expected) {} | ||
} | ||
} |
Oops, something went wrong.