Public Vars

Back

into-array (clj)

(source)

function

(into-array aseq) (into-array type aseq)
Returns an array with components set to the values in aseq. The array's component type is type if provided, or the type of the first value in aseq if present, or Object. All values in aseq must be compatible with the component type. Class objects for the primitive types can be obtained using, e.g., Integer/TYPE.

Examples

clojure/core.typed
(ns clojure.core.typed.test-rt
  (:require [clojure.core.typed :as t]
            [cljs.core.typed :as tcljs]
            [clojure.core.typed.errors :as err]
            [clojure.java.io :as io])
  (:use clojure.test))

(deftest checking-ops
  (is (err/tc-error-thrown?
        (t/load-if-needed)))
  (is (err/tc-error-thrown?
        (t/reset-caches)))
  (is (err/tc-error-thrown?
        (t/method-type 'foo)))
  (is (err/tc-error-thrown?
        (t/into-array> 'foo 'bar [1])))
  (is (err/tc-error-thrown?
        (t/cf 1)))
  (is (err/tc-error-thrown?
        (t/check-form* 1)))
  (is (err/tc-error-thrown?
        (t/check-form-info 1)))
  (is (err/tc-error-thrown?
        (t/check-ns 'foo)))
  (is (err/tc-error-thrown?
        (t/check-ns-info 'foo)))
  (is (err/tc-error-thrown?
        (t/statistics ['foo])))
  (is (err/tc-error-thrown?
        (t/var-coverage))))
mikera/core.matrix
(ns clojure.core.matrix.test-arrays
  (:refer-clojure :exclude [vector?])
  (:require [clojure.core.matrix.compliance-tester :as compliance]
            [clojure.core.matrix :as m]
            ;[clojure.core.matrix.utils :refer :all]
            [clojure.test :refer [deftest testing is]]
            [clojure.core.matrix.macros :refer [array?]]))

(deftest compliance-tests
  (compliance/instance-test (int-array [1 2 3]))
  (compliance/instance-test (float-array [1 2 3]))
  (compliance/instance-test (long-array []))
  (compliance/instance-test (char-array [\a \b \c]))
  (compliance/instance-test (object-array [(double-array [1 2 3])]))
  (compliance/instance-test (object-array [(short-array (map short [1 2 3]))]))
  (compliance/instance-test (into-array (Class/forName "[D")
                                        [(double-array [1 2])
                                         (double-array [3 4])])))
mikera/core.matrix
(ns clojure.core.matrix.test-double-array
  (:refer-clojure :exclude [==])
  (:require [clojure.core.matrix.protocols :as mp]
            [clojure.core.matrix.impl.pprint :as pprint]
            [clojure.core.matrix.dataset :as ds]
            [clojure.core.matrix.compliance-tester]
            [clojure.core.matrix :refer :all]
            [clojure.core.matrix.operators :refer [==]]
            [clojure.core.matrix.macros :refer [error]]
            [clojure.core.matrix.macros-clj :refer [error?]]
            [clojure.test :refer :all]))

(deftest test-assign
  (testing "assign from a persistent vector"
    (let [da (double-array [1 2])]
      (assign! da [2 3])
      (is (= [2.0 3.0] (seq da)))))
  (testing "assign from an array"
    (let [da (double-array [1 2])]
      (assign! da (double-array [2 4]))
      (is (= [2.0 4.0] (seq da)))))
  (testing "assign from a Number array"
    (let [da (double-array [1 2])]
      (mp/assign-array! da (into-array Number [2 5]))
      (is (= [2.0 5.0] (seq da))))))
typedclojure/typedclojure
(ns clojure.core.typed.test.jvm.array
  (:require [clojure.core.typed :refer [ann check-ns into-array> cf print-env ann-form]
             :as t]
            [typed.clj.checker.test-utils :refer :all]
            [clojure.test :refer :all]
            ))

(deftest array-test
  (is-clj (= (Class/forName "[I") 
             (eval `(class (t/into-array> ~'int [1])))))
  (is-clj (clj (= (Class/forName "[Ljava.lang.Object;") 
                  (eval `(class (t/into-array> (t/U nil ~'int) [1]))))))
  (is-clj (clj (= (Class/forName "[Ljava.lang.Number;") 
                  (eval `(class (t/into-array> (t/U nil Number) [1]))))))
  (is-clj (clj (= (Class/forName "[Ljava.lang.Object;") 
                  (eval `(class (t/into-array> (t/U t/Sym Number) [1]))))))
  (is-clj (= (Class/forName "[Ljava.lang.Object;") 
             (eval `(class (t/into-array> Object (t/U t/Sym Number) [1])))))
  )

(deftest array-primitive-hint-test
  (is-tc-e (let [^ints a (t/into-array> int [(int 1)])]
             (alength a))))

; FIXME this is wrong, should not just be nil
#_(deftest array-first-test
    (is-cf (let [a (clojure.core.typed/into-array> Long [1 2])]
             (first a))))

(deftest array-reflection-test
  (is-tc-e (fn make-process [script]
             {:post [%]}
             (let [^Runtime r (Runtime/getRuntime)
                   _ (assert r)
                   ^"[Ljava.lang.String;" arr (into-array> String ["echo 'hello'"])]
               (.exec r arr)))
           [String -> java.lang.Process]))
originrose/think.image
(ns think.image.byte-color-test
  (:require [clojure.test :refer :all]
            [clojure.core.matrix.macros :refer [c-for]])
  (:import [think.image ByteColor]
           [java.awt.image BufferedImage]))


(deftest convert-gray-scale
  (let [test-color (ByteColor. 255 128 64)
        color-array (into-array ByteColor (repeat 16 test-color))
        gray-color (ByteColor. (unchecked-byte
                                (+ (* 255.0 0.2989)
                                   (* 128.0 0.5870)
                                   (* 64.0 0.1140)
                                   0.5)))
        answer (into-array ByteColor (repeat 16 gray-color))
        byte-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB Byte/TYPE BufferedImage/TYPE_BYTE_GRAY ByteColor/Black)
        int-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB Integer/TYPE BufferedImage/TYPE_BYTE_GRAY ByteColor/Black)
        byte-color-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB ByteColor BufferedImage/TYPE_BYTE_GRAY ByteColor/Black)
        int-result-color
        (ByteColor/convertAllocate int-result BufferedImage/TYPE_BYTE_GRAY ByteColor BufferedImage/TYPE_BYTE_GRAY ByteColor/Black)
        byte-result-color
        (ByteColor/convertAllocate byte-result BufferedImage/TYPE_BYTE_GRAY ByteColor BufferedImage/TYPE_BYTE_GRAY ByteColor/Black)]

(deftest convert-alpha-blend
  (let [test-color (ByteColor. 255 128 64 128)
        blend-color (ByteColor. 0)
        result (ByteColor. 128 64 32 255)
        color-array (into-array ByteColor (repeat 16 test-color))
        answer (into-array ByteColor (repeat 16 result))
        byte-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB Byte/TYPE BufferedImage/TYPE_INT_RGB ByteColor/Black)
        int-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB Integer/TYPE BufferedImage/TYPE_INT_RGB ByteColor/Black)
        byte-color-result
        (ByteColor/convertAllocate color-array BufferedImage/TYPE_INT_ARGB ByteColor BufferedImage/TYPE_INT_RGB ByteColor/Black)
        int-result-color
        (ByteColor/convertAllocate int-result BufferedImage/TYPE_INT_RGB ByteColor BufferedImage/TYPE_INT_RGB ByteColor/Black)
        byte-result-color
        (ByteColor/convertAllocate byte-result BufferedImage/TYPE_INT_RGB ByteColor BufferedImage/TYPE_INT_RGB ByteColor/Black)]
    (is (= (vec answer) (vec byte-color-result)))
    (is (= (vec answer) (vec int-result-color)))
    (is (= (vec answer) (vec byte-result-color)))))