PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test16.sc

http://github.com/barak/scheme2c
Scala | 140 lines | 123 code | 17 blank | 0 comment | 2 complexity | 60e01022e2c71cce214d31a54caec5f8 MD5 | raw file
Possible License(s): Unlicense
  1. ;;;
  2. ;;; Scheme->C test program
  3. ;;;
  4. ;;;
  5. ;;; Test functions for basic Scheme functions.
  6. ;;;
  7. ;* Copyright (c) 1989-1993 Hewlett-Packard Development Company, L.P.
  8. ;* All Rights Reserved
  9. ;* Permission is hereby granted, free of charge, to any person obtaining a
  10. ;* copy of this software and associated documentation files (the "Software"),
  11. ;* to deal in the Software without restriction, including without limitation
  12. ;* the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. ;* and/or sell copies of the Software, and to permit persons to whom the
  14. ;* Software is furnished to do so, subject to the following conditions:
  15. ;*
  16. ;* The above copyright notice and this permission notice shall be included in
  17. ;* all copies or substantial portions of the Software.
  18. ;*
  19. ;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. ;* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. ;* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. ;* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. ;* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. ;* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. ;* DEALINGS IN THE SOFTWARE.
  26. (module test16)
  27. (define-external (chk testnum result expected) testchk)
  28. (define BIGENDIAN (not (eq? (c-byte-ref "A" (- 1 c-sizeof-tscp)) 1)))
  29. (define LSB-SHORT (if bigendian (/ c-sizeof-int 2) 0))
  30. ; byte offset to lsb short
  31. (define MSB-SHORT (if bigendian 0 (/ c-sizeof-int 2)))
  32. ; byte offset to msb short
  33. (define (test16)
  34. ;;; *.* Extensions for accessing C structures. Byte order is computed
  35. (let ((s (make-string 10 #\*)))
  36. (c-byte-set! s 0 (char->integer #\S))
  37. (c-byte-set! s 1 (char->integer #\c))
  38. (c-byte-set! s 2 (char->integer #\h))
  39. (c-byte-set! s 3 (char->integer #\e))
  40. (c-byte-set! s 4 (char->integer #\m))
  41. (c-byte-set! s 5 (char->integer #\e))
  42. (c-byte-set! s 6 0.0)
  43. (c-byte-set! s 7 -1)
  44. (c-byte-set! s 8 255)
  45. (chk 1 (c-string->string s) "Scheme")
  46. (chk 2 (integer->char (c-byte-ref s 0)) #\S)
  47. (chk 3 (integer->char (c-byte-ref s 2)) #\h)
  48. (chk 4 (c-byte-ref s 7) 255)
  49. (chk 5 (c-byte-ref s 8) 255))
  50. (let ((s (make-string 10 #\*)))
  51. (cond ((and (= c-sizeof-int 4) (= c-sizeof-short 2))
  52. (c-int-set! s 0 #xffff)
  53. (chk 10 (c-int-ref s 0) #xffff)
  54. (chk 11 (c-shortunsigned-ref s lsb-short) #xffff)
  55. (chk 12 (c-shortunsigned-ref s msb-short) 0)
  56. (chk 13 (c-shortint-ref s lsb-short) -1)
  57. (chk 14 (c-shortint-ref s msb-short) 0)
  58. (c-shortint-set! s msb-short -1)
  59. (chk 15 (c-int-ref s 0) -1)
  60. (c-shortunsigned-set! s lsb-short #xfffe)
  61. (chk 16 (c-int-ref s 0) -2))
  62. (else (format #t "Tests 10-16 omitted~%"))))
  63. (let ((s (make-string 20 #\*)))
  64. (cond ((and (= c-sizeof-int 4) (= c-sizeof-tscp 4))
  65. (c-unsigned-set! s 0 (- (expt 2 32) 1))
  66. (c-int-set! s 4 4)
  67. (chk 20 (c-int-ref s 0) -1)
  68. (chk 21 (c-unsigned-ref s 0) (- (expt 2 32) 1))
  69. (chk 22 (c-int-ref s 4) 4)
  70. (chk 23 (c-unsigned-ref s 4) 4)
  71. (chk 24 (c-tscp-ref s 4) 1)
  72. (c-tscp-set! s 0 -1)
  73. (chk 25 (c-int-ref s 0) -4))
  74. ((and (= c-sizeof-int 4) (= c-sizeof-tscp 8))
  75. (c-unsigned-set! s 0 (- (expt 2 32) 1))
  76. (c-int-set! s 4 4)
  77. (chk 20 (c-int-ref s 0) -1)
  78. (chk 21 (c-unsigned-ref s 0) (- (expt 2 32) 1))
  79. (chk 22 (c-int-ref s 4) 4)
  80. (chk 23 (c-unsigned-ref s 4) 4)
  81. (c-int-set! s 8 -4)
  82. (c-unsigned-set! s 12 (- (expt 2 32) 1))
  83. (chk 24 (c-tscp-ref s 8) -1)
  84. (c-tscp-set! s 0 -1)
  85. (chk 25 (c-int-ref s 0) -4))
  86. (else (format #t "Tests 20-25 omitted~%"))))
  87. (let ((s (make-string 20)))
  88. (c-float-set! s 0 -1)
  89. (chk 30 (c-float-ref s 0) -1.0)
  90. (c-double-set! s 0 -1)
  91. (chk 31 (c-double-ref s 0) -1.0))
  92. (let ((s (make-string 10 #\*))
  93. (v (make-vector 10 -1)))
  94. (chk 40 (scheme-byte-ref s (if bigendian (- c-sizeof-tscp 2) 1)) 10)
  95. (chk 41 (scheme-byte-ref s c-sizeof-tscp) (char->integer #\*))
  96. (chk 42 (scheme-byte-ref s (+ 10 c-sizeof-tscp)) 0)
  97. (scheme-byte-set! s (+ c-sizeof-tscp 1) (char->integer #\^))
  98. (scheme-byte-set! s (+ c-sizeof-tscp 5) (char->integer #\^))
  99. (chk 43 s "*^***^****")
  100. (chk 44 (scheme-s2cuint-ref s 0) (+ 2560 134))
  101. (chk 45 (scheme-int-ref v c-sizeof-tscp)
  102. (if (and bigendian (= c-sizeof-int 2) (= c-sizeof-tscp 4))
  103. -1
  104. -4))
  105. (scheme-int-set! v c-sizeof-tscp 4)
  106. (scheme-int-set! v (+ c-sizeof-int c-sizeof-tscp) 0)
  107. (chk 46 (scheme-tscp-ref v c-sizeof-tscp)
  108. (if (and bigendian (= c-sizeof-int 2) (= c-sizeof-tscp 4))
  109. (expt 2 16)
  110. 1))
  111. (chk 47 (scheme-int-ref v c-sizeof-tscp) 4)
  112. (scheme-tscp-set! v c-sizeof-tscp "This is the TSCP")
  113. (chk 48 (vector-ref v 0) "This is the TSCP"))
  114. ;;; *.* Bit operations
  115. (chk 50 (bit-and 1) 1)
  116. (chk 51 (bit-or 1) 1)
  117. (chk 52 (bit-xor 1) 1)
  118. (chk 53 (bit-not (bit-not 1)) 1)
  119. (chk 54 (bit-and 1 3 5) 1)
  120. (chk 55 (bit-or 1 3 5) 7)
  121. (chk 56 (bit-xor 1 3 5) 7)
  122. (chk 57 (bit-lsh 1 31) 2147483648.)
  123. (chk 58 (bit-rsh -1 31) 1))