bitvectors.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;;; bitvectors.test --- tests guile's bitvectors -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2010, 2011, 2013, 2014, 2020 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-bit-set? bv 0) #f))
  37. (pass-if (eqv? (bitvector-bit-set? bv 2) #t))
  38. (bitvector-set-bit! bv 0)
  39. (pass-if (eqv? (bitvector-bit-set? bv 0) #t))
  40. (pass-if (eqv? (bitvector-bit-clear? bv 0) #f))
  41. (bitvector-clear-bit! bv 0)
  42. (pass-if (eqv? (bitvector-bit-set? bv 0) #f))
  43. (pass-if (eqv? (bitvector-bit-clear? bv 0) #t))))
  44. (with-test-prefix "as array"
  45. (let ((bv (list->bitvector '(#f #f #t #f #t))))
  46. (pass-if (eqv? (array-ref bv 0) #f))
  47. (pass-if (eqv? (array-ref bv 2) #t))
  48. (array-set! bv #t 0)
  49. (pass-if (eqv? (array-ref bv 0) #t)))))
  50. (with-test-prefix "all bits"
  51. (let ((bv (make-bitvector 5)))
  52. (pass-if-equal #*00000 bv)
  53. (bitvector-set-all-bits! bv)
  54. (pass-if-equal #*11111 bv)
  55. (bitvector-clear-all-bits! bv)
  56. (pass-if-equal #*00000 bv)
  57. (bitvector-flip-all-bits! bv)
  58. (pass-if-equal #*11111 bv)
  59. (bitvector-flip-all-bits! bv)
  60. (pass-if-equal #*00000 bv)))
  61. (with-test-prefix "bitvector-set-bits!"
  62. (pass-if "#t"
  63. (let ((v (bitvector #t #t #f #f)))
  64. (bitvector-set-bits! v #*1010)
  65. (equal? v #*1110)))
  66. (pass-if "#t, shorter"
  67. (let ((v (bitvector #t #t #f #f)))
  68. (bitvector-set-bits! v #*101)
  69. (equal? v #*1110))))
  70. (with-test-prefix "bitvector-clear-bits!"
  71. (pass-if "#f"
  72. (let ((v (bitvector #t #t #f #f)))
  73. (bitvector-clear-bits! v #*1010)
  74. (equal? v #*0100)))
  75. (pass-if "#f, shorter"
  76. (let ((v (bitvector #t #t #f #f)))
  77. (bitvector-clear-bits! v #*101)
  78. (equal? v #*0100))))
  79. (with-test-prefix "bitvector-count"
  80. (pass-if-equal 6 (bitvector-count #*01110111))
  81. (pass-if-equal 2 (let ((bv #*01110111))
  82. (- (bitvector-length bv) (bitvector-count bv)))))
  83. (with-test-prefix "bitvector-position"
  84. (pass-if-equal 0 (bitvector-position #*01110111 #f))
  85. (pass-if-equal 1 (bitvector-position #*01110111 #t))
  86. (pass-if-equal 4 (bitvector-position #*01110111 #f 1))
  87. (pass-if-equal 4 (bitvector-position #*01110111 #f 4))
  88. (pass-if-equal 5 (bitvector-position #*01110111 #t 5))
  89. (pass-if-equal #f (bitvector-position #*01110111 #f 5)))
  90. (with-test-prefix "bitvector-count-bits"
  91. (pass-if-equal 3 (bitvector-count-bits #*01110111 #*11001101)))