bitvectors.test 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;;;; bitvectors.test --- tests guile's bitvectors -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite test-bitvectors)
  19. #:use-module (test-suite lib))
  20. (with-test-prefix "predicates"
  21. (pass-if (bitvector? #*1010101010))
  22. (pass-if (array? #*1010101010))
  23. (pass-if (eq? (array-type #*1010101010) 'b)))
  24. (with-test-prefix "equality"
  25. (pass-if (equal? #*1010101 #*1010101))
  26. (pass-if (array-equal? #*1010101 #*1010101))
  27. (pass-if (not (equal? #*10101010 #*1010101)))
  28. (pass-if (not (array-equal? #*10101010 #*1010101))))
  29. (with-test-prefix "lists"
  30. (pass-if (equal? (bitvector->list #*10010) '(#t #f #f #t #f)))
  31. (pass-if (equal? (array->list #*10010) '(#t #f #f #t #f)))
  32. (pass-if (equal? #*10010 (list->bitvector '(#t #f #f #t #f)))))
  33. (with-test-prefix "ref and set"
  34. (with-test-prefix "as bitvector"
  35. (let ((bv (list->bitvector '(#f #f #t #f #t))))
  36. (pass-if (eqv? (bitvector-ref bv 0) #f))
  37. (pass-if (eqv? (bitvector-ref bv 2) #t))
  38. (bitvector-set! bv 0 #t)
  39. (pass-if (eqv? (bitvector-ref bv 0) #t))))
  40. (with-test-prefix "as array"
  41. (let ((bv (list->bitvector '(#f #f #t #f #t))))
  42. (pass-if (eqv? (array-ref bv 0) #f))
  43. (pass-if (eqv? (array-ref bv 2) #t))
  44. (array-set! bv #t 0)
  45. (pass-if (eqv? (array-ref bv 0) #t)))))
  46. (with-test-prefix "bit-set*!"
  47. (pass-if "#t"
  48. (let ((v (bitvector #t #t #f #f)))
  49. (bit-set*! v #*1010 #t)
  50. (equal? v #*1110)))
  51. (pass-if "#f"
  52. (let ((v (bitvector #t #t #f #f)))
  53. (bit-set*! v #*1010 #f)
  54. (equal? v #*0100)))
  55. (pass-if "#t, shorter"
  56. (let ((v (bitvector #t #t #f #f)))
  57. (bit-set*! v #*101 #t)
  58. (equal? v #*1110)))
  59. (pass-if "#f, shorter"
  60. (let ((v (bitvector #t #t #f #f)))
  61. (bit-set*! v #*101 #f)
  62. (equal? v #*0100))))
  63. (with-test-prefix "bit-count*"
  64. (pass-if-equal 3 (bit-count* #*01110111 #*11001101 #t))
  65. (pass-if-equal 2 (bit-count* #*01110111 #u32(7 0 4) #f)))