Update ooo320-m1
[ooovba.git] / comphelper / test / uno_iterators / uno_iterators.cxx
blobe610cb1d19806c33aabbc34000c0c57c66616223
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: uno_iterators.cxx,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 #include <algorithm>
31 #include <comphelper/stlunosequence.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <iostream>
34 #include <list>
35 #include <rtl/textenc.h>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/ustring.hxx>
38 #include <vector>
40 using namespace ::comphelper;
41 using namespace ::com::sun::star::uno;
42 using namespace ::rtl;
43 using namespace ::std;
45 // some helpers for testing (imperative)
46 void fill_testdata(Sequence<OUString>& seq)
48 OUStringBuffer buf;
49 for(sal_Int32 i=0; i<seq.getLength(); i++)
51 buf.appendAscii("Sampletext ");
52 buf.append(i);
53 seq[i] = buf.makeStringAndClear();
57 void print_oustring(const OUString& ustr)
58 { cout << OUStringToOString(ustr, RTL_TEXTENCODING_ASCII_US).getStr() << endl; }
60 void print_sequence(const Sequence<OUString>& seq)
62 cout << "Sequence of " << seq.getLength() << " OUStrings: " << endl;
63 for(sal_Int32 i=0; i<seq.getLength(); i++)
64 print_oustring(seq[i]);
65 cout << endl;
68 // some helpers for testing (functional)
69 class TestdataGenerator
71 public:
72 TestdataGenerator() : m_Index(0) {};
73 OUString operator()() { return m_Buf.appendAscii("Sampletext ").append(m_Index++).makeStringAndClear(); };
74 OUStringBuffer m_Buf;
75 sal_Int32 m_Index;
78 void fill_testdata_stl(StlUnoSequence<OUString>& stl_seq)
80 generate(stl_seq.begin(), stl_seq.end(), TestdataGenerator());
83 void print_sequence_stl(const StlUnoSequence<OUString>& stl_seq)
85 cout << "Sequence of " << stl_seq.size() << " OUStrings: " << endl;
86 for_each(stl_seq.begin(), stl_seq.end(), &print_oustring);
87 cout << endl;
91 // code samples
93 // imperative loops (just to show they work, using for_each would be better most of the time
94 void classic_loops()
96 Sequence<OUString> s(10);
97 fill_testdata(s);
98 StlUnoSequence<OUString>::iterator stl_s_it;
100 cout << "for iteration" << endl;
101 for(stl_s_it = stl_begin(s); stl_s_it != stl_end(s); stl_s_it++)
102 cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl;
104 cout << "reverse for iteration" << endl;
105 for(stl_s_it = stl_end(s); stl_s_it != stl_begin(s); stl_s_it--)
106 cout << OUStringToOString(*(stl_s_it-1), RTL_TEXTENCODING_ASCII_US).getStr() << endl;
108 cout << "skipping for iteration" << endl;
109 for(stl_s_it = stl_begin(s); stl_s_it != stl_end(s); stl_s_it+=2)
110 cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl;
112 cout << "skipping reverse for iteration" << endl;
113 for(stl_s_it = stl_end(s); stl_s_it != stl_begin(s); stl_s_it-=2)
114 std::cout << OUStringToOString(*(stl_s_it-1), RTL_TEXTENCODING_ASCII_US).getStr() << endl;
117 void stl_algos()
119 Sequence<OUString> s(10);
120 fill_testdata(s);
122 random_shuffle(stl_begin(s), stl_end(s));
123 cout << "shuffed" << std::endl;
124 print_sequence(s);
126 sort(stl_begin(s), stl_end(s));
127 cout << "sorted" << std::endl;
128 print_sequence(s);
131 void stl_conversions()
133 Sequence<OUString> s(10);
134 fill_testdata(s);
135 StlUnoSequence<OUString> stl_s = StlUnoSequence<OUString>::createInstance(s);
137 // convert to stl::vector, modify in vector, copy back, print
138 cout << "vector conversion" << endl;
139 vector<OUString> vec(stl_s.begin(), stl_s.end());
140 vec[2] = OUString::createFromAscii("changed in vector");
141 copy(vec.begin(), vec.end(), stl_s.begin());
142 print_sequence(s);
144 // convert to stl::list, modify in list, copy back, print
145 cout << "list conversion" << endl;
146 list<OUString> l(stl_s.begin(), stl_s.end());
147 l.pop_back();
148 l.push_back(OUString::createFromAscii("changed in list"));
149 copy(l.begin(), l.end(), stl_s.begin());
150 print_sequence(s);
153 // inserts the second half of the second sequence after the first element of the first sequence
154 void stl_inserting()
156 Sequence<OUString> s1(10);
157 Sequence<OUString> s2(10);
158 Sequence<OUString> result(15);
159 StlUnoSequence<OUString> stl_s1 = StlUnoSequence<OUString>::createInstance(s1);
160 StlUnoSequence<OUString> stl_s2 = StlUnoSequence<OUString>::createInstance(s2);
161 StlUnoSequence<OUString> stl_result = StlUnoSequence<OUString>::createInstance(result);
162 fill_testdata(s1);
163 fill_testdata(s2);
165 list<OUString> temp(stl_s1.begin(), stl_s1.end());
166 copy(stl_s2.begin()+5, stl_s2.end(), insert_iterator<list<OUString> >(temp, ++temp.begin()));
167 copy(temp.begin(), temp.end(), stl_result.begin());
168 print_sequence(result);
171 void stl_compare()
173 Sequence<OUString> s1(10);
174 Sequence<OUString> s2(10);
175 StlUnoSequence<OUString> stl_s1 = StlUnoSequence<OUString>::createInstance(s1);
176 StlUnoSequence<OUString> stl_s2 = StlUnoSequence<OUString>::createInstance(s2);
177 if (stl_s1 == stl_s2)
178 cout << "sequences are equal." << endl;
179 s2[9] = OUString::createFromAscii("ZZZZZ");
180 if(stl_s1 < stl_s2)
181 cout << "first sequence is smaller." << endl;
184 void stl_const_sequence()
186 const Sequence<OUString> s(10);
187 for(StlUnoSequence<OUString>::const_iterator stl_s_it = stl_begin(s); stl_s_it != stl_end(s); stl_s_it++)
188 cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl;
191 void stl_helpers()
193 Sequence<OUString> s(10);
194 StlUnoSequence<OUString> stl_s = StlUnoSequence<OUString>::createInstance(s);
195 fill_testdata_stl(stl_s);
196 print_sequence_stl(stl_s);
199 int main()
201 cout << "--- CLASSIC LOOPS" << endl;
202 classic_loops();
204 cout << "--- SOME STL ALGORITHMS" << endl;
205 stl_algos();
207 cout << "--- SOME STL CONVERSIONS" << endl;
208 stl_conversions();
210 cout << "--- INSERTING IN SEQUENCE" << endl;
211 stl_inserting();
213 cout << "--- COMPARING" << endl;
214 stl_compare();
216 cout << "--- CONST SEQUENCE" << endl;
217 stl_const_sequence();
219 cout << "--- HELPERS IN STL-STYLE" << endl;
220 stl_helpers();