• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <langinfo.h>
32 
TEST(langinfo,category_CTYPE)33 TEST(langinfo, category_CTYPE) {
34   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
35 
36   EXPECT_STREQ("UTF-8", nl_langinfo(CODESET));
37 }
38 
TEST(langinfo,category_TIME)39 TEST(langinfo, category_TIME) {
40   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
41 
42 #if defined(__BIONIC__)
43   // bionic's C locale is ISO rather than en_US.
44   EXPECT_STREQ("%F %T %z", nl_langinfo(D_T_FMT));
45   EXPECT_STREQ("%F", nl_langinfo(D_FMT));
46 #else
47   EXPECT_STREQ("%a %d %b %Y %r %Z", nl_langinfo(D_T_FMT));
48   EXPECT_STREQ("%m/%d/%Y", nl_langinfo(D_FMT));
49 #endif
50   EXPECT_STREQ("%T", nl_langinfo(T_FMT));
51   EXPECT_STREQ("%I:%M:%S %p", nl_langinfo(T_FMT_AMPM));
52   EXPECT_STREQ("AM", nl_langinfo(AM_STR));
53   EXPECT_STREQ("PM", nl_langinfo(PM_STR));
54   EXPECT_STREQ("Sunday", nl_langinfo(DAY_1));
55   EXPECT_STREQ("Monday", nl_langinfo(DAY_2));
56   EXPECT_STREQ("Tuesday", nl_langinfo(DAY_3));
57   EXPECT_STREQ("Wednesday", nl_langinfo(DAY_4));
58   EXPECT_STREQ("Thursday", nl_langinfo(DAY_5));
59   EXPECT_STREQ("Friday", nl_langinfo(DAY_6));
60   EXPECT_STREQ("Saturday", nl_langinfo(DAY_7));
61   EXPECT_STREQ("Sun", nl_langinfo(ABDAY_1));
62   EXPECT_STREQ("Mon", nl_langinfo(ABDAY_2));
63   EXPECT_STREQ("Tue", nl_langinfo(ABDAY_3));
64   EXPECT_STREQ("Wed", nl_langinfo(ABDAY_4));
65   EXPECT_STREQ("Thu", nl_langinfo(ABDAY_5));
66   EXPECT_STREQ("Fri", nl_langinfo(ABDAY_6));
67   EXPECT_STREQ("Sat", nl_langinfo(ABDAY_7));
68   EXPECT_STREQ("January", nl_langinfo(MON_1));
69   EXPECT_STREQ("February", nl_langinfo(MON_2));
70   EXPECT_STREQ("March", nl_langinfo(MON_3));
71   EXPECT_STREQ("April", nl_langinfo(MON_4));
72   EXPECT_STREQ("May", nl_langinfo(MON_5));
73   EXPECT_STREQ("June", nl_langinfo(MON_6));
74   EXPECT_STREQ("July", nl_langinfo(MON_7));
75   EXPECT_STREQ("August", nl_langinfo(MON_8));
76   EXPECT_STREQ("September", nl_langinfo(MON_9));
77   EXPECT_STREQ("October", nl_langinfo(MON_10));
78   EXPECT_STREQ("November", nl_langinfo(MON_11));
79   EXPECT_STREQ("December", nl_langinfo(MON_12));
80   EXPECT_STREQ("Jan", nl_langinfo(ABMON_1));
81   EXPECT_STREQ("Feb", nl_langinfo(ABMON_2));
82   EXPECT_STREQ("Mar", nl_langinfo(ABMON_3));
83   EXPECT_STREQ("Apr", nl_langinfo(ABMON_4));
84   EXPECT_STREQ("May", nl_langinfo(ABMON_5));
85   EXPECT_STREQ("Jun", nl_langinfo(ABMON_6));
86   EXPECT_STREQ("Jul", nl_langinfo(ABMON_7));
87   EXPECT_STREQ("Aug", nl_langinfo(ABMON_8));
88   EXPECT_STREQ("Sep", nl_langinfo(ABMON_9));
89   EXPECT_STREQ("Oct", nl_langinfo(ABMON_10));
90   EXPECT_STREQ("Nov", nl_langinfo(ABMON_11));
91   EXPECT_STREQ("Dec", nl_langinfo(ABMON_12));
92   EXPECT_STREQ("", nl_langinfo(ERA));
93   EXPECT_STREQ("", nl_langinfo(ERA_D_FMT));
94   EXPECT_STREQ("", nl_langinfo(ERA_D_T_FMT));
95   EXPECT_STREQ("", nl_langinfo(ERA_T_FMT));
96   EXPECT_STREQ("", nl_langinfo(ALT_DIGITS));
97 }
98 
TEST(langinfo,category_NUMERIC)99 TEST(langinfo, category_NUMERIC) {
100   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
101 
102   EXPECT_STREQ(".", nl_langinfo(RADIXCHAR));
103   EXPECT_STREQ("", nl_langinfo(THOUSEP));
104 }
105 
TEST(langinfo,category_MESSAGES)106 TEST(langinfo, category_MESSAGES) {
107   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
108 
109   EXPECT_STREQ("^[yY]", nl_langinfo(YESEXPR));
110   EXPECT_STREQ("^[nN]", nl_langinfo(NOEXPR));
111 }
112 
TEST(langinfo,category_MONETARY)113 TEST(langinfo, category_MONETARY) {
114   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
115 
116   // POSIX says that if the currency symbol is the empty string (as it is for
117   // the C locale), an implementation can return the empty string and not
118   // include the leading [+-.] that signifies where the currency symbol should
119   // appear. For consistency with localeconv (which POSIX says to prefer for
120   // RADIXCHAR, THOUSEP, and CRNCYSTR) we return the empty string. glibc
121   // disagrees.
122 #if defined(__BIONIC__)
123   EXPECT_STREQ("", nl_langinfo(CRNCYSTR));
124 #else
125   EXPECT_STREQ("-", nl_langinfo(CRNCYSTR));
126 #endif
127 }
128 
TEST(langinfo,invalid)129 TEST(langinfo, invalid) {
130   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
131 
132   EXPECT_STREQ("", nl_langinfo(-1));
133   EXPECT_STREQ("", nl_langinfo(0));
134   EXPECT_STREQ("", nl_langinfo(666));
135 }
136 
TEST(langinfo,matches_localeconv)137 TEST(langinfo, matches_localeconv) {
138   ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
139 
140   EXPECT_STREQ(localeconv()->decimal_point, nl_langinfo(RADIXCHAR));
141   EXPECT_STREQ(localeconv()->thousands_sep, nl_langinfo(THOUSEP));
142 #if defined(__BIONIC__)
143   // (See comment in category_MONETARY test.)
144   EXPECT_STREQ(localeconv()->currency_symbol, nl_langinfo(CRNCYSTR));
145 #endif
146 }
147