Skip to content

Commit

Permalink
CTS tests for java.util.function changes
Browse files Browse the repository at this point in the history
Bug: 26814204
(cherry-picked from commit 7cf6b7d)

Change-Id: I0b3157581f2b540292d6ddfd669cfb966b5279df
  • Loading branch information
nfuller committed Feb 18, 2016
1 parent 3b2eb53 commit 668d346
Show file tree
Hide file tree
Showing 19 changed files with 1,299 additions and 1 deletion.
20 changes: 19 additions & 1 deletion JavaLibrary.mk
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ LOCAL_CORE_LIBRARY := true
LOCAL_REQUIRED_MODULES := tzdata
include $(BUILD_JAVA_LIBRARY)

# A library that exists to satisfy javac when
# compiling source code that contains lambdas.
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(openjdk_lambda_stub_files)
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_DX_FLAGS := --core-library
LOCAL_MODULE_TAGS := optional
LOCAL_JAVA_LANGUAGE_VERSION := 1.8
LOCAL_MODULE := core-lambda-stubs
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/JavaLibrary.mk
LOCAL_JAVA_LIBRARIES := core-all
LOCAL_NOTICE_FILE := $(LOCAL_PATH)/ojluni/NOTICE
LOCAL_CORE_LIBRARY := true
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_JAVA_LIBRARY)

ifeq ($(LIBCORE_SKIP_TESTS),)
# A guaranteed unstripped version of core-oj and core-libart. This is required for ART testing in
# preopted configurations. See b/24535627.
Expand Down Expand Up @@ -170,9 +187,10 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(test_src_files)
LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-oj core-libart okhttp core-junit bouncycastle mockito-target
LOCAL_JAVA_LIBRARIES := core-oj core-libart core-lambda-stubs okhttp core-junit bouncycastle mockito-target
LOCAL_STATIC_JAVA_LIBRARIES := core-tests-support sqlite-jdbc mockwebserver nist-pkix-tests
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_JAVA_LANGUAGE_VERSION := 1.8
LOCAL_MODULE := core-tests
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/JavaLibrary.mk
include $(BUILD_STATIC_JAVA_LIBRARY)
Expand Down
37 changes: 37 additions & 0 deletions luni/src/test/java/libcore/java/util/ObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

public class ObjectsTest extends junit.framework.TestCase {
public static final class Hello {
Expand Down Expand Up @@ -103,6 +104,32 @@ public void test_requireNonNull_T_String() throws Exception {
}
}

public void test_requireNonNull_T_Supplier() throws Exception {
Hello h = new Hello();
assertEquals(h, Objects.requireNonNull(h, () -> "test"));
try {
Objects.requireNonNull(null, () -> "message");
fail();
} catch (NullPointerException expected) {
assertEquals("message", expected.getMessage());
}

// The supplier is unexpectedly null.
try {
Objects.requireNonNull(null, (Supplier<String>) null);
fail();
} catch (NullPointerException expected) {
}

// The message returned by the supplier is null.
try {
Objects.requireNonNull(null, () -> null);
fail();
} catch (NullPointerException expected) {
assertEquals(null, expected.getMessage());
}
}

public void test_toString_Object() throws Exception {
assertEquals("hello", Objects.toString(new Hello()));
assertEquals("null", Objects.toString(null));
Expand All @@ -113,4 +140,14 @@ public void test_toString_Object_String() throws Exception {
assertEquals("world", Objects.toString(null, "world"));
assertEquals(null, Objects.toString(null, null));
}

public void test_isNull() throws Exception {
assertTrue(Objects.isNull(null));
assertFalse(Objects.isNull(new Hello()));
}

public void test_nonNull() throws Exception {
assertFalse(Objects.nonNull(null));
assertTrue(Objects.nonNull(new Hello()));
}
}
45 changes: 45 additions & 0 deletions luni/src/test/java/libcore/java/util/function/BiConsumerTest.java
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 luni/src/test/java/libcore/java/util/function/BiFunctionTest.java
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 luni/src/test/java/libcore/java/util/function/BiPredicateTest.java
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);
}
}
}
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) {}
}
}
Loading

0 comments on commit 668d346

Please sign in to comment.