test-utils-test.scm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (use-modules
  2. ;; SRFI 64 for unit testing facilities
  3. (srfi srfi-64)
  4. ;; utils - the code to be tested
  5. (utils test))
  6. (test-begin "test-utils-test")
  7. (test-group
  8. "approximately-equal?"
  9. ;; (value1 value2 epsilon)
  10. ;; simple numbers
  11. (test-assert "approximately-equal?-1"
  12. (approximately-equal? 1 1.001 0.002))
  13. (test-assert "approximately-equal?-2"
  14. (not (approximately-equal? 1 0.99 0.002)))
  15. (test-assert "approximately-equal?-3"
  16. (approximately-equal? 1 0.999 0.002))
  17. (test-assert "approximately-equal?-4"
  18. ;; Cannot specify as decimal numbers here, because of floating point
  19. ;; inprecision.
  20. (approximately-equal? 1 (/ 999 1000) (/ 1 1000))))
  21. (test-group
  22. "vectors-approximately-equal?"
  23. ;; (v1 v2 epsilon compare-proc)
  24. ;; simple vectors
  25. (test-assert "vectors-approximately-equal?-1"
  26. (vectors-approximately-equal?
  27. #(1 -2 3 4.1 -5.001)
  28. #(1 -1.998 3 4.1 -5.001)
  29. #|epsilon|#
  30. 0.003
  31. approximately-equal?))
  32. ;; Test if it also works for other things than numbers.
  33. (test-assert "vectors-approximately-equal?-2"
  34. (vectors-approximately-equal?
  35. #("a" "b" "cc" "a")
  36. #("" "ba" "acc" "a")
  37. "a"
  38. (lambda (s1 s2 epsilon)
  39. (or (string=? s1 s2)
  40. (string=? (string-append s1 epsilon) s2)
  41. (string=? (string-append epsilon s1) s2)
  42. (string=? (string-append epsilon s2) s1)
  43. (string=? (string-append s2 epsilon) s1))))))
  44. (test-group
  45. "lists-of-vectors-approximately-equal?"
  46. ;; (l1 l2 epsilon compare-proc)
  47. ;; list of vectors
  48. (test-assert "lists-of-vectors-approximately-equal?-1"
  49. (not
  50. (lists-of-vectors-approximately-equal?
  51. (list #(1 2 3) #(1.1 1.9 3))
  52. (list #(1 2 3) #(1.1 1.9 3.15))
  53. 0.1
  54. (lambda (le1 le2 epsilon)
  55. (vectors-approximately-equal? le1 le2 epsilon approximately-equal?)))))
  56. (test-assert "lists-of-vectors-approximately-equal?-2"
  57. (lists-of-vectors-approximately-equal?
  58. (list (vector 1 2 3)
  59. (vector 1 2 3))
  60. (list (vector 1 2 3)
  61. (vector (/ 11 10) (/ 19 10) (/ 31 10)))
  62. (/ 1 10)
  63. (lambda (le1 le2 epsilon)
  64. (vectors-approximately-equal? le1 le2 epsilon approximately-equal?)))))
  65. (test-group
  66. "vector-set"
  67. ;; Test if the update works.
  68. (test-equal "vector-set-1"
  69. (vector 1 1 10 1)
  70. (vector-set (vector 1 1 1 1) 2 10))
  71. ;; Test if the original vector is unchanged.
  72. (test-equal "vector-set-2"
  73. (vector 1 1 1 1)
  74. (let ([original-vector (vector 1 1 1 1)])
  75. (vector-set original-vector 2 10)
  76. original-vector)))
  77. (test-end "test-utils-test")