1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5 *******************************************************************************
6 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
7 * others. All Rights Reserved.                                                *
8 *******************************************************************************
9 *
10 */
11
12package android.icu.dev.test.timescale;
13
14import org.junit.Test;
15import org.junit.runner.RunWith;
16import org.junit.runners.JUnit4;
17
18import android.icu.dev.test.TestFmwk;
19import android.icu.math.BigDecimal;
20import android.icu.util.UniversalTimeScale;
21import android.icu.testsharding.MainTestShard;
22
23/**
24 * Test UniversalTimeScale API
25 */
26@MainTestShard
27@RunWith(JUnit4.class)
28public class TimeScaleAPITest extends TestFmwk
29{
30
31    /**
32     *
33     */
34    public TimeScaleAPITest()
35    {
36    }
37
38    @Test
39    public void TestBigDecimalFromBigDecimal()
40    {
41        BigDecimal bigZero = new BigDecimal(0);
42
43        try {
44            UniversalTimeScale.bigDecimalFrom(bigZero, -1);
45            errln("bigDecimalFrom(bigZero, -1) did not throw IllegalArgumentException.");
46        } catch (IllegalArgumentException iae) {
47            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
48        }
49
50        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
51            try {
52                UniversalTimeScale.bigDecimalFrom(bigZero, scale);
53            } catch (IllegalArgumentException iae) {
54                errln("bigDecimalFrom(bigZero, " + scale + ") threw IllegalArgumentException.");
55            }
56        }
57
58        try {
59            UniversalTimeScale.bigDecimalFrom(bigZero, UniversalTimeScale.MAX_SCALE);
60            errln("from(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
61        } catch (IllegalArgumentException iae) {
62            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
63        }
64    }
65
66    @Test
67    public void TestBigDecimalFromDouble()
68    {
69        try {
70            UniversalTimeScale.bigDecimalFrom(0.0, -1);
71            errln("bigDecimalFrom(0.0, -1) did not throw IllegalArgumentException.");
72        } catch (IllegalArgumentException iae) {
73            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
74        }
75
76        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
77            try {
78                UniversalTimeScale.bigDecimalFrom(0.0, scale);
79            } catch (IllegalArgumentException iae) {
80                errln("bigDecimalFrom(0.0, " + scale + ") threw IllegalArgumentException.");
81            }
82       }
83
84        try {
85            UniversalTimeScale.bigDecimalFrom(0.0, UniversalTimeScale.MAX_SCALE);
86            errln("from(0.0, MAX_SCALE) did not throw IllegalArgumetException.");
87        } catch (IllegalArgumentException iae) {
88            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
89        }
90    }
91
92    @Test
93    public void TestBigDecimalFromLong()
94    {
95        try {
96            UniversalTimeScale.bigDecimalFrom(0L, -1);
97            errln("bigDecimalFrom(0L, -1) did not throw IllegalArgumentException.");
98        } catch (IllegalArgumentException iae) {
99            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
100        }
101
102        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
103            try {
104                UniversalTimeScale.bigDecimalFrom(0L, scale);
105            } catch (IllegalArgumentException iae) {
106                errln("bigDecimalFrom(0L, " + scale + ") threw IllegalArgumentException.");
107            }
108       }
109
110        try {
111            UniversalTimeScale.bigDecimalFrom(0L, UniversalTimeScale.MAX_SCALE);
112            errln("from(0L, MAX_SCALE) did not throw IllegalArgumetException.");
113        } catch (IllegalArgumentException iae) {
114            logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
115        }
116    }
117
118    @Test
119    public void TestFromLong()
120    {
121        long result;
122
123        try {
124            result = UniversalTimeScale.from(0L, -1);
125            errln("from(0L, -1) did not throw IllegalArgumentException.");
126        } catch (IllegalArgumentException iae) {
127            logln("PASS: UniversalTimeScale.from failed as expected");
128        }
129
130        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
131            long fromMin = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.FROM_MIN_VALUE);
132            long fromMax = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.FROM_MAX_VALUE);
133
134            try {
135                result = UniversalTimeScale.from(0L, scale);
136                logln("from(0L, " + scale + ") returned " + result);
137            } catch (IllegalArgumentException iae) {
138                errln("from(0L, " + scale + ") threw IllegalArgumentException.");
139            }
140
141            try {
142                result = UniversalTimeScale.from(fromMin, scale);
143                logln("from(fromMin, " + scale + ") returned " + result);
144            } catch (IllegalArgumentException iae) {
145                errln("from(fromMin, " + scale + ") threw IllegalArgumentException.");
146            }
147
148            if (fromMin > Long.MIN_VALUE) {
149                try {
150                    result = UniversalTimeScale.from(fromMin - 1, scale);
151                    errln("from(fromMin - 1, " + scale + ") did not throw IllegalArgumentException.");
152                } catch (IllegalArgumentException iae) {
153                    logln("PASS: UniversalTimeScale.from failed as expected");
154                }
155            }
156
157            try {
158                result = UniversalTimeScale.from(fromMax, scale);
159                logln("from(fromMax, " + scale + ") returned " + result);
160            } catch (IllegalArgumentException iae) {
161                errln("from(fromMax, " + scale + ") threw IllegalArgumentException.");
162            }
163
164            if (fromMax < Long.MAX_VALUE) {
165                try {
166                    result = UniversalTimeScale.from(fromMax + 1, scale);
167                    errln("from(fromMax + 1, " + scale + ") did not throw IllegalArgumentException.");
168               } catch (IllegalArgumentException iae) {
169                logln("PASS: UniversalTimeScale.from failed as expected");
170               }
171            }
172       }
173
174        try {
175            result = UniversalTimeScale.from(0L, UniversalTimeScale.MAX_SCALE);
176            errln("from(0L, MAX_SCALE) did not throw IllegalArgumetException.");
177        } catch (IllegalArgumentException iae) {
178            logln("PASS: UniversalTimeScale.from failed as expected");
179        }
180    }
181
182    @Test
183    public void TestGetTimeScale()
184    {
185        long value;
186
187        try {
188            value = UniversalTimeScale.getTimeScaleValue(-1, 0);
189            errln("getTimeScaleValue(-1, 0) did not throw IllegalArgumentException.");
190        } catch (IllegalArgumentException iae) {
191            logln("PASS: UniversalTimeScale.getTimeScaleValue failed as expected");
192        }
193
194        try {
195            value = UniversalTimeScale.getTimeScaleValue(0, -1);
196            errln("getTimeScaleValue(0, -1) did not throw IllegalArgumentException.");
197        } catch (IllegalArgumentException iae) {
198            logln("PASS: UniversalTimeScale.getTimeScaleValue failed as expected");
199        }
200
201        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
202            try {
203                value = UniversalTimeScale.getTimeScaleValue(scale, 0);
204                logln("getTimeScaleValue(" + scale + ", 0) returned " + value);
205            } catch (IllegalArgumentException iae) {
206                errln("getTimeScaleValue(" + scale + ", 0) threw IllegalArgumentException.");
207            }
208        }
209
210        try {
211            value = UniversalTimeScale.getTimeScaleValue(UniversalTimeScale.MAX_SCALE, 0);
212            errln("getTimeScaleValue(MAX_SCALE, 0) did not throw IllegalArgumentException");
213        } catch (IllegalArgumentException iae) {
214            logln("PASS: UniversalTimeScale.getTimeScaleValue failed as expected");
215        }
216
217        try {
218            value = UniversalTimeScale.getTimeScaleValue(0, UniversalTimeScale.MAX_SCALE_VALUE);
219            errln("getTimeScaleValue(0, MAX_SCALE_VALUE) did not throw IllegalArgumentException");
220        } catch (IllegalArgumentException iae) {
221            logln("PASS: UniversalTimeScale.getTimeScaleValue failed as expected");
222        }
223    }
224
225    @Test
226    public void TestToBigDecimalFromBigDecimal()
227    {
228        BigDecimal bigZero = new BigDecimal(0);
229
230        try {
231            UniversalTimeScale.toBigDecimal(bigZero, -1);
232            errln("toBigDecimal(bigZero, -1) did not throw IllegalArgumentException.");
233        } catch (IllegalArgumentException iae) {
234            logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
235        }
236
237        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
238            try {
239                UniversalTimeScale.toBigDecimal(bigZero, scale);
240            } catch (IllegalArgumentException iae) {
241                errln("toBigDecimal(bigZero, " + scale + ") threw IllegalArgumentException.");
242            }
243        }
244
245        try {
246            UniversalTimeScale.toBigDecimal(bigZero, UniversalTimeScale.MAX_SCALE);
247            errln("toBigDecimal(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
248        } catch (IllegalArgumentException iae) {
249            logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
250        }
251    }
252
253    @Test
254    public void TestToBigDecimalTrunc()
255    {
256        BigDecimal bigZero = new BigDecimal(0);
257
258        try {
259            UniversalTimeScale.toBigDecimalTrunc(bigZero, -1);
260            errln("toBigDecimalTrunc(bigZero, -1) did not throw IllegalArgumentException.");
261        } catch (IllegalArgumentException iae) {
262            logln("PASS: UniversalTimeScale.toBigDecimalTrunc failed as expected");
263        }
264
265        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
266            try {
267                UniversalTimeScale.toBigDecimalTrunc(bigZero, scale);
268            } catch (IllegalArgumentException iae) {
269                errln("toBigDecimalTrunc(bigZero, " + scale + ") threw IllegalArgumentException.");
270            }
271        }
272
273        try {
274            UniversalTimeScale.toBigDecimalTrunc(bigZero, UniversalTimeScale.MAX_SCALE);
275            errln("toBigDecimalTrunc(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
276        } catch (IllegalArgumentException iae) {
277            logln("PASS: UniversalTimeScale.toBigDecimalTrunc failed as expected");
278        }
279    }
280
281    @Test
282    public void TestToBigDecimalFromLong()
283    {
284        try {
285            UniversalTimeScale.toBigDecimal(0L, -1);
286            errln("toBigDecimal(0L, -1) did not throw IllegalArgumentException.");
287        } catch (IllegalArgumentException iae) {
288            logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
289        }
290
291        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
292            try {
293                UniversalTimeScale.toBigDecimal(0L, scale);
294            } catch (IllegalArgumentException iae) {
295                errln("toBigDecimal(0L, " + scale + ") threw IllegalArgumentException.");
296            }
297        }
298
299        try {
300            UniversalTimeScale.toBigDecimal(0L, UniversalTimeScale.MAX_SCALE);
301            errln("toBigDecimal(0L, MAX_SCALE) did not throw IllegalArgumetException.");
302        } catch (IllegalArgumentException iae) {
303            logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
304        }
305    }
306
307    @Test
308    public void TestToLong()
309    {
310        long result;
311
312        try {
313            result = UniversalTimeScale.toLong(0L, -1);
314            errln("toLong(0L, -1) did not throw IllegalArgumentException.");
315        } catch (IllegalArgumentException iae) {
316            logln("PASS: UniversalTimeScale.toLong failed as expected");
317        }
318
319        for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
320            long toMin = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.TO_MIN_VALUE);
321            long toMax = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.TO_MAX_VALUE);
322
323            try {
324                result = UniversalTimeScale.toLong(0L, scale);
325                logln("toLong(0L, " + scale + ") returned " + result);
326            } catch (IllegalArgumentException iae) {
327                errln("toLong(0L, " + scale + ") threw IllegalArgumentException.");
328            }
329
330            try {
331                result = UniversalTimeScale.toLong(toMin, scale);
332                logln("toLong(toMin, " + scale + ") returned " + result);
333            } catch (IllegalArgumentException iae) {
334                errln("toLong(toMin, " + scale + ") threw IllegalArgumentException.");
335            }
336
337            if (toMin > Long.MIN_VALUE) {
338                try {
339                    result = UniversalTimeScale.toLong(toMin - 1, scale);
340                    errln("toLong(toMin - 1, " + scale + ") did not throw IllegalArgumentException.");
341                } catch (IllegalArgumentException iae) {
342                    logln("PASS: UniversalTimeScale.toLong failed as expected");
343                }
344            }
345
346            try {
347                result = UniversalTimeScale.toLong(toMax, scale);
348                logln("toLong(toMax, " + scale + ") returned " + result);
349            } catch (IllegalArgumentException iae) {
350                errln("toLong(toMax, " + scale + ") threw IllegalArgumentException.");
351            }
352
353            if (toMax < Long.MAX_VALUE) {
354                try {
355                    result = UniversalTimeScale.toLong(toMax + 1, scale);
356                    errln("toLong(toMax + 1, " + scale + ") did not throw IllegalArgumentException.");
357               } catch (IllegalArgumentException iae) {
358                logln("PASS: UniversalTimeScale.toLong failed as expected");
359               }
360            }
361       }
362
363        try {
364            result = UniversalTimeScale.toLong(0L, UniversalTimeScale.MAX_SCALE);
365            errln("toLong(0L, MAX_SCALE) did not throw IllegalArgumetException.");
366        } catch (IllegalArgumentException iae) {
367            logln("PASS: UniversalTimeScale.toLong failed as expected");
368        }
369    }
370}
371