test-circular.stk 10.3 KB
Newer Older
Erick's avatar
Erick committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
;;;;
;;;;
;;;; test-circular.stk         -- Test circular structures
;;;;
;;;; Copyright © 2018 Erick Gallesio - I3S-CNRS/Polytech Nice-Sophia <eg@unice.fr>
;;;;
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;;; USA.
;;;;
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 11-Jun-2018 15:19
Erick's avatar
Erick committed
25
;;;; Last file update: 27-Nov-2018 18:42 (eg)
Erick's avatar
Erick committed
26 27 28 29 30 31 32
;;;;


(require "test")

(test-section "Circular structures")

Erick's avatar
Erick committed
33
(define (circular-list val1 . vals)             ; The SRFI-1 function
34 35 36 37 38 39 40
  (let ((ans (cons val1 vals)))
    (set-cdr! (last-pair ans) ans)
    ans))

(define (->str lst)
  (with-output-to-string (lambda () (write* lst))))

Erick's avatar
Erick committed
41 42 43 44 45 46 47 48 49 50 51 52
;; ======================================================================
;; Writing
;;
(define (generate-circular n)
  (let ((l '(end)))
    (dotimes (i n)
      (let ((x (list i i)))
        (set-cdr! x x)
        (set! l (cons (list x l (cdr l))
                      l))))
    l))

53

Erick's avatar
Erick committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
(test "write.circular1"
      "#0=(a . #0#)"
      (let ((x (list 'a)))
        (set-cdr! x x)
        (with-output-to-string (lambda () (write* x)))))

(test "write.circular2"
      "(#0=(a . #1=(b)) #0# #1#)"
      (let* ((x (list 'a 'b))
             (y (list x x (cdr x))))
        (with-output-to-string (lambda () (write* y)))))

(test "write.circular3"
      "#(1 #0=(a b) #0# 2)"
      (let* ((x (list 'a 'b))
             (y (vector 1 x x 2)))
        (with-output-to-string (lambda () (write* y)))))

(test "write.circular4"
      "#0=#(#0# #0#)"
      (let* ((x (vector 0 0)))
        (vector-set! x 0 x)
        (vector-set! x 1 x)
        (with-output-to-string (lambda () (write* x)))))

(test "write.circular5"
      "((#0=#(#0# 1) . #1=(#0#)) #0# (x #1#))"
      (let ((x (vector 0 1)))
        (vector-set! x 0 x)
        (let ((y (list x x)))
          (with-output-to-string (lambda () (write* `(,y ,x (x ,(cdr y)))))))))

(test "write.circular6"
      "((#0=(24 . #0#) #1=((#2=(23 . #2#) #3=((#4=(22 . #4#) #5=((#6=(21 . #6#) #7=((#8=(20 . #8#) #9=((#10=(19 . #10#) #11=((#12=(18 . #12#) #13=((#14=(17 . #14#) #15=((#16=(16 . #16#) #17=((#18=(15 . #18#) #19=((#20=(14 . #20#) #21=((#22=(13 . #22#) #23=((#24=(12 . #24#) #25=((#26=(11 . #26#) #27=((#28=(10 . #28#) #29=((#30=(9 . #30#) #31=((#32=(8 . #32#) #33=((#34=(7 . #34#) #35=((#36=(6 . #36#) #37=((#38=(5 . #38#) #39=((#40=(4 . #40#) #41=((#42=(3 . #42#) #43=((#44=(2 . #44#) #45=((#46=(1 . #46#) #47=((#48=(0 . #48#) #49=(end) ()) . #49#) #49#) . #47#) #47#) . #45#) #45#) . #43#) #43#) . #41#) #41#) . #39#) #39#) . #37#) #37#) . #35#) #35#) . #33#) #33#) . #31#) #31#) . #29#) #29#) . #27#) #27#) . #25#) #25#) . #23#) #23#) . #21#) #21#) . #19#) #19#) . #17#) #17#) . #15#) #15#) . #13#) #13#) . #11#) #11#) . #9#) #9#) . #7#) #7#) . #5#) #5#) . #3#) #3#) . #1#)"
      (with-output-to-string (lambda ()
                               (write* (generate-circular 25)))))
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152


;;; Chibi tests
(test "chibi-circular.1" "#0=(1 . #0#)"   (->str (circular-list 1)))
(test "chibi-circular.2" "#0=(1 2 . #0#)" (->str (circular-list 1 2)))
(test "chibi-circular.3" "(1 . #0=(2 . #0#))" (->str (cons 1 (circular-list 2))))
(test "chibi-circular.4" "#0=(1 #0# 3)"
      (let ((x (list 1 2 3)))
        (set-car! (cdr x) x)
       (->str x)))

(test "chibi-circular.5" "(#0=(1 #0# 3))"
      (let ((x (list 1 2 3)))
        (set-car! (cdr x) x)
        (->str (list x))))
(test "chibi-circular.6" "(#0=(1 #0# 3) #0#)"
      (let ((x (list 1 2 3)))
        (set-car! (cdr x) x)
        (->str (list x x))))
(test "chibi-circular.7" "(#0=(1 . #0#) #1=(1 . #1#))"
      (->str (list (circular-list 1) (circular-list 1))))

(test "chibi-circular.8" "(#0=(1 . 2) #1=(1 . 2) #2=(3 . 4) #0# #1# #2#)"
      (let ((a (cons 1 2))
            (b (cons 1 2))
            (c (cons 3 4)))
        (->str (list a b c a b c))))

(test "chibi-circular.9" "((1 . 2) (1 . 2) (3 . 4) (1 . 2) (1 . 2) (3 . 4))"
      (let ((a (cons 1 2)) (b (cons 1 2)) (c (cons 3 4)))
        (with-output-to-string (lambda () (write (list a b c a b c))))))

(test "chibi-circular.10" "#0=((1 . 2) (1 . 2) (3 . 4) . #0#)"
      (let* ((a (cons 1 2))
             (b (cons 1 2))
             (c (cons 3 4))
             (ls (list a b c)))
        (set-cdr! (cddr ls) ls)
        (->str ls)))

(test "chibi-circular.11" "#0=#(#0#)"
      (let ((x (vector 1)))
        (vector-set! x 0 x)
        (->str x)))
(test "chibi-circular.12" "#0=#(1 #0#)"
      (let ((x (vector 1 2)))
        (vector-set! x 1 x)
        (->str x)))

(test "chibi-circular.13" "#0=#(1 #0# 3)"
      (let ((x (vector 1 2 3)))
        (vector-set! x 1 x)
        (->str x)))
(test "chibi-circular.14" "(#0=#(1 #0# 3))"
      (let ((x (vector 1 2 3)))
        (vector-set! x 1 x)
        (->str (list x))))
(test "chibi-circular.15" "#0=#(#0# 2 #0#)"
      (let ((x (vector 1 2 3)))
        (vector-set! x 0 x)
        (vector-set! x 2 x)
        (->str x)))

Erick's avatar
Erick committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
;; ======================================================================
;; Reading
;;
(test "read.circular1"
      #t
      (let ((l '#0=(0 #0#)))
        (and (eq? (car l) 0)
           (eq? (cadr l) l))))

(test "read.circular2"
      #t
      (let* ((l1 '(#0=(1 2 3) #0# #1=(2 #0#) #1#))
             (l2 '(#0=(1 2 3) #0# #1=(2 #0#) #1#)))
        (equal? l1 l2)))


(test "read.circular3"
      #t
      (let* ((l1 '((#0=(1 2 3) #0# #1=(2 #0#) #1#)))
             (l2 '((#3=(1 2 3) #3# #4=(2 #3#) #4#))))
        (equal? l1 l2)))


(test "read.circular4"
      #t
      (let* ((l1 (with-output-to-string (lambda()
                                         (write* (generate-circular 3)))))
             (l2 (with-input-from-string l1 read))
             (l3 (with-output-to-string (lambda() (write* l2)))))
        (equal? l1 l3)))

(test "useless.circular1"
      '((0 1) 2 3)
      (cons '#0=(0 1)  '#1=(2 3)))

(test "useless.circular2"
      '((0 1)  2 3)
      '(#0=(0 1) .  #0=(2 3)))


Erick's avatar
Erick committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
;; ======================================================================
;; Tests from William D Clinger equiv? implementtation
;;

(test "Clinger.1" #t
      (let ()
        (define x
          (let ((x1 (vector 'h))
                (x2 (let ((x (list #f))) (set-car! x x) x)))
            (vector x1 (vector 'h) x1 (vector 'h) x1 x2)))
        (define y
          (let ((y1 (vector 'h))
                (y2 (vector 'h))
                (y3 (let ((x (list #f))) (set-car! x x) x)))
            (vector (vector 'h) y1 y1 y2 y2 y3)))
        (equal? x y)))

(test "Clinger.2" #t
      (let ()
        (define x
          (let ((x (cons (cons #f 'a) 'a)))
            (set-car! (car x) x)
            x))
        (define y
          (let ((y (cons (cons #f 'a) 'a)))
            (set-car! (car y) (car y))
            y))
        (equal? x y)))

(test "Clinger.3" #t
      (let ((k 100))
        (define x
          (let ((x1 (cons
                     (let f ((n k))
                       (if (= n 0)
                           (let ((x0 (cons #f #f)))
                             (set-car! x0 x0)
                             (set-cdr! x0 x0)
                             x0)
                           (let ((xi (cons #f (f (- n 1)))))
                             (set-car! xi xi)
                             xi)))
                     #f)))
            (set-cdr! x1 x1)
            x1))
        (define y
          (let* ((y2 (cons #f #f)) (y1 (cons y2 y2)))
            (set-car! y2 y1)
            (set-cdr! y2 y1)
            y1))
        (equal? x y)))

(test "Clinger.4 (as 3 but read)" #t
      (equal?
       '#0=(#1=(#1# . #2=(#2# . #3=(#3# . #4=(#4# . #5=(#5# . #6=(#6# . #7=(#7# . #8=(#8# . #9=(#9# . #10=(#10# . #11=(#11# . #12=(#12# . #13=(#13# . #14=(#14# . #15=(#15# . #16=(#16# . #17=(#17# . #18=(#18# . #19=(#19# . #20=(#20# . #21=(#21# . #22=(#22# . #23=(#23# . #24=(#24# . #25=(#25# . #26=(#26# . #27=(#27# . #28=(#28# . #29=(#29# . #30=(#30# . #31=(#31# . #32=(#32# . #33=(#33# . #34=(#34# . #35=(#35# . #36=(#36# . #37=(#37# . #38=(#38# . #39=(#39# . #40=(#40# . #41=(#41# . #42=(#42# . #43=(#43# . #44=(#44# . #45=(#45# . #46=(#46# . #47=(#47# . #48=(#48# . #49=(#49# . #50=(#50# . #51=(#51# . #52=(#52# . #53=(#53# . #54=(#54# . #55=(#55# . #56=(#56# . #57=(#57# . #58=(#58# . #59=(#59# . #60=(#60# . #61=(#61# . #62=(#62# . #63=(#63# . #64=(#64# . #65=(#65# . #66=(#66# . #67=(#67# . #68=(#68# . #69=(#69# . #70=(#70# . #71=(#71# . #72=(#72# . #73=(#73# . #74=(#74# . #75=(#75# . #76=(#76# . #77=(#77# . #78=(#78# . #79=(#79# . #80=(#80# . #81=(#81# . #82=(#82# . #83=(#83# . #84=(#84# . #85=(#85# . #86=(#86# . #87=(#87# . #88=(#88# . #89=(#89# . #90=(#90# . #91=(#91# . #92=(#92# . #93=(#93# . #94=(#94# . #95=(#95# . #96=(#96# . #97=(#97# . #98=(#98# . #99=(#99# . #100=(#100# . #101=(#101# . #101#))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) . #0#)
       '#200=(#201=(#200# . #200#) . #201#)))


(test "vectors.1" #t
      (let ((v1 (vector 0 1 2 3))
            (v2 (vector 0 1 2 3)))
        (vector-set! v1 2 v1)
        (vector-set! v2 2 v2)
        (equal? v1 v2)))

(test "vectors.2" #t
      (let ((v1 (vector 0 1 2 3))
            (v2 (vector 0 1 2 3)))
        (vector-set! v1 2 v2)
        (vector-set! v2 2 v1)
        (equal? v1 v2)))

(test "vectors.3" #f
      (let ((v1 (vector 0 1 2 3 4))
            (v2 (vector 0 1 2 3)))
        (vector-set! v1 2 v2)
        (vector-set! v2 2 v1)
        (equal? v1 v2)))

(test "vectors.4" #t
      (let ((v1 (vector 0))
            (v2 (vector 0)))
        (vector-set! v1 0 v1)
        (vector-set! v2 0 v2)
        (equal? v1 v2)))


(test "vectors.5" #t (equal? '#0=#(a #0# b #0#) '#1=#(a #1# b #1#)))
(test "vectors.6" #f (equal? '#0=#(a #0# a #0#) '#1=#(a #1# b #1#)))

Erick's avatar
Erick committed
283 284 285

;; ======================================================================
(test-section-end)