test-string-to-number.scm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; Copyright (C) 2023 David Thompson
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. (use-modules (srfi srfi-64)
  15. (test utils))
  16. (test-begin "test-string-to-number")
  17. (define-syntax test-string->number
  18. (syntax-rules ()
  19. ((_ str) (test-string->number str str))
  20. ((_ expected-output str)
  21. (test-string->number expected-output expected-output str))
  22. ((_ scheme-repr reflect-repr str)
  23. (test-call reflect-repr (lambda (x) (string->number x)) str))))
  24. (test-string->number "42")
  25. (test-string->number "-42")
  26. (test-string->number "42000.0" "42e3")
  27. (test-string->number "0.042" "42e-3")
  28. (test-string->number "42" "#b101010")
  29. (test-string->number "42" "#o52")
  30. (test-string->number "42" "#d42")
  31. (test-string->number "-42" "#d-42")
  32. (test-string->number "42" "#x2a")
  33. (test-string->number "42" "#x2A")
  34. (test-string->number "1/2")
  35. (test-string->number "-1/2")
  36. (test-string->number "+inf.0")
  37. (test-string->number "-inf.0")
  38. (test-string->number "+nan.0")
  39. (test-string->number "+nan.0" "-nan.0")
  40. (test-string->number "6/5" "#e1.2")
  41. (test-string->number "42.0" "#i42")
  42. (test-string->number "255.0" "#i#xff")
  43. (test-string->number "255.0" "#x#iff")
  44. (test-string->number "42.0" "42.")
  45. (test-string->number "0.0+1.0i" "+i")
  46. (test-string->number "0.0-1.0i" "-i")
  47. (test-string->number "2.0+1.0i" "2+i")
  48. (test-string->number "1.0-2.0i" "1-2i")
  49. (test-string->number "2" "2@0")
  50. ;; Some invalid strings
  51. (test-string->number "#f" "")
  52. (test-string->number "#f" ".")
  53. (test-string->number "#f" "foo")
  54. (test-string->number "#f" "+foo")
  55. (test-string->number "#f" "-foo")
  56. (test-string->number "#f" "#xfoo")
  57. (test-string->number "#f" "1/foo")
  58. (test-string->number "#f" "foo/2")
  59. (test-string->number "#f" "foo.0")
  60. (test-string->number "#f" "-1/-2")
  61. (test-string->number "#f" "1.0/2")
  62. (test-string->number "#f" "#x1.2")
  63. (test-string->number "#f" "+inf")
  64. (test-string->number "#f" "inf.0")
  65. (test-string->number "#f" "nan.0")
  66. (test-end* "test-string-to-number")