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: ...@@ -48,9 +48,9 @@ cache:
- master - master
# Validate merge requests using JDK7 # Validate merge requests using JDK7
validate:jdk7: #validate:jdk7:
<<: *validate # <<: *validate
image: maven:3.3.9-jdk-7 # image: maven:3.3.9-jdk-7
# Validate merge requests using JDK8 # Validate merge requests using JDK8
validate:jdk8: validate:jdk8:
...@@ -58,9 +58,9 @@ validate:jdk8: ...@@ -58,9 +58,9 @@ validate:jdk8:
image: maven:3.3.9-jdk-8 image: maven:3.3.9-jdk-8
# Verify merge requests using JDK7 # Verify merge requests using JDK7
verify:jdk7: #verify:jdk7:
<<: *verify # <<: *verify
image: maven:3.3.9-jdk-7 # image: maven:3.3.9-jdk-7
# Verify merge requests using JDK8 # Verify merge requests using JDK8
verify:jdk8: verify:jdk8:
......
...@@ -7,22 +7,35 @@ ...@@ -7,22 +7,35 @@
<groupId>passive.directory</groupId> <groupId>passive.directory</groupId>
<artifactId>markov</artifactId> <artifactId>markov</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.0.3</junit.jupiter.version> <junit.jupiter.version>5.0.3</junit.jupiter.version>
<junit.vintage.version>${junit.version}.3</junit.vintage.version> <junit.platform.version>1.0.3</junit.platform.version>
<junit.platform.version>1.1.0-RC1</junit.platform.version>
</properties> </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> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version> <version>3.7.0</version>
<configuration> <configuration>
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>
...@@ -38,15 +51,6 @@ ...@@ -38,15 +51,6 @@
<include>**/*Tests.java</include> <include>**/*Tests.java</include>
<include>**/*TestCase.java</include> <include>**/*TestCase.java</include>
</includes> </includes>
<properties>
<!-- <includeTags>fast</includeTags> -->
<excludeTags>slow</excludeTags>
<!--
<configurationParameters>
junit.jupiter.conditions.deactivate = *
</configurationParameters>
-->
</properties>
</configuration> </configuration>
<dependencies> <dependencies>
<dependency> <dependency>
...@@ -59,18 +63,4 @@ ...@@ -59,18 +63,4 @@
</plugins> </plugins>
</build> </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> </project>
\ No newline at end of file
package directory.passive.markov; package markov;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Collection; import java.util.Collection;
......
package directory.passive.markov; package markov;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
......
package directory.passive.markov; package markov;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
// FIXME rename // FIXME rename
public class Lookup { public class Lookup {
public static final Lookup empty = new Lookup() { public static final Lookup empty = new Lookup() {
public Token forRandom(double random) { public Token forRandom(double random) {
return Token.EMPTY; 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() { int getTotalCounts() {
return tokens.values().stream().mapToInt(i -> i.intValue()).sum(); return tokens.values().stream().mapToInt(i -> i.intValue()).sum();
} }
int getDistinctTokens() { int getDistinctTokens() {
return tokens.size(); return tokens.size();
} }
int getAmount(Token t) { int getAmount(Token t) {
return tokens.getOrDefault(t, 0); return tokens.getOrDefault(t, 0);
} }
public void add(Token t) { public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0); Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1); tokens.put(t, i + 1);
} }
public Token forRandom(double random) { public Token forRandom(double random) {
int id = (int) Math.floor(random * getTotalCounts()); if(random <0 || random >=1) {
int i = 0; throw new IllegalArgumentException("expected double [0; 1)");
for (Entry<Token, Integer> entry : tokens.entrySet()) { }
i += entry.getValue(); int id = (int) Math.floor(random * getTotalCounts());
if (id < i) { int i = 0;
return entry.getKey(); for (Entry<Token, Integer> entry : tokens.entrySet()) {
} i += entry.getValue();
} if (id < i) {
throw new IllegalStateException(String.format( return entry.getKey();
"failed to find a random token with seed %d within %s", id, }
this)); }
throw new IllegalStateException(String.format(
"failed to find a random token with seed %d within %s", id,
this));
} }
public Token average() { public Token average() {
return tokens return tokens
.entrySet().stream().sorted((e1, e2) -> Integer .entrySet().stream().sorted((e1, e2) -> Integer
.compare(e2.getValue(), e1.getValue())) .compare(e2.getValue(), e1.getValue()))
.findFirst().get().getKey(); .findFirst().get().getKey();
} }
@Override public Set<Token> allPosible() {
public String toString() { return this.tokens.keySet();
return "Lookup [tokens= " }
+ tokens.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue(), @Override
e1.getValue())) public String toString() {
.map(e -> String.format("%d*%s", e.getValue(), return "Lookup [tokens= "
e.getKey())) + tokens.entrySet().stream()
.collect(Collectors.joining(", ")) .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 class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(input);
System.exit(0);
int prefixLength = 2; int prefixLength = 2;
Data data = new Parser_first_version(prefixLength).parse(input); Data data = new Parser_first_version(prefixLength).parse(input);
Builder b = new Builder(prefixLength, data); Builder b = new Builder(prefixLength, data);
......
package directory.passive.markov; package markov;
public interface Parser { public interface Parser {
} }
package directory.passive.markov; package markov;
public class ParserStreamStyle { public class ParserStreamStyle {
......
package directory.passive.markov; package markov;
public class Parser_first_version { public class Parser_first_version {
private final int prefix_length; private final int prefix_length;
public Parser_first_version(int prefix_length) { public Parser_first_version(int prefix_length) {
super(); super();
this.prefix_length = prefix_length; this.prefix_length = prefix_length;
} }
public Data parse(String input) { public Data parse(String input) {
Data data = new Data(); Data data = new Data();
for (String line : input.split("\n")) { for (String line : input.split("\n")) {
line = line.replace("Kai☺UWE empfiehlt ", ""); line = line.replace("Kai☺UWE empfiehlt ", "");
Token[] tokens = tokenize(line); Token[] tokens = tokenize(line);
for (int i = 0 + prefix_length; i < tokens.length; i++) { for (int i = 0 + prefix_length; i < tokens.length; i++) {
Prefix p = getPrefix(tokens, i); Prefix p = getPrefix(tokens, i);
Token t = getToken(tokens, i); Token t = getToken(tokens, i);
data.add(p, t); data.add(p, t);
} }
} }
return data; return data;
} }
private Token[] tokenize(String line) { private Token[] tokenize(String line) {
String[] strings = line.split(" "); String[] strings = line.split(" ");
Token[] tokens = new Token[strings.length + prefix_length + 1]; Token[] tokens = new Token[strings.length + prefix_length + 1];
int i = 0; int i = 0;
for (; i < prefix_length; i++) { for (; i < prefix_length; i++) {
tokens[i] = Token.START; tokens[i] = Token.START;
} }
for (; i < strings.length + prefix_length; i++) { for (; i < strings.length + prefix_length; i++) {
tokens[i] = new Token(strings[i - prefix_length]); tokens[i] = new Token(strings[i - prefix_length]);
} }
tokens[i] = Token.END; tokens[i] = Token.END;
return tokens; return tokens;
} }
private Prefix getPrefix(Token[] tokens, int index) { private Prefix getPrefix(Token[] tokens, int index) {
Token[] prefixTokens = new Token[prefix_length]; Token[] prefixTokens = new Token[prefix_length];
for (int i = 0; i < prefix_length; i++) { System.arraycopy(tokens, index - prefix_length + 1, prefixTokens, 0, prefix_length);
prefixTokens[i] = tokens[index - prefix_length + i]; // for (int i = 0; i < prefix_length; i++) {
} // prefixTokens[i] = tokens[index - prefix_length + i];
return new Prefix(prefixTokens); // }
} return new Prefix(prefixTokens);
}
private Token getToken(Token[] tokens, int i) {
return tokens[i]; private Token getToken(Token[] tokens, int i) {
} return tokens[i];
}
} }
package directory.passive.markov; package markov;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
// FIXME rename // FIXME rename
public class Prefix { public class Prefix {
private final List<Token> tokens; private final List<Token> tokens;
public Prefix(Token[] tokens) { public Prefix(Token[] tokens) {
this.tokens = compressLeadingStarts(Arrays.asList(tokens)); this.tokens = compressLeadingStarts(new ArrayList<>(Arrays.asList(tokens)));
} }
public Prefix(List<Token> tokens) { public Prefix(List<Token> tokens) {
this.tokens= compressLeadingStarts(tokens); this.tokens = compressLeadingStarts(new ArrayList<>(tokens));
} }
public Prefix slide(Token newToken) { public Prefix slide(Token newToken) {
List<Token> newTokens = new ArrayList<>(this.tokens); List<Token> newTokens = new ArrayList<>(this.tokens);
newTokens.remove(0); newTokens.remove(0);
newTokens.add(newToken); newTokens.add(newToken);
return new Prefix(newTokens); return new Prefix(newTokens);
} }
private List<Token> compressLeadingStarts(List<Token> tokens) { private List<Token> compressLeadingStarts(List<Token> tokens) {
if(tokens.get(0) == Token.START) { if (tokens.size() > 1 && tokens.get(0) == Token.START) {
for (int i = 1; i < tokens.size(); i++) { Iterator<Token> iterator = tokens.iterator();
if (tokens.get(i) == Token.START) { for (Token t = iterator.next(); iterator.hasNext(); t = iterator.next()) {
tokens.remove(i); if (t == Token.START) {
}else { iterator.remove();
break; } else {
} break;
} }
} }
return tokens; }
} return tokens;
}
@Override @Override
public String toString() { public String toString() {
return "P=" + tokens; return "P=" + tokens;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + tokens.hashCode(); result = prime * result + tokens.hashCode();
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Prefix other = (Prefix) obj; Prefix other = (Prefix) obj;
if (!tokens.equals(other.tokens)) if (!tokens.equals(other.tokens))
return false; return false;
return true; return true;
} }
} }
package directory.passive.markov; package markov;
import java.util.Collection; import java.util.Collection;
import java.util.Map.Entry; import java.util.Map.Entry;
......
package directory.passive.markov; package markov;
public class Token { 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; package markov;
import static org.junit.Assert.assertNotEquals;
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 org.junit.jupiter.api.Test;
import directory.passive.markov.Token;
public class TokenTests { 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