xref: /trunk/main/sax/qa/gtest/test_converter.cxx (revision 10079efc)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "preextstl.h"
25 #include "gtest/gtest.h"
26 #include "postextstl.h"
27 
28 #include <rtl/ustrbuf.hxx>
29 
30 #include <com/sun/star/util/DateTime.hpp>
31 #include <com/sun/star/util/Date.hpp>
32 #include <com/sun/star/util/Duration.hpp>
33 
34 #include "sax/tools/converter.hxx"
35 
36 
37 using namespace ::com::sun::star;
38 using sax::Converter;
39 
40 
41 namespace {
42 
43 class ConverterTest
44     : public ::testing::Test
45 {
46 public:
47     virtual void SetUp();
48     virtual void TearDown();
49 };
50 
SetUp()51 void ConverterTest::SetUp()
52 {
53 }
54 
TearDown()55 void ConverterTest::TearDown()
56 {
57 }
58 
eqDuration(util::Duration a,util::Duration b)59 static bool eqDuration(util::Duration a, util::Duration b) {
60     return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days
61         && a.Hours == b.Hours && a.Minutes == b.Minutes
62         && a.Seconds == b.Seconds
63         && a.MilliSeconds == b.MilliSeconds
64         && a.Negative == b.Negative;
65 }
66 
doTest(util::Duration const & rid,char const * const pis,char const * const i_pos=0)67 static void doTest(util::Duration const & rid, char const*const pis,
68         char const*const i_pos = 0)
69 {
70     char const*const pos((i_pos) ? i_pos : pis);
71     util::Duration od;
72     ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
73     bool bSuccess = Converter::convertDuration(od, is);
74     OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dm",
75         od.Negative, od.Years, od.Months, od.Days,
76         od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
77     ASSERT_TRUE(bSuccess);
78     ASSERT_TRUE(eqDuration(rid, od));
79     ::rtl::OUStringBuffer buf;
80     Converter::convertDuration(buf, od);
81     OSL_TRACE(
82         ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8));
83     ASSERT_TRUE(buf.makeStringAndClear().equalsAscii(pos));
84 }
85 
doTestDurationF(char const * const pis)86 static void doTestDurationF(char const*const pis)
87 {
88     util::Duration od;
89     bool bSuccess = Converter::convertDuration(od,
90             ::rtl::OUString::createFromAscii(pis));
91     OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dH",
92         od.Negative, od.Years, od.Months, od.Days,
93         od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
94     ASSERT_TRUE(!bSuccess);
95 }
96 
TEST_F(ConverterTest,testDuration)97 TEST_F(ConverterTest, testDuration)
98 {
99     OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n");
100     doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
101     doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
102     doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
103     doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
104     doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
105     doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
106     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190), "PT0.19S" );
107     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90), "PT0.09S" );
108     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.009S" );
109     doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999),
110             "PT9.999999999999999999999999999999S", "PT9.999S" );
111     doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
112     doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10),
113             "-P7Y6M5DT4H3M2.01S" );
114     doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
115     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
116     doTestDurationF("1Y1M");        // invalid: no ^P
117     doTestDurationF("P-1Y1M");      // invalid: - after P
118     doTestDurationF("P1M1Y");       // invalid: Y after M
119     doTestDurationF("PT1Y");        // invalid: Y after T
120     doTestDurationF("P1Y1M1M");     // invalid: M twice, no T
121     doTestDurationF("P1YT1MT1M");   // invalid: T twice
122     doTestDurationF("P1YT");        // invalid: T but no H,M,S
123     doTestDurationF("P99999999999Y");   // cannot parse so many Ys
124     doTestDurationF("PT.1S");       // invalid: no 0 preceding .
125     doTestDurationF("PT5M.134S");   // invalid: no 0 preceding .
126     doTestDurationF("PT1.S");       // invalid: no digit following .
127     OSL_TRACE("\nSAX CONVERTER TEST END\n");
128 }
129 
130 
eqDateTime(util::DateTime a,util::DateTime b)131 static bool eqDateTime(util::DateTime a, util::DateTime b) {
132     return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
133         && a.Hours == b.Hours && a.Minutes == b.Minutes
134         && a.Seconds == b.Seconds
135         && a.HundredthSeconds == b.HundredthSeconds;
136 }
137 
doTest(util::DateTime const & rdt,char const * const pis,char const * const i_pos=0)138 static void doTest(util::DateTime const & rdt, char const*const pis,
139         char const*const i_pos = 0)
140 {
141     char const*const pos((i_pos) ? i_pos : pis);
142     ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
143     util::DateTime odt;
144     bool bSuccess( Converter::convertDateTime(odt, is) );
145     OSL_TRACE("Y:%d M:%d D:%d  H:%d M:%d S:%d H:%d",
146         odt.Year, odt.Month, odt.Day,
147         odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
148     ASSERT_TRUE(bSuccess);
149     ASSERT_TRUE(eqDateTime(rdt, odt));
150     ::rtl::OUStringBuffer buf;
151     Converter::convertDateTime(buf, odt, true);
152     OSL_TRACE(
153         ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8));
154     ASSERT_TRUE(buf.makeStringAndClear().equalsAscii(pos));
155 }
156 
doTestDateTimeF(char const * const pis)157 static void doTestDateTimeF(char const*const pis)
158 {
159     util::DateTime odt;
160     bool bSuccess = Converter::convertDateTime(odt,
161             ::rtl::OUString::createFromAscii(pis));
162     OSL_TRACE("Y:%d M:%d D:%d  H:%dH M:%d S:%d H:%d",
163         odt.Year, odt.Month, odt.Day,
164         odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
165     ASSERT_TRUE(!bSuccess);
166 }
167 
TEST_F(ConverterTest,testDateTime)168 TEST_F(ConverterTest, testDateTime)
169 {
170     OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n");
171     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), "0001-01-01T00:00:00" );
172     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
173             "0001-01-01T00:00:00Z", "0001-01-01T00:00:00" );
174 //    doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00" );
175 //    doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00Z" );
176     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
177             "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00" );
178     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
179             "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00" );
180     doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 2, 1, 1)*/,
181             "0001-01-02T00:00:00-12:00", "0001-01-02T00:00:00" );
182 //            "0001-02-01T12:00:00" );
183     doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 1, 1, 1)*/,
184             "0001-01-02T00:00:00+12:00", "0001-01-02T00:00:00" );
185 //            "0001-01-01T12:00:00" );
186     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
187             "9999-12-31T23:59:59.99" );
188     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
189             "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.99" );
190     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
191             "9999-12-31T23:59:59.9999999999999999999999999999999999999",
192             "9999-12-31T23:59:59.99" );
193     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
194             "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
195             "9999-12-31T23:59:59.99" );
196     doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333)
197                 /*(0, 0, 0, 0, 2, 1, 333)*/,
198             "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
199     doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
200     doTestDateTimeF( "1-01-01T00:00:00" ); // invalid: < 4 Y
201     doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
202     doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
203     doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
204     doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
205     doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
206     doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
207     doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
208     doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
209     doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
210     doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
211     doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
212     doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: H > 59
213     doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
214     doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
215     doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
216     doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
217     doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
218     doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
219     doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
220     doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
221     OSL_TRACE("\nSAX CONVERTER TEST END\n");
222 }
223 
224 
225 }
226 
main(int argc,char ** argv)227 int main(int argc, char **argv)
228 {
229     ::testing::InitGoogleTest(&argc, argv);
230     return RUN_ALL_TESTS();
231 }
232