• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2013-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 * File RELDATEFMTTEST.CPP
10 *
11 *******************************************************************************
12 */
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include "intltest.h"
17 
18 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
19 
20 #include "unicode/localpointer.h"
21 #include "unicode/numfmt.h"
22 #include "unicode/reldatefmt.h"
23 #include "cmemory.h"
24 
25 static const char *DirectionStr(UDateDirection direction);
26 static const char *RelativeUnitStr(UDateRelativeUnit unit);
27 static const char *AbsoluteUnitStr(UDateAbsoluteUnit unit);
28 
29 typedef struct WithQuantityExpected {
30     double value;
31     UDateDirection direction;
32     UDateRelativeUnit unit;
33     const char *expected;
34 } WithQuantityExpected;
35 
36 typedef struct WithoutQuantityExpected {
37     UDateDirection direction;
38     UDateAbsoluteUnit unit;
39     const char *expected;
40 } WithoutQuantityExpected;
41 
42 static WithQuantityExpected kEnglish[] = {
43         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0 seconds"},
44         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0.5 seconds"},
45         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 1 second"},
46         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 2 seconds"},
47         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 0 minutes"},
48         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 0.5 minutes"},
49         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 1 minute"},
50         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 2 minutes"},
51         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 0 hours"},
52         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 0.5 hours"},
53         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 1 hour"},
54         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 2 hours"},
55         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 0 days"},
56         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 0.5 days"},
57         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 1 day"},
58         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 2 days"},
59         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 0 weeks"},
60         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 0.5 weeks"},
61         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 1 week"},
62         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 2 weeks"},
63         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 0 months"},
64         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 0.5 months"},
65         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 1 month"},
66         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 2 months"},
67         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 0 years"},
68         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 0.5 years"},
69         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 1 year"},
70         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 2 years"},
71 
72         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0 seconds ago"},
73         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0.5 seconds ago"},
74         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "1 second ago"},
75         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "2 seconds ago"},
76         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0 minutes ago"},
77         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0.5 minutes ago"},
78         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "1 minute ago"},
79         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "2 minutes ago"},
80         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0 hours ago"},
81         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0.5 hours ago"},
82         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "1 hour ago"},
83         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "2 hours ago"},
84         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0 days ago"},
85         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0.5 days ago"},
86         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "1 day ago"},
87         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "2 days ago"},
88         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0 weeks ago"},
89         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0.5 weeks ago"},
90         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "1 week ago"},
91         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "2 weeks ago"},
92         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0 months ago"},
93         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0.5 months ago"},
94         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "1 month ago"},
95         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "2 months ago"},
96         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0 years ago"},
97         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0.5 years ago"},
98         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "1 year ago"},
99         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "2 years ago"}
100 };
101 
102 static WithQuantityExpected kEnglishCaps[] = {
103         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "In 0 seconds"},
104         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "In 0.5 seconds"},
105         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "In 1 second"},
106         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "In 2 seconds"},
107         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "In 0 minutes"},
108         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "In 0.5 minutes"},
109         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "In 1 minute"},
110         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "In 2 minutes"},
111         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "In 0 hours"},
112         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "In 0.5 hours"},
113         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "In 1 hour"},
114         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "In 2 hours"},
115         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "In 0 days"},
116         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "In 0.5 days"},
117         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "In 1 day"},
118         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "In 2 days"},
119         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "In 0 weeks"},
120         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "In 0.5 weeks"},
121         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "In 1 week"},
122         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "In 2 weeks"},
123         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "In 0 months"},
124         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "In 0.5 months"},
125         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "In 1 month"},
126         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "In 2 months"},
127         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "In 0 years"},
128         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "In 0.5 years"},
129         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "In 1 year"},
130         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "In 2 years"},
131 
132         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0 seconds ago"},
133         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0.5 seconds ago"},
134         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "1 second ago"},
135         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "2 seconds ago"},
136         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0 minutes ago"},
137         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0.5 minutes ago"},
138         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "1 minute ago"},
139         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "2 minutes ago"},
140         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0 hours ago"},
141         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0.5 hours ago"},
142         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "1 hour ago"},
143         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "2 hours ago"},
144         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0 days ago"},
145         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0.5 days ago"},
146         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "1 day ago"},
147         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "2 days ago"},
148         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0 weeks ago"},
149         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0.5 weeks ago"},
150         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "1 week ago"},
151         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "2 weeks ago"},
152         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0 months ago"},
153         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0.5 months ago"},
154         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "1 month ago"},
155         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "2 months ago"},
156         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0 years ago"},
157         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0.5 years ago"},
158         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "1 year ago"},
159         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "2 years ago"}
160 };
161 
162 static WithQuantityExpected kEnglishShort[] = {
163         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0 sec."},
164         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0.5 sec."},
165         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 1 sec."},
166         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 2 sec."},
167         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 0 min."},
168         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 0.5 min."},
169         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 1 min."},
170         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MINUTES, "in 2 min."},
171         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 0 hr."},
172         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 0.5 hr."},
173         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 1 hr."},
174         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_HOURS, "in 2 hr."},
175         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 0 days"},
176         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 0.5 days"},
177         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 1 day"},
178         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, "in 2 days"},
179         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 0 wk."},
180         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 0.5 wk."},
181         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 1 wk."},
182         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_WEEKS, "in 2 wk."},
183         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 0 mo."},
184         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 0.5 mo."},
185         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 1 mo."},
186         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "in 2 mo."},
187         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 0 yr."},
188         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 0.5 yr."},
189         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 1 yr."},
190         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_YEARS, "in 2 yr."},
191 
192         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0 sec. ago"},
193         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "0.5 sec. ago"},
194         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "1 sec. ago"},
195         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_SECONDS, "2 sec. ago"},
196         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0 min. ago"},
197         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "0.5 min. ago"},
198         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "1 min. ago"},
199         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, "2 min. ago"},
200         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0 hr. ago"},
201         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "0.5 hr. ago"},
202         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "1 hr. ago"},
203         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, "2 hr. ago"},
204         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0 days ago"},
205         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "0.5 days ago"},
206         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "1 day ago"},
207         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, "2 days ago"},
208         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0 wk. ago"},
209         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "0.5 wk. ago"},
210         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "1 wk. ago"},
211         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_WEEKS, "2 wk. ago"},
212         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0 mo. ago"},
213         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "0.5 mo. ago"},
214         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "1 mo. ago"},
215         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MONTHS, "2 mo. ago"},
216         {0.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0 yr. ago"},
217         {0.5, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "0.5 yr. ago"},
218         {1.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "1 yr. ago"},
219         {2.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, "2 yr. ago"}
220 };
221 
222 static WithQuantityExpected kEnglishDecimal[] = {
223         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0.0 seconds"},
224         {0.5, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 0.5 seconds"},
225         {1.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 1.0 seconds"},
226         {2.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, "in 2.0 seconds"}
227 };
228 
229 static WithQuantityExpected kSerbian[] = {
230         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 0 \\u043c\\u0435\\u0441\\u0435\\u0446\\u0438"},
231         {1.2, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 1,2 \\u043c\\u0435\\u0441\\u0435\\u0446\\u0430"},
232         {21.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 21 \\u043c\\u0435\\u0441\\u0435\\u0446"}
233 };
234 
235 static WithQuantityExpected kSerbianNarrow[] = {
236         {0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 0 \\u043c."},
237         {1.2, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 1,2 \\u043c."},
238         {21.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, "\\u0437\\u0430 21 \\u043c."}
239 };
240 
241 static WithoutQuantityExpected kEnglishNoQuantity[] = {
242         {UDAT_DIRECTION_NEXT_2, UDAT_ABSOLUTE_DAY, ""},
243 
244         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, "tomorrow"},
245         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEEK, "next week"},
246         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONTH, "next month"},
247         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, "next year"},
248         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONDAY, "next Monday"},
249         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_TUESDAY, "next Tuesday"},
250         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEDNESDAY, "next Wednesday"},
251         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_THURSDAY, "next Thursday"},
252         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_FRIDAY, "next Friday"},
253         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SATURDAY, "next Saturday"},
254         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, "next Sunday"},
255 
256         {UDAT_DIRECTION_LAST_2, UDAT_ABSOLUTE_DAY, ""},
257 
258         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, "yesterday"},
259         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEEK, "last week"},
260         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONTH, "last month"},
261         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_YEAR, "last year"},
262         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONDAY, "last Monday"},
263         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_TUESDAY, "last Tuesday"},
264         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEDNESDAY, "last Wednesday"},
265         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_THURSDAY, "last Thursday"},
266         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_FRIDAY, "last Friday"},
267         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SATURDAY, "last Saturday"},
268         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, "last Sunday"},
269 
270         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, "today"},
271         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEEK, "this week"},
272         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONTH, "this month"},
273         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_YEAR, "this year"},
274         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONDAY, "this Monday"},
275         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_TUESDAY, "this Tuesday"},
276         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEDNESDAY, "this Wednesday"},
277         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_THURSDAY, "this Thursday"},
278         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_FRIDAY, "this Friday"},
279         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SATURDAY, "this Saturday"},
280         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, "this Sunday"},
281 
282         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_DAY, "day"},
283         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEEK, "week"},
284         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONTH, "month"},
285         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_YEAR, "year"},
286         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONDAY, "Monday"},
287         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_TUESDAY, "Tuesday"},
288         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEDNESDAY, "Wednesday"},
289         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_THURSDAY, "Thursday"},
290         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_FRIDAY, "Friday"},
291         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SATURDAY, "Saturday"},
292         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, "Sunday"},
293 
294         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, "now"}
295 };
296 
297 static WithoutQuantityExpected kEnglishNoQuantityCaps[] = {
298         {UDAT_DIRECTION_NEXT_2, UDAT_ABSOLUTE_DAY, ""},
299 
300         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, "Tomorrow"},
301         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEEK, "Next week"},
302         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONTH, "Next month"},
303         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, "Next year"},
304         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONDAY, "Next Monday"},
305         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_TUESDAY, "Next Tuesday"},
306         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEDNESDAY, "Next Wednesday"},
307         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_THURSDAY, "Next Thursday"},
308         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_FRIDAY, "Next Friday"},
309         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SATURDAY, "Next Saturday"},
310         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, "Next Sunday"},
311 
312         {UDAT_DIRECTION_LAST_2, UDAT_ABSOLUTE_DAY, ""},
313 
314         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, "Yesterday"},
315         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEEK, "Last week"},
316         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONTH, "Last month"},
317         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_YEAR, "Last year"},
318         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONDAY, "Last Monday"},
319         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_TUESDAY, "Last Tuesday"},
320         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEDNESDAY, "Last Wednesday"},
321         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_THURSDAY, "Last Thursday"},
322         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_FRIDAY, "Last Friday"},
323         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SATURDAY, "Last Saturday"},
324         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, "Last Sunday"},
325 
326         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, "Today"},
327         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEEK, "This week"},
328         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONTH, "This month"},
329         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_YEAR, "This year"},
330         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONDAY, "This Monday"},
331         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_TUESDAY, "This Tuesday"},
332         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEDNESDAY, "This Wednesday"},
333         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_THURSDAY, "This Thursday"},
334         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_FRIDAY, "This Friday"},
335         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SATURDAY, "This Saturday"},
336         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, "This Sunday"},
337 
338         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_DAY, "Day"},
339         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEEK, "Week"},
340         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONTH, "Month"},
341         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_YEAR, "Year"},
342         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONDAY, "Monday"},
343         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_TUESDAY, "Tuesday"},
344         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEDNESDAY, "Wednesday"},
345         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_THURSDAY, "Thursday"},
346         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_FRIDAY, "Friday"},
347         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SATURDAY, "Saturday"},
348         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, "Sunday"},
349 
350         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, "Now"}
351 };
352 
353 static WithoutQuantityExpected kEnglishNoQuantityShort[] = {
354         {UDAT_DIRECTION_NEXT_2, UDAT_ABSOLUTE_DAY, ""},
355 
356         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, "tomorrow"},
357         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEEK, "next wk."},
358         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONTH, "next mo."},
359         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, "next yr."},
360         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONDAY, "next Mon."},
361         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_TUESDAY, "next Tue."},
362         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEDNESDAY, "next Wed."},
363         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_THURSDAY, "next Thu."},
364         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_FRIDAY, "next Fri."},
365         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SATURDAY, "next Sat."},
366         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, "next Sun."},
367 
368         {UDAT_DIRECTION_LAST_2, UDAT_ABSOLUTE_DAY, ""},
369 
370         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, "yesterday"},
371         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEEK, "last wk."},
372         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONTH, "last mo."},
373         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_YEAR, "last yr."},
374         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONDAY, "last Mon."},
375         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_TUESDAY, "last Tue."},
376         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEDNESDAY, "last Wed."},
377         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_THURSDAY, "last Thu."},
378         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_FRIDAY, "last Fri."},
379         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SATURDAY, "last Sat."},
380         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, "last Sun."},
381 
382         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, "today"},
383         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEEK, "this wk."},
384         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONTH, "this mo."},
385         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_YEAR, "this yr."},
386         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONDAY, "this Mon."},
387         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_TUESDAY, "this Tue."},
388         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEDNESDAY, "this Wed."},
389         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_THURSDAY, "this Thu."},
390         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_FRIDAY, "this Fri."},
391         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SATURDAY, "this Sat."},
392         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, "this Sun."},
393 
394         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_DAY, "day"},
395         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEEK, "wk."},
396         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONTH, "mo."},
397         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_YEAR, "yr."},
398         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONDAY, "Mo"},
399         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_TUESDAY, "Tu"},
400         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEDNESDAY, "We"},
401         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_THURSDAY, "Th"},
402         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_FRIDAY, "Fr"},
403         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SATURDAY, "Sa"},
404         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, "Su"},
405 
406         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, "now"}
407 };
408 
409 static WithoutQuantityExpected kEnglishNoQuantityNarrow[] = {
410         {UDAT_DIRECTION_NEXT_2, UDAT_ABSOLUTE_DAY, ""},
411 
412         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, "tomorrow"},
413         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEEK, "next wk."},
414         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONTH, "next mo."},
415         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, "next yr."},
416         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_MONDAY, "next M"},
417         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_TUESDAY, "next Tu"},
418         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_WEDNESDAY, "next W"},
419         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_THURSDAY, "next Th"},
420         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_FRIDAY, "next F"},
421         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SATURDAY, "next Sa"},
422         {UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, "next Su"},
423 
424         {UDAT_DIRECTION_LAST_2, UDAT_ABSOLUTE_DAY, ""},
425 
426         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, "yesterday"},
427         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEEK, "last wk."},
428         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONTH, "last mo."},
429         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_YEAR, "last yr."},
430         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_MONDAY, "last M"},
431         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_TUESDAY, "last Tu"},
432         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_WEDNESDAY, "last W"},
433         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_THURSDAY, "last Th"},
434         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_FRIDAY, "last F"},
435         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SATURDAY, "last Sa"},
436         {UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, "last Su"},
437 
438         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, "today"},
439         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEEK, "this wk."},
440         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONTH, "this mo."},
441         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_YEAR, "this yr."},
442         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_MONDAY, "this M"},
443         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_TUESDAY, "this Tu"},
444         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_WEDNESDAY, "this W"},
445         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_THURSDAY, "this Th"},
446         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_FRIDAY, "this F"},
447         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SATURDAY, "this Sa"},
448         {UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, "this Su"},
449 
450         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_DAY, "day"},
451         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEEK, "wk."},
452         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONTH, "mo."},
453         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_YEAR, "yr."},
454         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_MONDAY, "M"},
455         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_TUESDAY, "T"},
456         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_WEDNESDAY, "W"},
457         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_THURSDAY, "T"},
458         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_FRIDAY, "F"},
459         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SATURDAY, "S"},
460         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, "S"},
461 
462         {UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, "now"}
463 };
464 
465 static WithoutQuantityExpected kSpanishNoQuantity[] = {
466         {UDAT_DIRECTION_NEXT_2, UDAT_ABSOLUTE_DAY, "pasado ma\\u00F1ana"},
467         {UDAT_DIRECTION_LAST_2, UDAT_ABSOLUTE_DAY, "anteayer"}
468 };
469 
470 class RelativeDateTimeFormatterTest : public IntlTest {
471 public:
RelativeDateTimeFormatterTest()472     RelativeDateTimeFormatterTest() {
473     }
474 
475     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
476 private:
477     void TestEnglish();
478     void TestEnglishCaps();
479     void TestEnglishShort();
480     void TestEnglishNarrow();
481     void TestSerbian();
482     void TestSerbianFallback();
483     void TestEnglishNoQuantity();
484     void TestEnglishNoQuantityCaps();
485     void TestEnglishNoQuantityShort();
486     void TestEnglishNoQuantityNarrow();
487     void TestSpanishNoQuantity();
488     void TestFormatWithQuantityIllegalArgument();
489     void TestFormatWithoutQuantityIllegalArgument();
490     void TestCustomNumberFormat();
491     void TestGetters();
492     void TestCombineDateAndTime();
493     void TestBadDisplayContext();
494     void RunTest(
495             const Locale& locale,
496             const WithQuantityExpected* expectedResults,
497             int32_t expectedResultLength);
498     void RunTest(
499             const Locale& locale,
500             UDateRelativeDateTimeFormatterStyle style,
501             const WithQuantityExpected* expectedResults,
502             int32_t expectedResultLength);
503     void RunTest(
504             const Locale& locale,
505             const WithoutQuantityExpected* expectedResults,
506             int32_t expectedResultLength);
507     void RunTest(
508             const Locale& locale,
509             UDateRelativeDateTimeFormatterStyle style,
510             const WithoutQuantityExpected* expectedResults,
511             int32_t expectedResultLength);
512     void RunTest(
513             const RelativeDateTimeFormatter& fmt,
514             const WithQuantityExpected* expectedResults,
515             int32_t expectedResultLength,
516             const char *description);
517     void RunTest(
518             const RelativeDateTimeFormatter& fmt,
519             const WithoutQuantityExpected* expectedResults,
520             int32_t expectedResultLength,
521             const char *description);
522     void CheckExpectedResult(
523             const RelativeDateTimeFormatter& fmt,
524             const WithQuantityExpected& expectedResult,
525             const char* description);
526     void CheckExpectedResult(
527             const RelativeDateTimeFormatter& fmt,
528             const WithoutQuantityExpected& expectedResult,
529             const char* description);
530     void VerifyIllegalArgument(
531             const RelativeDateTimeFormatter& fmt,
532             UDateDirection direction,
533             UDateRelativeUnit unit);
534     void VerifyIllegalArgument(
535             const RelativeDateTimeFormatter& fmt,
536             UDateDirection direction,
537             UDateAbsoluteUnit unit);
538     void TestSidewaysDataLoading(void);
539 };
540 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)541 void RelativeDateTimeFormatterTest::runIndexedTest(
542         int32_t index, UBool exec, const char *&name, char *) {
543     if (exec) {
544         logln("TestSuite RelativeDateTimeFormatterTest: ");
545     }
546     TESTCASE_AUTO_BEGIN;
547     TESTCASE_AUTO(TestEnglish);
548     TESTCASE_AUTO(TestEnglishCaps);
549     TESTCASE_AUTO(TestEnglishShort);
550     TESTCASE_AUTO(TestEnglishNarrow);
551     TESTCASE_AUTO(TestSerbian);
552     TESTCASE_AUTO(TestSerbianFallback);
553     TESTCASE_AUTO(TestEnglishNoQuantity);
554     TESTCASE_AUTO(TestEnglishNoQuantityCaps);
555     TESTCASE_AUTO(TestEnglishNoQuantityShort);
556     TESTCASE_AUTO(TestEnglishNoQuantityNarrow);
557     TESTCASE_AUTO(TestSpanishNoQuantity);
558     TESTCASE_AUTO(TestFormatWithQuantityIllegalArgument);
559     TESTCASE_AUTO(TestFormatWithoutQuantityIllegalArgument);
560     TESTCASE_AUTO(TestCustomNumberFormat);
561     TESTCASE_AUTO(TestGetters);
562     TESTCASE_AUTO(TestCombineDateAndTime);
563     TESTCASE_AUTO(TestBadDisplayContext);
564     TESTCASE_AUTO(TestSidewaysDataLoading);
565     TESTCASE_AUTO_END;
566 }
567 
TestEnglish()568 void RelativeDateTimeFormatterTest::TestEnglish() {
569     RunTest("en", kEnglish, UPRV_LENGTHOF(kEnglish));
570 }
571 
TestEnglishCaps()572 void RelativeDateTimeFormatterTest::TestEnglishCaps() {
573     UErrorCode status = U_ZERO_ERROR;
574     RelativeDateTimeFormatter fmt(
575             "en",
576             NULL,
577             UDAT_STYLE_LONG,
578             UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE,
579             status);
580     if (U_FAILURE(status)) {
581         dataerrln("Failed call to RelativeDateTimeFormatter(\"en\", NULL, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, status); : %s", u_errorName(status));
582         return;
583     }
584     RelativeDateTimeFormatter fmt3(status);
585 
586     // Test assignment and copy constructor with capitalization on.
587     RelativeDateTimeFormatter fmt2(fmt);
588     fmt3 = fmt2;
589     assertSuccess("", status);
590     RunTest(fmt3, kEnglishCaps, UPRV_LENGTHOF(kEnglishCaps), "en caps");
591 }
592 
TestEnglishShort()593 void RelativeDateTimeFormatterTest::TestEnglishShort() {
594     RunTest("en", UDAT_STYLE_SHORT, kEnglishShort, UPRV_LENGTHOF(kEnglishShort));
595 }
596 
TestEnglishNarrow()597 void RelativeDateTimeFormatterTest::TestEnglishNarrow() {
598     RunTest("en", UDAT_STYLE_NARROW, kEnglishShort, UPRV_LENGTHOF(kEnglishShort));
599 }
600 
TestSerbian()601 void RelativeDateTimeFormatterTest::TestSerbian() {
602     RunTest("sr", kSerbian, UPRV_LENGTHOF(kSerbian));
603 }
604 
TestSerbianFallback()605 void RelativeDateTimeFormatterTest::TestSerbianFallback() {
606     RunTest("sr", UDAT_STYLE_NARROW, kSerbianNarrow, UPRV_LENGTHOF(kSerbianNarrow));
607 }
608 
TestEnglishNoQuantity()609 void RelativeDateTimeFormatterTest::TestEnglishNoQuantity() {
610     RunTest("en", kEnglishNoQuantity, UPRV_LENGTHOF(kEnglishNoQuantity));
611 }
612 
TestEnglishNoQuantityCaps()613 void RelativeDateTimeFormatterTest::TestEnglishNoQuantityCaps() {
614     UErrorCode status = U_ZERO_ERROR;
615     RelativeDateTimeFormatter fmt(
616             "en",
617             NULL,
618             UDAT_STYLE_LONG,
619             UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE,
620             status);
621     if (assertSuccess("RelativeDateTimeFormatter", status, TRUE) == FALSE) {
622         return;
623     }
624     RunTest(
625             fmt,
626             kEnglishNoQuantityCaps,
627             UPRV_LENGTHOF(kEnglishNoQuantityCaps),
628             "en caps no quantity");
629 }
630 
TestEnglishNoQuantityShort()631 void RelativeDateTimeFormatterTest::TestEnglishNoQuantityShort() {
632     RunTest(
633             "en",
634             UDAT_STYLE_SHORT,
635             kEnglishNoQuantityShort,
636             UPRV_LENGTHOF(kEnglishNoQuantityShort));
637 }
638 
TestEnglishNoQuantityNarrow()639 void RelativeDateTimeFormatterTest::TestEnglishNoQuantityNarrow() {
640     RunTest(
641             "en",
642             UDAT_STYLE_NARROW,
643             kEnglishNoQuantityNarrow,
644             UPRV_LENGTHOF(kEnglishNoQuantityNarrow));
645 }
646 
TestSpanishNoQuantity()647 void RelativeDateTimeFormatterTest::TestSpanishNoQuantity() {
648     RunTest("es", kSpanishNoQuantity, UPRV_LENGTHOF(kSpanishNoQuantity));
649 }
650 
TestFormatWithQuantityIllegalArgument()651 void RelativeDateTimeFormatterTest::TestFormatWithQuantityIllegalArgument() {
652     UErrorCode status = U_ZERO_ERROR;
653     RelativeDateTimeFormatter fmt("en", status);
654     if (U_FAILURE(status)) {
655         dataerrln("Failure creating format object - %s", u_errorName(status));
656         return;
657     }
658     VerifyIllegalArgument(fmt, UDAT_DIRECTION_PLAIN, UDAT_RELATIVE_DAYS);
659     VerifyIllegalArgument(fmt, UDAT_DIRECTION_THIS, UDAT_RELATIVE_DAYS);
660 }
661 
TestFormatWithoutQuantityIllegalArgument()662 void RelativeDateTimeFormatterTest::TestFormatWithoutQuantityIllegalArgument() {
663     UErrorCode status = U_ZERO_ERROR;
664     RelativeDateTimeFormatter fmt("en", status);
665     if (U_FAILURE(status)) {
666         dataerrln("Failure creating format object - %s", u_errorName(status));
667         return;
668     }
669     VerifyIllegalArgument(fmt, UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_NOW);
670     VerifyIllegalArgument(fmt, UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_NOW);
671     VerifyIllegalArgument(fmt, UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_NOW);
672 }
673 
TestCustomNumberFormat()674 void RelativeDateTimeFormatterTest::TestCustomNumberFormat() {
675     NumberFormat *nf;
676     UErrorCode status = U_ZERO_ERROR;
677     {
678         RelativeDateTimeFormatter fmt("en", status);
679         if (U_FAILURE(status)) {
680             dataerrln(
681                     "Failure creating format object - %s", u_errorName(status));
682             return;
683         }
684         nf = (NumberFormat *) fmt.getNumberFormat().clone();
685     }
686     nf->setMinimumFractionDigits(1);
687     nf->setMaximumFractionDigits(1);
688     RelativeDateTimeFormatter fmt("en", nf, status);
689 
690     // Test copy constructor.
691     RelativeDateTimeFormatter fmt2(fmt);
692     RunTest(fmt2, kEnglishDecimal, UPRV_LENGTHOF(kEnglishDecimal), "en decimal digits");
693 
694     // Test assignment
695     fmt = RelativeDateTimeFormatter("es", status);
696     RunTest(fmt, kSpanishNoQuantity, UPRV_LENGTHOF(kSpanishNoQuantity), "assignment operator");
697 
698 }
699 
TestGetters()700 void RelativeDateTimeFormatterTest::TestGetters() {
701     UErrorCode status = U_ZERO_ERROR;
702     RelativeDateTimeFormatter fmt(
703             "en",
704             NULL,
705             UDAT_STYLE_NARROW,
706             UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,
707             status);
708     if (U_FAILURE(status)) {
709         dataerrln("Failed call to RelativeDateTimeFormatter(\"en\", NULL, UDAT_STYLE_NARROW, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, status);) : %s", u_errorName(status));
710         return;
711     }
712     RelativeDateTimeFormatter fmt3(status);
713 
714     // copy and assignment.
715     RelativeDateTimeFormatter fmt2(fmt);
716     fmt3 = fmt2;
717     assertEquals("style", (int32_t)UDAT_STYLE_NARROW, fmt3.getFormatStyle());
718     assertEquals(
719             "context",
720             (int32_t)UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,
721             fmt3.getCapitalizationContext());
722     assertSuccess("", status);
723 }
724 
TestCombineDateAndTime()725 void RelativeDateTimeFormatterTest::TestCombineDateAndTime() {
726     UErrorCode status = U_ZERO_ERROR;
727     RelativeDateTimeFormatter fmt("en", status);
728     if (U_FAILURE(status)) {
729         dataerrln("Failure creating format object - %s", u_errorName(status));
730         return;
731     }
732     UnicodeString actual;
733     fmt.combineDateAndTime(
734         UnicodeString("yesterday"),
735         UnicodeString("3:50"),
736         actual,
737         status);
738     UnicodeString expected("yesterday, 3:50");
739     if (expected != actual) {
740         errln("Expected "+expected+", got "+actual);
741     }
742 }
743 
TestBadDisplayContext()744 void RelativeDateTimeFormatterTest::TestBadDisplayContext() {
745     UErrorCode status = U_ZERO_ERROR;
746     RelativeDateTimeFormatter fmt(
747             "en", NULL, UDAT_STYLE_LONG, UDISPCTX_STANDARD_NAMES, status);
748     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
749         errln("Expected U_ILLEGAL_ARGUMENT_ERROR, got %s", u_errorName(status));
750     }
751 }
752 
753 
754 
RunTest(const Locale & locale,const WithQuantityExpected * expectedResults,int32_t expectedResultLength)755 void RelativeDateTimeFormatterTest::RunTest(
756         const Locale& locale,
757         const WithQuantityExpected* expectedResults,
758         int32_t expectedResultLength) {
759     UErrorCode status = U_ZERO_ERROR;
760     RelativeDateTimeFormatter fmt(locale, status);
761     if (U_FAILURE(status)) {
762         dataerrln("Unable to create format object - %s", u_errorName(status));
763         return;
764     }
765     RunTest(fmt, expectedResults, expectedResultLength, locale.getName());
766 }
767 
RunTest(const Locale & locale,UDateRelativeDateTimeFormatterStyle style,const WithQuantityExpected * expectedResults,int32_t expectedResultLength)768 void RelativeDateTimeFormatterTest::RunTest(
769         const Locale& locale,
770         UDateRelativeDateTimeFormatterStyle style,
771         const WithQuantityExpected* expectedResults,
772         int32_t expectedResultLength) {
773     UErrorCode status = U_ZERO_ERROR;
774     RelativeDateTimeFormatter fmt(
775             locale, NULL, style, UDISPCTX_CAPITALIZATION_NONE, status);
776     if (U_FAILURE(status)) {
777         dataerrln("Unable to create format object - %s", u_errorName(status));
778         return;
779     }
780     RunTest(fmt, expectedResults, expectedResultLength, locale.getName());
781 }
782 
RunTest(const Locale & locale,const WithoutQuantityExpected * expectedResults,int32_t expectedResultLength)783 void RelativeDateTimeFormatterTest::RunTest(
784         const Locale& locale,
785         const WithoutQuantityExpected* expectedResults,
786         int32_t expectedResultLength) {
787     UErrorCode status = U_ZERO_ERROR;
788     RelativeDateTimeFormatter fmt(locale, status);
789     if (U_FAILURE(status)) {
790         dataerrln("Unable to create format object - %s", u_errorName(status));
791         return;
792     }
793     RunTest(fmt, expectedResults, expectedResultLength, locale.getName());
794 }
795 
RunTest(const Locale & locale,UDateRelativeDateTimeFormatterStyle style,const WithoutQuantityExpected * expectedResults,int32_t expectedResultLength)796 void RelativeDateTimeFormatterTest::RunTest(
797         const Locale& locale,
798         UDateRelativeDateTimeFormatterStyle style,
799         const WithoutQuantityExpected* expectedResults,
800         int32_t expectedResultLength) {
801     UErrorCode status = U_ZERO_ERROR;
802     RelativeDateTimeFormatter fmt(
803             locale, NULL, style, UDISPCTX_CAPITALIZATION_NONE, status);
804     if (U_FAILURE(status)) {
805         dataerrln("Unable to create format object - %s", u_errorName(status));
806         return;
807     }
808     RunTest(fmt, expectedResults, expectedResultLength, locale.getName());
809 }
810 
RunTest(const RelativeDateTimeFormatter & fmt,const WithQuantityExpected * expectedResults,int32_t expectedResultLength,const char * description)811 void RelativeDateTimeFormatterTest::RunTest(
812         const RelativeDateTimeFormatter& fmt,
813         const WithQuantityExpected* expectedResults,
814         int32_t expectedResultLength,
815         const char *description) {
816     for (int32_t i = 0; i < expectedResultLength; ++i) {
817         CheckExpectedResult(fmt, expectedResults[i], description);
818     }
819 }
820 
RunTest(const RelativeDateTimeFormatter & fmt,const WithoutQuantityExpected * expectedResults,int32_t expectedResultLength,const char * description)821 void RelativeDateTimeFormatterTest::RunTest(
822         const RelativeDateTimeFormatter& fmt,
823         const WithoutQuantityExpected* expectedResults,
824         int32_t expectedResultLength,
825         const char *description) {
826     for (int32_t i = 0; i < expectedResultLength; ++i) {
827         CheckExpectedResult(fmt, expectedResults[i], description);
828     }
829 }
830 
CheckExpectedResult(const RelativeDateTimeFormatter & fmt,const WithQuantityExpected & expectedResult,const char * description)831 void RelativeDateTimeFormatterTest::CheckExpectedResult(
832         const RelativeDateTimeFormatter& fmt,
833         const WithQuantityExpected& expectedResult,
834         const char* description) {
835     UErrorCode status = U_ZERO_ERROR;
836     UnicodeString actual;
837     fmt.format(expectedResult.value, expectedResult.direction, expectedResult.unit, actual, status);
838     UnicodeString expected(expectedResult.expected, -1, US_INV);
839     expected = expected.unescape();
840     char buffer[256];
841     sprintf(
842             buffer,
843             "%s, %f, %s, %s",
844             description,
845             expectedResult.value,
846             DirectionStr(expectedResult.direction),
847             RelativeUnitStr(expectedResult.unit));
848     if (actual != expected) {
849         errln(UnicodeString("Fail: Expected: ") + expected
850                 + ", Got: " + actual
851                 + ", For: " + buffer);
852     }
853 }
854 
CheckExpectedResult(const RelativeDateTimeFormatter & fmt,const WithoutQuantityExpected & expectedResult,const char * description)855 void RelativeDateTimeFormatterTest::CheckExpectedResult(
856         const RelativeDateTimeFormatter& fmt,
857         const WithoutQuantityExpected& expectedResult,
858         const char* description) {
859     UErrorCode status = U_ZERO_ERROR;
860     UnicodeString actual;
861     fmt.format(expectedResult.direction, expectedResult.unit, actual, status);
862     UnicodeString expected(expectedResult.expected, -1, US_INV);
863     expected = expected.unescape();
864     char buffer[256];
865     sprintf(
866             buffer,
867             "%s, %s, %s",
868             description,
869             DirectionStr(expectedResult.direction),
870             AbsoluteUnitStr(expectedResult.unit));
871     if (actual != expected) {
872         errln(UnicodeString("Fail: Expected: ") + expected
873                 + ", Got: " + actual
874                 + ", For: " + buffer);
875     }
876 }
877 
VerifyIllegalArgument(const RelativeDateTimeFormatter & fmt,UDateDirection direction,UDateRelativeUnit unit)878 void RelativeDateTimeFormatterTest::VerifyIllegalArgument(
879         const RelativeDateTimeFormatter& fmt,
880         UDateDirection direction,
881         UDateRelativeUnit unit) {
882     UnicodeString appendTo;
883     UErrorCode status = U_ZERO_ERROR;
884     fmt.format(1.0, direction, unit, appendTo, status);
885     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
886         errln("Expected U_ILLEGAL_ARGUMENT_ERROR, got %s", u_errorName(status));
887     }
888 }
889 
VerifyIllegalArgument(const RelativeDateTimeFormatter & fmt,UDateDirection direction,UDateAbsoluteUnit unit)890 void RelativeDateTimeFormatterTest::VerifyIllegalArgument(
891         const RelativeDateTimeFormatter& fmt,
892         UDateDirection direction,
893         UDateAbsoluteUnit unit) {
894     UnicodeString appendTo;
895     UErrorCode status = U_ZERO_ERROR;
896     fmt.format(direction, unit, appendTo, status);
897     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
898         errln("Expected U_ILLEGAL_ARGUMENT_ERROR, got %s", u_errorName(status));
899     }
900 }
901 
902 /* Add tests to check "sideways" data loading. */
TestSidewaysDataLoading(void)903 void RelativeDateTimeFormatterTest::TestSidewaysDataLoading(void) {
904     UErrorCode status = U_ZERO_ERROR;
905     UnicodeString actual;
906     UnicodeString expected;
907     Locale enGbLocale("en_GB");
908 
909     RelativeDateTimeFormatter fmt(enGbLocale, NULL, UDAT_STYLE_NARROW,
910                                   UDISPCTX_CAPITALIZATION_NONE, status);
911     if (U_FAILURE(status)) {
912         dataerrln("Unable to create RelativeDateTimeFormatter - %s", u_errorName(status));
913         return;
914     }
915 
916     status = U_ZERO_ERROR;
917     actual = "";
918     fmt.format(3.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_MONTHS, actual, status);
919     expected = "in 3 mo";
920     assertEquals("narrow in 3 mo", expected, actual);
921 
922     fmt.format(3.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, actual.remove(), status);
923     expected = "3 days ago";
924     assertEquals("3 days ago (positive 3.0): ", expected, actual);
925 
926     expected = "-3 days ago";
927     fmt.format(-3.0, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, actual.remove(), status);
928     assertEquals("3 days ago (negative 3.0): ", expected, actual);
929 
930     expected = "next yr.";
931     fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, actual.remove(), status);
932     assertEquals("next year: ", expected, actual);
933 
934     // Testing the SHORT style
935     RelativeDateTimeFormatter fmtshort(enGbLocale, NULL, UDAT_STYLE_SHORT,
936                                   UDISPCTX_CAPITALIZATION_NONE, status);
937     expected = "now";
938     fmtshort.format(0.0, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_SECONDS, actual.remove(), status);
939 
940     expected = "next yr.";
941     fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_YEAR, actual.remove(), status);
942     assertEquals("next year: ", expected, actual);
943 }
944 
945 static const char *kLast2 = "Last_2";
946 static const char *kLast = "Last";
947 static const char *kThis = "This";
948 static const char *kNext = "Next";
949 static const char *kNext2 = "Next_2";
950 static const char *kPlain = "Plain";
951 
952 static const char *kSeconds = "Seconds";
953 static const char *kMinutes = "Minutes";
954 static const char *kHours = "Hours";
955 static const char *kDays = "Days";
956 static const char *kWeeks = "Weeks";
957 static const char *kMonths = "Months";
958 static const char *kYears = "Years";
959 
960 static const char *kSunday = "Sunday";
961 static const char *kMonday = "Monday";
962 static const char *kTuesday = "Tuesday";
963 static const char *kWednesday = "Wednesday";
964 static const char *kThursday = "Thursday";
965 static const char *kFriday = "Friday";
966 static const char *kSaturday = "Saturday";
967 static const char *kDay = "Day";
968 static const char *kWeek = "Week";
969 static const char *kMonth = "Month";
970 static const char *kYear = "Year";
971 static const char *kNow = "Now";
972 
973 static const char *kUndefined = "Undefined";
974 
DirectionStr(UDateDirection direction)975 static const char *DirectionStr(
976         UDateDirection direction) {
977     switch (direction) {
978         case UDAT_DIRECTION_LAST_2:
979             return kLast2;
980         case UDAT_DIRECTION_LAST:
981             return kLast;
982         case UDAT_DIRECTION_THIS:
983             return kThis;
984         case UDAT_DIRECTION_NEXT:
985             return kNext;
986         case UDAT_DIRECTION_NEXT_2:
987             return kNext2;
988         case UDAT_DIRECTION_PLAIN:
989             return kPlain;
990         default:
991             return kUndefined;
992     }
993     return kUndefined;
994 }
995 
RelativeUnitStr(UDateRelativeUnit unit)996 static const char *RelativeUnitStr(
997         UDateRelativeUnit unit) {
998     switch (unit) {
999         case UDAT_RELATIVE_SECONDS:
1000             return kSeconds;
1001         case UDAT_RELATIVE_MINUTES:
1002             return kMinutes;
1003         case UDAT_RELATIVE_HOURS:
1004             return kHours;
1005         case UDAT_RELATIVE_DAYS:
1006             return kDays;
1007         case UDAT_RELATIVE_WEEKS:
1008             return kWeeks;
1009         case UDAT_RELATIVE_MONTHS:
1010             return kMonths;
1011         case UDAT_RELATIVE_YEARS:
1012             return kYears;
1013         default:
1014             return kUndefined;
1015     }
1016     return kUndefined;
1017 }
1018 
AbsoluteUnitStr(UDateAbsoluteUnit unit)1019 static const char *AbsoluteUnitStr(
1020         UDateAbsoluteUnit unit) {
1021     switch (unit) {
1022         case UDAT_ABSOLUTE_SUNDAY:
1023             return kSunday;
1024         case UDAT_ABSOLUTE_MONDAY:
1025             return kMonday;
1026         case UDAT_ABSOLUTE_TUESDAY:
1027             return kTuesday;
1028         case UDAT_ABSOLUTE_WEDNESDAY:
1029             return kWednesday;
1030         case UDAT_ABSOLUTE_THURSDAY:
1031             return kThursday;
1032         case UDAT_ABSOLUTE_FRIDAY:
1033             return kFriday;
1034         case UDAT_ABSOLUTE_SATURDAY:
1035             return kSaturday;
1036         case UDAT_ABSOLUTE_DAY:
1037             return kDay;
1038         case UDAT_ABSOLUTE_WEEK:
1039             return kWeek;
1040         case UDAT_ABSOLUTE_MONTH:
1041             return kMonth;
1042         case UDAT_ABSOLUTE_YEAR:
1043             return kYear;
1044         case UDAT_ABSOLUTE_NOW:
1045             return kNow;
1046         default:
1047             return kUndefined;
1048     }
1049     return kUndefined;
1050 }
1051 
createRelativeDateTimeFormatterTest()1052 extern IntlTest *createRelativeDateTimeFormatterTest() {
1053     return new RelativeDateTimeFormatterTest();
1054 }
1055 
1056 #endif
1057