forked from cxwtc123/csvmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
216 lines (166 loc) · 5.44 KB
/
Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*!
* \file Test.cpp
* \brief CSV文件管理类 测试代码
* \details CSV文件管理类 测试代码,对CSVManage类的方法进行测试
* \author cxw
* \version 1.0
* \date 2012-7-10
* \bug 报告BUG,请联系 cxwtc123@gmail.com
* \copyright free
*/
#include <iostream>
#include <fstream>
#include "CSVManage.h"
using namespace std;
void newFileTest(CXW::CSVManage& csvMan);
void loadFileTest(CXW::CSVManage& csvMan);
void getRowFromStreamTest(void);
void addRowToStreamTest(void);
void sortTest(CXW::CSVManage& csvMan);
int main(void)
{
CXW::CSVManage csvMan;
// newFileTest(csvMan);
loadFileTest(csvMan);
//getRowFromStreamTest();
//addRowToStreamTest ();
//sortTest(csvMan);
return 0;
};
void newFileTest(CXW::CSVManage& csvMan)
{
CXW::CSVManage::Title title;
title.push_back ("编号");
title.push_back ("测点名称");
title.push_back ("时间戳");
title.push_back ("数值");
title.push_back ("质量码");
csvMan.newFile ("csvCreate.csv",title);
CXW::CSVManage::Row row;
char buf[32];
for (int i = 0 ; i < 1000000 ; i++)
{
row.clear ();
row.push_back (_itoa (i,buf,10));
row.push_back ("TagName001");
row.push_back ("2012-7-12 9:23:00");
row.push_back (_itoa (rand(),buf,10));
row.push_back (_itoa (i,buf,16));
csvMan.addRow (row);
}
csvMan.save();
}
void loadFileTest(CXW::CSVManage& csvMan)
{
// 首次测试验证
csvMan.loadFile ("test.csv",true);
for (int i = 0 ;i < csvMan.getRowSize () ; i++)
{
for (int j = 0 ;j < csvMan.getColSize () ; j++)
{
string str = csvMan.getXY (i,j).c_str() ;
// str += "add";
// csvMan.modify (i,j,str.c_str());
}
}
cout << "csvMan.loadFile (\"test.csv\",true); test" << endl;
cout << "hastitle ? " << csvMan.hasTitle () << endl;
cout << "datasize: row = " << csvMan.getRowSize () <<
" col = " << csvMan.getColSize () << endl;
csvMan.saveToFile ("testsave.csv");
cout << "csvMan.saveToFile (\"testsave.csv\"); test" << endl;
cout << "--------------------------------------------" << endl << endl;
// 二次测试验证
//csvMan.loadFile ("test.csv");
//for (int i = 0 ;i < csvMan.getRowSize () ; i++)
//{
// for (int j = 0 ;j < csvMan.getColSize () ; j++)
// {
// string str = csvMan.getXY (i,j).c_str() ;
// str += "add";
// csvMan.modify (i,j,str.c_str());
// }
//}
//cout << "csvMan.loadFile (\"test.csv\"); test" << endl;
//cout << "hastitle ? " << csvMan.hasTitle () << endl;
//cout << "datasize: row = " << csvMan.getRowSize () <<
// " col = " << csvMan.getColSize () << endl;
//csvMan.saveToFile ("testsave.csv");
//cout << "csvMan.saveToFile (\"testsave.csv\"); test" << endl;
//cout << "--------------------------------------------" << endl << endl;
}
//静态方法测试
void getRowFromStreamTest()
{
CXW::CSVManage::Row row;
fstream inf;
inf.open ("test.csv");
if(inf.fail())
{
cout << "open file error! " << endl;
return ;
}
row = CXW::CSVManage::getRowFromStream (inf);
while (0 != row.size ())
{
cout << "getRowFromStream success ! " << " row size : " << row.size ()
<< " row[0] = [" << row[0] << "]"
<< " row[" << row.size ()-1 << "] = [" << row[row.size ()-1] << "]" << endl;
row = CXW::CSVManage::getRowFromStream (inf);
}
}
//静态方法测试
void addRowToStreamTest()
{
fstream inf;
inf.open ("addRowtoStreamTest.csv",ios_base::app );
//如果文件不存在,则创建并添加标题行
if(inf.fail())
{
cout << "file does not exist,create it! " << endl;
inf.open ("addRowtoStreamTest.csv",ios_base::out);
CXW::CSVManage::Title title;
title.push_back ("编号");
title.push_back ("测点名称");
title.push_back ("时间戳");
title.push_back ("数值");
title.push_back ("质量码");
CXW::CSVManage::addRowToStream (inf,title);
}
CXW::CSVManage::Row row;
char buf[32];
for (int i = 0 ; i < 10000 ; i++)
{
row.clear ();
row.push_back (_itoa (i,buf,10));
row.push_back ("TagName001");
row.push_back ("2012-7-12 9:23:00");
row.push_back (_itoa (rand(),buf,10));
row.push_back (_itoa (i,buf,16));
CXW::CSVManage::addRowToStream (inf,row);
}
inf.close();
}
void sortTest(CXW::CSVManage& csvMan)
{
csvMan.sort (1,CXW::CSVManage::STRING_A_TO_Z);
csvMan.saveToFile ("./test/test_STRING_A_TO_Z.csv");
csvMan.sort (1,CXW::CSVManage::STRING_Z_TO_A);
csvMan.saveToFile ("./test/test_STRING_Z_TO_A.csv");
csvMan.sort (1,CXW::CSVManage::STRING_Aa_TO_Zz);
csvMan.saveToFile ("./test/test_STRING_Aa_TO_Zz.csv");
csvMan.sort (1,CXW::CSVManage::STRING_Zz_TO_Aa);
csvMan.saveToFile ("./test/test_STRING_Zz_TO_Aa.csv");
csvMan.sort (1,CXW::CSVManage::INT_LITTLE_TO_LARGE);
csvMan.saveToFile ("./test/test_INT_LITTLE_TO_LARGE.csv");
csvMan.sort (1,CXW::CSVManage::INT_LARGE_TO_LITTLE);
csvMan.saveToFile ("./test/test_INT_LARGE_TO_LITTLE.csv");
csvMan.sort (1,CXW::CSVManage::FLOAT_LITTLE_TO_LARGE);
csvMan.saveToFile ("./test/test_FLOAT_LITTLE_TO_LARGE.csv");
csvMan.sort (1,CXW::CSVManage::FLOAT_LARGE_TO_LITTLE);
csvMan.saveToFile ("./test/test_FLOAT_LARGE_TO_LITTLE.csv");
csvMan.sort (1,CXW::CSVManage::DOUBLE_LITTLE_TO_LARGE);
csvMan.saveToFile ("./test/test_DOUBLE_LITTLE_TO_LARGE.csv");
csvMan.sort (1,CXW::CSVManage::DOUBLE_LARGE_TO_LITTLE);
csvMan.saveToFile ("./test/test_DOUBLE_LARGE_TO_LITTLE.csv");
}