test-strings.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. (with-additional-imports
  45. ((only (guile) char-set:lower-case
  46. string-index string-rindex
  47. string-prefix? string-prefix-ci?
  48. string-reverse
  49. string-suffix? string-suffix-ci?
  50. string-trim string-trim-right string-trim-both))
  51. (test-call "2" (lambda (str c) (string-index str c)) "fox" #\x)
  52. (test-call "2" (lambda (str c) (string-index str c)) "fox"
  53. (lambda (c) (char=? c #\x)))
  54. (test-call "0" (lambda (str) (string-index str char-set:lower-case))
  55. "fox")
  56. (test-call "2" (lambda (str c start) (string-index str c start)) "fox" #\x 1)
  57. (test-call "1" (lambda (str c start end) (string-index str c start end))
  58. "fox" #\o 0 2)
  59. (test-call "3" (lambda (str c) (string-rindex str c)) "foxx" #\x)
  60. (test-call "3" (lambda (str c) (string-rindex str c)) "foxx"
  61. (lambda (c) (char=? c #\x)))
  62. (test-call "3" (lambda (str c start) (string-rindex str c start))
  63. "foxx" #\x 1)
  64. (test-call "3" (lambda (str c start end) (string-rindex str c start end))
  65. "foxx" #\x 1 4)
  66. (test-call "#t" (lambda (str1 str2) (string-prefix? str1 str2)) "fo" "fox")
  67. (test-call "#t" (lambda (str1 str2 start1 end1 start2 end2)
  68. (string-prefix? str1 str2 start1 end1 start2 end2))
  69. "bird" "birdie" 1 3 1 4)
  70. (test-call "#f" (lambda (str1 str2) (string-prefix? str1 str2)) "Fo" "fox")
  71. (test-call "#f" (lambda (str1 str2) (string-prefix? str1 str2)) "of" "fox")
  72. (test-call "#f" (lambda (str1 str2 start1 end1 start2 end2)
  73. (string-prefix? str1 str2 start1 end1 start2 end2))
  74. "bird" "birdie" 1 3 2 4)
  75. (test-call "#t" (lambda (str1 str2) (string-prefix-ci? str1 str2)) "fO" "FoX")
  76. (test-call "#t" (lambda (str1 str2 start1 end1 start2 end2)
  77. (string-prefix-ci? str1 str2 start1 end1 start2 end2))
  78. "bIrD" "BiRdIe" 1 3 1 4)
  79. (test-call "#f" (lambda (str1 str2) (string-prefix-ci? str1 str2)) "oF" "FoX")
  80. (test-call "#f" (lambda (str1 str2 start1 end1 start2 end2)
  81. (string-prefix-ci? str1 str2 start1 end1 start2 end2))
  82. "bIrD" "BiRdIe" 1 3 2 4)
  83. (test-call "\"esrever\"" (lambda (str) (string-reverse str)) "reverse")
  84. (test-call "\"rerevse\"" (lambda (str start end) (string-reverse str start end))
  85. "reverse" 2 5)
  86. (test-call "#t" (lambda (str1 str2) (string-suffix? str1 str2)) "ox" "fox")
  87. (test-call "#t" (lambda (str1 str2 start1 end1 start2 end2)
  88. (string-suffix? str1 str2 start1 end1 start2 end2))
  89. "rdi" "birdie" 1 3 1 5)
  90. (test-call "#f" (lambda (str1 str2) (string-suffix? str1 str2)) "Ox" "fox")
  91. (test-call "#f" (lambda (str1 str2) (string-suffix? str1 str2)) "xo" "fox")
  92. (test-call "#f" (lambda (str1 str2 start1 end1 start2 end2)
  93. (string-suffix? str1 str2 start1 end1 start2 end2))
  94. "rdi" "birdie" 1 3 1 3)
  95. (test-call "#t" (lambda (str1 str2) (string-suffix-ci? str1 str2)) "Ox" "FoX")
  96. (test-call "#t" (lambda (str1 str2 start1 end1 start2 end2)
  97. (string-suffix-ci? str1 str2 start1 end1 start2 end2))
  98. "IrDi" "BiRdIe" 1 3 1 4)
  99. (test-call "#f" (lambda (str1 str2) (string-suffix-ci? str1 str2)) "oF" "FoX")
  100. (test-call "#f" (lambda (str1 str2 start1 end1 start2 end2)
  101. (string-suffix-ci? str1 str2 start1 end1 start2 end2))
  102. "IrDi" "BiRdIe" 1 3 1 5)
  103. (test-call "\"fox \"" (lambda (str) (string-trim str)) " fox ")
  104. (test-call "\" fox\"" (lambda (str) (string-trim-right str)) " fox ")
  105. (test-call "\"fox\"" (lambda (str) (string-trim-both str)) " fox "))
  106. ;; String mutation
  107. (with-additional-imports
  108. ((only (hoot strings) mutable-string?))
  109. (test-call "#f" (lambda (a) (mutable-string? a)) "abc")
  110. (test-call "#t" (lambda () (mutable-string? (make-string 1))))
  111. (test-call "#t" (lambda () (mutable-string? (string-copy "abc"))))
  112. (test-call "#t" (lambda () (mutable-string? (string #\a #\b #\c)))))
  113. (test-call "\"1@3\"" (lambda (a)
  114. (let ((a (string-copy a)))
  115. (string-set! a 1 #\@)
  116. a))
  117. "123")
  118. (test-call "\"a123e\"" (lambda (a)
  119. (let ((b (string-copy "abcde")))
  120. (string-copy! b 1 a 0 3)
  121. b))
  122. "12345")
  123. (test-call "\"a!!!!f\"" (lambda ()
  124. (let ((a (string-copy "abcdef")))
  125. (string-fill! a #\! 1 5)
  126. a)))
  127. (test-equal "passing big strings to the host"
  128. (string-append "\"" (make-string 200001 #\W) "\"")
  129. (call-wasm (compile-main '(lambda (str) (string-upcase str)))
  130. (compile-aux (make-string 200001 #\w))))
  131. (test-end* "test-strings")