io-read.lisp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (in-package :translator-test)
  2. (defun %my-io-read (file result &key (offset +minus-one-ll+)
  3. (amount 2048))
  4. (assert-equal result
  5. (octets-to-string
  6. (io-read file
  7. :offset offset
  8. :amount amount))))
  9. (def-test-method io-read-test ((test io-test))
  10. (let ((file (concatenate-string +main-dir+ "/a")))
  11. (with-testport (p (file-name-lookup file))
  12. (multiple-value-bind (ret err)
  13. (io-read p)
  14. (assert-equal ret nil)
  15. (assert-equal err :bad-fd)))
  16. (with-testport (p (file-name-lookup (concatenate-string +main-dir+ "/f")
  17. :flags '(:nolink :notrans :read)))
  18. (%my-io-read p "c"))
  19. (with-testport (p (file-name-lookup file :flags '(:read)))
  20. (let* ((stat (io-stat p))
  21. (size (stat-get stat 'st-size)))
  22. (assert-equal size (io-readable p))
  23. ; Try to read everything.
  24. (%my-io-read p "abcdefghijklmnopqrstuvwxyz")
  25. (assert-true (io-seek p :offset 0
  26. :whence :seek-set))
  27. ; Read byte by byte
  28. (loop for i from 0 to size
  29. do (progn
  30. (assert-equal (- size i)
  31. (io-readable p))
  32. (if (= i size)
  33. (%my-io-read p "" :amount 1)
  34. (%my-io-read p (octets-to-string `(,(+ 97 i)))
  35. :amount 1))))
  36. ; Now without seeking
  37. (loop for i from 0 below size
  38. do (%my-io-read p (octets-to-string `(,(+ 97 i)))
  39. :offset i
  40. :amount 1))
  41. ; Read outside
  42. (%my-io-read p ""
  43. :offset 2048
  44. :amount 20)
  45. ; Read last byte
  46. (%my-io-read p "z"
  47. :offset (1- size)
  48. :amount 2048)
  49. ; Read in the middle
  50. (%my-io-read p "fgh"
  51. :offset 5
  52. :amount 3)
  53. ; Read sizeable pieces
  54. (assert-true (io-seek p :offset 0 :whence :seek-set))
  55. (%my-io-read p "abcdef"
  56. :amount 6)
  57. (%my-io-read p "ghijkl"
  58. :amount 6)
  59. (assert-true (io-seek p :offset 3))
  60. (%my-io-read p "pqrstu"
  61. :amount 6)
  62. (%my-io-read p "vwxyz"
  63. :amount 6)
  64. (%my-io-read p ""
  65. :amount 6)))))