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

log*/0,exp*/0: add more math functions #96

Merged
merged 1 commit into from
Aug 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,67 @@ protected double f(final double v) {
return Math.log10(v) / Math.log10(2);
}
}

@AutoService(Function.class)
@BuiltinFunction("log/0")
public static class LogFunction extends MathFunction {
@Override
protected double f(final double v) {
return Math.log(v);
}
}

@AutoService(Function.class)
@BuiltinFunction("log10/0")
public static class Log10Function extends MathFunction {
@Override
protected double f(final double v) {
return Math.log10(v);
}
}

@AutoService(Function.class)
@BuiltinFunction(value = "log1p/0", version = "[1.6, )")
public static class Log1pFunction extends MathFunction {
@Override
protected double f(final double v) {
return Math.log1p(v);
}
}

@AutoService(Function.class)
@BuiltinFunction("exp/0")
public static class ExpFunction extends MathFunction {
@Override
protected double f(final double v) {
return Math.exp(v);
}
}

@AutoService(Function.class)
@BuiltinFunction(value = "expm1/0", version = "[1.6, )")
public static class Expm1Function extends MathFunction {
@Override
protected double f(final double v) {
return Math.expm1(v);
}
}

@AutoService(Function.class)
@BuiltinFunction("exp2/0")
public static class Exp2Function extends MathFunction {
@Override
protected double f(final double v) {
return Math.pow(2, v);
}
}

