Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / test-suite / tests / encoding-iso88597.test
1 ;;;; strings.test --- test suite for Guile's string functions -*- mode: scheme; coding: iso-8859-7 -*-
2 ;;;;
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
19
20 (define-module (test-strings)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1))
23
24 (define exception:conversion
25 (cons 'misc-error "^cannot convert to output locale"))
26
27 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
28 (define (string-ints . args)
29 (apply string (map integer->char args)))
30
31 (define oldlocale #f)
32 (if (defined? 'setlocale)
33 (set! oldlocale (setlocale LC_ALL "")))
34
35 (define s1 "Ðåñß")
36 (define s2 "ôçò")
37 (define s3 "êñéôéêÞò")
38 (define s4 "êáé")
39
40 (with-test-prefix "string length"
41
42 (pass-if "s1"
43 (eq? (string-length s1) 4))
44
45 (pass-if "s2"
46 (eq? (string-length s2) 3))
47
48 (pass-if "s3"
49 (eq? (string-length s3) 8))
50
51 (pass-if "s4"
52 (eq? (string-length s4) 3)))
53
54 (with-test-prefix "internal encoding"
55
56 (pass-if "s1"
57 (string=? s1 (string-ints #x03a0 #x03b5 #x03c1 #x03af)))
58
59 (pass-if "s2"
60 (string=? s2 (string-ints #x03c4 #x03b7 #x03c2)))
61
62 (pass-if "s3"
63 (string=? s3 (string-ints #x03ba #x03c1 #x03b9 #x03c4 #x03b9 #x03ba #x03ae #x03c2)))
64
65 (pass-if "s4"
66 (string=? s4 (string-ints #x03ba #x03b1 #x03b9))))
67
68 (with-test-prefix "chars"
69
70 (pass-if "s1"
71 (list= eqv? (string->list s1)
72 (list #\Ð #\å #\ñ #\ß)))
73
74 (pass-if "s2"
75 (list= eqv? (string->list s2)
76 (list #\ô #\ç #\ò)))
77
78 (pass-if "s3"
79 (list= eqv? (string->list s3)
80 (list #\ê #\ñ #\é #\ô #\é #\ê #\Þ #\ò)))
81
82 (pass-if "s4"
83 (list= eqv? (string->list s4)
84 (list #\ê #\á #\é))))
85
86 ;; Testing that the display of the string is output in the ISO-8859-7
87 ;; encoding
88 (with-test-prefix "display"
89
90 (pass-if "s1"
91 (let ((pt (open-output-string)))
92 (set-port-encoding! pt "ISO-8859-7")
93 (display s1 pt)
94 (list= eqv?
95 (list #xd0 #xe5 #xf1 #xdf)
96 (u8vector->list
97 (get-output-locale-u8vector pt)))))
98 (pass-if "s2"
99 (let ((pt (open-output-string)))
100 (set-port-encoding! pt "ISO-8859-7")
101 (display s2 pt)
102 (list= eqv?
103 (list #xf4 #xe7 #xf2)
104 (u8vector->list
105 (get-output-locale-u8vector pt))))))
106
107 (with-test-prefix "symbols == strings"
108
109 (pass-if "Ðåñß"
110 (eq? (string->symbol s1) 'Ðåñß))
111
112 (pass-if "ôçò"
113 (eq? (string->symbol s2) 'ôçò))
114
115 (pass-if "êñéôéêÞò"
116 (eq? (string->symbol s3) 'êñéôéêÞò))
117
118 (pass-if "êáé"
119 (eq? (string->symbol s4) 'êáé)))
120
121 (with-test-prefix "non-ascii variable names"
122
123 (pass-if "1"
124 (let ((á 1)
125 (ñ 2))
126 (eq? (+ á ñ) 3))))
127
128 (with-test-prefix "output errors"
129
130 (pass-if-exception "char #x0400"
131 exception:conversion
132 (let ((pt (open-output-string)))
133 (set-port-encoding! pt "ISO-8859-7")
134 (set-port-conversion-strategy! pt 'error)
135 (display (string-ints #x0400) pt))))
136
137 ;; Reset locale
138 (if (defined? 'setlocale)
139 (setlocale LC_ALL oldlocale))