Debian Codegraph @

test-suite/tests/i18n.test

raw
1 ;;;; i18n.test --- Exercise the i18n API. -*- coding: utf-8; mode: scheme; -*-
2 ;;;;
3 ;;;; Copyright (C) 2006-2007,2009-2019,2021 Free Software Foundation, Inc.
4 ;;;; Author: Ludovic Courtès
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite i18n)
21 #:use-module (ice-9 i18n)
22 #:use-module (ice-9 format)
23 #:use-module (srfi srfi-1)
24 #:use-module (test-suite lib))
25
26 ;; Start from a pristine locale state.
27 (setlocale LC_ALL "C")
28
29 (define exception:locale-error
30 (cons 'system-error "Failed to install locale"))
31
32
33
34 (with-test-prefix "locale objects"
35
36 (pass-if "make-locale (2 args)"
37 (not (not (make-locale LC_ALL "C"))))
38
39 (pass-if "make-locale (2 args, list)"
40 (not (not (make-locale (list LC_COLLATE LC_NUMERIC) "C"))))
41
42 (pass-if "make-locale (3 args)"
43 (not (not (make-locale (list LC_COLLATE) "C"
44 (make-locale (list LC_NUMERIC) "C")))))
45
46 (pass-if-exception "make-locale with unknown locale" exception:locale-error
47 (make-locale LC_ALL "does-not-exist"))
48
49 (pass-if "locale?"
50 (and (locale? (make-locale (list LC_ALL) "C"))
51 (locale? (make-locale (list LC_TIME LC_NUMERIC) "C"
52 (make-locale (list LC_CTYPE) "C")))))
53
54 (pass-if "%global-locale"
55 (and (locale? %global-locale))
56 (locale? (make-locale (list LC_MONETARY) "C"
57 %global-locale))))
58
59
60
61 (with-test-prefix "text collation (English)"
62
63 (pass-if "string-locale<?"
64 (and (string-locale<? "hello" "world")
65 (string-locale<? "hello" "world"
66 (make-locale (list LC_COLLATE) "C"))))
67
68 (pass-if "char-locale<?"
69 (and (char-locale<? #\a #\b)
70 (char-locale<? #\a #\b (make-locale (list LC_COLLATE) "C"))))
71
72 (pass-if "string-locale-ci=?"
73 (and (string-locale-ci=? "Hello" "HELLO")
74 (string-locale-ci=? "Hello" "HELLO"
75 (make-locale (list LC_COLLATE) "C"))))
76
77 (pass-if "string-locale-ci<?"
78 (and (string-locale-ci<? "hello" "WORLD")
79 (string-locale-ci<? "hello" "WORLD"
80 (make-locale (list LC_COLLATE) "C"))))
81 (pass-if "large strings"
82 ;; In Guile <= 2.2.4, these would overflow the C stack and crash.
83 (let ((large (make-string 4000000 #\a)))
84 (and (string-locale-ci=? large large)
85 (not (string-locale-ci<? large large))
86 (not (string-locale<? large large))))))
87
88
89 (define mingw?
90 (string-contains %host-type "-mingw32"))
91
92 (define %french-locale-name
93 (if mingw?
94 "fra_FRA.850"
95 "fr_FR.iso88591")) ;"iso88591" is the "normalized codeset"
96
97 ;; What we really want for the following locales is that they be Unicode
98 ;; capable, not necessarily UTF-8, which Windows does not provide.
99
100 (define %french-utf8-locale-name
101 (if mingw?
102 "fra_FRA.1252"
103 "fr_FR.utf8")) ;"utf8" is the "normalized codeset"
104
105 (define %turkish-utf8-locale-name
106 (if mingw?
107 "tur_TRK.1254"
108 "tr_TR.utf8"))
109
110 (define %german-utf8-locale-name
111 (if mingw?
112 "deu_DEU.1252"
113 "de_DE.utf8"))
114
115 (define %greek-utf8-locale-name
116 (if mingw?
117 "grc_ELL.1253"
118 "el_GR.utf8"))
119
120 (define %american-english-locale-name
121 "en_US.utf8")
122
123 (define %french-locale
124 (false-if-exception
125 (make-locale (list LC_CTYPE LC_COLLATE LC_NUMERIC LC_TIME LC_MONETARY)
126 %french-locale-name)))
127
128 (define %french-utf8-locale
129 (false-if-exception
130 (make-locale (list LC_CTYPE LC_COLLATE LC_NUMERIC LC_TIME LC_MONETARY)
131 %french-utf8-locale-name)))
132
133 (define %german-utf8-locale
134 (false-if-exception
135 (make-locale LC_ALL
136 %german-utf8-locale-name)))
137
138 (define %greek-utf8-locale
139 (false-if-exception
140 (make-locale LC_ALL
141 %greek-utf8-locale-name)))
142
143 (define %turkish-utf8-locale
144 (false-if-exception
145 (make-locale LC_ALL
146 %turkish-utf8-locale-name)))
147
148 (define %american-english-locale
149 (false-if-exception
150 (make-locale LC_ALL
151 %american-english-locale-name)))
152
153 (define (under-locale-or-unresolved locale thunk)
154 ;; On non-GNU systems, an exception may be raised only when the locale is
155 ;; actually used rather than at `make-locale'-time. Thus, we must guard
156 ;; against both.
157 (if locale
158 (if (string-contains %host-type "-gnu")
159 (thunk)
160 (catch 'system-error thunk
161 (lambda (key . args)
162 (throw 'unresolved))))
163 (throw 'unresolved)))
164
165 (define (under-french-locale-or-unresolved thunk)
166 (under-locale-or-unresolved %french-locale thunk))
167
168 (define (under-french-utf8-locale-or-unresolved thunk)
169 (under-locale-or-unresolved %french-utf8-locale thunk))
170
171 (define (under-turkish-utf8-locale-or-unresolved thunk)
172 ;; FreeBSD 8.2 and 9.1, Solaris 2.10, Darwin 8.11.0, Cygwin, and MinGW have
173 ;; a broken tr_TR locale where `i' is mapped to uppercase `I'
174 ;; instead of `İ', so disable tests on that platform.
175 (if (or (string-contains %host-type "freebsd8")
176 (string-contains %host-type "freebsd9")
177 (string-contains %host-type "solaris2.10")
178 (string-contains %host-type "darwin8")
179 (string-contains %host-type "mingw32")
180 (string-contains %host-type "cygwin"))
181 (throw 'unresolved)
182 (under-locale-or-unresolved %turkish-utf8-locale thunk)))
183
184 (define (under-german-utf8-locale-or-unresolved thunk)
185 (under-locale-or-unresolved %german-utf8-locale thunk))
186
187 (define (under-greek-utf8-locale-or-unresolved thunk)
188 (under-locale-or-unresolved %greek-utf8-locale thunk))
189
190 (define (under-american-english-locale-or-unresolved thunk)
191 (under-locale-or-unresolved %american-english-locale thunk))
192
193
194 (with-test-prefix "text collation (French)"
195
196 (pass-if "string-locale<?"
197 (under-french-locale-or-unresolved
198 (lambda ()
199 (string-locale<? "été" "hiver" %french-locale))))
200
201 (pass-if "char-locale<?"
202 (under-french-locale-or-unresolved
203 (lambda ()
204 (char-locale<? #\é #\h %french-locale))))
205
206 (pass-if "string-locale-ci=?"
207 (under-french-locale-or-unresolved
208 (lambda ()
209 (string-locale-ci=? "ÉTÉ" "été" %french-locale))))
210
211 (pass-if "string-locale-ci=? (2 args, wide strings)"
212 (under-french-utf8-locale-or-unresolved
213 (lambda ()
214 ;; Note: Character `œ' is not part of Latin-1, so these are wide
215 ;; strings.
216 (dynamic-wind
217 (lambda ()
218 (setlocale LC_ALL %french-utf8-locale-name))
219 (lambda ()
220 (string-locale-ci=? "œuf" "ŒUF"))
221 (lambda ()
222 (setlocale LC_ALL "C"))))))
223
224 (pass-if "string-locale<?, bis"
225 (under-french-utf8-locale-or-unresolved
226 (lambda ()
227 (let* ((strings (list "œa" "œb"))
228 (heads (map (lambda (s) (substring/shared s 0 1)) strings)))
229 (not (string-locale<? (car heads) (cadr heads)
230 %french-utf8-locale))))))
231
232 (pass-if "string-locale-ci=?, bis"
233 (under-french-utf8-locale-or-unresolved
234 (lambda ()
235 (let* ((strings (list "œa" "œb"))
236 (heads (map (lambda (s) (substring/shared s 0 1)) strings)))
237 (string-locale-ci=? (car heads) (cadr heads)
238 %french-utf8-locale)))))
239
240 (pass-if "string-locale-ci=? (3 args, wide strings)"
241 (under-french-utf8-locale-or-unresolved
242 (lambda ()
243 (string-locale-ci=? "œuf" "ŒUF" %french-utf8-locale))))
244
245 (pass-if "string-locale-ci<>?"
246 (under-french-locale-or-unresolved
247 (lambda ()
248 (and (string-locale-ci<? "été" "Hiver" %french-locale)
249 (string-locale-ci>? "HiVeR" "été" %french-locale)))))
250
251 (pass-if "string-locale-ci<>? (wide strings)"
252 (under-french-utf8-locale-or-unresolved
253 (lambda ()
254 ;; One of the strings is UCS-4, the other is Latin-1.
255 (and (string-locale-ci<? "Œdème" "œuf" %french-utf8-locale)
256 (string-locale-ci>? "Œuf" "œdÈMe" %french-utf8-locale)))))
257
258 (pass-if "string-locale-ci<>? (wide and narrow strings)"
259 (under-french-utf8-locale-or-unresolved
260 (lambda ()
261 ;; One of the strings is UCS-4, the other is Latin-1.
262 (and (string-locale-ci>? "Œdème" "odyssée" %french-utf8-locale)
263 (string-locale-ci<? "Odyssée" "œdème" %french-utf8-locale)))))
264
265 (pass-if "char-locale-ci<>?"
266 (under-french-locale-or-unresolved
267 (lambda ()
268 (and (char-locale-ci<? #\é #\H %french-locale)
269 (char-locale-ci>? #\h #\É %french-locale)))))
270
271 (pass-if "char-locale-ci<>? (wide)"
272 (under-french-utf8-locale-or-unresolved
273 (lambda ()
274 (and (char-locale-ci<? #\o #\œ %french-utf8-locale)
275 (char-locale-ci>? #\Œ #\e %french-utf8-locale))))))
276
277
278 (with-test-prefix "text collation (German)"
279
280 (pass-if "string-locale-ci=?"
281 (under-german-utf8-locale-or-unresolved
282 (lambda ()
283 (let ((de (make-locale LC_ALL %german-utf8-locale-name)))
284 (string-locale-ci=? "Straße" "STRASSE"))))))
285
286
287 (with-test-prefix "text collation (Greek)"
288
289 (pass-if "string-locale-ci=?"
290 (under-greek-utf8-locale-or-unresolved
291 (lambda ()
292 (let ((gr (make-locale LC_ALL %greek-utf8-locale-name)))
293 (string-locale-ci=? "ΧΑΟΣ" "χαος" gr))))))
294
295
296 (with-test-prefix "text collation (Czech)"
297
298 (pass-if "string-locale<? for 'ch'"
299 (under-locale-or-unresolved
300 "cs_CZ.utf8"
301 (lambda ()
302 ;; Czech sorts digraph 'ch' between 'h' and 'i'.
303 ;;
304 ;; GNU libc 2.22 gets this wrong:
305 ;; <https://sourceware.org/bugzilla/show_bug.cgi?id=18589>. For
306 ;; now, just skip it if it fails (XXX).
307 (or (and (string-locale>? "chxxx" "cxxx")
308 (string-locale>? "chxxx" "hxxx")
309 (string-locale<? "chxxxx" "ixxx"))
310 (throw 'unresolved))))))
311
312
313 (with-test-prefix "character mapping"
314
315 (pass-if "char-locale-downcase"
316 (and (eqv? #\a (char-locale-downcase #\A))
317 (eqv? #\a (char-locale-downcase #\A (make-locale LC_ALL "C")))))
318
319 (pass-if "char-locale-upcase"
320 (and (eqv? #\Z (char-locale-upcase #\z))
321 (eqv? #\Z (char-locale-upcase #\z (make-locale LC_ALL "C")))))
322
323 (pass-if "char-locale-titlecase"
324 (and (eqv? #\T (char-locale-titlecase #\t))
325 (eqv? #\T (char-locale-titlecase #\t (make-locale LC_ALL "C")))))
326
327 (pass-if "char-locale-titlecase Dž"
328 (and (eqv? #\762 (char-locale-titlecase #\763))
329 (eqv? #\762 (char-locale-titlecase #\763 (make-locale LC_ALL "C")))))
330
331 (pass-if "char-locale-upcase Turkish"
332 (under-turkish-utf8-locale-or-unresolved
333 (lambda ()
334 (eqv? #\İ (char-locale-upcase #\i %turkish-utf8-locale)))))
335
336 (pass-if "char-locale-downcase Turkish"
337 (under-turkish-utf8-locale-or-unresolved
338 (lambda ()
339 (eqv? #\i (char-locale-downcase #\İ %turkish-utf8-locale))))))
340
341
342 (with-test-prefix "string mapping"
343
344 (pass-if "string-locale-downcase"
345 (and (string=? "a" (string-locale-downcase "A"))
346 (string=? "a" (string-locale-downcase "A" (make-locale LC_ALL "C")))))
347
348 (pass-if "string-locale-upcase"
349 (and (string=? "Z" (string-locale-upcase "z"))
350 (string=? "Z" (string-locale-upcase "z" (make-locale LC_ALL "C")))))
351
352 (pass-if "string-locale-titlecase"
353 (and (string=? "Hello, World" (string-locale-titlecase "hello, world"))
354 (string=? "Hello, World" (string-locale-titlecase
355 "hello, world" (make-locale LC_ALL "C")))))
356
357 (pass-if "large strings"
358 ;; In Guile <= 2.2.4, these would overflow the C stack and crash.
359 (let ((hellos (string-join (make-list 700000 "hello")))
360 (HELLOs (string-join (make-list 700000 "HELLO")))
361 (Hellos (string-join (make-list 700000 "Hello"))))
362 (and (string=? hellos (string-locale-downcase Hellos))
363 (string=? HELLOs (string-locale-upcase Hellos))
364 (string=? Hellos (string-locale-titlecase hellos)))))
365
366 (pass-if "string-locale-upcase German"
367 (under-german-utf8-locale-or-unresolved
368 (lambda ()
369 (let ((de (make-locale LC_ALL %german-utf8-locale-name)))
370 (string=? "STRASSE"
371 (string-locale-upcase "Straße" de))))))
372
373 (pass-if "string-locale-upcase Greek"
374 (under-greek-utf8-locale-or-unresolved
375 (lambda ()
376 (let ((el (make-locale LC_ALL %greek-utf8-locale-name)))
377 (string=? "ΧΑΟΣ"
378 (string-locale-upcase "χαος" el))))))
379
380 (pass-if "string-locale-upcase Greek (two sigmas)"
381 (under-greek-utf8-locale-or-unresolved
382 (lambda ()
383 (let ((el (make-locale LC_ALL %greek-utf8-locale-name)))
384 (string=? "ΓΕΙΆ ΣΑΣ"
385 (string-locale-upcase "Γειά σας" el))))))
386
387 (pass-if "string-locale-downcase Greek"
388 (under-greek-utf8-locale-or-unresolved
389 (lambda ()
390 (let ((el (make-locale LC_ALL %greek-utf8-locale-name)))
391 (string=? "χαος"
392 (string-locale-downcase "ΧΑΟΣ" el))))))
393
394 (pass-if "string-locale-downcase Greek (two sigmas)"
395 (under-greek-utf8-locale-or-unresolved
396 (lambda ()
397 (let ((el (make-locale LC_ALL %greek-utf8-locale-name)))
398 (string=? "γειά σας"
399 (string-locale-downcase "ΓΕΙΆ ΣΑΣ" el))))))
400
401 (pass-if "string-locale-upcase Turkish"
402 (under-turkish-utf8-locale-or-unresolved
403 (lambda ()
404 (string=? "İI" (string-locale-upcase "iı" %turkish-utf8-locale)))))
405
406 (pass-if "string-locale-downcase Turkish"
407 (under-turkish-utf8-locale-or-unresolved
408 (lambda ()
409 (string=? "iı" (string-locale-downcase "İI" %turkish-utf8-locale))))))
410
411
412 (with-test-prefix "number parsing"
413
414 (pass-if "locale-string->integer"
415 (call-with-values (lambda () (locale-string->integer "123"))
416 (lambda (result char-count)
417 (and (equal? result 123)
418 (equal? char-count 3)))))
419
420 (pass-if "locale-string->inexact"
421 (call-with-values
422 (lambda ()
423 (locale-string->inexact "123.456"
424 (make-locale (list LC_NUMERIC) "C")))
425 (lambda (result char-count)
426 (and (equal? result 123.456)
427 (equal? char-count 7)))))
428
429 (pass-if "locale-string->inexact (French)"
430 (under-french-locale-or-unresolved
431 (lambda ()
432 (call-with-values
433 (lambda ()
434 (locale-string->inexact "123,456" %french-locale))
435 (lambda (result char-count)
436 (and (equal? result 123.456)
437 (equal? char-count 7))))))))
438
439
440 ;;;
441 ;;; `nl-langinfo'
442 ;;;
443
444 (setlocale LC_ALL "C")
445 (define %c-locale (make-locale LC_ALL "C"))
446
447 (define %english-days
448 '("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"))
449
450 (define (every? . args)
451 (not (not (apply every args))))
452
453
454 (with-test-prefix "nl-langinfo et al."
455
456 (pass-if "locale-day (1 arg)"
457 (every? equal?
458 %english-days
459 (map locale-day (map 1+ (iota 7)))))
460
461 (pass-if "locale-day (2 args)"
462 (every? equal?
463 %english-days
464 (map (lambda (day)
465 (locale-day day %c-locale))
466 (map 1+ (iota 7)))))
467
468 (pass-if "locale-day (2 args, using `%global-locale')"
469 (every? equal?
470 %english-days
471 (map (lambda (day)
472 (locale-day day %global-locale))
473 (map 1+ (iota 7)))))
474
475 (pass-if "locale-day (French)"
476 (under-french-locale-or-unresolved
477 (lambda ()
478 (let ((result (locale-day 3 %french-locale)))
479 (and (string? result)
480 (string-ci=? result "mardi"))))))
481
482 (pass-if "locale-day (French, using `%global-locale')"
483 ;; Make sure `%global-locale' captures the current locale settings as
484 ;; installed using `setlocale'.
485 (under-french-locale-or-unresolved
486 (lambda ()
487 (dynamic-wind
488 (lambda ()
489 (setlocale LC_TIME %french-locale-name))
490 (lambda ()
491 (let* ((fr (make-locale (list LC_MONETARY) "C" %global-locale))
492 (result (locale-day 3 fr)))
493 (setlocale LC_ALL "C")
494 (and (string? result)
495 (string-ci=? result "mardi"))))
496 (lambda ()
497 (setlocale LC_ALL "C"))))))
498
499 (pass-if "default locale"
500 ;; Make sure the default locale does not capture the current locale
501 ;; settings as installed using `setlocale'. The default locale should be
502 ;; "C".
503 (under-french-locale-or-unresolved
504 (lambda ()
505 (dynamic-wind
506 (lambda ()
507 (setlocale LC_ALL %french-locale-name))
508 (lambda ()
509 (let* ((locale (make-locale (list LC_MONETARY) "C"))
510 (result (locale-day 3 locale)))
511 (setlocale LC_ALL "C")
512 (and (string? result)
513 (string-ci=? result "Tuesday"))))
514 (lambda ()
515 (setlocale LC_ALL "C"))))))
516
517 (pass-if "locale-am-string"
518 (not (not (member (locale-am-string)
519 '("AM" "am" "A.M." "a.m.")))))
520
521 (pass-if "locale-am-string (greek)"
522 (under-greek-utf8-locale-or-unresolved
523 (lambda ()
524 (not (not (member (locale-am-string %greek-utf8-locale)
525 '("ΠΜ" "πμ" "Π.Μ." "π.μ.")))))))
526
527 (pass-if "locale-pm-string"
528 (not (not (member (locale-pm-string)
529 '("PM" "pm" "P.M." "p.m.")))))
530
531 (pass-if "locale-pm-string (Greek)"
532 (under-greek-utf8-locale-or-unresolved
533 (lambda ()
534 (not (not (member (locale-pm-string %greek-utf8-locale)
535 '("ΜΜ" "μμ" "Μ.Μ." "μ.μ.")))))))
536
537 (pass-if "locale-digit-grouping"
538 ;; In the C locale, there is no rule for grouping.
539 (null? (locale-digit-grouping)))
540
541 (pass-if "locale-digit-grouping (French)"
542 (under-french-locale-or-unresolved
543 (lambda ()
544 ;; All systems that have a GROUPING nl_item should know
545 ;; that French numbers are grouped in 3 digit chunks.
546 ;; Those systems that have no GROUPING nl_item may use
547 ;; the hard-coded default of no grouping.
548 (let ((result (locale-digit-grouping %french-locale)))
549 (cond
550 ((null? result)
551 (throw 'unresolved))
552 ((eqv? 3 (false-if-exception (car result)))
553 #t)
554 (else
555 #f))))))
556
557 (pass-if "locale-positive-separated-by-space?"
558 ;; In any locale, this must be a boolean.
559 (let ((result (locale-positive-separated-by-space? #f)))
560 (or (eqv? #t result)
561 (eqv? #f result))))
562
563 (pass-if "locale-positive-separated-by-space? (international)"
564 ;; In any locale, this must be a boolean.
565 (let ((result (locale-positive-separated-by-space? #t)))
566 (or (eqv? #t result)
567 (eqv? #f result))))
568
569 (pass-if "locale-monetary-grouping"
570 ;; In the C locale, there is no rule for grouping of digits
571 ;; of monetary values.
572 (null? (locale-monetary-grouping)))
573
574 (pass-if "locale-monetary-grouping (French)"
575 (under-french-utf8-locale-or-unresolved
576 (lambda ()
577 ;; All systems that have a MON_GROUPING nl_item should know
578 ;; that French monetary values are grouped in 3 digit chunks.
579 ;; Those systems that have no MON_GROUPING nl_item may use the
580 ;; hard-coded default of no grouping.
581 (let ((result (locale-monetary-grouping %french-utf8-locale)))
582 (cond
583 ((null? result)
584 (throw 'unresolved))
585 ((eqv? 3 (false-if-exception (car result)))
586 #t)
587 (else
588 #f)))))))
589
590
591 ;;;
592 ;;; Numbers.
593 ;;;
594
595 (define (french-number-string=? expected result)
596 ;; Return true if RESULT is equal to EXPECTED, modulo white space.
597 ;; This is meant to deal with French locales: glibc 2.27+ uses
598 ;; NO-BREAK SPACE to separate 3-digit groups, whereas earlier versions
599 ;; used SPACE.
600 (or (string=? expected result)
601 (string=? (string-map (lambda (chr)
602 (case chr
603 ((#\space) #\240)
604 (else chr))) ;NO-BREAK SPACE
605 expected)
606 result)))
607
608 (with-test-prefix "number->locale-string"
609
610 ;; We assume the global locale is "C" at this point.
611
612 (with-test-prefix "C"
613
614 (pass-if-equal "no thousand separator"
615 ""
616 ;; Unlike in English, the "C" locale has no thousand separator.
617 ;; If this doesn't hold, the following tests will fail.
618 (locale-thousands-separator))
619
620 (pass-if-equal "integer"
621 "123456"
622 (number->locale-string 123456))
623
624 (pass-if-equal "fraction"
625 "1234.567"
626 (number->locale-string 1234.567))
627
628 (pass-if-equal "fraction, 1 digit"
629 "1234.6"
630 (number->locale-string 1234.567 1))
631
632 (pass-if-equal "fraction, 10 digits"
633 "0.0000300000"
634 (number->locale-string .00003 10))
635
636 (pass-if-equal "trailing zeros"
637 "-10.00000"
638 (number->locale-string -10.0 5))
639
640 (pass-if-equal "positive inexact zero, 1 digit"
641 "0.0"
642 (number->locale-string .0 1)))
643
644 (with-test-prefix "French"
645
646 (pass-if "integer"
647 (under-french-locale-or-unresolved
648 (lambda ()
649 (let ((fr (make-locale LC_ALL %french-locale-name)))
650 (french-number-string=? "123 456"
651 (number->locale-string 123456 #t fr))))))
652
653 (pass-if "negative integer"
654 (under-french-locale-or-unresolved
655 (lambda ()
656 (let ((fr (make-locale LC_ALL %french-locale-name)))
657 (french-number-string=? "-1 234 567"
658 (number->locale-string -1234567 #t fr))))))
659
660 (pass-if "fraction"
661 (under-french-locale-or-unresolved
662 (lambda ()
663 (let ((fr (make-locale LC_ALL %french-locale-name)))
664 (french-number-string=? "1 234,567"
665 (number->locale-string 1234.567 #t fr))))))
666
667 (pass-if "fraction, 1 digit"
668 (under-french-locale-or-unresolved
669 (lambda ()
670 (let ((fr (make-locale LC_ALL %french-locale-name)))
671 (french-number-string=? "1 234,6"
672 (number->locale-string 1234.567 1 fr))))))))
673
674 (with-test-prefix "format ~h"
675
676 ;; Some systems like Darwin lack the `GROUPING' nl_item, and thus
677 ;; `locale-digit-grouping' defaults to '(); skip the tests in that
678 ;; case.
679
680 (with-test-prefix "French"
681
682 (pass-if "12345.678"
683 (under-french-locale-or-unresolved
684 (lambda ()
685 (if (null? (locale-digit-grouping %french-locale))
686 (throw 'unresolved)
687 (french-number-string=? "12 345,678"
688 (format #f "~:h" 12345.678
689 %french-locale)))))))
690
691 (with-test-prefix "English"
692
693 (pass-if-equal "12345.678"
694 "12,345.678"
695 (under-american-english-locale-or-unresolved
696 (lambda ()
697 (if (null? (locale-digit-grouping %american-english-locale))
698 (throw 'unresolved)
699 (format #f "~:h" 12345.678
700 %american-english-locale)))))))
701
702 (with-test-prefix "monetary-amount->locale-string"
703
704 (with-test-prefix "French"
705
706 (pass-if "integer"
707 (under-french-locale-or-unresolved
708 (lambda ()
709 (let* ((fr (make-locale LC_ALL %french-locale-name))
710 (str (string-trim-both (monetary-amount->locale-string 123456 #f fr))))
711 ;; Check for both NO-BREAK SPACE and SPACE.
712 (or (string=? "123 456,00 +EUR" str)
713 (string=? "123 456,00 +EUR" str))))))
714
715 (pass-if "fraction"
716 (under-french-locale-or-unresolved
717 (lambda ()
718 (let* ((fr (make-locale LC_ALL %french-locale-name))
719 (str (monetary-amount->locale-string 1234.567 #t fr)))
720 ;; Check for both NO-BREAK SPACE and SPACE.
721 (or (string=? "1 234,57 EUR " str)
722 (string=? "1 234,57 EUR " str))))))
723
724 (pass-if-equal "positive inexact zero"
725 "0,00 +EUR"
726 (under-french-locale-or-unresolved
727 (lambda ()
728 (let ((fr (make-locale LC_ALL %french-locale-name)))
729 (string-trim-both (monetary-amount->locale-string 0. #f fr))))))
730
731 (pass-if-equal "one cent"
732 "0,01 EUR "
733 (under-french-locale-or-unresolved
734 (lambda ()
735 (let ((fr (make-locale LC_ALL %french-locale-name)))
736 (monetary-amount->locale-string .01 #t fr)))))
737
738 (pass-if-equal "very little money"
739 "0,00 EUR "
740 (under-french-locale-or-unresolved
741 (lambda ()
742 (let ((fr (make-locale LC_ALL %french-locale-name)))
743 (monetary-amount->locale-string .00003 #t fr)))))))