test-strings.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  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. ;;; Commentary:
  15. ;;;
  16. ;;; String tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-strings")
  22. (test-call "3" (lambda (str) (string-length str)) "fox")
  23. (test-call "#\\f" (lambda (str) (string-ref str 0)) "fox")
  24. (test-call "#\\x" (lambda (str) (string-ref str 2)) "fox")
  25. (test-call "#t" (lambda (a b) (string=? a b)) "Even" "Even")
  26. (test-call "#t" (lambda (a b) (string>? a b)) "Odd" "Even")
  27. (test-call "#t" (lambda (a b) (string<? a b)) "Even" "Odd")
  28. (test-call "\"abc\"" (lambda (a) (string-copy a)) "abc")
  29. (test-call "\"IBM\""
  30. (lambda (str)
  31. (string-map (lambda (c)
  32. (integer->char (+ 1 (char->integer c))))
  33. str))
  34. "HAL")
  35. ;; Enable when string-map supports 2+ strings.
  36. ;; (test-call "\"StUdLyCaPs\""
  37. ;; (lambda (a b)
  38. ;; (string-map
  39. ;; (lambda (c k)
  40. ;; ((if (eqv? k #\u) char-upcase char-downcase) c))
  41. ;; a b))
  42. ;; "studlycaps xxx"
  43. ;; "ululululul")
  44. ;; String mutation
  45. (with-additional-imports
  46. ((only (hoot strings) mutable-string?))
  47. (test-call "#f" (lambda (a) (mutable-string? a)) "abc")
  48. (test-call "#t" (lambda () (mutable-string? (make-string 1))))
  49. (test-call "#t" (lambda () (mutable-string? (string-copy "abc"))))
  50. (test-call "#t" (lambda () (mutable-string? (string #\a #\b #\c)))))
  51. (test-call "\"1@3\"" (lambda (a)
  52. (let ((a (string-copy a)))
  53. (string-set! a 1 #\@)
  54. a))
  55. "123")
  56. (test-call "\"a123e\"" (lambda (a)
  57. (let ((b (string-copy "abcde")))
  58. (string-copy! b 1 a 0 3)
  59. b))
  60. "12345")
  61. (test-call "\"a!!!!f\"" (lambda ()
  62. (let ((a (string-copy "abcdef")))
  63. (string-fill! a #\! 1 5)
  64. a)))
  65. (test-equal "passing big strings to the host"
  66. (string-append "\"" (make-string 200001 #\W) "\"")
  67. (compile-call* '(lambda (str) (string-upcase str))
  68. (make-string 200001 #\w)))
  69. (test-end* "test-strings")