Skip to content

Commit

Permalink
Merge pull request #27 from ericlesaquiles/sequences
Browse files Browse the repository at this point in the history
Added sequence tests
  • Loading branch information
Affonso-Gui authored Aug 3, 2018
2 parents 02a8765 + b422050 commit efaedce
Show file tree
Hide file tree
Showing 15 changed files with 1,413 additions and 1,104 deletions.
28 changes: 28 additions & 0 deletions auxiliary/eus-character.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(in-package "LISP")

(defun characterp (ch)
"Returns T if ch is a character, false otherwise."
(if (or (floatp ch) (derivedp ch object) (< ch 0))
nil
t))

(defun char-int (ch) (assert (characterp ch)) ch)
(defun char-not-equal (x y)
(assert (and (characterp x) (characterp y)))
(not (equal x y)))

(setf (symbol-function 'char<) #'<
(symbol-function 'char=) #'=
(symbol-function 'char>) #'>
(symbol-function 'char/=) #'/=
(symbol-function 'char<=) #'<=
(symbol-function 'char>=) #'>=
(symbol-function 'char-code) #'char-int
(symbol-function 'char-equal) #'equal
(symbol-function 'char-lessp) #'<
(symbol-function 'char-greaterp) #'>
(symbol-function 'char-not-lessp) #'>=)

(export '(characterp char-int char-code char-equal char-not-equal
char-lessp char-greaterp char-not-lessp
char< char= char> char/= char<= char>=))
80 changes: 80 additions & 0 deletions auxiliary/eus-predicates.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(in-package "LISP")


(defmacro complement (fn)
`(function (lambda (&rest args) (not (apply ,fn args)))))

;;; EQUALP -- public.
;;
(defun equalp (x y)
"Just like EQUAL, but more liberal in several respects.
Numbers may be of different types, as long as the values are identical
after coercion. Characters may differ in alphabetic case. Vectors and
arrays must have identical dimensions and EQUALP elements, but may differ
in their type restriction."
(cond ((eq x y) t)
;;((characterp x) (and (characterp y) (char-equal x y))) <- characters are numbers
((numberp x) (and (numberp y) (= x y)))
((consp x)
(and (consp y)
(equalp (car x) (car y))
(equalp (cdr x) (cdr y))))
((pathnamep x)
;; (and (pathnamep y) (pathname= x y)))
(error "pathname is not yet supported here. Sorry."))
((hash-table-p x)
(error "hash table is not yet supported here. Sorry."))
;; (and (hash-table-p y)
;; (eql (hash-table-count x) (hash-table-count y))
;; (eql (hash-table-test x) (hash-table-test y))
;; (with-hash-table-iterator (next x)
;; (loop
;; (multiple-value-bind (more x-key x-value)
;; (next)
;; (cond (more
;; (multiple-value-bind (y-value foundp)
;; (gethash x-key y)
;; (unless (and foundp (equalp x-value y-value))
;; (return-from equalp nil))))
;; (t
;; (return-from equalp t))))))))
;; ((%instancep x)
;; (let* ((layout-x (%instance-layout x))
;; (len (layout-length layout-x)))
;; (and (%instancep y)
;; (eq layout-x (%instance-layout y))
;; (structure-class-p (layout-class layout-x))
;; (do ((i 1 (1+ i)))
;; ((= i len) t)
;; (declare (fixnum i))
;; (let ((x-el (%instance-ref x i))
;; (y-el (%instance-ref y i)))
;; (unless (or (eq x-el y-el)
;; (equalp x-el y-el))
;; (return-from equalp nil)))))))
((vectorp x)
(let ((length (length x)))
(and (vectorp y)
(= length (length y))
(dotimes (i length t)
(let ((x-el (aref x i))
(y-el (aref y i)))
(if (not (or (eq x-el y-el)
(equalp x-el y-el)))
(return-from equalp nil)))))))
((arrayp x)
(and (arrayp y)
(= (array-rank x) (array-rank y))
(dotimes (axis (array-rank x) t)
(unless (= (array-dimension x axis)
(array-dimension y axis))
(return-from equalp nil)))
(dotimes (index (array-total-size x) t)
(let ((x-el (row-major-aref x index))
(y-el (row-major-aref y index)))
(unless (or (eq x-el y-el)
(equalp x-el y-el))
(return-from equalp nil))))))
(t nil)))

(export '(complement equalp))
152 changes: 152 additions & 0 deletions auxiliary/eus-sequences.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
;; Shadows a few substitute functions
;; - substitute should accept a from-end keyword
;; - count = nil should default to count = 10^6 (due to hacky reasons)

(in-package :lisp)


;; TODO: implement from-end in sequence.c
(defmacro defsubstitute (name fn arg-lst
newitem olditem seq test testnot key iftest ifnottest start end count)
`(defun ,name ,arg-lst
(let ((count (or (and count (< count 0) 0) count 1000000))
len)
(unless end (setq len (length seq) end len))
(if from-end
(let ((len (or len (length seq))))
(reverse (,fn ,newitem ,olditem (reverse ,seq) ,test ,testnot ,key ,iftest ,ifnottest (- len ,end) (- len ,start) ,count)))
(,fn ,newitem ,olditem ,seq ,test ,testnot ,key ,iftest ,ifnottest ,start ,end ,count)))))

(defsubstitute substitute system::raw-substitute
(newitem olditem seq
&key (start 0) (end nil)
(test #'eq) (test-not nil)
(count 1000000)
(key #'identity)
(from-end nil))
newitem olditem seq test test-not key nil nil start end count)

(defsubstitute substitute-if system::raw-substitute
(newitem pred seq
&key (start 0) (end nil)
(count 1000000)
(key #'identity)
(from-end nil))
newitem nil seq nil nil key pred nil start end count)

(defsubstitute substitute-if-not system::raw-substitute
(newitem pred seq
&key (start 0) (end nil)
(count 1000000)
(key #'identity)
(from-end nil))
newitem nil seq nil nil key nil pred start end count)

(defsubstitute nsubstitute system::raw-nsubstitute
(newitem olditem seq
&key (start 0) (end nil)
(test #'eq) (test-not nil)
(count 1000000)
(key #'identity)
(from-end nil))
newitem olditem seq test test-not key nil nil start end count)

(defsubstitute nsubstitute-if system::raw-nsubstitute
(newitem pred seq
&key (start 0) (end nil)
(key #'identity)
(count 1000000)
(from-end nil))
newitem nil seq nil nil key pred nil start end count)

(defsubstitute nsubstitute-if-not system::raw-nsubstitute
(newitem pred seq
&key (start 0) (end nil)
(count 1000000)
(key #'identity)
(from-end nil))
newitem nil seq nil nil key nil pred start end count)

;; ;; Shadows replace function
;; ;; - replace should default nil to entire string

(defun replace (dest src &key (start1 0) (end1 nil)
(start2 0) (end2 nil))
(let ((end1 (or end1 (length dest)))
(end2 (or end2 (length src))))
(let ((result dest) (count (min (- end1 start1) (- end2 start2))))
(cond ((listp dest)
(setq dest (nthcdr start1 dest))
(cond ((listp src)
(setq src (nthcdr start2 src))
(dotimes (c count)
(setq (dest . car) (pop src))
(pop dest)))
(t
(dotimes (c count)
(setq (dest . car) (aref src start2))
(inc start2) (pop dest)))))
((listp src) ; list --> vector
(setq src (nthcdr start2 src))
(dotimes (c count)
(aset dest start1 (pop src))
(inc start1)))
(t (system::vector-replace dest src start1 end1 start2 end2)))
result)))


;; Add mismatch function

(defmacro mismatch-core (from-end)
`(when (if test-not
(funcall test-not
(funcall key (elt seq1 index1))
(funcall key (elt seq2 index2)))
(not (funcall test
(funcall key (elt seq1 index1))
(funcall key (elt seq2 index2)))))
(return-from mismatch ,(if from-end
'(1+ index1)
'index1))))

(defun mismatch (seq1 seq2 &key (from-end nil)
(test #'eql)
(test-not nil)
(start1 0)
(start2 0)
(end1 nil)
(end2 nil)
(key #'identity))
"The specified subsequences of Sequence1 and Sequence2 are compared
element-wise. If they are of equal length and match in every element, the
result is NIL. Otherwise, the result is a non-negative integer, the index
within Sequence1 of the leftmost position at which they fail to match; or,
if one is shorter than and a matching prefix of the other, the index within
Sequence1 beyond the last position tested is returned. If a non-Nil
:From-End keyword argument is given, then one plus the index of the
rightmost position in which the sequences differ is returned."
(let ((end1 (or end1 (length seq1)))
(end2 (or end2 (length seq2))))
(if (or (zerop end1) (zerop end2)) (return-from mismatch 0))
(if from-end
(loop
for index1 from (- end1 1) downto start1
for index2 from (- end2 1) downto start2
do
(mismatch-core t))
(loop
for index1 from start1 below end1
for index2 from start2 below end2
do
(mismatch-core nil)))
(cond
((> (- end1 start1) (- end2 start2)) end2)
((< (- end1 start1) (- end2 start2)) end1)
(t nil))))

;; ;; Auxiliary function to shadow peek-char
;; (defun OOpeek-char (&optional (stream *standard-input*)
;; (eof-errorp t) eof-value recursive-p)
;; (peek-char stream eof-errorp eof-value recursive-p))

(export '(mismatch))
12 changes: 7 additions & 5 deletions auxiliary/search-aux.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
(let* ((s *searched-list*) (len (length s)))
(loop for x from 0 to 8 nconc
(loop for y from 0 to (- len x)
collect (subseq s y (+ y x)))))
collect (if (= y len)
nil
(subseq s y (+ y x))))))
:test #'equal))

(defparameter *searched-vector*
Expand All @@ -34,7 +36,7 @@
(loop for x from 0 to 8 nconc
(loop for y from 0 to (- len x)
collect (subseq s y (+ y x)))))
:test #'equalp))
:test #'equal))

(defparameter *searched-string*
"1101111111010111010111000010010000001011010010001100100001101010001011010011111000001011111010110101")
Expand All @@ -45,9 +47,9 @@
(loop for x from 0 to 8 nconc
(loop for y from 0 to (- len x)
collect (subseq s y (+ y x)))))
:test #'equalp))
:test #'equal))

(defun subseq-equalp (seq1 seq2 start1 start2 len &key (test #'equalp))
(defun subseq-equalp (seq1 seq2 start1 start2 len &key (test #'equal))
(assert
(and
(>= start1 0)
Expand All @@ -68,7 +70,7 @@

(defun search-check (pattern searched pos
&key (start1 0) (end1 nil) (start2 0) (end2 nil)
key from-end (test #'equalp))
key from-end (test #'equal))
(unless end1 (setq end1 (length pattern)))
(unless end2 (setq end2 (length searched)))
(assert (<= start1 end1))
Expand Down
6 changes: 5 additions & 1 deletion eus-test.l
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
;; LOAD CL-compatibility libraries
(load "auxiliary/eus-multiple-values.l")
(load "auxiliary/eus-loop.l")

(load "auxiliary/eus-character.l")
(load "auxiliary/eus-predicates.l")
(load "auxiliary/eus-sequences.l")
(load "auxiliary/eus-declarations.l")
(load "auxiliary/eus-numbers.l")
(load "auxiliary/eus-types.l")
Expand Down Expand Up @@ -146,8 +150,8 @@

;; (load "packages/load.lsp")
(load "reader/load.lsp")
;; (load "sequences/load.lsp")
(load "streams/load.lsp")
(load "sequences/load.lsp")
(load "system-construction/load.lsp")
(load "structures/load.lsp")
;; (load "types-and-classes/load.lsp")
Expand Down
48 changes: 35 additions & 13 deletions reports/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@
- define-method-combination
- deftype

** 6 Sequence
*** Caveats
- (map 'list #'identity a), where a is a sequence, does not work
- Undefined functions:
- complement
- characterp
- stable-sort
- do-special-integer-vectors
- map-into
- constantly
- simple-vector-p
- check-type-error
- type-of
- Non-complyinh functions
- no such keyword :from-end in (position-if-not
- integer expected in (position-if-not #'oddp #(1 3 1 4 3 2 1 8 9) :end nil)
- No such keyword :from-end in find
- No such keyword :from-end in count-if-not
- No such keyword :key in (reduce #'+ '(1 2 3) :key '1+)
** DONE 6 Misc
*** Caveats
- "no such package":
Expand Down Expand Up @@ -267,17 +286,20 @@
* Results


| Tests-version | Test-num | Passed | Failure |
|-----------------------+----------+--------+---------|
| 1 | 5557 | 2983 | 2574 |
|-----------------------+----------+--------+---------|
| 2 - Add Arrays tests | 6297 | 3087 | 3210 |
|-----------------------+----------+--------+---------|
| 3 | 7676 | 3489 | 4187 |
|-----------------------+----------+--------+---------|
| 4 - Add misc tests | 8187 | 3506 | 4681 |
|-----------------------+----------+--------+---------|
| 5 - Add numbers tests | 9582 | 3988 | 5594 |
|-----------------------+----------+--------+---------|
| | | | |
| Tests-version | Test-num | Passed | Failure |
|-------------------------+----------+--------+---------|
| 1 | 5557 | 2983 | 2574 |
|-------------------------+----------+--------+---------|
| 2 - Add Arrays tests | 6297 | 3087 | 3210 |
|-------------------------+----------+--------+---------|
| 3 | 7676 | 3489 | 4187 |
|-------------------------+----------+--------+---------|
| 4 - Add misc tests | 8187 | 3506 | 4681 |
|-------------------------+----------+--------+---------|
| 5 - Add numbers tests | 9582 | 3988 | 5594 |
|-------------------------+----------+--------+---------|
| 6 - Add sequences tests | 11249 | 4659 | 6590 |
|-------------------------+----------+--------+---------|
| | | | |


Loading

0 comments on commit efaedce

Please sign in to comment.