I was trying to write a test.check property for this, and it seems to have
a found a different bug around `count` and `range`.
```
*clojure-version*
;;=> {:major 1, :minor 7, :incremental 0, :qualifier "beta1"}
(require '[clojure.test.check :as tc])
(require '[clojure.test.check.generators :as gen])
(require '[clojure.test.check.properties :as prop])
(def range-count-is-ceil-of-space-divided-by-step
(prop/for-all [start gen/int
end gen/int
step (gen/such-that #(> % 0)
gen/nat)]
(= (count (range start end step))
(long (Math/ceil (double (/ (max 0 (- end start))
step)))))))
(tc/quick-check 100 range-count-is-ceil-of-space-divided-by-step)
;;=> {:result false, :seed 1429389499284, :failing-size 5, :num-tests 6,
:fail [-2 -1 3], :shrunk {:total-nodes-visited 9, :depth 3, :result false,
:smallest [-1 0 2]}}
(range -1 0 2)
;;=> (-1)
(count (range -1 0 2))
;;=> 0
(tc/quick-check 100 range-count-is-ceil-of-space-divided-by-step)
;;=> {:result false, :seed 1429389621085, :failing-size 4, :num-tests 5,
:fail [-3 4 5], :shrunk {:total-nodes-visited 11, :depth 5, :result false,
:smallest [0 1 2]}}
(range 0 1 2)
;;=> (0)
(count (range 0 1 2))
;;=> 0
```
On Sat, Apr 18, 2015 at 2:32 PM, Mathias De Wachter <
[email protected]> wrote:
> Hi,
>
> this looks like quite a serious bug to me (at least it messed up my
> project):
>
> First taking the code taken from grimoire:
>
> clojurechess.position> (defn range
> "Returns a lazy seq of nums from start (inclusive) to end
> (exclusive), by step, where start defaults to 0, step to 1, and end to
> infinity. When step is equal to 0, returns an infinite sequence of
> start. When start is equal to end, returns empty list."
> {:added "1.0"
> :static true}
> ([] (range 0 Double/POSITIVE_INFINITY 1))
> ([end] (range 0 end 1))
> ([start end] (range start end 1))
> ([start end step]
> (lazy-seq
> (let [b (chunk-buffer 32)
> comp (cond (or (zero? step) (= start end)) not=
> (pos? step) <
> (neg? step) >)]
> (loop [i start]
> (if (and (< (count b) 32)
> (comp i end))
> (do
> (chunk-append b i)
> (recur (+ i step)))
> (chunk-cons (chunk b)
> (when (comp i end)
> (range i end step)))))))))
> WARNING: range already refers to: #'clojure.core/range in namespace:
> clojurechess.position, being replaced by: #'clojurechess.position/range
> #'clojurechess.position/range
> clojurechess.position> (range 0 11 2)
> (0 2 4 6 8 10)
>
> which is what I'd expect and relied on.
>
> Now, looking at the new code:
>
> clojurechess.position> (clojure.repl/source clojure.core/range)
> (defn range
> "Returns a lazy seq of nums from start (inclusive) to end
> (exclusive), by step, where start defaults to 0, step to 1, and end to
> infinity. When step is equal to 0, returns an infinite sequence of
> start. When start is equal to end, returns empty list."
> {:added "1.0"
> :static true}
> ([]
> (iterate inc' 0))
> ([end]
> (if (instance? Long end)
> (clojure.lang.LongRange/create end)
> (clojure.lang.Range/create end)))
> ([start end]
> (if (and (instance? Long start) (instance? Long end))
> (clojure.lang.LongRange/create start end)
> (clojure.lang.Range/create start end)))
> ([start end step]
> (if (and (instance? Long start) (instance? Long end) (instance? Long
> step))
> (clojure.lang.LongRange/create start end step)
> (clojure.lang.Range/create start end step))))
> nil
> clojurechess.position> (clojure.lang.Range/create 0 11 2)
> (0 2 4 6 8 10)
> clojurechess.position> (clojure.lang.LongRange/create 0 11 2)
> (0 2 4 6 8)
> clojurechess.position> (clojure.core/range 0 11 2)
> (0 2 4 6 8)
>
> So the culprit is clojure.lang.LongRange/create.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.