<commit_before>;; Copyright 2015 <NAME> <<EMAIL>>
;;
;; Licensed under the Apache License, Version 2.0 (the "License")
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(ns octet.tests.core
  (:require #?(:cljs [cljs.test :as t]
               :clj  [clojure.test :as t])
            [octet.core :as buf]
            [octet.buffer :as impl])
  #?(:clj
     (:import java.nio.ByteBuffer
              io.netty.buffer.ByteBuf)))

#?(:clj
   (do
     (defn random-bytes
       [^long numbytes]
       (let [sr (java.util.Random.)
             buffer (byte-array numbytes)]
         (.nextBytes sr buffer)
         buffer))

     (defn equals?
       [^bytes a ^bytes b]
       (java.util.Arrays/equals a b)))

   :cljs
   (do
     (defn random-bytes
       [^long numbytes]
       (let [buffer (js/Int8Array. numbytes)]
         (doseq [i (range numbytes)]
           (aset buffer i (rand-int 10)))
         buffer))

     (defn equals?
       [^bytes a ^bytes b]
       (if (not= (.-length a) (.-length b))
         false
         (reduce (fn [acc i]
                   (if (not= (aget a i) (aget b i))
                     (reduced false)
                     true))
                 true
                 (range (.-length a)))))))


#?(:clj
   (t/deftest allocate-heap-nio-buffer
     (let [buffer (buf/allocate 16)]
       (t/is (not (.isDirect buffer)))
       (t/is (instance? ByteBuffer buffer)))))

#?(:clj
   (t/deftest allocate-direct-nio-buffer
     (let [buffer (buf/allocate 16 {:type :direct})]
       (t/is (.isDirect buffer))
       (t/is (instance? ByteBuffer buffer)))))

#?(:clj
   (t/deftest allocate-heap-netty-buffer
     (let [buffer (buf/allocate 16 {:type :heap :impl :netty})]
       (t/is (not (.isDirect buffer)))
       (t/is (instance? ByteBuf buffer)))))

#?(:clj
   (t/deftest allocate-direct-netty-buffer
     (let [buffer (buf/allocate 16 {:type :direct :impl :netty})]
       (t/is (.isDirect buffer))
       (t/is (instance? ByteBuf buffer)))))
#?(:clj
   (t/deftest indexed-specs-write-with-offset
     (let [spec (buf/spec (buf/int32))
           buffer (buf/allocate 12)
           data [500]]
       (t/is (= (buf/write! buffer data spec {:offset 3}) 4))
       (t/is (= (.getInt buffer 3) 500)))))

#?(:cljs
   (t/deftest allocate-direct-es6-buffer
     (let [buffer (buf/allocate 16 {:impl :es6})]
       (t/is (instance? js/DataView buffer)))))<commit_msg>Minor change on tests.<commit_after>;; Copyright 2015 <NAME> <<EMAIL>>
;;
;; Licensed under the Apache License, Version 2.0 (the "License")
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(ns octet.tests.core
  (:require #?(:cljs [cljs.test :as t]
               :clj  [clojure.test :as t])
            [octet.core :as buf]
            [octet.buffer :as impl])
  #?(:clj
     (:import java.nio.ByteBuffer
              io.netty.buffer.ByteBuf)))

#?(:clj
   (do
     (defn random-bytes
       [^long numbytes]
       (let [sr (java.util.Random.)
             buffer (byte-array numbytes)]
         (.nextBytes sr buffer)
         buffer))

     (defn equals?
       [^bytes a ^bytes b]
       (java.util.Arrays/equals a b)))

   :cljs
   (do
     (defn random-bytes
       [^long numbytes]
       (let [buffer (js/Int8Array. numbytes)]
         (doseq [i (range numbytes)]
           (aset buffer i (rand-int 10)))
         buffer))

     (defn equals?
       [^bytes a ^bytes b]
       (if (not= (.-length a) (.-length b))
         false
         (reduce (fn [acc i]
                   (if (not= (aget a i) (aget b i))
                     (reduced false)
                     true))
                 true
                 (range (.-length a)))))))


#?(:clj
   (t/deftest allocate-heap-nio-buffer
     (let [buffer (buf/allocate 16)]
       (t/is (not (.isDirect buffer)))
       (t/is (instance? ByteBuffer buffer)))))

#?(:clj
   (t/deftest allocate-direct-nio-buffer
     (let [buffer (buf/allocate 16 {:type :direct})]
       (t/is (.isDirect buffer))
       (t/is (instance? ByteBuffer buffer)))))

#?(:clj
   (t/deftest allocate-heap-netty-buffer
     (let [buffer (buf/allocate 16 {:type :heap :impl :netty})]
       (t/is (not (.isDirect buffer)))
       (t/is (instance? ByteBuf buffer)))))

#?(:clj
   (t/deftest allocate-direct-netty-buffer
     (let [buffer (buf/allocate 16 {:type :direct :impl :netty})]
       (t/is (.isDirect buffer))
       (t/is (instance? ByteBuf buffer)))))

#?(:clj
   (t/deftest indexed-specs-write-with-offset
     (let [spec (buf/spec (buf/int32))
           buffer (buf/allocate 12)
           data [500]]
       (t/is (= (buf/write! buffer data spec {:offset 3}) 4))
       (t/is (= (.getInt buffer 3) 500)))))

#?(:cljs
   (t/deftest allocate-direct-es6-buffer
     (let [buffer (buf/allocate 16 {:impl :es6})]
       (t/is (instance? js/DataView buffer)))))
{'fraction_non_alphanumeric': 0.15123239436619718, 'fraction_numerical': 0.01267605633802817, 'mean_word_length': 2.2315130830489194, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2458315', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1819, 'n_words': 576}