1
0
mirror of synced 2026-05-04 15:16:50 +00:00
Files
Interlisp.medley/internal/test/LANGUAGE/AUTO/18-2-STRING-LESSP.TEST

95 lines
3.1 KiB
Plaintext

;; Function To Be Tested: string-lessp
;;
;; Source: CLtL p. 301
;; Chapter 18: Strings Section 2: String Comparison
;;
;; Created By: Peter Reidy
;;
;; Creation Date: 18 July 86
;;
;; Last Update: 14 December 86
;;
;; Filed As: {eris}<lispcore>cml>test>18-2-string-lessp.test
;;
;;
;; Syntax: string-lessp string1 string2 &key :start1 :end1 :start2 :end2
;;
;; Function Description: The function compares the respective strings (with the keywords for the respective strings counting from 0 and the :end keywords designating the character after the last compared) lexicographically (i.e. it compares the corresponding characters by the function char>). Comparison is case-insensitive.
;;
;; Argument(s): string1, string2: character strings
;; :start1, start2: the start-comparison position in the respective strings (counting from 0)
;; :end1, end2: the last character + 1 (counting from 0) to compare - i.e. if comparing "string" with a :end keyword of 4, the last character compared will be the letter i.
;;
;; Returns: If all characters satisfy char>: the index (counting from 0) of the first non-identical character - i.e. the length of the portion of the strings that satisfies the predicate.
;; If not: nil
;;
(do-test-group string-lessp-group :before
(progn
(test-setq
upcase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowcase "abcdefghijklmnopqrstuvwxyz"
alphalength (length upcase)
digits "0123456789"
diglength (length digits)
)
(test-defun strictly-less (string1 string2)
(eq 0 (string-lessp string1 string2))
)
) ; progn
;;
(do-test "A...Y is string-lessp B...Z;comparison is case-insensitive"
(AND
(= (length lowcase) alphalength) ; make sure I set things up right
(strictly-less
(string-trim '(#\Z) upcase)
(string-trim '(#\a) lowcase)
)
(strictly-less
(string-trim '(#\z) lowcase)
(string-trim '(#\A) upcase)
)
)
)
;;
(do-test "digits and alpha characters are strictly outside each other for string-lessp as for the character inequalities"
(or (strictly-less "9" "A" ) (strictly-less "Z" "0" ))
)
;;
(do-test "string-lessp with digit strings created with make-array"
(strictly-less
(make-array (1- diglength) :element-type 'string-char :fill-pointer t :adjustable t :displaced-to (string-trim `(#\9) digits))
(make-array (1- diglength) :initial-contents '(#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) :element-type 'string-char)
)
)
;;
(do-test "a string is not string-lessp itself"
(every 'null
(list
(string-lessp (make-array 2 :element-type 'string-char :initial-element #\newline) "
")
(string-lessp upcase upcase)
(string-lessp upcase lowcase)
)
)
)
;;
(do-test "string-lessp stops as soon as it finds a nil comparison"
(and
(= (1- diglength) (string-lessp "0123456788" digits))
(= 3 (string-lessp digits "0124456789"))
)
)
;;
(do-test "string-lessp keywords"
(and
(string-lessp "zSTuvw" "vwxyZ" :start1 1)
(string-lessp "ABC" "XYzbcD" :start2 3)
(string-lessp "0123456" "012012" :end1 3)
(string-lessp "abc" "lMnABC" :end2 3)
)
)
)
STOP