1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-13 17:06:49 +00:00

Added some tests (suite 5)

This commit is contained in:
J.-F. Cap 2019-02-25 18:49:04 +01:00
parent febfefa4b2
commit fb5119bf43
3 changed files with 69 additions and 20 deletions

View File

@ -1,3 +1,5 @@
# naive matrix implementation for testing typed array
(defmacro printf [& xs] ['print ['string/format (splice xs)]])
(defn matrix [nrow ncol] {:nrow nrow :ncol ncol :array (tarray/new :float64 (* nrow ncol))})
@ -66,24 +68,16 @@
(matrix/set** A i j i))
(matrix/print A)
(print "tarray")
(tarray/print (A :array))
(printf "properties:\n%p" (tarray/properties (A :array)))
(printf "row properties:\n%p" (tarray/properties (matrix/row A 1)))
# test marshalling
(def a (tarray/new :float64 20))
(set (a 0) math/pi)
(set (a 1) 1234)
(def b (unmarshal (marshal a)))
(printf "%p" (tarray/properties b))
(print (b 0))
(print (b 1))
(for i 0 nr
(printf "row properties:[%i]\n%p" i (tarray/properties (matrix/row A i))))
(for i 0 nc
(printf "col properties:[%i]\n%p" i (tarray/properties (matrix/column A i))))

View File

@ -205,7 +205,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
index = (size_t)janet_unwrap_integer(key); \
TA_View_##type * array=(TA_View_##type *)p; \
if (index >= array->size) { \
janet_panic("typed array out of bound"); \
janet_panic("typed array out of bounds"); \
} \
array->data[index*array->stride]=(ta_##type##_t)janet_unwrap_number(value); \
}
@ -222,7 +222,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
ta_buffer_init(buf,buf_size); \
} \
if (buf->size<buf_size) { \
janet_panic("bad buffer size"); \
janet_panicf("bad buffer size : %i bytes allocated < %i required",buf->size,buf_size); \
} \
tview->buffer=buf; \
tview->stride=stride; \
@ -488,7 +488,7 @@ static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) {
ps += step_src;
}
} else {
janet_panic("typed array copy out of bound");
janet_panic("typed array copy out of bounds");
}
return janet_wrap_nil();
}
@ -518,7 +518,7 @@ static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) {
ps += step_src;
}
} else {
janet_panic("typed array swap out of bound");
janet_panic("typed array swap out of bounds");
}
return janet_wrap_nil();
}

55
test/suite5.janet Normal file
View File

@ -0,0 +1,55 @@
# Copyright (c) 2019 Calvin Rose & contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
(import test/helper :prefix "" :exit true)
(start-suite 5)
# some tests typed array
(assert-no-error
"create some typed array"
(do
(def a (tarray/new :float64 10))
(def b (tarray/new :float64 5 2 0 a))
(def c (tarray/new :uint32 20))
))
(assert-no-error
"create some typed array from buffer"
(do
(def buf (tarray/buffer (+ 64 (* (+ 1 (* (- 10 1) 2)) 8))))
(def b (tarray/new :float64 10 2 64 buf))))
(def a (tarray/new :float64 10))
(def b (tarray/new :float64 5 2 0 a))
(assert-no-error
"fill tarray"
(for i 0 (tarray/length a)
(set (a i) i)))
(assert (= (tarray/buffer a) (tarray/buffer b)) "tarray views pointing same buffer")
(assert (= (a 2) (b 1) ) "tarray views pointing same buffer")
(assert (= ((tarray/slice b) 3) (b 3) (a 6) 6) "tarray slice")
(assert (= ((tarray/slice b 1) 2) (b 3) (a 6) 6) "tarray slice")
(assert (= ((unmarshal (marshal b)) 3) (b 3)) "marshal")
(end-suite)