Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cool stuff :) #54

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>xyz.cliserkad</groupId>
<artifactId>smp</artifactId>
<packaging>jar</packaging>
<version>0.0.19</version>
<version>0.0.20</version>
<name>Simple Data Format</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -35,7 +35,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.4.0</version>
<version>3.5.0</version>
<configuration>
<failIfNoTests>true</failIfNoTests>
</configuration>
Expand Down
5 changes: 4 additions & 1 deletion src/main/antlr4/SimpleLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ fragment LETTER : UPLETTER | DNLETTER;
fragment ALPHANUM : LETTER | DIGIT;
fragment UNDERSCORE : '_';
CHAR_LIT: '\'' . '\'';
KEYNAME : .+;

// the specification says you can use any character that is not : but this is much more restrictive
// this might be changed later to be more permissive, but eliminating more characters makes parsing less ambiguous
KEY_FRAGMENT: ~[-+_:; \n\t\r{}[\],.\\/*'"];
3 changes: 2 additions & 1 deletion src/main/antlr4/SimpleParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ decimal: NEGATIVE? DIGIT+ DOT DIGIT+;

value: object | list | bool | integer | decimal | STRING_LIT | CHAR_LIT;
list: BRACE_OPEN (value SEPARATOR)* BRACE_CLOSE;
pair: KEYNAME (ASSIGN value) PAIR_END;
key: KEY_FRAGMENT+?;
pair: key ASSIGN value PAIR_END;
object: BODY_OPEN pair* BODY_CLOSE;

root: object*;
3 changes: 2 additions & 1 deletion src/test/java/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import xyz.cliserkad.smp.Verifier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static xyz.cliserkad.smp.SimpleEncoder.encode;

public class EncodeTest {
Expand All @@ -13,7 +14,7 @@ public class EncodeTest {
@Test
public void testCar() throws IllegalAccessException {
assertEquals(EXPECTED, encode(new Car()));
assert Verifier.verifyOrPrint(encode(new Car()));
assertTrue(Verifier.verifyOrPrint(encode(new Car())));
}

}
65 changes: 65 additions & 0 deletions src/test/java/MathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package test.java;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static xyz.cliserkad.util.Math.pow;

public class MathTest {

@Test
public void test0RaisedTo0Is1() {
assertEquals(1, pow(0, 0));
}

@Test
public void test0RaisedTo1Is0() {
assertEquals(0, pow(0, 1));
}

@Test
public void test1RaisedTo0Is1() {
assertEquals(1, pow(1, 0));
}

@Test
public void test1RaisedTo1Is1() {
assertEquals(1, pow(1, 1));
}

@Test
public void test1RaisedTo2Is1() {
assertEquals(1, pow(1, 2));
}

@Test
public void test2RaisedTo0Is1() {
assertEquals(1, pow(2, 0));
}

@Test
public void test2RaisedTo1Is2() {
assertEquals(2, pow(2, 1));
}

@Test
public void test2RaisedTo2Is4() {
assertEquals(4, pow(2, 2));
}

@Test
public void test2RaisedTo3Is8() {
assertEquals(8, pow(2, 3));
}

@Test
public void test2RaisedTo4Is16() {
assertEquals(16, pow(2, 4));
}

@Test
public void testLargeExponentDoesntCauseStackOverflow() {
assertEquals(-2659239065858430293L, pow(3, Integer.MAX_VALUE));
}

}
2 changes: 1 addition & 1 deletion src/test/java/RangeIterationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RangeIterationTest {
@Test
public void testRange() {
Range range = new Range(0, 10);
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(EXPECTED_OUTPUT_1.length());
for(int i : range) {
sb.append(i);
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/TestDuo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test.java;

import org.junit.jupiter.api.Test;
import xyz.cliserkad.util.Duo;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestDuo {

@Test
public void testPrinting() {
Duo<String, String> duo = new Duo<>("Hello", "World");
assertEquals(duo.toString(), "{\n a: \"Hello\";\n b: \"World\";\n}");
}

@Test
public void testEquality() {
Duo<String, String> duo1 = new Duo<>("Hello", "World");
Duo<String, String> duo2 = new Duo<>("Hello", "World");
assertEquals(duo1, duo2);
}

@Test
public void testInequality() {
Duo<String, String> duo1 = new Duo<>("Hello", "World");
Duo<String, String> duo2 = new Duo<>("Hello", "World!");
assertEquals(duo1.equals(duo2), false);
}

@Test
public void testHashing() {
Duo<String, String> duo1 = new Duo<>("Hello", "World");
Duo<String, String> duo2 = new Duo<>("Hello", "World");
assertEquals(duo1.hashCode(), duo2.hashCode());
}

}
60 changes: 60 additions & 0 deletions src/test/java/TestSerialVersionUIDGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test.java;

import org.junit.jupiter.api.Test;
import xyz.cliserkad.util.*;

import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestSerialVersionUIDGenerator {

@Test
public void testUIDsAreUnique() {
BestList<Class<?>> classesToTest = new BestList<>();

classesToTest.add(BaseConverter.class);
classesToTest.add(BestList.class);
classesToTest.add(CheckedFunction.class);
classesToTest.add(Copier.class);
classesToTest.add(DiskLoc.class);
classesToTest.add(Duo.class);
classesToTest.add(ElapseTimer.class);
classesToTest.add(ExceptionPack.class);
classesToTest.add(Levenshtein.class);
classesToTest.add(MergeSort.class);
classesToTest.add(NamedObject.class);
classesToTest.add(NamedObjectList.class);
classesToTest.add(Path.class);
classesToTest.add(PlaceHolder.class);
classesToTest.add(Range.class);
classesToTest.add(SerialCopier.class);
classesToTest.add(SerialVersionUIDGenerator.class);
classesToTest.add(Sizes.class);
classesToTest.add(StringList.class);
classesToTest.add(Text.class);
classesToTest.add(TrackedMap.class);
classesToTest.add(Trio.class);
classesToTest.add(Tuple.class);
classesToTest.add(TupleBase.class);
classesToTest.add(UnimplementedException.class);
classesToTest.add(Union.class);
classesToTest.add(Union2.class);
classesToTest.add(Union2Extendable.class);
classesToTest.add(Union3.class);
classesToTest.add(Union3Extendable.class);
classesToTest.add(UnionMember.class);
classesToTest.add(Vector2i.class);
classesToTest.add(Zipper.class);

Set<Long> serialUIDs = new HashSet<>();
for(Class<?> clazz : classesToTest) {
long serialUID = SerialVersionUIDGenerator.generateSerialVersionUID(clazz);
assertTrue(serialUIDs.add(serialUID));
}
assertEquals(serialUIDs.size(), classesToTest.size());
}

}
38 changes: 38 additions & 0 deletions src/test/java/TestTrio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package test.java;

import org.junit.jupiter.api.Test;
import xyz.cliserkad.util.Trio;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class TestTrio {

@Test
public void testPrinting() {
Trio<String, String, String> trio = new Trio<>("Hello", "World", "!");
assertEquals(trio.toString(), "{\n a: \"Hello\";\n b: \"World\";\n c: \"!\";\n}");
}

@Test
public void testEquality() {
Trio<String, String, String> trio1 = new Trio<>("Hello", "World", "!");
Trio<String, String, String> trio2 = new Trio<>("Hello", "World", "!");
assertEquals(trio1, trio2);
}

@Test
public void testInequality() {
Trio<String, String, String> trio1 = new Trio<>("Hello", "World", "!");
Trio<String, String, String> trio2 = new Trio<>("Hello", "World", "?");
assertFalse(trio1.equals(trio2));
}

@Test
public void testHashing() {
Trio<String, String, String> trio1 = new Trio<>("Hello", "World", "!");
Trio<String, String, String> trio2 = new Trio<>("Hello", "World", "!");
assertEquals(trio1.hashCode(), trio2.hashCode());
}

}
57 changes: 57 additions & 0 deletions src/test/java/TestUnion3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package test.java;

import org.junit.jupiter.api.Test;
import xyz.cliserkad.util.Union3;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestUnion3 {

public Union3<String, Integer, Double> data;

public String lambdaMatch() {
return data.match((str -> {
return str;
}), (i -> {
return "Integers get multiplied: " + i * 5;
}), (dbl -> {
return "Doubles get addition: " + (dbl + 3.14);
}));
}

public String switchMatch() {
return switch(data) {
case Union3.A<String, ?, ?> a -> a.getValue();
case Union3.B<?, Integer, ?> b -> "Integers get multiplied: " + b.getValue() * 5;
case Union3.C<?, ?, Double> c -> "Doubles get addition: " + (c.getValue() + 3.14);
};
}

@Test
public void testWithString() {
TestUnion3 example = new TestUnion3();
example.data = new Union3.A<>("Hello, World!");
assertEquals(example.lambdaMatch(), example.switchMatch());
assertEquals(example.lambdaMatch(), "Hello, World!");
assertEquals(example.switchMatch(), "Hello, World!");
}

@Test
public void testWithInteger() {
TestUnion3 example = new TestUnion3();
example.data = new Union3.B<>(2);
assertEquals(example.lambdaMatch(), example.switchMatch());
assertEquals(example.lambdaMatch(), "Integers get multiplied: 10");
assertEquals(example.switchMatch(), "Integers get multiplied: 10");
}

@Test
public void testWithDouble() {
TestUnion3 example = new TestUnion3();
example.data = new Union3.C<>(3.14);
assertEquals(example.lambdaMatch(), example.switchMatch());
assertEquals(example.lambdaMatch(), "Doubles get addition: 6.28");
assertEquals(example.switchMatch(), "Doubles get addition: 6.28");
}

}
7 changes: 6 additions & 1 deletion src/xyz/cliserkad/smp/BadTemplateException.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package xyz.cliserkad.smp;

import java.io.Serial;

import static xyz.cliserkad.util.SerialVersionUIDGenerator.generateSerialVersionUID;

public class BadTemplateException extends Exception {

private static final long serialVersionUID = -7085178496879726407L;
@Serial
private static final long serialVersionUID = generateSerialVersionUID(BadTemplateException.class);
private final Class<?> template;
private final ReflectiveOperationException cause;

Expand Down
10 changes: 6 additions & 4 deletions src/xyz/cliserkad/smp/ParseData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import xyz.cliserkad.util.Path;

import java.io.Serial;

import static xyz.cliserkad.util.SerialVersionUIDGenerator.generateSerialVersionUID;

public class ParseData extends PathMap<Object> {

/**
*
*/
private static final long serialVersionUID = -4121653623739479034L;
@Serial
private static final long serialVersionUID = generateSerialVersionUID(ParseData.class);
public static final String DEFAULT_STRING = "";
public static final char DEFAULT_CHAR = ' ';
public static final int DEFAULT_INT = 0;
Expand Down
21 changes: 11 additions & 10 deletions src/xyz/cliserkad/smp/PathMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import xyz.cliserkad.util.Path;

import java.io.Serial;
import java.util.HashMap;

import static xyz.cliserkad.util.SerialVersionUIDGenerator.generateSerialVersionUID;

public class PathMap<V> extends HashMap<Path, V> {

private static final long serialVersionUID = 1504298304584057564L;
@Serial
private static final long serialVersionUID = generateSerialVersionUID(PathMap.class);

/**
* Retrieves the V related to specified Path. Returns null if no V is related. Will auto-cast String to Path.
Expand All @@ -16,15 +20,12 @@ public class PathMap<V> extends HashMap<Path, V> {
*/
@Override
public V get(final Object key) {
if(key instanceof String) {
return super.get(new Path((String) key));
} else if(key instanceof Path) {
return super.get(key);
} else if(key instanceof String[]) {
return super.get(new Path((String[]) key));
} else {
return null;
}
return switch(key) {
case String s -> super.get(new Path(s));
case Path ignored -> super.get(key);
case String[] strings -> super.get(new Path(strings));
case null, default -> null;
};
}

/**
Expand Down
Loading
Loading