1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * This file is part of OpenOffice.org.
10 * OpenOffice.org is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License version 3
12 * only, as published by the Free Software Foundation.
14 * OpenOffice.org is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License version 3 for more details
18 * (a copy is included in the LICENSE file that accompanied this code).
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with OpenOffice.org. If not, see
22 * <http://www.openoffice.org/license.html>
23 * for a copy of the LGPLv3 License.
25 ************************************************************************/
28 #include <comphelper/stlunosequence.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
32 #include <rtl/textenc.h>
33 #include <rtl/ustrbuf.hxx>
34 #include <rtl/ustring.hxx>
37 using namespace ::comphelper
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::rtl
;
40 using namespace ::std
;
42 // some helpers for testing (imperative)
43 void fill_testdata(Sequence
<OUString
>& seq
)
46 for(sal_Int32 i
=0; i
<seq
.getLength(); i
++)
48 buf
.appendAscii("Sampletext ");
50 seq
[i
] = buf
.makeStringAndClear();
54 void print_oustring(const OUString
& ustr
)
55 { cout
<< OUStringToOString(ustr
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
; }
57 void print_sequence(const Sequence
<OUString
>& seq
)
59 cout
<< "Sequence of " << seq
.getLength() << " OUStrings: " << endl
;
60 for(sal_Int32 i
=0; i
<seq
.getLength(); i
++)
61 print_oustring(seq
[i
]);
65 // some helpers for testing (functional)
66 class TestdataGenerator
69 TestdataGenerator() : m_Index(0) {};
70 OUString
operator()() { return m_Buf
.appendAscii("Sampletext ").append(m_Index
++).makeStringAndClear(); };
75 void fill_testdata_stl(StlUnoSequence
<OUString
>& stl_seq
)
77 generate(stl_seq
.begin(), stl_seq
.end(), TestdataGenerator());
80 void print_sequence_stl(const StlUnoSequence
<OUString
>& stl_seq
)
82 cout
<< "Sequence of " << stl_seq
.size() << " OUStrings: " << endl
;
83 for_each(stl_seq
.begin(), stl_seq
.end(), &print_oustring
);
90 // imperative loops (just to show they work, using for_each would be better most of the time
93 Sequence
<OUString
> s(10);
95 StlUnoSequence
<OUString
>::iterator stl_s_it
;
97 cout
<< "for iteration" << endl
;
98 for(stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
++)
99 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
101 cout
<< "reverse for iteration" << endl
;
102 for(stl_s_it
= stl_end(s
); stl_s_it
!= stl_begin(s
); stl_s_it
--)
103 cout
<< OUStringToOString(*(stl_s_it
-1), RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
105 cout
<< "skipping for iteration" << endl
;
106 for(stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
+=2)
107 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
109 cout
<< "skipping reverse for iteration" << endl
;
110 for(stl_s_it
= stl_end(s
); stl_s_it
!= stl_begin(s
); stl_s_it
-=2)
111 std::cout
<< OUStringToOString(*(stl_s_it
-1), RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
116 Sequence
<OUString
> s(10);
119 random_shuffle(stl_begin(s
), stl_end(s
));
120 cout
<< "shuffed" << std::endl
;
123 sort(stl_begin(s
), stl_end(s
));
124 cout
<< "sorted" << std::endl
;
128 void stl_conversions()
130 Sequence
<OUString
> s(10);
132 StlUnoSequence
<OUString
> stl_s
= StlUnoSequence
<OUString
>::createInstance(s
);
134 // convert to stl::vector, modify in vector, copy back, print
135 cout
<< "vector conversion" << endl
;
136 vector
<OUString
> vec(stl_s
.begin(), stl_s
.end());
137 vec
[2] = OUString::createFromAscii("changed in vector");
138 copy(vec
.begin(), vec
.end(), stl_s
.begin());
141 // convert to stl::list, modify in list, copy back, print
142 cout
<< "list conversion" << endl
;
143 list
<OUString
> l(stl_s
.begin(), stl_s
.end());
145 l
.push_back(OUString::createFromAscii("changed in list"));
146 copy(l
.begin(), l
.end(), stl_s
.begin());
150 // inserts the second half of the second sequence after the first element of the first sequence
153 Sequence
<OUString
> s1(10);
154 Sequence
<OUString
> s2(10);
155 Sequence
<OUString
> result(15);
156 StlUnoSequence
<OUString
> stl_s1
= StlUnoSequence
<OUString
>::createInstance(s1
);
157 StlUnoSequence
<OUString
> stl_s2
= StlUnoSequence
<OUString
>::createInstance(s2
);
158 StlUnoSequence
<OUString
> stl_result
= StlUnoSequence
<OUString
>::createInstance(result
);
162 list
<OUString
> temp(stl_s1
.begin(), stl_s1
.end());
163 copy(stl_s2
.begin()+5, stl_s2
.end(), insert_iterator
<list
<OUString
> >(temp
, ++temp
.begin()));
164 copy(temp
.begin(), temp
.end(), stl_result
.begin());
165 print_sequence(result
);
170 Sequence
<OUString
> s1(10);
171 Sequence
<OUString
> s2(10);
172 StlUnoSequence
<OUString
> stl_s1
= StlUnoSequence
<OUString
>::createInstance(s1
);
173 StlUnoSequence
<OUString
> stl_s2
= StlUnoSequence
<OUString
>::createInstance(s2
);
174 if (stl_s1
== stl_s2
)
175 cout
<< "sequences are equal." << endl
;
176 s2
[9] = OUString::createFromAscii("ZZZZZ");
178 cout
<< "first sequence is smaller." << endl
;
181 void stl_const_sequence()
183 const Sequence
<OUString
> s(10);
184 for(StlUnoSequence
<OUString
>::const_iterator stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
++)
185 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
190 Sequence
<OUString
> s(10);
191 StlUnoSequence
<OUString
> stl_s
= StlUnoSequence
<OUString
>::createInstance(s
);
192 fill_testdata_stl(stl_s
);
193 print_sequence_stl(stl_s
);
198 cout
<< "--- CLASSIC LOOPS" << endl
;
201 cout
<< "--- SOME STL ALGORITHMS" << endl
;
204 cout
<< "--- SOME STL CONVERSIONS" << endl
;
207 cout
<< "--- INSERTING IN SEQUENCE" << endl
;
210 cout
<< "--- COMPARING" << endl
;
213 cout
<< "--- CONST SEQUENCE" << endl
;
214 stl_const_sequence();
216 cout
<< "--- HELPERS IN STL-STYLE" << endl
;