Skip to content
Open
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
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Java CI

on: [push]

jobs:
build:
timeout-minutes: 5
strategy:
matrix:
jdk: ['11', '17', '19']
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5
- name: Set up JDK ${{ matrix.jdk }}
uses: actions/setup-java@v5
with:
java-version: ${{ matrix.jdk }}
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn test
- name: Configure settings.xml
run: |
mkdir -p ~/.m2
echo "<settings><servers><server><id>clojars</id><username>${{ secrets.CLOJARS_USER }}-clojars</username><password>${{ secrets.CLOJARS_PASSWORD }}</password></server></servers></settings>" > ~/.m2/settings.xml
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest] # macOS-latest, windows-latest]
java-version: ["8", "11", "17", "21"]
java-version: ["11", "17", "21"]
distribution: ["temurin", "corretto"]
profile: ["test-direct", "test-no-direct"]
runs-on: ${{ matrix.os }}
Expand Down
38 changes: 38 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;; https://clojure.org/dev/developing_patches#_run_an_individual_test
{:paths ["test"
"target/test-classes"]
:deps
{org.clojure/clojure {:local/root "."
:deps/manifest :pom} #_{:mvn/version "RELEASE"}
org.clojure/test.check {:mvn/version "1.1.1"}
org.clojure/test.generative {:mvn/version "1.0.0"}}
:aliases
{:dbg {:classpath-overrides {org.clojure/clojure "target/classes"}
:extra-deps {criterium/criterium {:mvn/version "0.4.4"}}}
:cognitest {:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "b3fd0d2"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test
:exec-args {:dirs ["test"]
:patterns [;; FIXME clojure.test-clojure.ns-libs has a test that is sensitive to loading order
;; FIXME clojure.test-clojure.java-interop doesn't seem to work on JDK 17 (untested on others)
;; regex ref: https://stackoverflow.com/a/2387072
"^((?!(clojure.test-clojure.ns-libs|clojure.test-clojure.java-interop)).)*$"
]}}
:test-example-script {:jvm-opts [;; from build.xml
"-Dclojure.test-clojure.exclude-namespaces=#{clojure.test-clojure.compilation.load-ns clojure.test-clojure.ns-libs-load-later}"
"-Dclojure.compiler.direct-linking=true"]
:main-opts ["-e" "(load-file,\"src/script/run_test.clj\")"]}
:test-generative-script {:jvm-opts [;; from build.xml
"-Dclojure.compiler.direct-linking=true"]
:main-opts ["-e" "(load-file,\"src/script/run_test_generative.clj\")"]}

:kaocha {:extra-deps {lambdaisland/kaocha {:mvn/version "1.60.977"}}
:exec-fn kaocha.runner/exec-fn
:exec-args {;:watch? true
:tests [{:id :unit
:test-paths ["test"]
:ns-patterns [".*"]}]
:reporter kaocha.report/dots
;; :plugins [:kaocha.plugin/profiling :kaocha.plugin/notifier]
}}}}
6 changes: 3 additions & 3 deletions src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -893,11 +893,11 @@
bounds, nth throws an exception unless not-found is supplied. nth
also works for strings, Java arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
{:inline (fn [c i & nf] `(. clojure.lang.RT (nth ~c ~i ~@nf)))
{:inline (fn [c i & nf] `(. clojure.lang.RT (nthImpl ~c ~i ~@nf)))
:inline-arities #{2 3}
:added "1.0"}
([coll index] (. clojure.lang.RT (nth coll index)))
([coll index not-found] (. clojure.lang.RT (nth coll index not-found))))
([coll index] (. clojure.lang.RT (nthImpl coll index)))
([coll index not-found] (. clojure.lang.RT (nthImpl coll index not-found))))

(defn <
"Returns non-nil if nums are in monotonically increasing order,
Expand Down
30 changes: 30 additions & 0 deletions src/jvm/clojure/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,24 @@ static public Object nth(Object coll, int n){
return nthFrom(Util.ret1(coll, coll = null), n);
}

static int castNthIdx(Object o) {
if (o instanceof Number) {
return intCast(o);
}
throw new UnsupportedOperationException(
"nth idx not supported on this type: " + o.getClass().getSimpleName());
}

/**
* Only for use in `clojure.core/nth`.
*/
static public Object nthImpl(Object coll, Object n){
int idx = castNthIdx(n);
if(coll instanceof Indexed)
return ((Indexed) coll).nth(idx);
return nthFrom(Util.ret1(coll, coll = null), idx);
}

static Object nthFrom(Object coll, int n){
if(coll == null)
return null;
Expand Down Expand Up @@ -960,6 +978,18 @@ static public Object nth(Object coll, int n, Object notFound){
return nthFrom(coll, n, notFound);
}

/**
* Only for use in `clojure.core/nth`.
*/
static public Object nthImpl(Object coll, Object n, Object notFound){
int idx = castNthIdx(n);
if(coll instanceof Indexed) {
Indexed v = (Indexed) coll;
return v.nth(idx, notFound);
}
return nthFrom(coll, idx, notFound);
}

static Object nthFrom(Object coll, int n, Object notFound){
if(coll == null)
return notFound;
Expand Down
10 changes: 10 additions & 0 deletions test/clojure/test_clojure/sequences.clj
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,16 @@
(is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. []) -1))) ; ???
(is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) -1))) ; ???

;; CLJ-2822
(is (thrown? UnsupportedOperationException (nth 1 [1 2 3])))
(is (try ^{:line 12345} (nth 1 [1 2 3])
(catch UnsupportedOperationException e
(let [line (->> (.getStackTrace e)
(drop-while #(= "clojure.lang.RT" (.getClassName ^StackTraceElement %)))
(first)
(#(.getLineNumber ^StackTraceElement %)))]
(= 12345 line)))))

(are [x y] (= x y)
(nth '(1) 0) 1
(nth '(1 2 3) 0) 1
Expand Down