-
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.
feat: provide inefficient AString reducers
- Loading branch information
Showing
20 changed files
with
1,912 additions
and
80 deletions.
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
120 changes: 120 additions & 0 deletions
120
astring/src/androidTest/java/xyz/tynn/astring/AStringReducerAndroidTest.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,120 @@ | ||
// Copyright 2023 Christian Schmitz | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package xyz.tynn.astring; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static xyz.tynn.astring.AStringFactory.createFromCharSequence; | ||
import static xyz.tynn.astring.test.AStringAssert.assertParcelableAStringInvocation; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
public class AStringReducerAndroidTest { | ||
|
||
private final Iterable<AString> aStrings = List.of(AString.Null); | ||
|
||
@Test | ||
public void delegate_should_implement_parcelable() { | ||
String value = "value"; | ||
assertParcelableAStringInvocation(Delegate.wrap($ -> value, aStrings)); | ||
assertParcelableAStringInvocation(Delegate.wrap($ -> value, | ||
AString.Null, createFromCharSequence("value"))); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("Convert2Lambda") | ||
public void interface_should_not_be_efficient() { | ||
assertNotEquals(AStringFactory.reduce(aStrings, new AString.Reducer() { | ||
@Override | ||
public CharSequence invoke(@NonNull Iterable<? extends CharSequence> values) { | ||
return ""; | ||
} | ||
}), AStringFactory.reduce(aStrings, new AString.Reducer() { | ||
@Override | ||
public CharSequence invoke(@NonNull Iterable<? extends CharSequence> values) { | ||
return ""; | ||
} | ||
})); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("Convert2Lambda") | ||
public void interface_ref_should_be_efficient() { | ||
AString.Reducer function = new AString.Reducer() { | ||
@Override | ||
public CharSequence invoke(@NonNull Iterable<? extends CharSequence> values) { | ||
return ""; | ||
} | ||
}; | ||
assertEquals(AStringFactory.reduce(aStrings, function), | ||
AStringFactory.reduce(aStrings, function)); | ||
} | ||
|
||
@Test | ||
public void instance_should_be_efficient() { | ||
assertEquals(AStringFactory.reduce(aStrings, new Reducer()), | ||
AStringFactory.reduce(aStrings, new Reducer())); | ||
} | ||
|
||
@Test | ||
public void instance_ref_should_be_efficient() { | ||
AString.Reducer function = new Reducer(); | ||
assertEquals(AStringFactory.reduce(aStrings, function), | ||
AStringFactory.reduce(aStrings, function)); | ||
} | ||
|
||
@Test | ||
public void function_should_not_be_efficient() { | ||
assertNotEquals(AStringFactory.reduce(aStrings, this::function), | ||
AStringFactory.reduce(aStrings, this::function)); | ||
} | ||
|
||
@Test | ||
public void function_ref_should_be_efficient() { | ||
AString.Reducer function = this::function; | ||
assertEquals(AStringFactory.reduce(aStrings, function), | ||
AStringFactory.reduce(aStrings, function)); | ||
} | ||
|
||
@Test | ||
public void lambda_should_not_be_efficient() { | ||
assertNotEquals(AStringFactory.reduce(aStrings, values -> ""), | ||
AStringFactory.reduce(aStrings, values -> "")); | ||
} | ||
|
||
@Test | ||
public void lambda_ref_should_be_efficient() { | ||
AString.Reducer function = values -> ""; | ||
assertEquals(AStringFactory.reduce(aStrings, function), | ||
AStringFactory.reduce(aStrings, function)); | ||
} | ||
|
||
private CharSequence function(Iterable<? extends CharSequence> values) { | ||
return ""; | ||
} | ||
|
||
private final static class Reducer implements AString.Reducer { | ||
|
||
@Nullable | ||
@Override | ||
public CharSequence invoke(@NonNull Iterable<? extends CharSequence> values) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object obj) { | ||
return obj instanceof Reducer; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 0; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.