]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/crelativedateformattest.c
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / crelativedateformattest.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2016, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST FOR DATE INTERVAL FORMAT */
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
12
13 #include "unicode/ureldatefmt.h"
14 #include "unicode/unum.h"
15 #include "unicode/udisplaycontext.h"
16 #include "unicode/ustring.h"
17 #include "cintltst.h"
18 #include "cmemory.h"
19 #include "cformtst.h"
20
21 static void TestRelDateFmt(void);
22 static void TestNumericField(void);
23 static void TestCombineDateTime(void);
24 static void TestFields(void);
25
26 void addRelativeDateFormatTest(TestNode** root);
27
28 #define TESTCASE(x) addTest(root, &x, "tsformat/crelativedateformattest/" #x)
29
30 void addRelativeDateFormatTest(TestNode** root)
31 {
32 TESTCASE(TestRelDateFmt);
33 TESTCASE(TestNumericField);
34 TESTCASE(TestCombineDateTime);
35 TESTCASE(TestFields);
36 }
37
38 static const double offsets[] = { -5.0, -2.2, -2.0, -1.0, -0.7, -0.0, 0.0, 0.7, 1.0, 2.0, 5.0 };
39 enum { kNumOffsets = UPRV_LENGTHOF(offsets) };
40
41 typedef struct {
42 int32_t field;
43 int32_t beginPos;
44 int32_t endPos;
45 } FieldsDat;
46
47 static const char* en_decDef_long_midSent_sec[kNumOffsets*2] = {
48 /* text numeric */
49 "5 seconds ago", "5 seconds ago", /* -5 */
50 "2.2 seconds ago", "2.2 seconds ago", /* -2.2 */
51 "2 seconds ago", "2 seconds ago", /* -2 */
52 "1 second ago", "1 second ago", /* -1 */
53 "0.7 seconds ago", "0.7 seconds ago", /* -0.7 */
54 "now", "0 seconds ago", /* -0 */
55 "now", "in 0 seconds", /* 0 */
56 "in 0.7 seconds", "in 0.7 seconds", /* 0.7 */
57 "in 1 second", "in 1 second", /* 1 */
58 "in 2 seconds", "in 2 seconds", /* 2 */
59 "in 5 seconds", "in 5 seconds" /* 5 */
60 };
61
62 static const FieldsDat en_attrDef_long_midSent_sec[kNumOffsets*2] = {
63 /* text numeric text numeric */
64 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 seconds ago", "5 seconds ago", -5 */
65 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "2.2 seconds ago", "2.2 seconds ago", -2.2 */
66 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 seconds ago", "2 seconds ago", -2 */
67 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "1 second ago", "1 second ago", -1 */
68 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "0.7 seconds ago", "0.7 seconds ago", -0.7 */
69 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "now", "0 seconds ago", -0 */
70 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "now", "in 0 seconds", 0 */
71 {UDAT_REL_NUMERIC_FIELD, 3, 6}, {UDAT_REL_NUMERIC_FIELD, 3, 6}, /* "in 0.7 seconds", "in 0.7 seconds", 0.7 */
72 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 1 second", "in 1 second", 1 */
73 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 seconds", "in 2 seconds", 2 */
74 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 seconds", "in 5 seconds" 5 */
75 };
76
77 static const char* en_decDef_long_midSent_week[kNumOffsets*2] = {
78 /* text numeric */
79 "5 weeks ago", "5 weeks ago", /* -5 */
80 "2.2 weeks ago", "2.2 weeks ago", /* -2.2 */
81 "2 weeks ago", "2 weeks ago", /* -2 */
82 "last week", "1 week ago", /* -1 */
83 "0.7 weeks ago", "0.7 weeks ago", /* -0.7 */
84 "this week", "0 weeks ago", /* -0 */
85 "this week", "in 0 weeks", /* 0 */
86 "in 0.7 weeks", "in 0.7 weeks", /* 0.7 */
87 "next week", "in 1 week", /* 1 */
88 "in 2 weeks", "in 2 weeks", /* 2 */
89 "in 5 weeks", "in 5 weeks" /* 5 */
90 };
91
92 static const FieldsDat en_attrDef_long_midSent_week[kNumOffsets*2] = {
93 /* text numeric text numeric */
94 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 weeks ago", "5 weeks ago", -5 */
95 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "2.2 weeks ago", "2.2 weeks ago", -2.2 */
96 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 weeks ago", "2 weeks ago", -2 */
97 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "last week", "1 week ago", -1 */
98 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "0.7 weeks ago", "0.7 weeks ago", -0.7 */
99 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "this week", "0 weeks ago", -0 */
100 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "this week", "in 0 weeks", 0 */
101 {UDAT_REL_NUMERIC_FIELD, 3, 6}, {UDAT_REL_NUMERIC_FIELD, 3, 6}, /* "in 0.7 weeks", "in 0.7 weeks", 0.7 */
102 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "next week", "in 1 week", 1 */
103 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 weeks", "in 2 weeks", 2 */
104 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 weeks", "in 5 weeks" 5 */
105 };
106
107 static const char* en_dec0_long_midSent_week[kNumOffsets*2] = {
108 /* text numeric */
109 "5 weeks ago", "5 weeks ago", /* -5 */
110 "2 weeks ago", "2 weeks ago", /* -2.2 */
111 "2 weeks ago", "2 weeks ago", /* -2 */
112 "last week", "1 week ago", /* -1 */
113 "0 weeks ago", "0 weeks ago", /* -0.7 */
114 "this week", "0 weeks ago", /* -0 */
115 "this week", "in 0 weeks", /* 0 */
116 "in 0 weeks", "in 0 weeks", /* 0.7 */
117 "next week", "in 1 week", /* 1 */
118 "in 2 weeks", "in 2 weeks", /* 2 */
119 "in 5 weeks", "in 5 weeks" /* 5 */
120 };
121
122 static const FieldsDat en_attr0_long_midSent_week[kNumOffsets*2] = {
123 /* text numeric text numeric */
124 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 weeks ago", "5 weeks ago", -5 */
125 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 weeks ago", "2 weeks ago", -2.2 */
126 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 weeks ago", "2 weeks ago", -2 */
127 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "last week", "1 week ago", -1 */
128 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "0 weeks ago", "0 weeks ago", -0.7 */
129 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "this week", "0 weeks ago", -0 */
130 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "this week", "in 0 weeks", 0 */
131 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 0 weeks", "in 0 weeks", 0.7 */
132 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "next week", "in 1 week", 1 */
133 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 weeks", "in 2 weeks", 2 */
134 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 weeks", "in 5 weeks" 5 */
135 };
136
137 static const char* en_decDef_short_midSent_week[kNumOffsets*2] = {
138 /* text numeric */
139 "5 wk. ago", "5 wk. ago", /* -5 */
140 "2.2 wk. ago", "2.2 wk. ago", /* -2.2 */
141 "2 wk. ago", "2 wk. ago", /* -2 */
142 "last wk.", "1 wk. ago", /* -1 */
143 "0.7 wk. ago", "0.7 wk. ago", /* -0.7 */
144 "this wk.", "0 wk. ago", /* -0 */
145 "this wk.", "in 0 wk.", /* 0 */
146 "in 0.7 wk.", "in 0.7 wk.", /* 0.7 */
147 "next wk.", "in 1 wk.", /* 1 */
148 "in 2 wk.", "in 2 wk.", /* 2 */
149 "in 5 wk.", "in 5 wk." /* 5 */
150 };
151
152 static const FieldsDat en_attrDef_short_midSent_week[kNumOffsets*2] = {
153 /* text numeric text numeric */
154 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 wk. ago", "5 wk. ago", -5 */
155 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "2.2 wk. ago", "2.2 wk. ago", -2.2 */
156 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 wk. ago", "2 wk. ago", -2 */
157 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "last wk.", "1 wk. ago", -1 */
158 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "0.7 wk. ago", "0.7 wk. ago", -0.7 */
159 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "this wk.", "0 wk. ago", -0 */
160 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "this wk.", "in 0 wk.", 0 */
161 {UDAT_REL_NUMERIC_FIELD, 3, 6}, {UDAT_REL_NUMERIC_FIELD, 3, 6}, /* "in 0.7 wk.", "in 0.7 wk.", 0.7 */
162 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "next wk.", "in 1 wk.", 1 */
163 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 wk.", "in 2 wk.", 2 */
164 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 wk.", "in 5 wk." 5 */
165 };
166
167 static const char* en_decDef_long_midSent_min[kNumOffsets*2] = {
168 /* text numeric */
169 "5 minutes ago", "5 minutes ago", /* -5 */
170 "2.2 minutes ago", "2.2 minutes ago", /* -2.2 */
171 "2 minutes ago", "2 minutes ago", /* -2 */
172 "1 minute ago", "1 minute ago", /* -1 */
173 "0.7 minutes ago", "0.7 minutes ago", /* -0.7 */
174 "0 minutes ago", "0 minutes ago", /* -0 */
175 "in 0 minutes", "in 0 minutes", /* 0 */
176 "in 0.7 minutes", "in 0.7 minutes", /* 0.7 */
177 "in 1 minute", "in 1 minute", /* 1 */
178 "in 2 minutes", "in 2 minutes", /* 2 */
179 "in 5 minutes", "in 5 minutes" /* 5 */
180 };
181
182 static const FieldsDat en_attrDef_long_midSent_min[kNumOffsets*2] = {
183 /* text numeric text numeric */
184 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 minutes ago", "5 minutes ago", -5 */
185 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "2.2 minutes ago", "2.2 minutes ago", -2.2 */
186 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 minutes ago", "2 minutes ago", -2 */
187 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "1 minute ago", "1 minute ago", -1 */
188 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3}, /* "0.7 minutes ago", "0.7 minutes ago", -0.7 */
189 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "0 minutes ago", "0 minutes ago", -0 */
190 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 0 minutes", "in 0 minutes", 0 */
191 {UDAT_REL_NUMERIC_FIELD, 3, 6}, {UDAT_REL_NUMERIC_FIELD, 3, 6}, /* "in 0.7 minutes", "in 0.7 minutes", 0.7 */
192 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 1 minute", "in 1 minute", 1 */
193 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 minutes", "in 2 minutes", 2 */
194 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 minutes", "in 5 minutes" 5 */
195 };
196
197 static const char* en_dec0_long_midSent_tues[kNumOffsets*2] = {
198 /* text numeric */
199 "5 Tuesdays ago", "5 Tuesdays ago", /* -5 */
200 ""/*no data */, ""/*no data */, /* -2.2 */
201 "2 Tuesdays ago", "2 Tuesdays ago", /* -2 */
202 "last Tuesday", "1 Tuesday ago", /* -1 */
203 ""/*no data */, ""/*no data */, /* -0.7 */
204 "this Tuesday", "0 Tuesdays ago", /* -0 */
205 "this Tuesday", "in 0 Tuesdays", /* 0 */
206 ""/*no data */, ""/*no data */, /* 0.7 */
207 "next Tuesday", "in 1 Tuesday", /* 1 */
208 "in 2 Tuesdays", "in 2 Tuesdays", /* 2 */
209 "in 5 Tuesdays", "in 5 Tuesdays", /* 5 */
210 };
211
212 static const FieldsDat en_attr0_long_midSent_tues[kNumOffsets*2] = {
213 /* text numeric text numeric */
214 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "5 Tuesdays ago", "5 Tuesdays ago", -5 */
215 { -1, -1, -1}, { -1, -1, -1}, /* "" , "" , -2.2 */
216 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "2 Tuesdays ago", "2 Tuesdays ago", -2 */
217 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "last Tuesday", "1 Tuesday ago", -1 */
218 { -1, -1, -1}, { -1, -1, -1}, /* "" , "" , -0.7 */
219 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1}, /* "this Tuesday", "0 Tuesdays ago", -0 */
220 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "this Tuesday", "in 0 Tuesdays", 0 */
221 { -1, -1, -1}, { -1, -1, -1}, /* "" , "" , 0.7 */
222 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "next Tuesday", "in 1 Tuesday", 1 */
223 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 2 Tuesdays", "in 2 Tuesdays", 2 */
224 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 3, 4}, /* "in 5 Tuesdays", "in 5 Tuesdays", 5 */
225 };
226
227 static const char* fr_decDef_long_midSent_day[kNumOffsets*2] = {
228 /* text numeric */
229 "il y a 5 jours", "il y a 5 jours", /* -5 */
230 "il y a 2,2 jours", "il y a 2,2 jours", /* -2.2 */
231 "avant-hier", "il y a 2 jours", /* -2 */
232 "hier", "il y a 1 jour", /* -1 */
233 "il y a 0,7 jour", "il y a 0,7 jour", /* -0.7 */
234 "aujourd\\u2019hui", "il y a 0 jour", /* -0 */
235 "aujourd\\u2019hui", "dans 0 jour", /* 0 */
236 "dans 0,7 jour", "dans 0,7 jour", /* 0.7 */
237 "demain", "dans 1 jour", /* 1 */
238 "apr\\u00E8s-demain", "dans 2 jours", /* 2 */
239 "dans 5 jours", "dans 5 jours" /* 5 */
240 };
241
242 static const FieldsDat fr_attrDef_long_midSent_day[kNumOffsets*2] = {
243 /* text numeric text numeric */
244 {UDAT_REL_NUMERIC_FIELD, 7, 8}, {UDAT_REL_NUMERIC_FIELD, 7, 8}, /* "il y a 5 jours", "il y a 5 jours", -5 */
245 {UDAT_REL_NUMERIC_FIELD, 7, 10}, {UDAT_REL_NUMERIC_FIELD, 7, 10}, /* "il y a 2,2 jours", "il y a 2,2 jours", -2.2 */
246 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 7, 8}, /* "avant-hier", "il y a 2 jours", -2 */
247 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 7, 8}, /* "hier", "il y a 1 jour", -1 */
248 {UDAT_REL_NUMERIC_FIELD, 7, 10}, {UDAT_REL_NUMERIC_FIELD, 7, 10}, /* "il y a 0,7 jour", "il y a 0,7 jour", -0.7 */
249 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 7, 8}, /* "aujourd\\u2019hui", "il y a 0 jour", -0 */
250 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 5, 6}, /* "aujourd\\u2019hui", "dans 0 jour", 0 */
251 {UDAT_REL_NUMERIC_FIELD, 5, 8}, {UDAT_REL_NUMERIC_FIELD, 5, 8}, /* "dans 0,7 jour", "dans 0,7 jour", 0.7 */
252 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 5, 6}, /* "demain", "dans 1 jour", 1 */
253 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 5, 6}, /* "apr\\u00E8s-demain", "dans 2 jours", 2 */
254 {UDAT_REL_NUMERIC_FIELD, 5, 6}, {UDAT_REL_NUMERIC_FIELD, 5, 6}, /* "dans 5 jours", "dans 5 jours" 5 */
255 };
256
257 static const char* ak_decDef_long_stdAlon_sec[kNumOffsets*2] = { // falls back to root
258 /* text numeric */
259 "-5 s", "-5 s", /* -5 */
260 "-2.2 s", "-2.2 s", /* -2.2 */
261 "-2 s", "-2 s", /* -2 */
262 "-1 s", "-1 s", /* -1 */
263 "-0.7 s", "-0.7 s", /* -0.7 */
264 "now", "-0 s", /* -0 */
265 "now", "+0 s", /* 0 */
266 "+0.7 s", "+0.7 s", /* 0.7 */
267 "+1 s", "+1 s", /* 1 */
268 "+2 s", "+2 s", /* 2 */
269 "+5 s", "+5 s", /* 5 */
270 };
271
272 static const FieldsDat ak_attrDef_long_stdAlon_sec[kNumOffsets*2] = {
273 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
274 {UDAT_REL_NUMERIC_FIELD, 1, 4}, {UDAT_REL_NUMERIC_FIELD, 1, 4},
275 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
276 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
277 {UDAT_REL_NUMERIC_FIELD, 1, 4}, {UDAT_REL_NUMERIC_FIELD, 1, 4},
278 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
279 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
280 {UDAT_REL_NUMERIC_FIELD, 1, 4}, {UDAT_REL_NUMERIC_FIELD, 1, 4},
281 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
282 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
283 {UDAT_REL_NUMERIC_FIELD, 1, 2}, {UDAT_REL_NUMERIC_FIELD, 1, 2},
284 };
285
286 static const char* enIN_decDef_short_midSent_weds[kNumOffsets*2] = {
287 /* text numeric */
288 "5 Wed. ago", "5 Wed. ago", /* -5 */
289 "2.2 Wed. ago", "2.2 Wed. ago", /* -2.2 */
290 "2 Wed. ago", "2 Wed. ago", /* -2 */
291 "last Wed", "1 Wed. ago", /* -1 */
292 "0.7 Wed. ago", "0.7 Wed. ago", /* -0.7 */
293 "this Wed", "0 Wed. ago", /* -0 */
294 "this Wed", "in 0 Wed.", /* 0 */
295 "in 0.7 Wed.", "in 0.7 Wed.", /* 0.7 */
296 "next Wed", "in 1 Wed", /* 1 */ // in 1 Wed. missing in logical group
297 "in 2 Wed.", "in 2 Wed.", /* 2 */
298 "in 5 Wed.", "in 5 Wed." /* 5 */
299 };
300
301 static const FieldsDat enIN_attrDef_short_midSent_weds[kNumOffsets*2] = {
302 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1},
303 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3},
304 {UDAT_REL_NUMERIC_FIELD, 0, 1}, {UDAT_REL_NUMERIC_FIELD, 0, 1},
305 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1},
306 {UDAT_REL_NUMERIC_FIELD, 0, 3}, {UDAT_REL_NUMERIC_FIELD, 0, 3},
307 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 0, 1},
308 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4},
309 {UDAT_REL_NUMERIC_FIELD, 3, 6}, {UDAT_REL_NUMERIC_FIELD, 3, 6},
310 { -1, -1, -1}, {UDAT_REL_NUMERIC_FIELD, 3, 4},
311 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4},
312 {UDAT_REL_NUMERIC_FIELD, 3, 4}, {UDAT_REL_NUMERIC_FIELD, 3, 4},
313 };
314
315 static const char* nb_decDef_long_midSent_day[kNumOffsets*2] = {
316 /* text numeric */
317 "for 5 dager siden", "for 5 dager siden", /* -5 */
318 "for 2,2 dager siden", "for 2,2 dager siden", /* -2.2 */
319 "i forg\\u00E5rs", "for 2 dager siden", /* -2 */
320 "i g\\u00E5r", "for 1 dag siden", /* -1 */
321 "for 0,7 dager siden", "for 0,7 dager siden", /* -0.7 */
322 "i dag", "for 0 dager siden", /* -0 */
323 "i dag", "om 0 dager", /* 0 */
324 "om 0,7 dager", "om 0,7 dager", /* 0.7 */
325 "i morgen", "om 1 dag", /* 1 */
326 "i overmorgen", "om 2 dager", /* 2 */
327 "om 5 dager", "om 5 dager" /* 5 */
328 };
329
330 // Need attrDef for nb
331
332
333 typedef struct {
334 const char* locale;
335 int32_t decPlaces; /* fixed decimal places; -1 to use default num formatter */
336 UDateRelativeDateTimeFormatterStyle width;
337 UDisplayContext capContext;
338 URelativeDateTimeUnit unit;
339 const char ** expectedResults; /* for the various offsets */
340 const FieldsDat* expectedAttributes;
341 } RelDateTimeFormatTestItem;
342
343 static const RelDateTimeFormatTestItem fmtTestItems[] = {
344 { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_SECOND,
345 en_decDef_long_midSent_sec, en_attrDef_long_midSent_sec },
346 { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK,
347 en_decDef_long_midSent_week, en_attrDef_long_midSent_week},
348 { "en", 0, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK,
349 en_dec0_long_midSent_week, en_attr0_long_midSent_week},
350 { "en", -1, UDAT_STYLE_SHORT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK,
351 en_decDef_short_midSent_week, en_attrDef_short_midSent_week},
352 { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_MINUTE,
353 en_decDef_long_midSent_min, en_attrDef_long_midSent_min},
354 { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_TUESDAY,
355 en_dec0_long_midSent_tues, en_attr0_long_midSent_tues},
356 { "fr", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_DAY,
357 fr_decDef_long_midSent_day, fr_attrDef_long_midSent_day},
358 { "ak", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDAT_REL_UNIT_SECOND,
359 ak_decDef_long_stdAlon_sec, ak_attrDef_long_stdAlon_sec},
360 { "en_IN", -1, UDAT_STYLE_SHORT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEDNESDAY,
361 enIN_decDef_short_midSent_weds, enIN_attrDef_short_midSent_weds},
362 //add suitable entry for nb
363 //{ "nb", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_DAY, nb_decDef_long_midSent_day ...
364 { NULL, 0, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, (URelativeDateTimeUnit)0, NULL, NULL } /* terminator */
365 };
366
367 enum { kUBufMax = 64, kBBufMax = 256 };
368
369 static void TestRelDateFmt()
370 {
371 const RelDateTimeFormatTestItem *itemPtr;
372 log_verbose("\nTesting ureldatefmt_open(), ureldatefmt_format(), ureldatefmt_formatNumeric() with various parameters\n");
373 for (itemPtr = fmtTestItems; itemPtr->locale != NULL; itemPtr++) {
374 URelativeDateTimeFormatter *reldatefmt = NULL;
375 UNumberFormat* nfToAdopt = NULL;
376 UErrorCode status = U_ZERO_ERROR;
377 int32_t iOffset;
378
379 if (itemPtr->decPlaces >= 0) {
380 nfToAdopt = unum_open(UNUM_DECIMAL, NULL, 0, itemPtr->locale, NULL, &status);
381 if ( U_FAILURE(status) ) {
382 log_data_err("FAIL: unum_open(UNUM_DECIMAL, ...) for locale %s: %s\n", itemPtr->locale, myErrorName(status));
383 continue;
384 }
385 unum_setAttribute(nfToAdopt, UNUM_MIN_FRACTION_DIGITS, itemPtr->decPlaces);
386 unum_setAttribute(nfToAdopt, UNUM_MAX_FRACTION_DIGITS, itemPtr->decPlaces);
387 unum_setAttribute(nfToAdopt, UNUM_ROUNDING_MODE, UNUM_ROUND_DOWN);
388 }
389 reldatefmt = ureldatefmt_open(itemPtr->locale, nfToAdopt, itemPtr->width, itemPtr->capContext, &status);
390 if ( U_FAILURE(status) ) {
391 log_data_err("FAIL: ureldatefmt_open() for locale %s, decPlaces %d, width %d, capContext %d: %s\n",
392 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
393 myErrorName(status) );
394 continue;
395 }
396
397 for (iOffset = 0; iOffset < kNumOffsets; iOffset++) {
398 UChar ubufget[kUBufMax];
399 int32_t ulenget;
400
401 if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY && offsets[iOffset] != -1.0 && offsets[iOffset] != 0.0 && offsets[iOffset] != 1.0) {
402 continue; /* we do not currently have data for this */
403 }
404
405 status = U_ZERO_ERROR;
406 ulenget = ureldatefmt_format(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status);
407 if ( U_FAILURE(status) ) {
408 log_err("FAIL: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n",
409 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
410 offsets[iOffset], (int)itemPtr->unit, myErrorName(status) );
411 } else {
412 UChar ubufexp[kUBufMax];
413 int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2], ubufexp, kUBufMax);
414 if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) {
415 char bbufget[kBBufMax];
416 u_austrncpy(bbufget, ubufget, kUBufMax);
417 log_err("ERROR: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n",
418 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
419 offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2], bbufget );
420 }
421 }
422
423 if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY) {
424 continue; /* we do not currently have numeric-style data for this */
425 }
426
427 status = U_ZERO_ERROR;
428 ulenget = ureldatefmt_formatNumeric(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status);
429 if ( U_FAILURE(status) ) {
430 log_err("FAIL: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n",
431 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
432 offsets[iOffset], (int)itemPtr->unit, myErrorName(status) );
433 } else {
434 UChar ubufexp[kUBufMax];
435 int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2 + 1], ubufexp, kUBufMax);
436 if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) {
437 char bbufget[kBBufMax];
438 u_austrncpy(bbufget, ubufget, kUBufMax);
439 log_err("ERROR: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n",
440 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
441 offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2 + 1], bbufget );
442 }
443 }
444 }
445
446 ureldatefmt_close(reldatefmt);
447 }
448 }
449
450 static void TestNumericField()
451 {
452 const RelDateTimeFormatTestItem *itemPtr;
453 log_verbose("\nTesting ureldatefmt_open(), ureldatefmt_formatForFields(), ureldatefmt_formatNumericForFields() with various parameters\n");
454 for (itemPtr = fmtTestItems; itemPtr->locale != NULL; itemPtr++) {
455 URelativeDateTimeFormatter *reldatefmt = NULL;
456 UNumberFormat* nfToAdopt = NULL;
457 UErrorCode status = U_ZERO_ERROR;
458 int32_t iOffset;
459
460 if (itemPtr->decPlaces >= 0) {
461 nfToAdopt = unum_open(UNUM_DECIMAL, NULL, 0, itemPtr->locale, NULL, &status);
462 if ( U_FAILURE(status) ) {
463 log_data_err("FAIL: unum_open(UNUM_DECIMAL, ...) for locale %s: %s\n", itemPtr->locale, myErrorName(status));
464 continue;
465 }
466 unum_setAttribute(nfToAdopt, UNUM_MIN_FRACTION_DIGITS, itemPtr->decPlaces);
467 unum_setAttribute(nfToAdopt, UNUM_MAX_FRACTION_DIGITS, itemPtr->decPlaces);
468 unum_setAttribute(nfToAdopt, UNUM_ROUNDING_MODE, UNUM_ROUND_DOWN);
469 }
470 reldatefmt = ureldatefmt_open(itemPtr->locale, nfToAdopt, itemPtr->width, itemPtr->capContext, &status);
471 if ( U_FAILURE(status) ) {
472 log_data_err("FAIL: ureldatefmt_open() for locale %s, decPlaces %d, width %d, capContext %d: %s\n",
473 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
474 myErrorName(status) );
475 continue;
476 }
477
478 for (iOffset = 0; iOffset < kNumOffsets; iOffset++) {
479 if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY && offsets[iOffset] != -1.0 && offsets[iOffset] != 0.0 && offsets[iOffset] != 1.0) {
480 continue; /* we do not currently have data for this */
481 }
482
483 /* Depend on the next one to verify the data */
484 status = U_ZERO_ERROR;
485 UFormattedRelativeDateTime* fv = ureldatefmt_openResult(&status);
486 if ( U_FAILURE(status) ) {
487 log_err("ureldatefmt_openResult fails, status %s\n", u_errorName(status));
488 continue;
489 }
490 ureldatefmt_formatToResult(reldatefmt, offsets[iOffset], itemPtr->unit, fv, &status);
491 if ( U_FAILURE(status) ) {
492 log_err("FAIL: ureldatefmt_formatForFields() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n",
493 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
494 offsets[iOffset], (int)itemPtr->unit, myErrorName(status) );
495 } else {
496 UChar ubufexp[kUBufMax];
497 int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2], ubufexp, kUBufMax);
498 int32_t ulenget;
499 const UChar* ubufget = ufmtval_getString(ureldatefmt_resultAsValue(fv, &status), &ulenget, &status);
500 assertUEquals("String content", ubufexp, ubufget);
501 assertIntEquals("String length", ulenexp, ulenget);
502
503 FieldsDat expectedAttr = itemPtr->expectedAttributes[iOffset*2];
504 UConstrainedFieldPosition* cfpos = ucfpos_open(&status);
505 UBool foundNumeric = FALSE;
506 while (TRUE) {
507 foundNumeric = ufmtval_nextPosition(ureldatefmt_resultAsValue(fv, &status), cfpos, &status);
508 if (!foundNumeric) {
509 break;
510 }
511 if (ucfpos_getCategory(cfpos, &status) == UFIELD_CATEGORY_RELATIVE_DATETIME
512 && ucfpos_getField(cfpos, &status) == UDAT_REL_NUMERIC_FIELD) {
513 break;
514 }
515 }
516 assertSuccess("Looking for numeric", &status);
517 int32_t beginPos, endPos;
518 ucfpos_getIndexes(cfpos, &beginPos, &endPos, &status);
519 if (expectedAttr.field == -1) {
520 if (foundNumeric) {
521 log_err("ureldatefmt_formatForFields as \"%s\"; expect no field, but got %d\n",
522 itemPtr->expectedResults[iOffset*2],
523 ucfpos_getField(cfpos, &status));
524 }
525 } else {
526 if (!foundNumeric ||
527 beginPos != expectedAttr.beginPos ||
528 endPos != expectedAttr.endPos) {
529 log_err("ureldatefmt_formatForFields as \"%s\"; expect field %d range %d-%d, get range %d-%d\n",
530 itemPtr->expectedResults[iOffset*2],
531 expectedAttr.field, expectedAttr.beginPos, expectedAttr.endPos,
532 beginPos, endPos);
533 }
534 }
535 ucfpos_close(cfpos);
536 }
537
538 if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY) {
539 ureldatefmt_closeResult(fv);
540 continue; /* we do not currently have numeric-style data for this */
541 }
542
543 /* Depend on the next one to verify the data */
544 status = U_ZERO_ERROR;
545 ureldatefmt_formatNumericToResult(reldatefmt, offsets[iOffset], itemPtr->unit, fv, &status);
546 if ( U_FAILURE(status) ) {
547 log_err("FAIL: ureldatefmt_formatNumericForFields() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n",
548 itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext,
549 offsets[iOffset], (int)itemPtr->unit, myErrorName(status) );
550 } else {
551 UChar ubufexp[kUBufMax];
552 int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2 + 1], ubufexp, kUBufMax);
553 int32_t ulenget;
554 const UChar* ubufget = ufmtval_getString(ureldatefmt_resultAsValue(fv, &status), &ulenget, &status);
555 assertUEquals("String content", ubufexp, ubufget);
556 assertIntEquals("String length", ulenexp, ulenget);
557
558 FieldsDat expectedAttr = itemPtr->expectedAttributes[iOffset*2 + 1];
559 UConstrainedFieldPosition* cfpos = ucfpos_open(&status);
560 UBool foundNumeric = FALSE;
561 while (TRUE) {
562 foundNumeric = ufmtval_nextPosition(ureldatefmt_resultAsValue(fv, &status), cfpos, &status);
563 if (!foundNumeric) {
564 break;
565 }
566 if (ucfpos_getCategory(cfpos, &status) == UFIELD_CATEGORY_RELATIVE_DATETIME
567 && ucfpos_getField(cfpos, &status) == UDAT_REL_NUMERIC_FIELD) {
568 break;
569 }
570 }
571 assertSuccess("Looking for numeric", &status);
572 int32_t beginPos, endPos;
573 ucfpos_getIndexes(cfpos, &beginPos, &endPos, &status);
574 if (expectedAttr.field == -1) {
575 if (foundNumeric) {
576 log_err("ureldatefmt_formatForFields as \"%s\"; expect no field, but got %d rang %d-%d\n",
577 itemPtr->expectedResults[iOffset*2],
578 ucfpos_getField(cfpos, &status), beginPos, endPos);
579 }
580 } else {
581 if (!foundNumeric ||
582 (beginPos != expectedAttr.beginPos || endPos != expectedAttr.endPos)) {
583 log_err("ureldatefmt_formatForFields as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n",
584 itemPtr->expectedResults[iOffset*2 + 1],
585 expectedAttr.field, expectedAttr.beginPos, expectedAttr.endPos,
586 ucfpos_getField(cfpos, &status), beginPos, endPos);
587 }
588 }
589 ucfpos_close(cfpos);
590 }
591 ureldatefmt_closeResult(fv);
592 }
593
594 ureldatefmt_close(reldatefmt);
595 }
596 }
597
598 typedef struct {
599 const char* locale;
600 UDateRelativeDateTimeFormatterStyle width;
601 UDisplayContext capContext;
602 const char * relativeDateString;
603 const char * timeString;
604 const char * expectedResult;
605 } CombineDateTimeTestItem;
606
607 static const CombineDateTimeTestItem combTestItems[] = {
608 { "en", UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "yesterday", "3:45 PM", "yesterday, 3:45 PM" },
609 { NULL, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, NULL, NULL, NULL } /* terminator */
610 };
611
612 static void TestCombineDateTime()
613 {
614 const CombineDateTimeTestItem *itemPtr;
615 log_verbose("\nTesting ureldatefmt_combineDateAndTime() with various parameters\n");
616 for (itemPtr = combTestItems; itemPtr->locale != NULL; itemPtr++) {
617 URelativeDateTimeFormatter *reldatefmt = NULL;
618 UErrorCode status = U_ZERO_ERROR;
619 UChar ubufreldate[kUBufMax];
620 UChar ubuftime[kUBufMax];
621 UChar ubufget[kUBufMax];
622 int32_t ulenreldate, ulentime, ulenget;
623
624 reldatefmt = ureldatefmt_open(itemPtr->locale, NULL, itemPtr->width, itemPtr->capContext, &status);
625 if ( U_FAILURE(status) ) {
626 log_data_err("FAIL: ureldatefmt_open() for locale %s, width %d, capContext %d: %s\n",
627 itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) );
628 continue;
629 }
630
631 ulenreldate = u_unescape(itemPtr->relativeDateString, ubufreldate, kUBufMax);
632 ulentime = u_unescape(itemPtr->timeString, ubuftime, kUBufMax);
633 ulenget = ureldatefmt_combineDateAndTime(reldatefmt, ubufreldate, ulenreldate, ubuftime, ulentime, ubufget, kUBufMax, &status);
634 if ( U_FAILURE(status) ) {
635 log_err("FAIL: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d: %s\n",
636 itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) );
637 } else {
638 UChar ubufexp[kUBufMax];
639 int32_t ulenexp = u_unescape(itemPtr->expectedResult, ubufexp, kUBufMax);
640 if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) {
641 char bbufget[kBBufMax];
642 u_austrncpy(bbufget, ubufget, kUBufMax);
643 log_err("ERROR: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d;\n expected %s\n get %s\n",
644 itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, itemPtr->expectedResult, bbufget );
645 }
646 }
647 // preflight test
648 status = U_ZERO_ERROR;
649 ulenget = ureldatefmt_combineDateAndTime(reldatefmt, ubufreldate, ulenreldate, ubuftime, ulentime, NULL, 0, &status);
650 if ( status != U_BUFFER_OVERFLOW_ERROR) {
651 log_err("FAIL: ureldatefmt_combineDateAndTime() preflight for locale %s, width %d, capContext %d: expected U_BUFFER_OVERFLOW_ERROR, got %s\n",
652 itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) );
653 } else {
654 UChar ubufexp[kUBufMax];
655 int32_t ulenexp = u_unescape(itemPtr->expectedResult, ubufexp, kUBufMax);
656 if (ulenget != ulenexp) {
657 log_err("ERROR: ureldatefmt_combineDateAndTime() preflight for locale %s, width %d, capContext %d;\n expected len %d, get len %d\n",
658 itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, ulenexp, ulenget );
659 }
660 }
661
662 ureldatefmt_close(reldatefmt);
663 }
664 }
665
666 static void TestFields() {
667 UErrorCode ec = U_ZERO_ERROR;
668 URelativeDateTimeFormatter* fmt = ureldatefmt_open(
669 "en-us",
670 NULL,
671 UDAT_STYLE_SHORT,
672 UDISPCTX_CAPITALIZATION_NONE,
673 &ec);
674 assertSuccess("Creating RelDTFmt", &ec);
675 UFormattedRelativeDateTime* frdt = ureldatefmt_openResult(&ec);
676 assertSuccess("Creating FmtVal", &ec);
677
678 ureldatefmt_formatNumericToResult(fmt, -50, UDAT_REL_UNIT_SATURDAY, frdt, &ec);
679 assertSuccess("formatNumeric", &ec);
680 {
681 const UFormattedValue* fv = ureldatefmt_resultAsValue(frdt, &ec);
682 assertSuccess("Should convert without error", &ec);
683 static const UFieldPositionWithCategory expectedFieldPositions[] = {
684 // category, field, begin index, end index
685 {UFIELD_CATEGORY_NUMBER, UNUM_INTEGER_FIELD, 0, 2},
686 {UFIELD_CATEGORY_RELATIVE_DATETIME, UDAT_REL_NUMERIC_FIELD, 0, 2},
687 {UFIELD_CATEGORY_RELATIVE_DATETIME, UDAT_REL_LITERAL_FIELD, 3, 11}};
688 checkMixedFormattedValue(
689 "FormattedRelativeDateTime as FormattedValue (numeric)",
690 fv,
691 u"50 Sat. ago",
692 expectedFieldPositions,
693 UPRV_LENGTHOF(expectedFieldPositions));
694 }
695
696 ureldatefmt_formatToResult(fmt, -1, UDAT_REL_UNIT_WEEK, frdt, &ec);
697 assertSuccess("format", &ec);
698 {
699 const UFormattedValue* fv = ureldatefmt_resultAsValue(frdt, &ec);
700 assertSuccess("Should convert without error", &ec);
701 static const UFieldPositionWithCategory expectedFieldPositions[] = {
702 // category, field, begin index, end index
703 {UFIELD_CATEGORY_RELATIVE_DATETIME, UDAT_REL_LITERAL_FIELD, 0, 8}};
704 checkMixedFormattedValue(
705 "FormattedRelativeDateTime as FormattedValue (relative)",
706 fv,
707 u"last wk.",
708 expectedFieldPositions,
709 UPRV_LENGTHOF(expectedFieldPositions));
710 }
711
712 ureldatefmt_closeResult(frdt);
713 ureldatefmt_close(fmt);
714 }
715
716 #endif /* #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION */