• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2006, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 
9 #ifndef _DATEFORMATROUNDTRIPTEST_
10 #define _DATEFORMATROUNDTRIPTEST_
11 
12 #include "unicode/utypes.h"
13 
14 #if !UCONFIG_NO_FORMATTING
15 
16 #include "unicode/unistr.h"
17 #include "unicode/datefmt.h"
18 #include "unicode/smpdtfmt.h"
19 #include "unicode/calendar.h"
20 #include "intltest.h"
21 
22 /**
23  * Performs round-trip tests for DateFormat
24  **/
25 class DateFormatRoundTripTest : public IntlTest {
26 
27     // IntlTest override
28     void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par ) override;
29 
30 public:
31     DateFormatRoundTripTest();
32     virtual ~DateFormatRoundTripTest();
33 
34     void TestDateFormatRoundTrip(void);
35     void TestCentury(void);
36     void test(const Locale& loc);
37     void test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly = false );
38     int32_t getField(UDate d, int32_t f);
39     UnicodeString& escape(const UnicodeString& src, UnicodeString& dst);
40     UDate generateDate(void);
41     UDate generateDate(UDate minDate);
42 
43 
44 //============================================================
45 // statics
46 //============================================================
47 
48 /**
49  * Return a random uint32_t
50  **/
randLong()51 static uint32_t randLong() {
52     // The portable IntlTest::random() function has sufficient
53     // resolution for a 16-bit value, but not for 32 bits.
54     return ((uint32_t) (IntlTest::random() * (1<<16))) |
55           (((uint32_t) (IntlTest::random() * (1<<16))) << 16);
56 }
57 
58 /**
59  * Return a random double 0 <= x <= 1.0
60  **/
randFraction()61 static double randFraction()
62 {
63     return (double)randLong() / (double)0xFFFFFFFF;
64 }
65 
66 /**
67  * Return a random value from -range..+range (closed).
68  **/
randDouble(double range)69 static double randDouble(double range)
70 {
71     double a = randFraction();
72     //while(TPlatformUtilities::isInfinite(a) || TPlatformUtilities::isNaN(a))
73     //    a = randFraction();
74     return (2.0 * range * a) - range;
75 }
76 
77 protected:
78     UBool failure(UErrorCode status, const char* msg);
79     UBool failure(UErrorCode status, const char* msg, const UnicodeString& str);
80 
81     const UnicodeString& fullFormat(UDate d);
82 
83 private:
84 
85     static int32_t SPARSENESS;
86     static int32_t TRIALS;
87     static int32_t DEPTH;
88 
89     UBool optionv; // true if @v option is given on command line
90     SimpleDateFormat *dateFormat;
91     UnicodeString fgStr;
92     Calendar *getFieldCal;
93 };
94 
95 #endif /* #if !UCONFIG_NO_FORMATTING */
96 
97 #endif // _DATEFORMATROUNDTRIPTEST_
98 //eof
99