-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy patharrays.lisp
72 lines (67 loc) · 3.12 KB
/
arrays.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;; Copyright 2013 Google Inc.
;;;
;;; 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.
(define-test basic-array-stuff
;; We make an 8x8 array and then fill it with a checkerboard pattern.
(let ((chess-board (make-array '(8 8))))
;; (DOTIMES (X 8) ...) will iterate with X taking values from 0 to 7.
(dotimes (x 8)
(dotimes (y 8)
;; AREF stands for "array reference".
(setf (aref chess-board x y) (if (evenp (+ x y)) :black :white))))
(assert-true (typep chess-board 'array))
(assert-equal ____ (aref chess-board 0 0))
(assert-equal ____ (aref chess-board 2 3))
;; The function ARRAY-RANK returns the number of dimensions of the array.
(assert-equal ____ (array-rank chess-board))
;; The function ARRAY-DIMENSIONS returns a list of the cardinality of the
;; array dimensions.
(assert-equal ____ (array-dimensions chess-board))
;; ARRAY-TOTAL-SIZE returns the total number of elements in the array.
(assert-equal ____ (array-total-size chess-board))))
(define-test make-your-own-array
;; Make your own array that satisfies the test.
(let ((color-cube ____))
;; You may need to modify your array after you create it.
(setf (____ color-cube ____ ____ ____) ____
(____ color-cube ____ ____ ____) ____)
(if (typep color-cube '(simple-array t (3 3 3)))
(progn
(assert-equal 3 (array-rank color-cube))
(assert-equal '(3 3 3) (array-dimensions color-cube))
(assert-equal 27 (array-total-size color-cube))
(assert-equal (aref color-cube 0 1 2) :red)
(assert-equal (aref color-cube 2 1 0) :white))
(assert-true nil))))
(define-test adjustable-array
;; The size of an array does not need to be constant.
(let ((x (make-array '(2 2) :initial-element 5 :adjustable t)))
(assert-equal ____ (aref x 1 0))
(assert-equal ____ (array-dimensions x))
(adjust-array x '(3 4))
(assert-equal ____ (array-dimensions x))))
(define-test make-array-from-list
;; One can create arrays with initial contents.
(let ((x (make-array '(4) :initial-contents '(:one :two :three :four))))
(assert-equal ____ (array-dimensions x))
(assert-equal ____ (aref x 0))))
(define-test row-major-index
;; Row major indexing is a way to access elements with a single integer,
;; rather than a list of integers.
(let ((my-array (make-array '(2 2 2 2))))
(dotimes (i (* 2 2 2 2))
(setf (row-major-aref my-array i) i))
(assert-equal ____ (aref my-array 0 0 0 0))
(assert-equal ____ (aref my-array 0 0 1 0))
(assert-equal ____ (aref my-array 0 1 0 0))
(assert-equal ____ (aref my-array 1 1 1 1))))