Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / qa / unit / variadictemplates.cxx
blob6b62204f487c5473ab8ade030d4f590364f61541
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <optional>
11 #include <sal/types.h>
12 #include <comphelper/unwrapargs.hxx>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
18 #include <sstream>
20 class VariadicTemplatesTest : public CppUnit::TestFixture
22 public:
23 void testUnwrapArgs();
25 CPPUNIT_TEST_SUITE(VariadicTemplatesTest);
26 CPPUNIT_TEST(testUnwrapArgs);
27 CPPUNIT_TEST_SUITE_END();
30 namespace {
32 namespace detail {
34 template <typename T>
35 void extract(
36 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
37 sal_Int32 nArg, T & v,
38 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
39 const& xErrorContext )
41 if (nArg >= seq.getLength()) {
42 throw ::com::sun::star::lang::IllegalArgumentException(
43 "No such argument available!",
44 xErrorContext, static_cast<sal_Int16>(nArg) );
46 if (! fromAny(seq[nArg], &v)) {
47 throw ::com::sun::star::lang::IllegalArgumentException(
48 "Cannot extract ANY { "
49 + seq[nArg].getValueType().getTypeName()
50 + " } to " + ::cppu::UnoType<T>::get().getTypeName(),
51 xErrorContext,
52 static_cast<sal_Int16>(nArg) );
56 template <typename T>
57 void extract(
58 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
59 sal_Int32 nArg, ::std::optional<T> & v,
60 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
61 const& xErrorContext )
63 if (nArg < seq.getLength()) {
64 T t;
65 extract( seq, nArg, t, xErrorContext );
66 v = t;
70 } // namespace detail
72 template < typename T0, typename T1, typename T2, typename T3, typename T4 >
73 void unwrapArgsBaseline(
74 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq,
75 T0& v0, T1& v1, T2& v2, T3& v3, T4& v4,
76 ::com::sun::star::uno::Reference<
77 ::com::sun::star::uno::XInterface> const& xErrorContext =
78 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() )
80 ::detail::extract( seq, 0, v0, xErrorContext );
81 ::detail::extract( seq, 1, v1, xErrorContext );
82 ::detail::extract( seq, 2, v2, xErrorContext );
83 ::detail::extract( seq, 3, v3, xErrorContext );
84 ::detail::extract( seq, 4, v4, xErrorContext );
89 void VariadicTemplatesTest::testUnwrapArgs() {
90 OUString tmp1 = "Test1";
91 sal_Int32 tmp2 = 42;
92 sal_uInt32 tmp3 = 42;
93 ::com::sun::star::uno::Any tmp6(
94 tmp1
96 ::com::sun::star::uno::Any tmp7(
97 tmp2
99 ::com::sun::star::uno::Any tmp8(
100 tmp3
102 ::com::sun::star::uno::Any tmp9(
103 OUString("Test2")
105 ::std::optional< ::com::sun::star::uno::Any > tmp10(
106 OUString("Test3")
108 ::std::optional< ::com::sun::star::uno::Any > tmp11(
109 tmp1
112 // test equality with the baseline and template specialization with
113 // std::optional< T >
114 try {
115 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq1(
116 static_cast< sal_uInt32 >( 5 ) );
117 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq2(
118 static_cast< sal_uInt32 >( 5 ) );
120 // tmp11 should be ignored as it is ::std::optional< T >
121 ::comphelper::unwrapArgs( seq1, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11 );
122 unwrapArgsBaseline( seq2, tmp6, tmp7, tmp8, tmp9, tmp10 );
123 ::com::sun::star::uno::Any* p1 = seq1.getArray();
124 ::com::sun::star::uno::Any* p2 = seq2.getArray();
126 for( sal_Int32 i = 0; i < seq1.getLength() && i < seq2.getLength(); ++i ) {
127 CPPUNIT_ASSERT_EQUAL_MESSAGE( "seq1 and seq2 are equal",
128 p1[i], p2[i] );
130 CPPUNIT_ASSERT_MESSAGE( "seq1 and seq2 are equal",
131 bool(seq1 == seq2) );
133 catch( ::com::sun::star::lang::IllegalArgumentException& err ) {
134 std::stringstream ss;
135 ss << "IllegalArgumentException when unwrapping arguments at: " <<
136 err.ArgumentPosition;
137 CPPUNIT_FAIL( ss.str() );
140 // test argument counting
141 try {
142 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
143 static_cast< sal_uInt32 >( 4 ) );
144 ::comphelper::unwrapArgs( seq, tmp6, tmp7, tmp10, tmp11, tmp10, tmp6 );
146 catch( ::com::sun::star::lang::IllegalArgumentException& err ) {
147 CPPUNIT_ASSERT_EQUAL( static_cast< short >( 5 ), err.ArgumentPosition );
150 OUString test1( "Test2" );
151 OUString test2( "Test2" );
152 OUString test3( "Test3" );
153 OUString test4( "Test4" );
154 OUString test5( "Test5" );
156 try {
157 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
158 static_cast< sal_uInt32 >( 4 ) );
159 ::comphelper::unwrapArgs( seq, test1, test2, test3, test4, test5 );
161 catch( ::com::sun::star::lang::IllegalArgumentException& err1 ) {
162 try {
163 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
164 static_cast< sal_uInt32 >( 4 ) );
165 unwrapArgsBaseline( seq, test1, test2, test3, test4, test5 );
166 CPPUNIT_FAIL( "unwrapArgs failed while the baseline did not throw" );
168 catch( ::com::sun::star::lang::IllegalArgumentException& err2 ) {
169 CPPUNIT_ASSERT_EQUAL_MESSAGE( "err1.ArgumentPosition == err2.ArgumentPosition",
170 err1.ArgumentPosition, err2.ArgumentPosition );
175 CPPUNIT_TEST_SUITE_REGISTRATION(VariadicTemplatesTest);
177 CPPUNIT_PLUGIN_IMPLEMENT();
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */