-
Notifications
You must be signed in to change notification settings - Fork 40
Add tests for partition
#893
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
Open
dgr
wants to merge
4
commits into
jank-lang:main
Choose a base branch
from
dgr:dgr-partition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| (ns clojure.core-test.partition | ||
| (:require [clojure.test :as t :refer [are deftest is testing]] | ||
| [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p])) | ||
|
|
||
| (when-var-exists partition | ||
| (deftest test-partition | ||
|
|
||
| ;; Docstring: | ||
| ;; [n coll] [n step coll] [n step pad coll] | ||
| ;; Returns a lazy sequence of lists of n items each, at offsets step | ||
| ;; apart. If step is not supplied, defaults to n, i.e. the partitions | ||
| ;; do not overlap. If a pad collection is supplied, use its elements as | ||
| ;; necessary to complete last partition upto n items. In case there are | ||
| ;; not enough padding elements, return a partition with less than n items. | ||
|
dgr marked this conversation as resolved.
|
||
|
|
||
| (testing "arity 2 - (partition n coll)" | ||
|
dgr marked this conversation as resolved.
|
||
| (let [s (partition 2 (range 10))] | ||
| (is (p/lazy-seq? s)) | ||
| (is (not (realized? s))) | ||
| (is (= '((0 1) (2 3) (4 5) (6 7) (8 9)) s))) | ||
|
|
||
| ;; The partition size, `n` should be able to be a number of all sorts. | ||
| (is (= '((0 1) (2 3) (4 5) (6 7) (8 9)) (partition 2N (range 10)))) | ||
|
|
||
| ;; These fail in JVM and some other dialects but should probably | ||
| ;; pass. Interestingly, since CLJS uses doubles as its only | ||
| ;; numeric type, they pass in CLJS. | ||
| ;; TODO: reenable these for JVM and other dialects when they pass | ||
| #?@(:cljs [(is (= '((0 1) (2 3) (4 5) (6 7) (8 9)) (partition 2.0 (range 10)))) | ||
| (is (= '((0 1) (2 3) (4 5) (6 7) (8 9)) (partition 2.0M (range 10))))]) | ||
|
|
||
| ;; Infinite range | ||
| (is (= '((0 1) (2 3) (4 5) (6 7)) (take 4 (partition 2 (range))))) | ||
|
|
||
| ;; empty list and nil for the collection | ||
| (is (= '() (partition 2 '()))) | ||
| (is (= '() (partition 2 nil)))) | ||
|
|
||
| (testing "arity 3 - (partition n step coll)" | ||
| ;; Step by 1 instead of default 2 | ||
| ;; Note that (range 6) stops before allowing a partial partition. | ||
| ;; Note that we tried testing non-positive steps, but that led | ||
| ;; to an infinite loop within `partition`. | ||
| (is (= '((0 1) (1 2) (2 3) (3 4) (4 5)) (partition 2 1 (range 6)))) | ||
|
|
||
| ;; Test steps of different number types | ||
| (is (= '((0 1) (1 2) (2 3) (3 4) (4 5)) (partition 2 1N (range 6)))) | ||
| (is (= '((0 1) (1 2) (2 3) (3 4) (4 5)) (partition 2 1.0 (range 6)))) | ||
| (is (= '((0 1) (1 2) (2 3) (3 4) (4 5)) (partition 2 1.0M (range 6)))) | ||
|
|
||
| ;; Try an infinite range | ||
| (is (= '((0 1) (1 2) (2 3) (3 4) (4 5)) (take 5 (partition 2 1 (range))))) | ||
|
|
||
| ;; empty list and nil | ||
| (is (= '() (partition 2 1 '()))) | ||
| (is (= '() (partition 2 1 nil)))) | ||
|
|
||
| (testing "arity 4 - (partition n step pad coll)" | ||
| ;; Use padding for the last element | ||
| (is (= '((0 1 2) (1 2 3) (2 3 4) (3 4 :a)) (partition 3 1 [:a :b :c] (range 5)))) | ||
| (is (= '((0 1 2) (3 4 5) (6 7 8) (9 :a :b)) (partition 3 3 [:a :b :c] (range 10)))) | ||
|
|
||
| ;; If the padding collection is short, then the last partition is short | ||
| (is (= '((0 1 2) (3 4 5) (6 7 8) (9 :a)) (partition 3 3 [:a] (range 10)))) | ||
|
|
||
| ;; If the padding is empty or `nil`, then the last partition has | ||
| ;; no padding added at all | ||
| (is (= '((0 1 2) (3 4 5) (6 7 8) (9)) (partition 3 3 [] (range 10)))) | ||
| (is (= '((0 1 2) (3 4 5) (6 7 8) (9)) (partition 3 3 '() (range 10)))) | ||
| (is (= '((0 1 2) (3 4 5) (6 7 8) (9)) (partition 3 3 nil (range 10)))) | ||
|
|
||
| ;; empty list and nil | ||
| (is (= '() (partition 3 1 [:a :a :a] '()))) | ||
| (is (= '() (partition 3 1 [:a :a :a] nil)))))) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.