Welcome! Please see the About page for a little more info on how this works.

0 votes
in core.rrb-vector by
I'm seeing some strange behaviour which I've been unable to find a small failing case for.


(defn quicksort [v]
  (if (<= (count v) 1)
    v
    (let [[x & xs] v]
      (rrb/catvec (quicksort (filterv #(<= % x) xs))
                  [x]
                  (quicksort (filterv #(> % x) xs))))))


We can check this implementation with test.check


(defn ascending? [coll]
  (every? (fn [[a b]] (<= a b))
          (partition 2 1 coll)))

(def property
  (prop/for-all [v (gen/vector gen/int)]
    (let [s (quicksort v)]
      (and (= (count v) (count s))
           (ascending? s)))))

(tc/quick-check 10000 property)

;; => {:result true, :num-tests 10000, :seed 1440948212354}


(I've bumped the number of checks to 1e6 but test.check was not able to find a failing case)

Still, for some reason I get a "ClassCastException: clojure.lang.PersistentVector$Node cannot be cast to [I" for the attached 1193 element vector.

{}
(quicksort (read-string (slurp "failing-1193.edn")))
{}

5 Answers

0 votes
by
_Comment made by: jonase_

The expression


(quicksort (shuffle (range 1e6)))


1. succeeds (~20%)
2. throws ArrayIndexOutOfBoundsException (~20%)


#error {
 :cause "33"
 :via
 [{:type java.lang.ArrayIndexOutOfBoundsException
   :message "33"
   :at [clojure.core.rrb_vector.rrbt.Vector$fn__3827 invoke "rrbt.clj" 621]}]
 :trace
 [[clojure.core.rrb_vector.rrbt.Vector$fn__3827 invoke "rrbt.clj" 621]
  [clojure.core.rrb_vector.rrbt.Vector arrayFor "rrbt.clj" 620]
  [clojure.core.rrb_vector.rrbt.VecSeq chunkedNext "rrbt.clj" 111]
  [clojure.core.rrb_vector.rrbt.VecSeq next "rrbt.clj" 74]
  [clojure.lang.RT next "RT.java" 674]
  [clojure.core$next__4112 invoke "core.clj" 64]
  [clojure.core$nthnext invoke "core.clj" 3038]
  [clojure.core$print_sequential invoke "core_print.clj" 49]
  [clojure.core$fn__5859 invoke "core_print.clj" 206]
  [clojure.lang.MultiFn invoke "MultiFn.java" 233]
  [clojure.tools.nrepl.middleware.pr_values$pr_values$fn$reify__600 send "pr_values.clj" 35]
  [sun.reflect.GeneratedMethodAccessor13 invoke nil -1]
  [sun.reflect.DelegatingMethodAccessorImpl invoke "DelegatingMethodAccessorImpl.java" 43]
  [java.lang.reflect.Method invoke "Method.java" 606]
  [clojure.lang.Reflector invokeMatchingMethod "Reflector.java" 93]
  [clojure.lang.Reflector invokeInstanceMethod "Reflector.java" 28]
  [clojure.tools.nrepl.middleware.load_file$wrap_load_file$fn$reify__787 send "load_file.clj" 93]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__623$fn__634 invoke "interruptible_eval.clj" 82]
  [clojure.main$repl$read_eval_print__7099 invoke "main.clj" 241]
  [clojure.main$repl$fn__7108 invoke "main.clj" 258]
  [clojure.main$repl doInvoke "main.clj" 258]
  [clojure.lang.RestFn invoke "RestFn.java" 1523]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__623 invoke "interruptible_eval.clj" 58]
  [clojure.lang.AFn applyToHelper "AFn.java" 152]
  [clojure.lang.AFn applyTo "AFn.java" 144]
  [clojure.core$apply invoke "core.clj" 630]
  [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1868]
  [clojure.lang.RestFn invoke "RestFn.java" 425]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
  [clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__665$fn__668 invoke "interruptible_eval.clj" 191]
  [clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__660 invoke "interruptible_eval.clj" 159]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1145]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 615]
  [java.lang.Thread run "Thread.java" 745]]}


3. Throws the above mentioned ClassCastException (~60%)


#error {
 :cause "clojure.lang.PersistentVector$Node cannot be cast to [I"
 :via
 [{:type clojure.lang.Compiler$CompilerException
   :message "java.lang.ClassCastException: clojure.lang.PersistentVector$Node cannot be cast to [I, compiling:(/Users/jonasenlund/dev/clojure/the-other-datastructures/src/the_other_datastructures/rrb.clj:221:1)"
   :at [clojure.lang.Compiler load "Compiler.java" 7239]}
  {:type java.lang.ClassCastException
   :message "clojure.lang.PersistentVector$Node cannot be cast to [I"
   :at [clojure.lang.Numbers ints "Numbers.java" 1396]}]
 :trace
 [[clojure.lang.Numbers ints "Numbers.java" 1396]
  [clojure.core.rrb_vector.nodes$fold_tail invoke "nodes.clj" 265]
  [clojure.core.rrb_vector.nodes$fold_tail invoke "nodes.clj" 268]
  [clojure.core.rrb_vector.rrbt$splice_rrbts invoke "rrbt.clj" 1357]
  [clojure.core.rrb_vector.rrbt.Vector splicev "rrbt.clj" 928]
  [clojure.core.rrb_vector$catvec invoke "rrb_vector.clj" 54]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 219]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 219]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 219]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 219]
  [the_other_datastructures.rrb$eval4682$quicksort__4683 invoke "rrb.clj" 217]
  [the_other_datastructures.rrb$eval4921 invoke "rrb.clj" 221]
  [clojure.lang.Compiler eval "Compiler.java" 6782]
  [clojure.lang.Compiler load "Compiler.java" 7227]
  [user$eval4909 invoke "form-init3543508351311666483.clj" 1]
  [clojure.lang.Compiler eval "Compiler.java" 6782]
  [clojure.lang.Compiler eval "Compiler.java" 6745]
  [clojure.core$eval invoke "core.clj" 3081]
  [clojure.main$repl$read_eval_print__7099$fn__7102 invoke "main.clj" 240]
  [clojure.main$repl$read_eval_print__7099 invoke "main.clj" 240]
  [clojure.main$repl$fn__7108 invoke "main.clj" 258]
  [clojure.main$repl doInvoke "main.clj" 258]
  [clojure.lang.RestFn invoke "RestFn.java" 1523]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__623 invoke "interruptible_eval.clj" 58]
  [clojure.lang.AFn applyToHelper "AFn.java" 152]
  [clojure.lang.AFn applyTo "AFn.java" 144]
  [clojure.core$apply invoke "core.clj" 630]
  [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1868]
  [clojure.lang.RestFn invoke "RestFn.java" 425]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
  [clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__665$fn__668 invoke "interruptible_eval.clj" 191]
  [clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__660 invoke "interruptible_eval.clj" 159]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1145]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 615]
  [java.lang.Thread run "Thread.java" 745]]}
