;; -*- geiser-scheme-implementation: guile -*-

;; Copyright 2019 Christopher Howard

;; 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 3 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, see <https://www.gnu.org/licenses/>.

(define-module (buffer repository tests))

(use-modules (buffer repository)
             (srfi srfi-64)
             (rnrs bytevectors))

(define determine-bucket-index (@@ (buffer repository) determine-bucket-index))

(test-begin "determine-bucket-index-test")

(define b (make-buffer-repository 8 24))

(test-equal (determine-bucket-index b 1000) 2)
(test-equal (determine-bucket-index b 15000) 6)
(test-equal (determine-bucket-index b 262144) 10)
(test-error 'no-match (determine-bucket-index b 16777217))

(test-end "determine-bucket-index-test")

(test-begin "attach-detach-test")

(define detach-buffer! (@@ (buffer repository) detach-buffer!))
(define attach-buffer! (@@ (buffer repository) attach-buffer!))
(define bR (make-buffer-repository 8 24))
(define bf1 (make-bytevector 1024 0))
(define bf2 (make-bytevector 1023 0))
(define bf3 (make-bytevector 1025 0))
(test-error 'buffer-bucket-empty (detach-buffer! bR 2))
(attach-buffer! bR 2 bf1)
(test-error 'buffer-bucket-mismatch (attach-buffer! bR 2 bf2))
(test-error 'buffer-bucket-mismatch (attach-buffer! bR 2 bf3))
(define bf-ret (detach-buffer! bR 2))
(test-eq bf1 bf-ret)
(test-error 'buffer-bucket-empty (detach-buffer! bR 2))

(test-end "attach-detach-test")

(test-begin "checkin-checkout-test")

(define bR (make-buffer-repository 8 24))
(test-error '(empty-bucket) (checkout-buffer! 1023 bR #:spawn #f))
(define b1 (checkout-buffer! 1023 bR))
(test-equal 1024 (bytevector-length b1))
(checkin-buffer! b1 bR)
(define b1 (checkout-buffer! 1023 bR))
(test-equal 1024 (bytevector-length b1))
(test-error '(empty-bucket) (checkout-buffer! 1023 bR #:spawn #f))
(define b2 (make-bytevector 1023))
(test-error '(buffer-bucket-mismatch) (checkin-buffer! b2 bR))
(test-error '(no-match) (checkout-buffer! 1000000000 bR))
(parameterize ((%buffer-repository (make-buffer-repository 8 24)))
  (test-error '(empty-bucket) (checkout-buffer! 1023 #:spawn #f))
  (let ((b1 (checkout-buffer! 1023)))
    (test-equal 1024 (bytevector-length b1))
    (checkin-buffer! b1))
  (let ((b1 (checkout-buffer! 1023)))
    (test-equal 1024 (bytevector-length b1))
    (test-error '(empty-bucket) (checkout-buffer! 1023 #:spawn #f)))
  (let ((b2 (make-bytevector 1023)))
    (test-error '(buffer-bucket-mismatch) (checkin-buffer! b2))
    (test-error '(no-match) (checkout-buffer! 1000000000))))

(test-end "checkin-checkout-test")