@AutoService(Function.class)
@BuiltinFunction(value = "exp10/0", version = "[1.6, )")
public static class Exp10Function extends MathFunction {
@Override
protected double f(final double v) {
return Math.pow(10, v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,55 @@ private static int orderValue(final JsonNodeType type) {
return value;
}

protected int compareNumberNode(final JsonNode o1, final JsonNode o2) {
final double a = o1.asDouble();
final double b = o2.asDouble();
if (Double.isNaN(a))
return -1;
if (Double.isNaN(b))
return 1;
return Double.compare(a, b);
}

protected int compareArrayNode(final JsonNode o1, final JsonNode o2) {
final int s1 = o1.size();
final int s2 = o2.size();
final int s = Math.min(s1, s2);
for (int i = 0; i < s; ++i) {
final int rr = compare(o1.get(i), o2.get(i));
if (rr != 0)
return rr;
}
return Integer.compare(s1, s2);
}

protected int compareObjectNode(final JsonNode o1, final JsonNode o2) {
final List<String> names1 = Lists.newArrayList(o1.fieldNames());
final List<String> names2 = Lists.newArrayList(o2.fieldNames());

// compare by keys
Collections.sort(names1);
Collections.sort(names2);
final int s = Math.min(names1.size(), names2.size());
for (int i = 0; i < s; ++i) {
final int rr = names1.get(i).compareTo(names2.get(i));
if (rr != 0)
return rr;
}
final int rr = Integer.compare(names1.size(), names2.size());
if (rr != 0)
return rr;

// compare by values (keys are sorted alphabetically)
for (final String name : names1) {
final int rrr = compare(o1.get(name), o2.get(name));
if (rrr != 0)
return rrr;
}

return 0;
}

// null
// false
// true
Expand All @@ -67,55 +116,18 @@ public int compare(final JsonNode o1, final JsonNode o2) {
return Boolean.compare(o1.asBoolean(), o2.asBoolean());

if (type == JsonNodeType.NUMBER) {
final double a = o1.asDouble();
final double b = o2.asDouble();
if (Double.isNaN(a))
return -1;
if (Double.isNaN(b))
return 1;
return Double.compare(a, b);
return compareNumberNode(o1, o2);
}

if (type == JsonNodeType.STRING || type == JsonNodeType.BINARY)
return o1.asText().compareTo(o2.asText());

if (type == JsonNodeType.ARRAY) {
final int s1 = o1.size();
final int s2 = o2.size();
final int s = Math.min(s1, s2);
for (int i = 0; i < s; ++i) {
final int rr = compare(o1.get(i), o2.get(i));
if (rr != 0)
return rr;
}
return Integer.compare(s1, s2);
return compareArrayNode(o1, o2);
}

if (type == JsonNodeType.OBJECT) {
final List<String> names1 = Lists.newArrayList(o1.fieldNames());
final List<String> names2 = Lists.newArrayList(o2.fieldNames());

// compare by keys
Collections.sort(names1);
Collections.sort(names2);
final int s = Math.min(names1.size(), names2.size());
for (int i = 0; i < s; ++i) {
final int rr = names1.get(i).compareTo(names2.get(i));
if (rr != 0)
return rr;
}
final int rr = Integer.compare(names1.size(), names2.size());
if (rr != 0)
return rr;

// compare by values (keys are sorted alphabetically)
for (final String name : names1) {
final int rrr = compare(o1.get(name), o2.get(name));
if (rrr != 0)
return rrr;
}

return 0;
return compareObjectNode(o1, o2);
}

throw new IllegalArgumentException("Unknown JsonNodeType: " + type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.thisptr.jackson.jq;

import java.util.Iterator;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.JsonNode;

import net.thisptr.jackson.jq.internal.misc.JsonNodeComparator;

public class JsonNodeComparatorForTests extends JsonNodeComparator {
private static final long serialVersionUID = 1L;

private final boolean strictFieldOrder;
private final double numericalErrors;

public JsonNodeComparatorForTests(final boolean strictFieldOrder, final double numericalErrors) {
this.strictFieldOrder = strictFieldOrder;
this.numericalErrors = numericalErrors;
}

@Override
protected int compareNumberNode(final JsonNode o1, final JsonNode o2) {
if (Math.abs(o1.doubleValue() - o2.doubleValue()) < numericalErrors)
return 0;
return super.compareNumberNode(o1, o2);
}

@Override
protected int compareObjectNode(final JsonNode o1, final JsonNode o2) {
if (strictFieldOrder) {
final Iterator<Entry<String, JsonNode>> it1 = o1.fields();
final Iterator<Entry<String, JsonNode>> it2 = o2.fields();
while (it1.hasNext() && it2.hasNext()) {
final Entry<String, JsonNode> entry1 = it1.next();
final Entry<String, JsonNode> entry2 = it2.next();

final int r0 = entry1.getKey().compareTo(entry2.getKey());
if (r0 != 0)
return r0;

final int r1 = compare(entry1.getValue(), entry2.getValue());
if (r1 != 0)
return r1;
}
return Integer.compare(o1.size(), o2.size());
} else {
return super.compareObjectNode(o1, o2);
}
}
}
24 changes: 16 additions & 8 deletions jackson-jq/src/test/java/net/thisptr/jackson/jq/JsonQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -32,12 +33,12 @@
import com.google.common.reflect.ClassPath.ResourceInfo;

import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.misc.JsonNodeComparator;
import net.thisptr.jackson.jq.internal.misc.VersionRangeDeserializer;
import net.thisptr.jackson.jq.test.evaluator.CachedEvaluator;
import net.thisptr.jackson.jq.test.evaluator.Evaluator;
import net.thisptr.jackson.jq.test.evaluator.Evaluator.Result;
import net.thisptr.jackson.jq.test.evaluator.TrueJqEvaluator;
import net.thisptr.jackson.jq.test.misc.ComparableJsonNode;

public class JsonQueryTest {
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
Expand Down Expand Up @@ -66,6 +67,9 @@ public static class TestCase {
@JsonProperty("ignore_true_jq_behavior")
public boolean ignoreTrueJqBehavior = false;

@JsonProperty("numerical_errors")
public double numericalErrors = 0;

@JsonInclude(Include.NON_NULL)
@JsonProperty("v")
@JsonDeserialize(using = VersionRangeDeserializer.class)
Expand Down Expand Up @@ -132,10 +136,6 @@ static Stream<String> defaultTestCases() throws Throwable {
});
}

private static List<ComparableJsonNode> wrap(final List<JsonNode> values) {
return ComparableJsonNode.wrap(values);
}

private static Map<Version, Boolean> hasJqCache = new ConcurrentHashMap<>();
private static Evaluator cachedJqEvaluator;

Expand All @@ -159,18 +159,24 @@ private void test(final TestCase tc, final Version version) throws Throwable {
return;
}

final Comparator<JsonNode> comparator = new JsonNodeComparatorForTests(false, tc.numericalErrors);

if (!tc.ignoreTrueJqBehavior && hasJqCache.computeIfAbsent(version, v -> TrueJqEvaluator.hasJq(v))) {
final Result result = cachedJqEvaluator.evaluate(tc.q, tc.in, version, 2000L);
assumeThat(result.error).as("%s", command).isNull();
assumeThat(wrap(tc.out)).as("%s", command).isEqualTo(wrap(result.values));
assumeThat(tc.out).as("%s", command)
.usingElementComparator(comparator)
.isEqualTo(result.values);
}

boolean failed = false;
try {
final JsonQuery q = JsonQuery.compile(tc.q, version);
final List<JsonNode> out = new ArrayList<>();
q.apply(scope, tc.in, out::add);
assertThat(wrap(out)).as("%s", command).isEqualTo(wrap(tc.out));
assertThat(out).as("%s", command)
.usingElementComparator(comparator)
.isEqualTo(tc.out);

// JsonQuery.compile($.toString()).toString() === $.toString()
final String s1 = q.toString();
Expand All @@ -181,7 +187,9 @@ private void test(final TestCase tc, final Version version) throws Throwable {
final JsonQuery q1 = JsonQuery.compile(s1, version);
final List<JsonNode> out1 = new ArrayList<>();
q1.apply(scope, tc.in, out1::add);
assertThat(wrap(out1)).as("bad tostring: %s", command).isEqualTo(wrap(tc.out));
assertThat(out1).as("bad tostring: %s", command)
.usingElementComparator(comparator)
.isEqualTo(tc.out);
} catch (final Throwable e) {
failed = true;
if (!tc.failing) {
Expand Down

This file was deleted.

Loading