| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import java.nio.ByteBuffer; |
| 4 |
import java.nio.charset.StandardCharsets; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.List; |
| 7 |
|
| 8 |
import org.assertj.core.api.Assertions; |
| 9 |
import org.junit.Test; |
| 10 |
|
| 11 |
import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 12 |
import com.carrotsearch.randomizedtesting.annotations.Name; |
| 13 |
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 14 |
|
| 15 |
public class SequenceEncodersTest extends RandomizedTest { |
| 16 |
private final ISequenceEncoder coder; |
| 17 |
|
| 18 |
public SequenceEncodersTest(@Name("coder") ISequenceEncoder coder) |
| 19 |
{ |
| 20 |
this.coder = coder; |
| 21 |
} |
| 22 |
|
| 23 |
@ParametersFactory |
| 24 |
public static List<Object[]> testFactory() { |
| 25 |
List<Object[]> encoders = new ArrayList<>(); |
| 26 |
for (EncoderType t : EncoderType.values()) { |
| 27 |
encoders.add(new Object [] {t.get()}); |
| 28 |
} |
| 29 |
return encoders; |
| 30 |
} |
| 31 |
|
| 32 |
@Test |
| 33 |
public void testEncodeSuffixOnRandomSequences() { |
| 34 |
for (int i = 0; i < 10000; i++) { |
| 35 |
assertRoundtripEncode( |
| 36 |
randomAsciiLettersOfLengthBetween(0, 500), |
| 37 |
randomAsciiLettersOfLengthBetween(0, 500)); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void testEncodeSamples() { |
| 43 |
assertRoundtripEncode("", ""); |
| 44 |
assertRoundtripEncode("abc", "ab"); |
| 45 |
assertRoundtripEncode("abc", "abx"); |
| 46 |
assertRoundtripEncode("ab", "abc"); |
| 47 |
assertRoundtripEncode("xabc", "abc"); |
| 48 |
assertRoundtripEncode("axbc", "abc"); |
| 49 |
assertRoundtripEncode("axybc", "abc"); |
| 50 |
assertRoundtripEncode("axybc", "abc"); |
| 51 |
assertRoundtripEncode("azbc", "abcxy"); |
| 52 |
|
| 53 |
assertRoundtripEncode("Niemcami", "Niemiec"); |
| 54 |
assertRoundtripEncode("Niemiec", "Niemcami"); |
| 55 |
} |
| 56 |
|
| 57 |
private void assertRoundtripEncode(String srcString, String dstString) { |
| 58 |
ByteBuffer source = ByteBuffer.wrap(srcString.getBytes(UTF8)); |
| 59 |
ByteBuffer target = ByteBuffer.wrap(dstString.getBytes(UTF8)); |
| 60 |
|
| 61 |
ByteBuffer encoded = coder.encode(ByteBuffer.allocate(randomInt(30)), source, target); |
| 62 |
ByteBuffer decoded = coder.decode(ByteBuffer.allocate(randomInt(30)), source, encoded); |
| 63 |
|
| 64 |
if (!decoded.equals(target)) { |
| 65 |
System.out.println("src: " + BufferUtils.toString(source, StandardCharsets.UTF_8)); |
| 66 |
System.out.println("dst: " + BufferUtils.toString(target, StandardCharsets.UTF_8)); |
| 67 |
System.out.println("enc: " + BufferUtils.toString(encoded, StandardCharsets.UTF_8)); |
| 68 |
System.out.println("dec: " + BufferUtils.toString(decoded, StandardCharsets.UTF_8)); |
| 69 |
Assertions.fail("Mismatch."); |
| 70 |
} |
| 71 |
} |
| 72 |
} |