0 votes
by
_Comment made by: jonase_

I'm not sure if this is related but the following expression throws an NPE (changing rrb/catvec to clojure.core/into yields the expected result [])


(nth (iterate pop (nth (iterate #(rrb/catvec [0] %) []) 963)) 963)



#error {
 :cause nil
 :via
 [{:type clojure.lang.Compiler$CompilerException
   :message "java.lang.NullPointerException, compiling:(/Users/jonasenlund/dev/clojure/the-other-datastructures/src/the_other_datastructures/rrb.clj:201:5)"
   :at [clojure.lang.Compiler load "Compiler.java" 7239]}
  {:type java.lang.NullPointerException
   :message nil
   :at [clojure.core.rrb_vector.nodes$reify__3483 regular "nodes.clj" 46]}]
 :trace
 [[clojure.core.rrb_vector.nodes$reify__3483 regular "nodes.clj" 46]
  [clojure.core.rrb_vector.rrbt.Vector popTail "rrbt.clj" 686]
  [clojure.core.rrb_vector.rrbt.Vector popTail "rrbt.clj" 727]
  [clojure.core.rrb_vector.rrbt.Vector pop "rrbt.clj" 487]
  [clojure.lang.RT pop "RT.java" 716]
  [clojure.core$pop invoke "core.clj" 1409]
  [clojure.lang.Iterate first "Iterate.java" 47]
  [clojure.lang.Iterate next "Iterate.java" 54]
  [clojure.lang.RT nthFrom "RT.java" 867]
  [clojure.lang.RT nth "RT.java" 840]
  [the_other_datastructures.rrb$eval7254 invoke "rrb.clj" 201]
  [clojure.lang.Compiler eval "Compiler.java" 6782]
  [clojure.lang.Compiler load "Compiler.java" 7227]
  [user$eval7241 invoke "form-init121723447646604580.clj" 1]
  [clojure.lang.Compiler eval "Compiler.java" 6782]
  [clojure.lang.Compiler eval "Compiler.java" 6745]
  [clojure.core$eval invoke "core.clj" 3081]
  [clojure.main$repl$read_eval_print__7099$fn__7102 invoke "main.clj" 240]
  [clojure.main$repl$read_eval_print__7099 invoke "main.clj" 240]
  [clojure.main$repl$fn__7108 invoke "main.clj" 258]
  [clojure.main$repl doInvoke "main.clj" 258]
  [clojure.lang.RestFn invoke "RestFn.java" 1523]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__623 invoke "interruptible_eval.clj" 58]
  [clojure.lang.AFn applyToHelper "AFn.java" 152]
  [clojure.lang.AFn applyTo "AFn.java" 144]
  [clojure.core$apply invoke "core.clj" 630]
  [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1868]
  [clojure.lang.RestFn invoke "RestFn.java" 425]
  [clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
  [clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__665$fn__668 invoke "interruptible_eval.clj" 191]
  [clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__660 invoke "interruptible_eval.clj" 159]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1145]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 615]
  [java.lang.Thread run "Thread.java" 745]]}
0 votes
by
_Comment made by: michalmarczyk_

Thanks again for the bug report and many thanks for the follow-ups! Would you be interested in wrapping any of these test cases in a patch?

As for a fix – I can see roughly what is happening with the original problem case, what remains is to track down exactly where the miscalculation happens… I'll cut a new release as soon as that's sorted.
0 votes
by

Comment made by: jonase

I added failing tests for the ClassCastException and the NullPointerException. For some reason, I'm only able to reproduce the ArrayIndexOutOfBoundsException at the repl

0 votes
by
Reference: https://clojure.atlassian.net/browse/CRRBV-12 (reported by jonase)
...