Merge "Change assert to assertTrue"
diff --git a/JavaLibrary.mk b/JavaLibrary.mk
index 7ca09bc..dfb0aaa 100644
--- a/JavaLibrary.mk
+++ b/JavaLibrary.mk
@@ -127,6 +127,23 @@
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.
@@ -170,9 +187,10 @@
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)
@@ -262,16 +280,32 @@
LOCAL_REQUIRED_MODULES := tzdata-host
include $(BUILD_HOST_DALVIK_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-hostdex
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/JavaLibrary.mk
+LOCAL_JAVA_LIBRARIES := core-all-hostdex
+LOCAL_CORE_LIBRARY := true
+include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
+
# Make the core-tests library.
ifeq ($(LIBCORE_SKIP_TESTS),)
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-hostdex core-libart-hostdex okhttp-hostdex bouncycastle-hostdex core-junit-hostdex core-tests-support-hostdex mockito-api-hostdex
+ LOCAL_JAVA_LIBRARIES := core-oj-hostdex core-libart-hostdex core-lambda-stubs-hostdex okhttp-hostdex bouncycastle-hostdex core-junit-hostdex core-tests-support-hostdex mockito-api-hostdex
LOCAL_STATIC_JAVA_LIBRARIES := sqlite-jdbc-host mockwebserver-host nist-pkix-tests-host
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_MODULE_TAGS := optional
+ LOCAL_JAVA_LANGUAGE_VERSION := 1.8
LOCAL_MODULE := core-tests-hostdex
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/JavaLibrary.mk
include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
diff --git a/luni/src/test/java/libcore/java/util/ObjectsTest.java b/luni/src/test/java/libcore/java/util/ObjectsTest.java
index 0701108..735f6c5 100644
--- a/luni/src/test/java/libcore/java/util/ObjectsTest.java
+++ b/luni/src/test/java/libcore/java/util/ObjectsTest.java
@@ -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 {
@@ -103,6 +104,32 @@
}
}
+ 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));
@@ -113,4 +140,14 @@
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()));
+ }
}
diff --git a/luni/src/test/java/libcore/java/util/function/BiConsumerTest.java b/luni/src/test/java/libcore/java/util/function/BiConsumerTest.java
new file mode 100644
index 0000000..4709138
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/BiConsumerTest.java
@@ -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) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/BiFunctionTest.java b/luni/src/test/java/libcore/java/util/function/BiFunctionTest.java
new file mode 100644
index 0000000..04e365a
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/BiFunctionTest.java
@@ -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));
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/BiPredicateTest.java b/luni/src/test/java/libcore/java/util/function/BiPredicateTest.java
new file mode 100644
index 0000000..6060f28
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/BiPredicateTest.java
@@ -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);
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/BinaryOperatorTest.java b/luni/src/test/java/libcore/java/util/function/BinaryOperatorTest.java
new file mode 100644
index 0000000..e3be671
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/BinaryOperatorTest.java
@@ -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) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/ConsumerTest.java b/luni/src/test/java/libcore/java/util/function/ConsumerTest.java
new file mode 100644
index 0000000..7cd4db6
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/ConsumerTest.java
@@ -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.Consumer;
+
+public class ConsumerTest extends TestCase {
+
+ public void testAndThen() throws Exception {
+ Consumer<StringBuilder> sweet = s -> s.append("sweet");
+ Consumer<StringBuilder> dude = s -> s.append("dude");
+
+ StringBuilder sb = new StringBuilder();
+ sweet.andThen(dude).accept(sb);
+ assertEquals("sweetdude", sb.toString());
+
+ sb.setLength(0);
+ dude.andThen(sweet).accept(sb);
+ assertEquals("dudesweet", sb.toString());
+ }
+
+ public void testAndThen_null() throws Exception {
+ Consumer<StringBuilder> one = s -> s.append("one");
+ try {
+ one.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/DoubleConsumerTest.java b/luni/src/test/java/libcore/java/util/function/DoubleConsumerTest.java
new file mode 100644
index 0000000..0931b11
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/DoubleConsumerTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.DoubleConsumer;
+
+public class DoubleConsumerTest extends TestCase {
+
+ public void testAndThen() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ DoubleConsumer one = d -> sb.append("one:" + d + ",");
+ DoubleConsumer two = d -> sb.append("two:" + d);
+
+ one.andThen(two).accept(1.0d);
+ assertEquals("one:1.0,two:1.0", sb.toString());
+ }
+
+ public void testAndThen_null() throws Exception {
+ DoubleConsumer one = s -> {};
+ try {
+ one.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/DoublePredicateTest.java b/luni/src/test/java/libcore/java/util/function/DoublePredicateTest.java
new file mode 100644
index 0000000..45bf5da
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/DoublePredicateTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.DoublePredicate;
+
+public class DoublePredicateTest extends TestCase {
+
+ public void testAnd() throws Exception {
+ double arg = 5.0d;
+
+ 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 };
+
+ DoublePredicate alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertEquals(x, arg); return true; };
+ DoublePredicate alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertEquals(x, arg); return true; };
+ DoublePredicate alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertEquals(x, arg); return false; };
+ DoublePredicate alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertEquals(x, arg); return false; };
+
+ // true && true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.and(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get());
+
+ // true && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysTrue.and(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get());
+
+ // false && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get());
+
+ // false && true
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get());
+ }
+
+ public void testAnd_null() throws Exception {
+ DoublePredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.and(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testNegate() throws Exception {
+ double arg = 5.0d;
+ DoublePredicate alwaysTrue = x -> { assertEquals(x, arg); return true; };
+ assertFalse(alwaysTrue.negate().test(arg));
+
+ DoublePredicate alwaysFalse = x -> { assertEquals(x, arg); return false; };
+ assertTrue(alwaysFalse.negate().test(arg));
+ }
+
+ public void testOr() throws Exception {
+ double arg = 5.0d;
+
+ 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 };
+
+ DoublePredicate alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertEquals(x, arg); return true; };
+ DoublePredicate alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertEquals(x, arg); return true; };
+ DoublePredicate alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertEquals(x, arg); return false; };
+ DoublePredicate alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertEquals(x, arg); return false; };
+
+ // true || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get());
+
+ // true || false
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get());
+
+ // false || false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.or(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get());
+
+ // false || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysFalse.or(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get());
+ }
+
+ public void testOr_null() throws Exception {
+ DoublePredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.or(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ private static void resetToFalse(AtomicBoolean... toResets) {
+ for (AtomicBoolean toReset : toResets) {
+ toReset.set(false);
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/DoubleUnaryOperatorTest.java b/luni/src/test/java/libcore/java/util/function/DoubleUnaryOperatorTest.java
new file mode 100644
index 0000000..65cfb96
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/DoubleUnaryOperatorTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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;
+import java.util.function.DoubleUnaryOperator;
+
+public class DoubleUnaryOperatorTest extends TestCase {
+
+ public void testIdentity() throws Exception {
+ assertEquals(1.0d, DoubleUnaryOperator.identity().applyAsDouble(1.0d));
+ assertEquals(Double.NaN, DoubleUnaryOperator.identity().applyAsDouble(Double.NaN));
+ assertEquals(Double.NEGATIVE_INFINITY,
+ DoubleUnaryOperator.identity().applyAsDouble(Double.NEGATIVE_INFINITY));
+ assertEquals(Double.MAX_VALUE,
+ DoubleUnaryOperator.identity().applyAsDouble(Double.MAX_VALUE));
+ }
+
+ public void testCompose() throws Exception {
+ DoubleUnaryOperator plusOne = x -> x + 1.0d;
+ DoubleUnaryOperator twice = x -> 2 *x;
+ assertEquals(11.0d, plusOne.compose(twice).applyAsDouble(5.0d));
+ }
+
+ public void testCompose_null() throws Exception {
+ DoubleUnaryOperator plusOne = x -> x + 1.0d;
+ try {
+ plusOne.compose(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testAndThen() throws Exception {
+ DoubleUnaryOperator plusOne = x -> x + 1.0d;
+ DoubleUnaryOperator twice = x -> 2 *x;
+ assertEquals(12.0d, plusOne.andThen(twice).applyAsDouble(5.0d));
+ }
+
+ public void testAndThen_null() throws Exception {
+ DoubleUnaryOperator plusOne = x -> x + 1.0d;
+ try {
+ plusOne.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/FunctionTest.java b/luni/src/test/java/libcore/java/util/function/FunctionTest.java
new file mode 100644
index 0000000..e364c11
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/FunctionTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.Function;
+
+public class FunctionTest extends TestCase {
+
+ public void testIdentity() throws Exception {
+ Object o = new Object();
+ assertSame(o, Function.identity().apply(o));
+ assertNull(Function.identity().apply(null));
+ }
+
+ public void testCompose() throws Exception {
+ Function<Double, Double> plusOne = x -> x + 1.0d;
+ Function<Double, Double> twice = x -> 2 *x;
+ assertEquals(11.0d, plusOne.compose(twice).apply(5.0d));
+ }
+
+ public void testCompose_null() throws Exception {
+ Function<Double, Double> plusOne = x -> x + 1.0d;
+ try {
+ plusOne.compose(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testAndThen() throws Exception {
+ Function<Double, Double> plusOne = x -> x + 1.0d;
+ Function<Double, Double> twice = x -> 2 *x;
+ assertEquals(12.0d, plusOne.andThen(twice).apply(5.0d));
+ }
+
+ public void testAndThen_null() throws Exception {
+ Function<Double, Double> plusOne = x -> x + 1.0d;
+ try {
+ plusOne.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/IntConsumerTest.java b/luni/src/test/java/libcore/java/util/function/IntConsumerTest.java
new file mode 100644
index 0000000..de04664
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/IntConsumerTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.IntConsumer;
+
+public class IntConsumerTest extends TestCase {
+
+ public void testAndThen() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ IntConsumer one = i -> sb.append("one:" + i + ",");
+ IntConsumer two = i -> sb.append("two:" + i);
+
+ one.andThen(two).accept(1);
+ assertEquals("one:1,two:1", sb.toString());
+ }
+
+ public void testAndThen_null() throws Exception {
+ IntConsumer one = s -> {};
+ try {
+ one.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/IntPredicateTest.java b/luni/src/test/java/libcore/java/util/function/IntPredicateTest.java
new file mode 100644
index 0000000..63a670f
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/IntPredicateTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.IntPredicate;
+
+public class IntPredicateTest extends TestCase {
+
+ public void testAnd() throws Exception {
+ int arg = 5;
+
+ 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 };
+
+ IntPredicate alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertEquals(x, arg); return true; };
+ IntPredicate alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertEquals(x, arg); return true; };
+ IntPredicate alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertEquals(x, arg); return false; };
+ IntPredicate alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertEquals(x, arg); return false; };
+
+ // true && true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.and(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get());
+
+ // true && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysTrue.and(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get());
+
+ // false && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get());
+
+ // false && true
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get());
+ }
+
+ public void testAnd_null() throws Exception {
+ IntPredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.and(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testNegate() throws Exception {
+ int arg = 5;
+ IntPredicate alwaysTrue = x -> { assertEquals(x, arg); return true; };
+ assertFalse(alwaysTrue.negate().test(arg));
+
+ IntPredicate alwaysFalse = x -> { assertEquals(x, arg); return false; };
+ assertTrue(alwaysFalse.negate().test(arg));
+ }
+
+ public void testOr() throws Exception {
+ int arg = 5;
+
+ 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 };
+
+ IntPredicate alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertEquals(x, arg); return true; };
+ IntPredicate alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertEquals(x, arg); return true; };
+ IntPredicate alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertEquals(x, arg); return false; };
+ IntPredicate alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertEquals(x, arg); return false; };
+
+ // true || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get());
+
+ // true || false
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get());
+
+ // false || false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.or(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get());
+
+ // false || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysFalse.or(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get());
+ }
+
+ public void testOr_null() throws Exception {
+ IntPredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.or(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ private static void resetToFalse(AtomicBoolean... toResets) {
+ for (AtomicBoolean toReset : toResets) {
+ toReset.set(false);
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/IntUnaryOperatorTest.java b/luni/src/test/java/libcore/java/util/function/IntUnaryOperatorTest.java
new file mode 100644
index 0000000..043ec1d
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/IntUnaryOperatorTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.IntUnaryOperator;
+
+public class IntUnaryOperatorTest extends TestCase {
+
+ public void testIdentity() throws Exception {
+ assertEquals(1, IntUnaryOperator.identity().applyAsInt(1));
+ assertEquals(Integer.MAX_VALUE, IntUnaryOperator.identity().applyAsInt(Integer.MAX_VALUE));
+ }
+
+ public void testCompose() throws Exception {
+ IntUnaryOperator plusOne = x -> x + 1;
+ IntUnaryOperator twice = x -> 2 *x;
+ assertEquals(11, plusOne.compose(twice).applyAsInt(5));
+ }
+
+ public void testCompose_null() throws Exception {
+ IntUnaryOperator plusOne = x -> x + 1;
+ try {
+ plusOne.compose(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testAndThen() throws Exception {
+ IntUnaryOperator plusOne = x -> x + 1;
+ IntUnaryOperator twice = x -> 2 *x;
+ assertEquals(12, plusOne.andThen(twice).applyAsInt(5));
+ }
+
+ public void testAndThen_null() throws Exception {
+ IntUnaryOperator plusOne = x -> x + 1;
+ try {
+ plusOne.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/LongConsumerTest.java b/luni/src/test/java/libcore/java/util/function/LongConsumerTest.java
new file mode 100644
index 0000000..d1aad73
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/LongConsumerTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.LongConsumer;
+
+public class LongConsumerTest extends TestCase {
+
+ public void testAndThen() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ LongConsumer one = l -> sb.append("one:" + l + ",");
+ LongConsumer two = l -> sb.append("two:" + l);
+
+ one.andThen(two).accept(1L);
+ assertEquals("one:1,two:1", sb.toString());
+ }
+
+ public void testAndThen_null() throws Exception {
+ LongConsumer one = s -> {};
+ try {
+ one.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/LongPredicateTest.java b/luni/src/test/java/libcore/java/util/function/LongPredicateTest.java
new file mode 100644
index 0000000..a875799
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/LongPredicateTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.LongPredicate;
+
+public class LongPredicateTest extends TestCase {
+
+ public void testAnd() throws Exception {
+ 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 };
+
+ LongPredicate alwaysTrue = x -> { alwaysTrueInvoked.set(true); return true; };
+ LongPredicate alwaysTrue2 = x -> { alwaysTrue2Invoked.set(true); return true; };
+ LongPredicate alwaysFalse = x -> { alwaysFalseInvoked.set(true); return false; };
+ LongPredicate alwaysFalse2 = x -> { alwaysFalse2Invoked.set(true); return false; };
+
+ // true && true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.and(alwaysTrue2).test(1L));
+ assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get());
+
+ // true && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysTrue.and(alwaysFalse).test(1L));
+ assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get());
+
+ // false && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysFalse2).test(1L));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get());
+
+ // false && true
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysTrue).test(1L));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get());
+ }
+
+ public void testAnd_null() throws Exception {
+ LongPredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.and(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testNegate() throws Exception {
+ long arg = 5L;
+ LongPredicate alwaysTrue = x -> { assertEquals(x, arg); return true; };
+ assertFalse(alwaysTrue.negate().test(arg));
+
+ LongPredicate alwaysFalse = x -> { assertEquals(x, arg); return false; };
+ assertTrue(alwaysFalse.negate().test(arg));
+ }
+
+ public void testOr() throws Exception {
+ 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 };
+
+ LongPredicate alwaysTrue = x -> { alwaysTrueInvoked.set(true); return true; };
+ LongPredicate alwaysTrue2 = x -> { alwaysTrue2Invoked.set(true); return true; };
+ LongPredicate alwaysFalse = x -> { alwaysFalseInvoked.set(true); return false; };
+ LongPredicate alwaysFalse2 = x -> { alwaysFalse2Invoked.set(true); return false; };
+
+ // true || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysTrue2).test(1L));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get());
+
+ // true || false
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysFalse).test(1L));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get());
+
+ // false || false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.or(alwaysFalse2).test(1L));
+ assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get());
+
+ // false || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysFalse.or(alwaysTrue).test(1L));
+ assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get());
+ }
+
+ public void testOr_null() throws Exception {
+ LongPredicate alwaysTrue = x -> true;
+ try {
+ alwaysTrue.or(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ private static void resetToFalse(AtomicBoolean... toResets) {
+ for (AtomicBoolean toReset : toResets) {
+ toReset.set(false);
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/LongUnaryOperatorTest.java b/luni/src/test/java/libcore/java/util/function/LongUnaryOperatorTest.java
new file mode 100644
index 0000000..9ab028c
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/LongUnaryOperatorTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.LongUnaryOperator;
+
+public class LongUnaryOperatorTest extends TestCase {
+
+ public void testIdentity() throws Exception {
+ assertEquals(1L, LongUnaryOperator.identity().applyAsLong(1L));
+ assertEquals(Long.MAX_VALUE, LongUnaryOperator.identity().applyAsLong(Long.MAX_VALUE));
+ }
+
+ public void testCompose() throws Exception {
+ LongUnaryOperator plusOne = x -> x + 1L;
+ LongUnaryOperator twice = x -> 2L *x;
+ assertEquals(11L, plusOne.compose(twice).applyAsLong(5L));
+ }
+
+ public void testCompose_null() throws Exception {
+ LongUnaryOperator plusOne = x -> x + 1;
+ try {
+ plusOne.compose(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testAndThen() throws Exception {
+ LongUnaryOperator plusOne = x -> x + 1L;
+ LongUnaryOperator twice = x -> 2L *x;
+ assertEquals(12, plusOne.andThen(twice).applyAsLong(5L));
+ }
+
+ public void testAndThen_null() throws Exception {
+ LongUnaryOperator plusOne = x -> x + 1L;
+ try {
+ plusOne.andThen(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/PredicateTest.java b/luni/src/test/java/libcore/java/util/function/PredicateTest.java
new file mode 100644
index 0000000..83a73eb
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/PredicateTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.Predicate;
+
+public class PredicateTest extends TestCase {
+
+ public void testAnd() throws Exception {
+ Object arg = new Object();
+
+ 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 };
+
+ Predicate<Object> alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertSame(x, arg); return true; };
+ Predicate<Object> alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertSame(x, arg); return true; };
+ Predicate<Object> alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertSame(x, arg); return false; };
+ Predicate<Object> alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertSame(x, arg); return false; };
+
+ // true && true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.and(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get());
+
+ // true && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysTrue.and(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get());
+
+ // false && false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get());
+
+ // false && true
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.and(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get());
+ }
+
+ public void testAnd_null() throws Exception {
+ Object arg = new Object();
+ Predicate<Object> alwaysTrue = x -> { assertSame(arg, x); return true; };
+ try {
+ alwaysTrue.and(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ public void testNegate() throws Exception {
+ Predicate<Object> alwaysTrue = x -> true;
+ assertFalse(alwaysTrue.negate().test(null));
+
+ Predicate<Object> alwaysFalse = x -> false;
+ assertTrue(alwaysFalse.negate().test(null));
+ }
+
+ public void testOr() throws Exception {
+ Object arg = new Object();
+
+ 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 };
+
+ Predicate<Object> alwaysTrue =
+ x -> { alwaysTrueInvoked.set(true); assertSame(x, arg); return true; };
+ Predicate<Object> alwaysTrue2 =
+ x -> { alwaysTrue2Invoked.set(true); assertSame(x, arg); return true; };
+ Predicate<Object> alwaysFalse =
+ x -> { alwaysFalseInvoked.set(true); assertSame(x, arg); return false; };
+ Predicate<Object> alwaysFalse2 =
+ x -> { alwaysFalse2Invoked.set(true); assertSame(x, arg); return false; };
+
+ // true || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysTrue2).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get());
+
+ // true || false
+ resetToFalse(invocationState);
+ assertTrue(alwaysTrue.or(alwaysFalse).test(arg));
+ assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get());
+
+ // false || false
+ resetToFalse(invocationState);
+ assertFalse(alwaysFalse.or(alwaysFalse2).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get());
+
+ // false || true
+ resetToFalse(invocationState);
+ assertTrue(alwaysFalse.or(alwaysTrue).test(arg));
+ assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get());
+ }
+
+ public void testOr_null() throws Exception {
+ Predicate<Object> alwaysTrue = x -> true;
+ try {
+ alwaysTrue.or(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ }
+
+ private static void resetToFalse(AtomicBoolean... toResets) {
+ for (AtomicBoolean toReset : toResets) {
+ toReset.set(false);
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/function/UnaryOperatorTest.java b/luni/src/test/java/libcore/java/util/function/UnaryOperatorTest.java
new file mode 100644
index 0000000..7a971a6
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/function/UnaryOperatorTest.java
@@ -0,0 +1,30 @@
+/*
+ * 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.UnaryOperator;
+
+public class UnaryOperatorTest extends TestCase {
+
+ public void testIdentity() throws Exception {
+ Object arg = new Object();
+ assertSame(arg, UnaryOperator.identity().apply(arg));
+ assertNull(null, UnaryOperator.identity().apply(null));
+ }
+}
diff --git a/ojluni/src/main/java/java/lang/Math.java b/ojluni/src/main/java/java/lang/Math.java
index aea7651..0e36ab7 100755
--- a/ojluni/src/main/java/java/lang/Math.java
+++ b/ojluni/src/main/java/java/lang/Math.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,8 @@
package java.lang;
import java.util.Random;
-
+import sun.misc.FloatConsts;
+import sun.misc.DoubleConsts;
/**
* The class {@code Math} contains methods for performing basic
@@ -591,7 +592,7 @@
/**
* Returns the closest {@code int} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>
* Special cases:
@@ -610,15 +611,37 @@
* @see java.lang.Integer#MIN_VALUE
*/
public static int round(float a) {
- if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
- return (int)floor(a + 0.5f);
- else
- return 0;
+ int intBits = Float.floatToRawIntBits(a);
+ int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
+ >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
+ int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
+ + FloatConsts.EXP_BIAS) - biasedExp;
+ if ((shift & -32) == 0) { // shift >= 0 && shift < 32
+ // a is a finite number such that pow(2,-32) <= ulp(a) < 1
+ int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
+ | (FloatConsts.SIGNIF_BIT_MASK + 1));
+ if (intBits < 0) {
+ r = -r;
+ }
+ // In the comments below each Java expression evaluates to the value
+ // the corresponding mathematical expression:
+ // (r) evaluates to a / ulp(a)
+ // (r >> shift) evaluates to floor(a * 2)
+ // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
+ // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
+ return ((r >> shift) + 1) >> 1;
+ } else {
+ // a is either
+ // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
+ // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
+ // - an infinity or NaN
+ return (int) a;
+ }
}
/**
* Returns the closest {@code long} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
@@ -637,10 +660,32 @@
* @see java.lang.Long#MIN_VALUE
*/
public static long round(double a) {
- if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
- return (long)floor(a + 0.5d);
- else
- return 0;
+ long longBits = Double.doubleToRawLongBits(a);
+ long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
+ >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
+ long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
+ + DoubleConsts.EXP_BIAS) - biasedExp;
+ if ((shift & -64) == 0) { // shift >= 0 && shift < 64
+ // a is a finite number such that pow(2,-64) <= ulp(a) < 1
+ long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
+ | (DoubleConsts.SIGNIF_BIT_MASK + 1));
+ if (longBits < 0) {
+ r = -r;
+ }
+ // In the comments below each Java expression evaluates to the value
+ // the corresponding mathematical expression:
+ // (r) evaluates to a / ulp(a)
+ // (r >> shift) evaluates to floor(a * 2)
+ // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
+ // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
+ return ((r >> shift) + 1) >> 1;
+ } else {
+ // a is either
+ // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
+ // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
+ // - an infinity or NaN
+ return (long) a;
+ }
}
private static class NoImagePreloadHolder {
diff --git a/ojluni/src/main/java/java/lang/StrictMath.java b/ojluni/src/main/java/java/lang/StrictMath.java
index b829a81..23978ea 100755
--- a/ojluni/src/main/java/java/lang/StrictMath.java
+++ b/ojluni/src/main/java/java/lang/StrictMath.java
@@ -614,7 +614,7 @@
/**
* Returns the closest {@code int} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
@@ -637,7 +637,7 @@
/**
* Returns the closest {@code long} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
diff --git a/ojluni/src/main/native/zip_util.h b/ojluni/src/main/native/zip_util.h
index 124e3f7..20d78b0 100755
--- a/ojluni/src/main/native/zip_util.h
+++ b/ojluni/src/main/native/zip_util.h
@@ -197,6 +197,11 @@
#endif
/*
+ * Use mmap for CEN & ENDHDR sections
+ */
+#define USE_MMAP 1
+
+/*
* Descriptor for a ZIP file.
*/
typedef struct jzfile { /* Zip file */