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
......@@ -36,6 +37,9 @@ public class Lookup {
}
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()) {
......@@ -57,6 +61,10 @@ public class Lookup {
.findFirst().get().getKey();
}
public Set<Token> allPosible() {
return this.tokens.keySet();
}
@Override
public String toString() {
return "Lookup [tokens= "
......
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 {
......@@ -40,9 +40,10 @@ public class Parser_first_version {
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];
}
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);
}
......
package directory.passive.markov;
package markov;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
// FIXME rename
......@@ -9,11 +10,11 @@ public class Prefix {
private final List<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) {
this.tokens= compressLeadingStarts(tokens);
this.tokens = compressLeadingStarts(new ArrayList<>(tokens));
}
public Prefix slide(Token newToken) {
......@@ -24,11 +25,12 @@ public class Prefix {
}
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 {
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;
}
}
......
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