Skip to content

Commit

Permalink
buf-test
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Dec 7, 2024
1 parent e933ec5 commit bc6a903
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
18 changes: 9 additions & 9 deletions src/fast_edn/EdnParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class EdnParser {
public final boolean throwOnEOF;
public final Object eofValue;

public Reader reader = null;
public char[] readBuf;
public int readPos = 0;
public int readLen = 0;
public char[] accumulator;
public int accumulatorLength;
public Reader reader = null;
public char[] readBuf;
public int readPos = 0;
public int readLen = 0;
public char[] accumulator;
public int accumulatorLength;

public EdnParser(int bufferSize, ILookup dataReaders, IFn defaultDataReader, boolean throwOnEOF, Object eofValue) {
this.dataReaders = dataReaders;
Expand Down Expand Up @@ -696,7 +696,7 @@ public Number finalizeInt(char[] buf, int start, int radixPos, int end) {
}

if (buf[start] == '0') {
if (end - start >= 3 && buf[start + 1] == 'x' || buf[start + 1] == 'X') {
if (end - start >= 3 && (buf[start + 1] == 'x' || buf[start + 1] == 'X')) {
radix = 16;
start = start + 2;
} else if (end - start >= 2) {
Expand All @@ -711,10 +711,10 @@ public Number finalizeInt(char[] buf, int start, int radixPos, int end) {
return BigInt.fromBigInteger(bn);
}

String str = new String(buf, start, end - start);
try {
return Long.valueOf(Long.parseLong(new String(buf, start, end - start), radix));
return Long.valueOf(Long.parseLong(str, radix));
} catch (Exception e) {
String str = new String(buf, start, end - start);
BigInteger bn = new BigInteger(str, radix);
return BigInt.fromBigInteger(bn);
}
Expand Down
53 changes: 53 additions & 0 deletions test/fast_edn/buf_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns fast-edn.buf-test
(:refer-clojure :exclude [rand-int])
(:require
[clojure.edn]
[clojure.data.generators :as gen]
[clojure.string :as str]
[clojure.test :refer [deftest is are testing]]
[clojure.test.generative :refer [defspec]]
[clojure.test.generative.runner :as runner]
[fast-edn.core :as edn]
[fast-edn.generators :as cgen]))

(defn rand-int
([n]
(rand-int 0 n))
([min max]
(+ min (clojure.core/rand-int (- max min)))))

(defn rand-coll []
(let [coll (distinct
(repeatedly (* (rand-int 0 10) 2) #(gen/one-of cgen/ednable-scalar)))]
(if (odd? (count coll))
(recur)
coll)))

(defn stringify [coll]
(let [[open close] (rand-nth [["[" "]"]
["(" ")"]
["{" "}"]
["#{" "}"]])]
(str/join
(interleave
(concat ["" open] (map pr-str coll) [close])
(repeatedly #(str/join (repeat (rand-int 1 25) " ")))))))

(defn rand-str []
(stringify (rand-coll)))

(defspec reader-should-not-break
(fn [str]
(edn/read-string {:buffer 10} str))
[^{:tag fast-edn.buf-test/rand-str} str]
(when-not (= (clojure.edn/read-string str) %)
(throw (ex-info "Value cannot roundtrip, see ex-data" {:printed str :read %}))))

(def cpus
8)

(def time-ms
10000)

(deftest buf-test
(runner/run cpus time-ms #'reader-should-not-break))
12 changes: 3 additions & 9 deletions test/fast_edn/gen_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

(ns fast-edn.gen-test
(:require
[fast-edn.generators :as cgen]
[fast-edn.core :as edn]
[clojure.test :refer [deftest is are testing]]
[clojure.test.generative :refer [defspec]]
[clojure.test.generative.runner :as runner]))
[clojure.test.generative.runner :as runner]
[fast-edn.core :as edn]
[fast-edn.generators :as cgen]))

(defn roundtrip
"Print an object and read it back as edn. Returns rather than throws
Expand Down Expand Up @@ -53,9 +53,3 @@

(deftest no-roundtrip-test
(runner/run cpus time-ms #'fast-edn.gen-test/types-that-should-not-roundtrip))

(comment
(try
(runner/run 10 10000 #'fast-edn.gen-test/types-that-should-roundtrip)
(catch clojure.lang.ExceptionInfo e
(:input (ex-data e)))))
7 changes: 5 additions & 2 deletions test/fast_edn/generators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
gen/date
gen/ratio
gen/bigint
gen/bigdec])
gen/bigdec
gen/double ;; added by fast-edn
])

(defn- call-through
"Recursively call x until it doesn't return a function."
Expand All @@ -108,7 +110,8 @@
(call-through (rand-nth ednable-scalars)))

(def ednable-collections
[[gen/vec [ednable-scalars]]
[[gen/list [ednable-scalars]] ;; added by fast-edn
[gen/vec [ednable-scalars]]
[gen/set [ednable-scalars]]
[gen/hash-map [ednable-scalars ednable-scalars]]])

Expand Down

0 comments on commit bc6a903

Please sign in to comment.