1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 // Qt includes
22 #include <QStringList>
23 
24 // CTK includes
25 #include "ctkUtils.h"
26 
27 // STD includes
28 #include <cstdlib>
29 #include <iostream>
30 #include <string>
31 #include <vector>
32 
33 //-----------------------------------------------------------------------------
ctkUtilsTest1(int argc,char * argv[])34 int ctkUtilsTest1(int argc, char * argv [] )
35 {
36   Q_UNUSED(argc);
37   Q_UNUSED(argv);
38 
39   // Test qListToSTLVector(const QStringList& list, std::vector<char*>& vector)
40 
41   QStringList inputStringList;
42   inputStringList << "Testing";
43   inputStringList << " is ";
44   inputStringList << "awesome !";
45 
46   std::vector<char*> outputCharVector;
47 
48   ctk::qListToSTLVector(inputStringList, outputCharVector);
49 
50   if (outputCharVector.size() != 3)
51     {
52     std::cerr << "Error in qListToSTLVector(const QStringList&, std::vector<char*>&)" << std::endl
53               << "outputCharVector should contains 3 elements." << std::endl;
54     return EXIT_FAILURE;
55     }
56 
57   if ((strcmp(outputCharVector[0], "Testing") != 0) ||
58       (strcmp(outputCharVector[1], " is ") != 0) ||
59       (strcmp(outputCharVector[2], "awesome !") != 0))
60     {
61     std::cerr << "Error in qListToSTLVector(const QStringList&, std::vector<char*>&)" << std::endl
62               << "Content of outputCharVector is incorrect" << std::endl
63               << "inputStringList[0] => [" << qPrintable(inputStringList[0]) << "]" << std::endl
64               << "inputStringList[1] => [" << qPrintable(inputStringList[1]) << "]" << std::endl
65               << "inputStringList[2] => [" << qPrintable(inputStringList[2]) << "]" << std::endl
66               << "outputCharVector[0] => [" << outputCharVector[0] << "]" << std::endl
67               << "outputCharVector[1] => [" << outputCharVector[1] << "]" << std::endl
68               << "outputCharVector[2] => [" << outputCharVector[2] << "]" << std::endl;
69     return EXIT_FAILURE;
70     }
71 
72   delete [] outputCharVector[0];
73   delete [] outputCharVector[1];
74   delete [] outputCharVector[2];
75 
76 
77 
78   // Test qListToSTLVector(const QStringList& list, std::vector<std::string>& vector)
79 
80   std::vector<std::string> outputStringVector;
81 
82   ctk::qListToSTLVector(inputStringList, outputStringVector);
83 
84   if (outputStringVector.size() != 3)
85     {
86     std::cerr << "Error in qListToSTLVector(const QStringList&, std::vector<std::string>&)" << std::endl
87               << "outputStringVector should contains 3 elements." << std::endl;
88     return EXIT_FAILURE;
89     }
90 
91   if ((outputStringVector[0].compare("Testing") != 0) ||
92       (outputStringVector[1].compare(" is ") != 0) ||
93       (outputStringVector[2].compare("awesome !") != 0))
94     {
95     std::cerr << "Error in qListToSTLVector(const QStringList&, std::vector<std::string>&)" << std::endl
96               << "Content of outputStringVector is incorrect" << std::endl
97               << "inputStringList[0] => [" << qPrintable(inputStringList[0]) << "]" << std::endl
98               << "inputStringList[1] => [" << qPrintable(inputStringList[1]) << "]" << std::endl
99               << "inputStringList[2] => [" << qPrintable(inputStringList[2]) << "]" << std::endl
100               << "outputStringVector[0] => [" << outputStringVector[0] << "]" << std::endl
101               << "outputStringVector[1] => [" << outputStringVector[1] << "]" << std::endl
102               << "outputStringVector[2] => [" << outputStringVector[2] << "]" << std::endl;
103     return EXIT_FAILURE;
104     }
105 
106 
107   // Test stlVectorToQList(const std::vector<std::string>& vector, QStringList& list)
108 
109   std::vector<std::string> inputStringVector;
110   inputStringVector.push_back("Testing");
111   inputStringVector.push_back(" is ");
112   inputStringVector.push_back("awesome !");
113 
114   QStringList ouputStringList;
115 
116   ctk::stlVectorToQList(inputStringVector, ouputStringList);
117 
118   if (ouputStringList.size() != 3)
119     {
120     std::cerr << "Error in stlVectorToQList(const std::vector<std::string>&, QStringList&)" << std::endl
121               << "ouputStringList should contains 3 elements." << std::endl;
122     return EXIT_FAILURE;
123     }
124 
125   if ((ouputStringList[0] != QLatin1String("Testing")) ||
126       (ouputStringList[1] != QLatin1String(" is ")) ||
127       (ouputStringList[2] != QLatin1String("awesome !")))
128     {
129     std::cerr << "Error in stlVectorToQList(const std::vector<std::string>&, QStringList&)" << std::endl
130               << "Content of ouputStringList is incorrect" << std::endl
131               << "inputStringVector[0] => [" << inputStringVector[0] << "]" << std::endl
132               << "inputStringVector[1] => [" << inputStringVector[1] << "]" << std::endl
133               << "inputStringVector[2] => [" << inputStringVector[2] << "]" << std::endl
134               << "ouputStringList[0] => [" << qPrintable(ouputStringList[0]) << "]" << std::endl
135               << "ouputStringList[1] => [" << qPrintable(ouputStringList[1]) << "]" << std::endl
136               << "ouputStringList[2] => [" << qPrintable(ouputStringList[2]) << "]" << std::endl;
137     return EXIT_FAILURE;
138     }
139 
140   return EXIT_SUCCESS;
141 }
142