Commit c071d812 by Hut

added some tests and moved files

parent ee2d9b5c
Pipeline #414 failed with stages
in 1 minute 54 seconds
......@@ -48,9 +48,9 @@ cache:
- master
# Validate merge requests using JDK7
validate:jdk7:
<<: *validate
image: maven:3.3.9-jdk-7
#validate:jdk7:
# <<: *validate
# image: maven:3.3.9-jdk-7
# Validate merge requests using JDK8
validate:jdk8:
......@@ -58,9 +58,9 @@ validate:jdk8:
image: maven:3.3.9-jdk-8
# Verify merge requests using JDK7
verify:jdk7:
<<: *verify
image: maven:3.3.9-jdk-7
#verify:jdk7:
# <<: *verify
# image: maven:3.3.9-jdk-7
# Verify merge requests using JDK8
verify:jdk8:
......
......@@ -7,22 +7,35 @@
<groupId>passive.directory</groupId>
<artifactId>markov</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.0.3</junit.jupiter.version>
<junit.vintage.version>${junit.version}.3</junit.vintage.version>
<junit.platform.version>1.1.0-RC1</junit.platform.version>
<junit.platform.version>1.0.3</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- To avoid compiler warnings about @API annotations in JUnit code -->
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
......@@ -38,15 +51,6 @@
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<properties>
<!-- <includeTags>fast</includeTags> -->
<excludeTags>slow</excludeTags>
<!--
<configurationParameters>
junit.jupiter.conditions.deactivate = *
</configurationParameters>
-->
</properties>
</configuration>
<dependencies>
<dependency>
......@@ -59,18 +63,4 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package directory.passive.markov;
package markov;
import java.util.AbstractMap;
import java.util.Collection;
......
package directory.passive.markov;
package markov;
import java.util.Arrays;
import java.util.HashMap;
......
package directory.passive.markov;
package markov;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
// FIXME rename
public class Lookup {
public static final Lookup empty = new Lookup() {
public Token forRandom(double random) {
return Token.EMPTY;
}
};
public static final Lookup empty = new Lookup() {
public Token forRandom(double random) {
return Token.EMPTY;
}
};
public Lookup() {
}
public Lookup() {
}
private final LinkedHashMap<Token, Integer> tokens = new LinkedHashMap<Token, Integer>();
private final LinkedHashMap<Token, Integer> tokens = new LinkedHashMap<Token, Integer>();
int getTotalCounts() {
return tokens.values().stream().mapToInt(i -> i.intValue()).sum();
}
int getTotalCounts() {
return tokens.values().stream().mapToInt(i -> i.intValue()).sum();
}
int getDistinctTokens() {
return tokens.size();
}
int getDistinctTokens() {
return tokens.size();
}
int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1);
}
public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1);
}
public Token forRandom(double random) {
int id = (int) Math.floor(random * getTotalCounts());
int i = 0;
for (Entry<Token, Integer> entry : tokens.entrySet()) {
i += entry.getValue();
if (id < i) {
return entry.getKey();
}
}
throw new IllegalStateException(String.format(
"failed to find a random token with seed %d within %s", id,
this));
public Token forRandom(double random) {
if(random <0 || random >=1) {
throw new IllegalArgumentException("expected double [0; 1)");
}
int id = (int) Math.floor(random * getTotalCounts());
int i = 0;
for (Entry<Token, Integer> entry : tokens.entrySet()) {
i += entry.getValue();
if (id < i) {
return entry.getKey();
}
}
throw new IllegalStateException(String.format(
"failed to find a random token with seed %d within %s", id,
this));
}
}
public Token average() {
return tokens
.entrySet().stream().sorted((e1, e2) -> Integer
.compare(e2.getValue(), e1.getValue()))
.findFirst().get().getKey();
}
public Token average() {
return tokens
.entrySet().stream().sorted((e1, e2) -> Integer
.compare(e2.getValue(), e1.getValue()))
.findFirst().get().getKey();
}
@Override
public String toString() {
return "Lookup [tokens= "
+ tokens.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue(),
e1.getValue()))
.map(e -> String.format("%d*%s", e.getValue(),
e.getKey()))
.collect(Collectors.joining(", "))
+ "]";
}
public Set<Token> allPosible() {
return this.tokens.keySet();
}
@Override
public String toString() {
return "Lookup [tokens= "
+ tokens.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue(),
e1.getValue()))
.map(e -> String.format("%d*%s", e.getValue(),
e.getKey()))
.collect(Collectors.joining(", "))
+ "]";
}
}
package directory.passive.markov;
package markov;
public class Main {
public static void main(String[] args) {
System.out.println(input);
System.exit(0);
int prefixLength = 2;
Data data = new Parser_first_version(prefixLength).parse(input);
Builder b = new Builder(prefixLength, data);
......
package directory.passive.markov;
package markov;
public interface Parser {
}
package directory.passive.markov;
package markov;
public class ParserStreamStyle {
......
package directory.passive.markov;
package markov;
public class Parser_first_version {
private final int prefix_length;
public Parser_first_version(int prefix_length) {
super();
this.prefix_length = prefix_length;
}
public Data parse(String input) {
Data data = new Data();
for (String line : input.split("\n")) {
line = line.replace("Kai☺UWE empfiehlt ", "");
Token[] tokens = tokenize(line);
for (int i = 0 + prefix_length; i < tokens.length; i++) {
Prefix p = getPrefix(tokens, i);
Token t = getToken(tokens, i);
data.add(p, t);
}
}
return data;
}
private Token[] tokenize(String line) {
String[] strings = line.split(" ");
Token[] tokens = new Token[strings.length + prefix_length + 1];
int i = 0;
for (; i < prefix_length; i++) {
tokens[i] = Token.START;
}
for (; i < strings.length + prefix_length; i++) {
tokens[i] = new Token(strings[i - prefix_length]);
}
tokens[i] = Token.END;
return tokens;
}
private Prefix getPrefix(Token[] tokens, int index) {
Token[] prefixTokens = new Token[prefix_length];
for (int i = 0; i < prefix_length; i++) {
prefixTokens[i] = tokens[index - prefix_length + i];
}
return new Prefix(prefixTokens);
}
private Token getToken(Token[] tokens, int i) {
return tokens[i];
}
private final int prefix_length;
public Parser_first_version(int prefix_length) {
super();
this.prefix_length = prefix_length;
}
public Data parse(String input) {
Data data = new Data();
for (String line : input.split("\n")) {
line = line.replace("Kai☺UWE empfiehlt ", "");
Token[] tokens = tokenize(line);
for (int i = 0 + prefix_length; i < tokens.length; i++) {
Prefix p = getPrefix(tokens, i);
Token t = getToken(tokens, i);
data.add(p, t);
}
}
return data;
}
private Token[] tokenize(String line) {
String[] strings = line.split(" ");
Token[] tokens = new Token[strings.length + prefix_length + 1];
int i = 0;
for (; i < prefix_length; i++) {
tokens[i] = Token.START;
}
for (; i < strings.length + prefix_length; i++) {
tokens[i] = new Token(strings[i - prefix_length]);
}
tokens[i] = Token.END;
return tokens;
}
private Prefix getPrefix(Token[] tokens, int index) {
Token[] prefixTokens = new Token[prefix_length];
System.arraycopy(tokens, index - prefix_length + 1, prefixTokens, 0, prefix_length);
// for (int i = 0; i < prefix_length; i++) {
// prefixTokens[i] = tokens[index - prefix_length + i];
// }
return new Prefix(prefixTokens);
}
private Token getToken(Token[] tokens, int i) {
return tokens[i];
}
}
package directory.passive.markov;
package markov;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
// FIXME rename
public class Prefix {
private final List<Token> tokens;
private final List<Token> tokens;
public Prefix(Token[] tokens) {
this.tokens = compressLeadingStarts(Arrays.asList(tokens));
}
public Prefix(Token[] tokens) {
this.tokens = compressLeadingStarts(new ArrayList<>(Arrays.asList(tokens)));
}
public Prefix(List<Token> tokens) {
this.tokens= compressLeadingStarts(tokens);
}
public Prefix(List<Token> tokens) {
this.tokens = compressLeadingStarts(new ArrayList<>(tokens));
}
public Prefix slide(Token newToken) {
List<Token> newTokens = new ArrayList<>(this.tokens);
newTokens.remove(0);
newTokens.add(newToken);
return new Prefix(newTokens);
}
public Prefix slide(Token newToken) {
List<Token> newTokens = new ArrayList<>(this.tokens);
newTokens.remove(0);
newTokens.add(newToken);
return new Prefix(newTokens);
}
private List<Token> compressLeadingStarts(List<Token> tokens) {
if(tokens.get(0) == Token.START) {
for (int i = 1; i < tokens.size(); i++) {
if (tokens.get(i) == Token.START) {
tokens.remove(i);
}else {
break;
}
}
}
return tokens;
}
private List<Token> compressLeadingStarts(List<Token> tokens) {
if (tokens.size() > 1 && tokens.get(0) == Token.START) {
Iterator<Token> iterator = tokens.iterator();
for (Token t = iterator.next(); iterator.hasNext(); t = iterator.next()) {
if (t == Token.START) {
iterator.remove();
} else {
break;
}
}
}
return tokens;
}
@Override
public String toString() {
return "P=" + tokens;
}
@Override
public String toString() {
return "P=" + tokens;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + tokens.hashCode();
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + tokens.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Prefix other = (Prefix) obj;
if (!tokens.equals(other.tokens))
return false;
return true;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Prefix other = (Prefix) obj;
if (!tokens.equals(other.tokens))
return false;
return true;
}
}
package directory.passive.markov;
package markov;
import java.util.Collection;
import java.util.Map.Entry;
......
package directory.passive.markov;
package markov;
public class Token {
......
package markov;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DataTests {
Data data;
@Test
public void isInstantiatedWithNew() {
data = new Data();
}
@Nested
public class WhenNew {
Prefix pa = new Prefix(new Token[]{new Token("")});
Token ta = new Token("a");
@BeforeEach
public void create() {
data = new Data();
data.add(pa, ta);
}
@Test
public void canReceieve() {
assertTrue(data.fetch(pa).allPosible().contains(new Token("a")));
assertEquals(1, data.fetch(pa).allPosible().size());
data.add(pa, new Token("b"));
assertTrue(data.fetch(pa).allPosible().contains(new Token("b")));
assertEquals(2, data.fetch(pa).allPosible().size());
}
@Test
public void cantReceieve() {
assertNull(data.fetch(new Prefix(Arrays.asList(new Token("a")))));
}
}
}
\ No newline at end of file
package markov;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LookupTests {
Lookup lookup;
@BeforeEach
public void initLookup() {
lookup = new Lookup();
lookup.add(new Token("a"));
}
@Test
public void add() {
lookup.add(new Token("b"));
assertTrue(lookup.allPosible().contains(new Token("b")));
assertTrue(lookup.allPosible().contains(new Token("a")));
}
@Test
public void forRandom() {
assertEquals(lookup.forRandom(0), new Token("a"));
assertEquals(lookup.forRandom(0.5d), new Token("a"));
// assertEquals(lookup.forRandom(1), new Token("a"));
assertThrows(IllegalArgumentException.class, () -> lookup.forRandom(1));
assertThrows(IllegalArgumentException.class, () -> lookup.forRandom(-1));
assertThrows(IllegalArgumentException.class, () -> lookup.forRandom(1.001d));
assertThrows(IllegalArgumentException.class, () -> lookup.forRandom(2));
lookup.add(new Token("b"));
Token a = lookup.forRandom(0);
Token b = lookup.forRandom(0.9d);
assertNotEquals(a, b);
lookup.add(new Token("c"));
a = lookup.forRandom(0);
b = lookup.forRandom(0.51d);
Token c = lookup.forRandom(0.9d);
assertNotEquals(a, b);
assertNotEquals(a, c);
assertNotEquals(b, c);
}
@Test
public void average() {
assertEquals(lookup.average(), new Token("a"));
lookup.add(new Token("b"));
Token token = lookup.average();
assertTrue(() -> token.equals(new Token("a")) || token.equals(new Token("b")));
lookup.add(new Token("a"));
assertEquals(lookup.average(), new Token("a"));
lookup.add(new Token("b"));
lookup.add(new Token("b"));
assertEquals(lookup.average(), new Token("b"));
}
@Test
public void allPossible() {
assertIterableEquals(lookup.allPosible(),
Arrays.asList(new Token("a")));
lookup.add(new Token("b"));
assertIterableEquals(lookup.allPosible(),
Arrays.asList(new Token("a"), new Token("b")));
lookup.add(new Token("c"));
assertIterableEquals(lookup.allPosible(),
Arrays.asList(new Token("a"), new Token("b"), new Token("c")));
lookup.add(new Token("a"));
assertIterableEquals(lookup.allPosible(),
Arrays.asList(new Token("a"), new Token("b"), new Token("c")));
}
@Test
public void getTotalCounts() {
assertEquals(lookup.getTotalCounts(), 1);
lookup.add(new Token("b"));
assertEquals(lookup.getTotalCounts(), 2);
lookup.add(new Token("b"));
assertEquals(lookup.getTotalCounts(), 3);
}
@Test
public void getDistinctTokens() {
assertEquals(lookup.getDistinctTokens(), 1);
lookup.add(new Token("b"));
assertEquals(lookup.getDistinctTokens(), 2);
lookup.add(new Token("b"));
assertEquals(lookup.getDistinctTokens(), 2);
}
@Test
public void getAmount() {
assertEquals(1, lookup.getAmount(new Token("a")));
lookup.add(new Token("a"));
assertEquals(2, lookup.getAmount(new Token("a")));
lookup.add(new Token("a"));
assertEquals(3, lookup.getAmount(new Token("a")));
lookup.add(new Token("b"));
}
}
\ No newline at end of file
package markov;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class PrefixTests {
Prefix prefix;
@Test
public void create() {
prefix = new Prefix(Arrays.asList(new Token("a")));
assertNotNull(prefix);
assertEquals(new Prefix(Arrays.asList(new Token("a"))), prefix);
}
// FIXME
@Test
public void testStartCompression() {
prefix = new Prefix(Arrays.asList(Token.START));
assertEquals(new Prefix(Arrays.asList(Token.START)), prefix);
assertEquals(new Prefix(Arrays.asList(Token.START, Token.START, Token.START)), prefix);
prefix = new Prefix(Arrays.asList(Token.START, Token.START));
assertEquals(new Prefix(Arrays.asList(Token.START)), prefix);
prefix = new Prefix(Arrays.asList(Token.START, new Token("")));
assertEquals(new Prefix(Arrays.asList(Token.START, new Token(""))), prefix);
prefix = new Prefix(Arrays.asList(Token.START, new Token(""), new Token("")));
assertEquals(new Prefix(Arrays.asList(Token.START, new Token(""), new Token(""))), prefix);
assertEquals(new Prefix(Arrays.asList(Token.START, Token.START, Token.START, new Token(""), new Token(""))),
prefix);
prefix = new Prefix(Arrays.asList(Token.START, Token.START, new Token(""), new Token("")));
assertEquals(new Prefix(Arrays.asList(Token.START, new Token(""), new Token(""))), prefix);
}
@Test
public void testSlide() {
prefix = new Prefix(Arrays.asList(new Token("a")));
Prefix p2 = prefix.slide(new Token("b"));
assertEquals(new Prefix(Arrays.asList(new Token("b"))), p2);
prefix = new Prefix(Arrays.asList(new Token("a"), new Token("b")));
p2 = prefix.slide(new Token("c"));
assertEquals(new Prefix(Arrays.asList(new Token("b"), new Token("c"))), p2);
}
}
\ No newline at end of file
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
package markov;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.Test;
import java.util.Arrays;
import directory.passive.markov.Prefix;
import directory.passive.markov.Token;
import org.junit.jupiter.api.Test;
public class TokenTests {
......
junit.jupiter.testinstance.lifecycle.default = per_class
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment